diff --git a/app/models/web.rb b/app/models/web.rb index 2283407c..8adc0d70 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -51,6 +51,10 @@ class Web < ActiveRecord::Base WikiFile.all(:order => sort_order, :conditions => ['web_id = ?', id]) end + def pages_that_link_to(file_name) + WikiReference.pages_that_link_to_file(self, file_name) + end + def description(file_name) file = WikiFile.find_by_file_name(file_name) file.description if file diff --git a/app/models/wiki_reference.rb b/app/models/wiki_reference.rb index 9bcb7557..b00f37cd 100644 --- a/app/models/wiki_reference.rb +++ b/app/models/wiki_reference.rb @@ -33,6 +33,15 @@ class WikiReference < ActiveRecord::Base names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] } 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) query = 'SELECT name FROM pages JOIN wiki_references ' + 'ON pages.id = wiki_references.page_id ' + diff --git a/app/views/wiki/file_list.html.erb b/app/views/wiki/file_list.html.erb index cc649404..2b6f00e7 100644 --- a/app/views/wiki/file_list.html.erb +++ b/app/views/wiki/file_list.html.erb @@ -18,7 +18,10 @@
  • <%= file.file_name%> (<%= file.created_at.asctime %>) + :id => file.file_name %>"><%= file.file_name%> (<%= file.created_at.asctime %>) <%= + "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(", ") %>
  • <%- end -%> diff --git a/lib/page_renderer.rb b/lib/page_renderer.rb index 9ad4609a..208dfa7d 100644 --- a/lib/page_renderer.rb +++ b/lib/page_renderer.rb @@ -97,14 +97,29 @@ class PageRenderer def wiki_words @wiki_words_cache ||= find_wiki_words(display_content) end - + 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 # 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 - 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 # 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 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) includes = include_chunks.map { |c| ( c.escaped? ? nil : c.page_name ) }.compact.uniq includes.each do |included_page_name|