Referring Pages for File List

For the file_list  action, include the pages which link to the given file(s).
This required rejiggering so that that information is actually retained in the database.
Unfortunately, you'll actually need to revise the page(s) in question, because that's the
only time this information is updated in the database.
This commit is contained in:
Jacques Distler 2009-01-10 00:18:25 -06:00
parent f456691609
commit 82e7aa52c7
4 changed files with 41 additions and 5 deletions

View file

@ -51,6 +51,10 @@ class Web < ActiveRecord::Base
WikiFile.all(:order => sort_order, :conditions => ['web_id = ?', id]) WikiFile.all(:order => sort_order, :conditions => ['web_id = ?', id])
end end
def pages_that_link_to(file_name)
WikiReference.pages_that_link_to_file(self, file_name)
end
def description(file_name) def description(file_name)
file = WikiFile.find_by_file_name(file_name) file = WikiFile.find_by_file_name(file_name)
file.description if file file.description if file

View file

@ -33,6 +33,15 @@ class WikiReference < ActiveRecord::Base
names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] } names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] }
end end
def self.pages_that_link_to_file(web, file_name)
query = 'SELECT name FROM pages JOIN wiki_references ' +
'ON pages.id = wiki_references.page_id ' +
'WHERE wiki_references.referenced_name = ? ' +
"AND wiki_references.link_type in ('#{FILE}') " +
"AND pages.web_id = '#{web.id}'"
names = connection.select_all(sanitize_sql([query, file_name])).map { |row| row['name'] }
end
def self.pages_that_include(web, page_name) def self.pages_that_include(web, page_name)
query = 'SELECT name FROM pages JOIN wiki_references ' + query = 'SELECT name FROM pages JOIN wiki_references ' +
'ON pages.id = wiki_references.page_id ' + 'ON pages.id = wiki_references.page_id ' +

View file

@ -18,7 +18,10 @@
<li> <li>
<input type="checkbox" name="<%= file.file_name %>" value="delete"/> <input type="checkbox" name="<%= file.file_name %>" value="delete"/>
<a href="<%= url_for :web => @web.address, :action => 'files', <a href="<%= url_for :web => @web.address, :action => 'files',
:id => file.file_name %>"><%= file.file_name%></a> (<%= file.created_at.asctime %>) :id => file.file_name %>"><%= file.file_name%></a> (<%= file.created_at.asctime %>) <span class="linked"><%=
"Linked to by: " unless @web.pages_that_link_to(file.file_name).empty?
@web.pages_that_link_to(file.file_name).collect { |referring_page|
link_to referring_page }.join(", ") %></span>
</li> </li>
<%- end -%> <%- end -%>
</ul> </ul>

View file

@ -97,14 +97,29 @@ class PageRenderer
def wiki_words def wiki_words
@wiki_words_cache ||= find_wiki_words(display_content) @wiki_words_cache ||= find_wiki_words(display_content)
end end
def find_wiki_words(rendering_result) def find_wiki_words(rendering_result)
wiki_links = rendering_result.find_chunks(WikiChunk::WikiLink) the_wiki_words = wiki_links(rendering_result)
# Exclude backslash-escaped wiki words, such as \WikiWord, as well as links to files # Exclude backslash-escaped wiki words, such as \WikiWord, as well as links to files
# and pictures, such as [[foo.txt:file]] or [[foo.jpg:pic]] # and pictures, such as [[foo.txt:file]] or [[foo.jpg:pic]]
wiki_links.delete_if { |link| link.escaped? or [:pic, :file].include?(link.link_type) } the_wiki_words.delete_if { |link| link.escaped? or [:pic, :file, :delete].include?(link.link_type) }
# convert to the list of unique page names # convert to the list of unique page names
wiki_links.map { |link| ( link.page_name ) }.uniq the_wiki_words.map { |link| ( link.page_name ) }.uniq
end
# Returns an array of all the WikiWords present in the content of this revision.
def wiki_files
@wiki_files_cache ||= find_wiki_files(display_content)
end
def find_wiki_files(rendering_result)
the_wiki_files = wiki_links(rendering_result)
the_wiki_files.delete_if { |link| ![:pic, :file].include?(link.link_type) }
the_wiki_files.map { |link| ( link.page_name ) }.uniq
end
def wiki_links(rendering_result)
rendering_result.find_chunks(WikiChunk::WikiLink)
end end
# Returns an array of all the WikiWords present in the content of this revision. # Returns an array of all the WikiWords present in the content of this revision.
@ -146,6 +161,11 @@ class PageRenderer
references.build :referenced_name => referenced_name, :link_type => link_type references.build :referenced_name => referenced_name, :link_type => link_type
end end
wiki_files = find_wiki_files(rendering_result)
wiki_files.each do |referenced_name|
references.build :referenced_name => referenced_name, :link_type => WikiReference::FILE
end
include_chunks = rendering_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|