2007-01-22 14:43:50 +01:00
|
|
|
class WikiReference < ActiveRecord::Base
|
|
|
|
|
|
|
|
LINKED_PAGE = 'L'
|
|
|
|
WANTED_PAGE = 'W'
|
2009-06-03 05:17:15 +02:00
|
|
|
REDIRECTED_PAGE = 'R'
|
2007-01-22 14:43:50 +01:00
|
|
|
INCLUDED_PAGE = 'I'
|
|
|
|
CATEGORY = 'C'
|
|
|
|
AUTHOR = 'A'
|
|
|
|
FILE = 'F'
|
|
|
|
WANTED_FILE = 'E'
|
|
|
|
|
|
|
|
belongs_to :page
|
2009-06-03 05:17:15 +02:00
|
|
|
validates_inclusion_of :link_type, :in => [LINKED_PAGE, WANTED_PAGE, REDIRECTED_PAGE, INCLUDED_PAGE, CATEGORY, AUTHOR, FILE, WANTED_FILE]
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2010-01-06 15:15:34 +01:00
|
|
|
def referenced_name
|
|
|
|
read_attribute(:referenced_name).as_utf8
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def self.link_type(web, page_name)
|
2009-06-11 03:49:48 +02:00
|
|
|
if web.has_page?(page_name) || self.page_that_redirects_for(web, page_name)
|
2009-06-06 05:48:32 +02:00
|
|
|
LINKED_PAGE
|
|
|
|
else
|
|
|
|
WANTED_PAGE
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2007-03-10 22:09:20 +01:00
|
|
|
def self.pages_that_reference(web, page_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 ('#{LINKED_PAGE}', '#{WANTED_PAGE}', '#{INCLUDED_PAGE}') " +
|
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2007-01-22 14:43:50 +01:00
|
|
|
names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] }
|
|
|
|
end
|
|
|
|
|
2007-03-10 22:09:20 +01:00
|
|
|
def self.pages_that_link_to(web, page_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 ('#{LINKED_PAGE}','#{WANTED_PAGE}') " +
|
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2007-01-22 14:43:50 +01:00
|
|
|
names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] }
|
|
|
|
end
|
2007-03-10 22:09:20 +01:00
|
|
|
|
2009-01-10 07:18:25 +01:00
|
|
|
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
|
|
|
|
|
2007-03-10 22:09:20 +01:00
|
|
|
def self.pages_that_include(web, page_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 = '#{INCLUDED_PAGE}' " +
|
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2007-01-22 14:43:50 +01:00
|
|
|
names = connection.select_all(sanitize_sql([query, page_name])).map { |row| row['name'] }
|
|
|
|
end
|
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
def self.pages_redirected_to(web, page_name)
|
|
|
|
names = []
|
2009-06-06 19:37:00 +02:00
|
|
|
redirected_pages = []
|
2009-06-05 07:39:12 +02:00
|
|
|
page = web.page(page_name)
|
2009-06-06 19:37:00 +02:00
|
|
|
redirected_pages.concat page.redirects
|
2009-06-07 04:08:08 +02:00
|
|
|
redirected_pages.concat Thread.current[:page_redirects][page] if
|
|
|
|
Thread.current[:page_redirects] && Thread.current[:page_redirects][page]
|
|
|
|
redirected_pages.uniq.each { |name| names.concat self.pages_that_reference(web, name) }
|
2009-06-05 07:39:12 +02:00
|
|
|
names.uniq
|
2009-06-03 05:17:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.page_that_redirects_for(web, page_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 = '#{REDIRECTED_PAGE}' " +
|
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2009-06-05 07:39:12 +02:00
|
|
|
row = connection.select_one(sanitize_sql([query, page_name]))
|
2010-01-24 19:23:47 +01:00
|
|
|
row['name'].as_utf8 if row
|
2009-06-03 05:17:15 +02:00
|
|
|
end
|
2010-01-29 06:37:10 +01:00
|
|
|
|
2007-03-10 22:09:20 +01:00
|
|
|
def self.pages_in_category(web, category)
|
2007-01-22 14:43:50 +01:00
|
|
|
query =
|
2007-03-10 22:09:20 +01:00
|
|
|
"SELECT name FROM pages JOIN wiki_references " +
|
|
|
|
"ON pages.id = wiki_references.page_id " +
|
|
|
|
"WHERE wiki_references.referenced_name = ? " +
|
|
|
|
"AND wiki_references.link_type = '#{CATEGORY}' " +
|
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2010-01-24 19:23:47 +01:00
|
|
|
names = connection.select_all(sanitize_sql([query, category])).map { |row| row['name'].as_utf8 }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2007-02-28 04:27:20 +01:00
|
|
|
def self.list_categories(web)
|
|
|
|
query = "SELECT DISTINCT wiki_references.referenced_name " +
|
|
|
|
"FROM wiki_references LEFT OUTER JOIN pages " +
|
|
|
|
"ON wiki_references.page_id = pages.id " +
|
|
|
|
"WHERE wiki_references.link_type = '#{CATEGORY}' " +
|
2007-03-09 04:57:21 +01:00
|
|
|
"AND pages.web_id = '#{web.id}'"
|
2010-01-06 15:15:34 +01:00
|
|
|
connection.select_all(query).map { |row| row['referenced_name'].as_utf8 }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_word?
|
|
|
|
linked_page? or wanted_page?
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_link?
|
|
|
|
linked_page? or wanted_page? or file? or wanted_file?
|
|
|
|
end
|
|
|
|
|
|
|
|
def linked_page?
|
|
|
|
link_type == LINKED_PAGE
|
|
|
|
end
|
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
def redirected_page?
|
|
|
|
link_type == REDIRECTED_PAGE
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def wanted_page?
|
|
|
|
link_type == WANTED_PAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
def included_page?
|
|
|
|
link_type == INCLUDED_PAGE
|
|
|
|
end
|
|
|
|
|
|
|
|
def file?
|
|
|
|
link_type == FILE
|
|
|
|
end
|
|
|
|
|
|
|
|
def wanted_file?
|
|
|
|
link_type == WANTED_FILE
|
|
|
|
end
|
|
|
|
|
2010-01-29 18:13:16 +01:00
|
|
|
def category?
|
|
|
|
link_type == CATEGORY
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|