2007-01-22 14:43:50 +01:00
|
|
|
class Web < ActiveRecord::Base
|
2009-09-06 06:02:52 +02:00
|
|
|
## Associations
|
|
|
|
|
|
|
|
has_many :pages, :dependent => :destroy
|
2008-12-29 04:36:37 +01:00
|
|
|
has_many :wiki_files, :dependent => :destroy
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
has_many :revisions, :through => :pages
|
|
|
|
|
|
|
|
## Hooks
|
|
|
|
|
|
|
|
before_save :sanitize_markup
|
|
|
|
after_save :create_files_directory
|
|
|
|
|
|
|
|
before_validation :validate_address
|
|
|
|
|
|
|
|
## Validations
|
|
|
|
|
|
|
|
validates_uniqueness_of :address
|
|
|
|
|
|
|
|
validates_length_of :color, :in => 3..6
|
|
|
|
|
|
|
|
## Methods
|
|
|
|
|
|
|
|
# @return [Wiki] a new Wiki instance
|
2007-01-22 14:43:50 +01:00
|
|
|
def wiki
|
|
|
|
Wiki.new
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def settings_changed?(markup, safe_mode, brackets_only)
|
|
|
|
self.markup != markup ||
|
|
|
|
self.safe_mode != safe_mode ||
|
|
|
|
self.brackets_only != brackets_only
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def add_page(name, content, time, author, renderer)
|
2009-09-06 06:02:52 +02:00
|
|
|
page = page(name) || pages.build(:name => name)
|
2009-06-03 05:17:15 +02:00
|
|
|
page.revise(content, name, time, author, renderer)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
# @return [Array<String>] a collection of all the names of the authors that
|
|
|
|
# have ever contributed to the pages for this Web
|
2007-01-22 14:43:50 +01:00
|
|
|
def authors
|
2009-09-06 06:02:52 +02:00
|
|
|
revisions.all(
|
|
|
|
:select => "DISTINCT revisions.author",
|
|
|
|
:order => "1"
|
|
|
|
).collect(&:author)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def categories
|
|
|
|
select.map { |page| page.categories }.flatten.uniq.sort
|
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @param [String] name the name of some associated Page record to find
|
|
|
|
# @return [Page, nil] the associated Page record, or +nil+ if no record is
|
|
|
|
# found with the provided name
|
2007-01-22 14:43:50 +01:00
|
|
|
def page(name)
|
2009-09-06 06:02:52 +02:00
|
|
|
pages.find_by_name(name)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
# @return [Page] the last associated Page record
|
2007-01-22 14:43:50 +01:00
|
|
|
def last_page
|
2009-09-06 06:02:52 +02:00
|
|
|
pages.last
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @param [String] name the name of some potential Page record
|
|
|
|
# @return [Boolean] whether or not a given Page record exists with a given
|
|
|
|
# name
|
2007-01-22 14:43:50 +01:00
|
|
|
def has_page?(name)
|
2009-09-06 06:02:52 +02:00
|
|
|
pages.exists?(:name => name)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
def has_redirect_for?(name)
|
|
|
|
WikiReference.page_that_redirects_for(self, name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def page_that_redirects_for(name)
|
|
|
|
page(WikiReference.page_that_redirects_for(self, name))
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def has_file?(file_name)
|
2009-09-06 06:02:52 +02:00
|
|
|
wiki_files.exists?(:file_name => file_name)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
def file_list(sort_order="file_name")
|
|
|
|
wiki_files.all(:order => sort_order)
|
2008-12-31 10:54:23 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
def pages_that_link_to(page_name)
|
|
|
|
WikiReference.pages_that_link_to(self, page_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pages_that_link_to_file(file_name)
|
2009-01-10 07:18:25 +01:00
|
|
|
WikiReference.pages_that_link_to_file(self, file_name)
|
|
|
|
end
|
2009-06-03 05:17:15 +02:00
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @param [String] file_name the name of some WikiFile of interest
|
|
|
|
# @return [String, nil] the description of some WikiFile of interest, nil if
|
|
|
|
# the WikiFile could not be found
|
2007-10-09 09:51:38 +02:00
|
|
|
def description(file_name)
|
2009-09-06 06:02:52 +02:00
|
|
|
wiki_files.find_by_file_name(file_name).try(:description)
|
2007-10-09 09:51:38 +02:00
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @return [Symbol] the type of markup used by this Web
|
2007-01-22 14:43:50 +01:00
|
|
|
def markup
|
2009-09-06 06:02:52 +02:00
|
|
|
self[:markup].to_sym
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @return [Hash] a Hash wherein the key is some author's name, and the
|
|
|
|
# values are an array of page names for that author.
|
2007-01-22 14:43:50 +01:00
|
|
|
def page_names_by_author
|
2009-09-06 06:02:52 +02:00
|
|
|
data = revisions.all(
|
|
|
|
:select => "DISTINCT revisions.author AS author, pages.name AS page_name",
|
|
|
|
:order => "pages.name"
|
|
|
|
)
|
|
|
|
|
|
|
|
data.inject({}) do |result, revision|
|
|
|
|
result[revision.author] ||= []
|
|
|
|
result[revision.author] << revision.page_name
|
|
|
|
result
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# OPTIMIZE Use the +delete_all+ with conditions for extra efficiency
|
2007-01-22 14:43:50 +01:00
|
|
|
def remove_pages(pages_to_be_removed)
|
|
|
|
pages_to_be_removed.each { |p| p.destroy }
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def revised_at
|
|
|
|
select.most_recent_revision
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def select(&condition)
|
|
|
|
PageSet.new(self, pages, condition)
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def select_all
|
|
|
|
PageSet.new(self, pages, nil)
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
# @return [String] uses the +address+ attribute for this record's parameter name
|
2007-01-22 14:43:50 +01:00
|
|
|
def to_param
|
|
|
|
address
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
# Called by an +after_save+ hook. Creates the directory that houses this
|
|
|
|
# Web's associated files.
|
|
|
|
#
|
|
|
|
# TODO Move this into the WikiFile model
|
2007-01-22 14:43:50 +01:00
|
|
|
def create_files_directory
|
|
|
|
return unless allow_uploads == 1
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
dummy_file = wiki_files.build(
|
|
|
|
:file_name => "0",
|
|
|
|
:description => "0",
|
|
|
|
:content => "0"
|
|
|
|
)
|
|
|
|
|
2007-03-10 18:26:30 +01:00
|
|
|
File.umask(0002)
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
begin
|
2009-09-06 06:02:52 +02:00
|
|
|
dummy_file.content_path.parent.mkpath
|
2007-01-22 14:43:50 +01:00
|
|
|
dummy_file.save
|
|
|
|
dummy_file.destroy
|
|
|
|
rescue => e
|
2009-09-06 06:02:52 +02:00
|
|
|
logger.error "Failed create files directory for #{address}: #{e}"
|
2007-01-22 14:43:50 +01:00
|
|
|
raise "Instiki could not create directory to store uploaded files. " +
|
|
|
|
"Please make sure that Instiki is allowed to create directory " +
|
2009-09-06 06:02:52 +02:00
|
|
|
"#{dummy_file.content_path.expand_path} and add files to it."
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
2009-08-28 18:10:34 +02:00
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @return [Pathname] the path to the files for this record
|
2009-01-26 08:39:04 +01:00
|
|
|
def files_path
|
2009-08-28 18:10:34 +02:00
|
|
|
path = Rails.root.join("webs")
|
2009-01-26 08:39:04 +01:00
|
|
|
if default_web?
|
2009-08-28 18:10:34 +02:00
|
|
|
path.join("files")
|
2009-01-26 08:39:04 +01:00
|
|
|
else
|
2009-08-28 18:10:34 +02:00
|
|
|
path.join(address, "files")
|
2009-01-26 08:39:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-06 06:02:52 +02:00
|
|
|
# @return [Pathname] the path to PNGs for this record
|
2009-01-27 18:35:05 +01:00
|
|
|
def blahtex_pngs_path
|
2009-08-28 18:10:34 +02:00
|
|
|
files_path.join("pngs")
|
2009-01-27 18:35:05 +01:00
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
# Returns an array of all the wiki words in any current revision
|
|
|
|
def wiki_words
|
|
|
|
pages.inject([]) { |wiki_words, page| wiki_words << page.wiki_words }.flatten.uniq
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
# Returns an array of all the page names on this web
|
|
|
|
def page_names
|
|
|
|
pages.map { |p| p.name }
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
protected
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def sanitize_markup
|
|
|
|
self.markup = markup.to_s
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def validate_address
|
|
|
|
unless address == CGI.escape(address)
|
|
|
|
self.errors.add(:address, 'should contain only valid URI characters')
|
|
|
|
raise Instiki::ValidationError.new("#{self.class.human_attribute_name('address')} #{errors.on(:address)}")
|
|
|
|
end
|
|
|
|
end
|
2009-09-06 06:02:52 +02:00
|
|
|
|
|
|
|
# @return [Boolean] whether or not this record is considered the default Web
|
2007-01-22 14:43:50 +01:00
|
|
|
def default_web?
|
2009-09-06 06:02:52 +02:00
|
|
|
defined?(DEFAULT_WEB) && address == DEFAULT_WEB
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|