Speeding up some stuff
This commit is contained in:
parent
5447b82ed9
commit
223a1f9de3
|
@ -46,7 +46,8 @@ class WikiController < ApplicationController
|
||||||
# Within a single web ---------------------------------------------------------
|
# Within a single web ---------------------------------------------------------
|
||||||
|
|
||||||
def authors
|
def authors
|
||||||
@authors = @web.select.authors.sort
|
@page_names_by_author = @web.page_names_by_author
|
||||||
|
@authors = @page_names_by_author.keys.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
def export_html
|
def export_html
|
||||||
|
|
|
@ -16,7 +16,7 @@ class Page < ActiveRecord::Base
|
||||||
# Try to render content to make sure that markup engine can take it,
|
# Try to render content to make sure that markup engine can take it,
|
||||||
renderer.revision = Revision.new(
|
renderer.revision = Revision.new(
|
||||||
:page => self, :content => content, :author => author, :revised_at => time)
|
:page => self, :content => content, :author => author, :revised_at => time)
|
||||||
renderer.display_content
|
renderer.display_content(update_references = true)
|
||||||
|
|
||||||
# A user may change a page, look at it and make some more changes - several times.
|
# A user may change a page, look at it and make some more changes - several times.
|
||||||
# Not to record every such iteration as a new revision, if the previous revision was done
|
# Not to record every such iteration as a new revision, if the previous revision was done
|
||||||
|
@ -59,10 +59,6 @@ class Page < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
|
||||||
revisions.collect { |rev| rev.author }
|
|
||||||
end
|
|
||||||
|
|
||||||
def references
|
def references
|
||||||
web.select.pages_that_reference(name)
|
web.select.pages_that_reference(name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,6 +46,9 @@ class PageSet < Array
|
||||||
end
|
end
|
||||||
|
|
||||||
def pages_authored_by(author)
|
def pages_authored_by(author)
|
||||||
|
all_pages_authored_by_the_author =
|
||||||
|
Page.connection.select_all(sanitize_sql([
|
||||||
|
"SELECT page_id FROM revision WHERE author = '?'", author]))
|
||||||
self.select { |page| page.authors.include?(author) }
|
self.select { |page| page.authors.include?(author) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -59,7 +62,7 @@ class PageSet < Array
|
||||||
# references and so cannot be orphans
|
# references and so cannot be orphans
|
||||||
# Pages that refer to themselves and have no links from outside are oprphans.
|
# Pages that refer to themselves and have no links from outside are oprphans.
|
||||||
def orphaned_pages
|
def orphaned_pages
|
||||||
never_orphans = web.select.authors + ['HomePage']
|
never_orphans = web.authors + ['HomePage']
|
||||||
self.select { |page|
|
self.select { |page|
|
||||||
if never_orphans.include? page.name
|
if never_orphans.include? page.name
|
||||||
false
|
false
|
||||||
|
@ -88,8 +91,4 @@ class PageSet < Array
|
||||||
}.flatten.uniq
|
}.flatten.uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
|
||||||
self.inject([]) { |authors, page| authors << page.authors }.flatten.uniq.sort
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,11 @@ class Web < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
def authors
|
||||||
select.authors
|
connection.select_all(
|
||||||
|
'SELECT DISTINCT r.author AS author ' +
|
||||||
|
'FROM revisions r ' +
|
||||||
|
'JOIN pages p ON p.id = r.page_id ' +
|
||||||
|
'ORDER by 1').collect { |row| row['author'] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def categories
|
def categories
|
||||||
|
@ -44,6 +48,21 @@ class Web < ActiveRecord::Base
|
||||||
read_attribute('markup').to_sym
|
read_attribute('markup').to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def page_names_by_author
|
||||||
|
connection.select_all(
|
||||||
|
'SELECT DISTINCT r.author AS author, p.name AS page_name ' +
|
||||||
|
'FROM revisions r ' +
|
||||||
|
'JOIN pages p ON r.page_id = p.id ' +
|
||||||
|
"WHERE p.web_id = #{self.id} " +
|
||||||
|
'ORDER by p.name'
|
||||||
|
).inject({}) { |result, row|
|
||||||
|
author, page_name = row['author'], row['page_name']
|
||||||
|
result[author] = [] unless result.has_key?(author)
|
||||||
|
result[author] << page_name
|
||||||
|
result
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def remove_pages(pages_to_be_removed)
|
def remove_pages(pages_to_be_removed)
|
||||||
pages_to_be_removed.each { |p| p.destroy }
|
pages_to_be_removed.each { |p| p.destroy }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
class Wiki
|
class Wiki
|
||||||
|
|
||||||
cattr_accessor :storage_path, :logger
|
cattr_accessor :storage_path, :logger
|
||||||
self.storage_path = "#{RAILS_ROOT}/storage/"
|
self.storage_path = "#{RAILS_ROOT}/storage/"
|
||||||
self.logger = RAILS_DEFAULT_LOGGER
|
self.logger = RAILS_DEFAULT_LOGGER
|
||||||
|
|
|
@ -4,9 +4,10 @@ class WikiReference < ActiveRecord::Base
|
||||||
WANTED_PAGE = 'W'
|
WANTED_PAGE = 'W'
|
||||||
INCLUDED_PAGE = 'I'
|
INCLUDED_PAGE = 'I'
|
||||||
CATEGORY = 'C'
|
CATEGORY = 'C'
|
||||||
|
AUTHOR = 'A'
|
||||||
|
|
||||||
belongs_to :page
|
belongs_to :page
|
||||||
validates_inclusion_of :link_type, :in => [LINKED_PAGE, WANTED_PAGE, INCLUDED_PAGE, CATEGORY]
|
validates_inclusion_of :link_type, :in => [LINKED_PAGE, WANTED_PAGE, INCLUDED_PAGE, CATEGORY, AUTHOR]
|
||||||
|
|
||||||
# FIXME all finders below MUST restrict their results to pages belonging to a particular web
|
# FIXME all finders below MUST restrict their results to pages belonging to a particular web
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<li>
|
<li>
|
||||||
<%= link_to_page author %>
|
<%= link_to_page author %>
|
||||||
co- or authored:
|
co- or authored:
|
||||||
<%= @web.select.pages_authored_by(author).collect { |page| link_to_page(page.name) }.sort.join ', ' %>
|
<%= @page_names_by_author[author].collect { |page_name| link_to_page(page_name) }.sort.join ', ' %>
|
||||||
</li>
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -20,11 +20,12 @@ class PageRenderer
|
||||||
|
|
||||||
def revision=(r)
|
def revision=(r)
|
||||||
@revision = r
|
@revision = r
|
||||||
@wiki_words_cache = @wiki_includes_cache = @wiki_references_cache = nil
|
@display_content = @display_published = @wiki_words_cache = @wiki_includes_cache =
|
||||||
|
@wiki_references_cache = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_content
|
def display_content(update_references = false)
|
||||||
@display_content ||= render
|
@display_content ||= render(:update_references => update_references)
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_content_for_export
|
def display_content_for_export
|
||||||
|
@ -87,12 +88,21 @@ class PageRenderer
|
||||||
private
|
private
|
||||||
|
|
||||||
def render(options = {})
|
def render(options = {})
|
||||||
result = WikiContent.new(@revision, @@url_generator, options).render!
|
|
||||||
|
rendering_result = WikiContent.new(@revision, @@url_generator, options).render!
|
||||||
|
|
||||||
|
if options[:update_references]
|
||||||
|
update_references(rendering_result)
|
||||||
|
end
|
||||||
|
rendering_result
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_references(rendering_result)
|
||||||
WikiReference.delete_all ['page_id = ?', @revision.page_id]
|
WikiReference.delete_all ['page_id = ?', @revision.page_id]
|
||||||
|
|
||||||
references = @revision.page.wiki_references
|
references = @revision.page.wiki_references
|
||||||
|
|
||||||
wiki_word_chunks = result.find_chunks(WikiChunk::WikiLink)
|
wiki_word_chunks = rendering_result.find_chunks(WikiChunk::WikiLink)
|
||||||
wiki_words = wiki_word_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
wiki_words = wiki_word_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
||||||
|
|
||||||
wiki_words.each do |referenced_name|
|
wiki_words.each do |referenced_name|
|
||||||
|
@ -105,17 +115,16 @@ class PageRenderer
|
||||||
references.create :referenced_name => referenced_name, :link_type => link_type
|
references.create :referenced_name => referenced_name, :link_type => link_type
|
||||||
end
|
end
|
||||||
|
|
||||||
include_chunks = result.find_chunks(Include)
|
include_chunks = rendering_result.find_chunks(Include)
|
||||||
includes = include_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
includes = include_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq
|
||||||
includes.each do |included_page_name|
|
includes.each do |included_page_name|
|
||||||
references.create :referenced_name => included_page_name,
|
references.create :referenced_name => included_page_name,
|
||||||
:link_type => WikiReference::INCLUDED_PAGE
|
:link_type => WikiReference::INCLUDED_PAGE
|
||||||
end
|
end
|
||||||
|
|
||||||
categories = result.find_chunks(Category).map { |cat| cat.list }.flatten
|
categories = rendering_result.find_chunks(Category).map { |cat| cat.list }.flatten
|
||||||
categories.each do |category|
|
categories.each do |category|
|
||||||
references.create :referenced_name => category, :link_type => WikiReference::CATEGORY
|
references.create :referenced_name => category, :link_type => WikiReference::CATEGORY
|
||||||
end
|
end
|
||||||
result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,7 +16,7 @@ Web.find_all.each do |web|
|
||||||
web.pages.find(:all, :order => 'name').each do |page|
|
web.pages.find(:all, :order => 'name').each do |page|
|
||||||
$stderr.puts "Processing page '#{page.name}'"
|
$stderr.puts "Processing page '#{page.name}'"
|
||||||
begin
|
begin
|
||||||
PageRenderer.new(page.current_revision).display_content
|
PageRenderer.new(page.current_revision).display_content(update_references = true)
|
||||||
rescue => e
|
rescue => e
|
||||||
puts e
|
puts e
|
||||||
puts e.backtrace
|
puts e.backtrace
|
||||||
|
|
|
@ -53,6 +53,9 @@ class WikiControllerTest < Test::Unit::TestCase
|
||||||
assert_success
|
assert_success
|
||||||
assert_equal %w(AnAuthor BreakingTheOrder DavidHeinemeierHansson Guest Me TreeHugger),
|
assert_equal %w(AnAuthor BreakingTheOrder DavidHeinemeierHansson Guest Me TreeHugger),
|
||||||
r.template_objects['authors']
|
r.template_objects['authors']
|
||||||
|
page_names_by_author = r.template_objects['page_names_by_author']
|
||||||
|
assert_equal r.template_objects['authors'], page_names_by_author.keys.sort
|
||||||
|
assert_equal %w(FirstPage HomePage), page_names_by_author['DavidHeinemeierHansson']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cancel_edit
|
def test_cancel_edit
|
||||||
|
|
|
@ -86,6 +86,14 @@ class WebTest < Test::Unit::TestCase
|
||||||
@web.select.orphaned_pages.collect{ |page| page.name }.sort
|
@web.select.orphaned_pages.collect{ |page| page.name }.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_page_names_by_author
|
||||||
|
page_names_by_author = webs(:test_wiki).page_names_by_author
|
||||||
|
assert_equal %w(AnAuthor DavidHeinemeierHansson Guest Me TreeHugger),
|
||||||
|
page_names_by_author.keys.sort
|
||||||
|
assert_equal %w(FirstPage HomePage), page_names_by_author['DavidHeinemeierHansson']
|
||||||
|
assert_equal %w(Oak), page_names_by_author['TreeHugger']
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def add_sample_pages
|
def add_sample_pages
|
||||||
|
|
Loading…
Reference in a new issue