2005-01-24 19:52:04 +01:00
|
|
|
require 'diff'
|
|
|
|
require 'wiki_content'
|
|
|
|
require 'chunks/wiki'
|
|
|
|
require 'date'
|
|
|
|
require 'author'
|
|
|
|
require 'page'
|
|
|
|
|
|
|
|
class Revision
|
|
|
|
|
|
|
|
attr_accessor :page, :number, :content, :created_at, :author
|
|
|
|
|
|
|
|
def initialize(page, number, content, created_at, author)
|
|
|
|
@page, @number, @created_at, @author = page, number, created_at, author
|
|
|
|
self.content = content
|
2005-02-20 10:28:41 +01:00
|
|
|
@display_cache = nil
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def created_on
|
|
|
|
Date.new(@created_at.year, @created_at.mon, @created_at.day)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_created_at
|
|
|
|
# Must use DateTime because Time doesn't support %e on at least some platforms
|
|
|
|
DateTime.new(
|
|
|
|
@created_at.year, @created_at.mon, @created_at.day, @created_at.hour, @created_at.min
|
|
|
|
).strftime "%B %e, %Y %H:%M"
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_revision
|
|
|
|
page.revisions[number + 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_revision
|
|
|
|
number > 0 ? page.revisions[number - 1] : nil
|
|
|
|
end
|
|
|
|
|
2005-03-27 20:13:43 +02:00
|
|
|
# Returns an array of all the WikiIncludes present in the content of this revision.
|
|
|
|
def wiki_includes
|
|
|
|
unless @wiki_includes_cache
|
|
|
|
chunks = display_content.find_chunks(Include)
|
|
|
|
@wiki_includes_cache = chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
|
|
|
end
|
|
|
|
@wiki_includes_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of all the WikiReferences present in the content of this revision.
|
|
|
|
def wiki_references
|
|
|
|
unless @wiki_references_cache
|
|
|
|
chunks = display_content.find_chunks(WikiChunk::WikiReference)
|
|
|
|
@wiki_references_cache = chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
|
|
|
end
|
|
|
|
@wiki_references_cache
|
|
|
|
end
|
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
# Returns an array of all the WikiWords present in the content of this revision.
|
|
|
|
def wiki_words
|
2005-04-03 07:15:56 +02:00
|
|
|
unless @wiki_words_cache
|
2005-01-24 19:52:04 +01:00
|
|
|
wiki_chunks = display_content.find_chunks(WikiChunk::WikiLink)
|
2005-03-27 20:13:43 +02:00
|
|
|
@wiki_words_cache = wiki_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
@wiki_words_cache
|
2005-04-03 07:15:56 +02:00
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
# Returns an array of all the WikiWords present in the content of this revision.
|
|
|
|
# that already exists as a page in the web.
|
|
|
|
def existing_pages
|
|
|
|
wiki_words.select { |wiki_word| page.web.pages[wiki_word] }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of all the WikiWords present in the content of this revision
|
|
|
|
# that *doesn't* already exists as a page in the web.
|
|
|
|
def unexisting_pages
|
|
|
|
wiki_words - existing_pages
|
|
|
|
end
|
|
|
|
|
2005-03-27 20:13:43 +02:00
|
|
|
# Explicit check for new type of display cache with chunks_by_type method.
|
2005-01-24 19:52:04 +01:00
|
|
|
# Ensures new version works with older snapshots.
|
|
|
|
def display_content
|
2005-03-27 20:13:43 +02:00
|
|
|
unless @display_cache && @display_cache.respond_to?(:chunks_by_type)
|
2005-01-24 19:52:04 +01:00
|
|
|
@display_cache = WikiContent.new(self)
|
2005-03-27 20:13:43 +02:00
|
|
|
@display_cache.render!
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
@display_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_diff
|
|
|
|
previous_revision ? HTMLDiff.diff(previous_revision.display_content, display_content) : display_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_display_cache
|
2005-04-03 07:15:56 +02:00
|
|
|
@wiki_words_cache = @published_cache = @display_cache = @wiki_includes_cache =
|
|
|
|
@wiki_references_cache = nil
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_published
|
2005-03-31 04:06:46 +02:00
|
|
|
unless @published_cache && @published_cache.respond_to?(:chunks_by_type)
|
|
|
|
@published_cache = WikiContent.new(self, {:mode => :publish})
|
|
|
|
@published_cache.render!
|
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
@published_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_content_for_export
|
2005-03-27 20:13:43 +02:00
|
|
|
WikiContent.new(self, {:mode => :export} ).render!
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2005-01-30 05:50:41 +01:00
|
|
|
|
|
|
|
def force_rendering
|
|
|
|
begin
|
2005-03-27 20:13:43 +02:00
|
|
|
display_content.render!
|
2005-01-30 05:50:41 +01:00
|
|
|
rescue Exception => e
|
|
|
|
ApplicationController.logger.error "Failed rendering page #{@name}"
|
|
|
|
ApplicationController.logger.error e
|
|
|
|
message = e.message.gsub(/\n/, '<br/>')
|
|
|
|
# substitute content with an error message
|
|
|
|
content = <<-EOL
|
|
|
|
<p>Markup engine has failed to render this page, raising the following error:</p>
|
|
|
|
<p>#{message}</p>
|
|
|
|
EOL
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
end
|