2005-08-15 21:17:32 +02:00
|
|
|
class Web < ActiveRecord::Base
|
2005-09-11 11:34:41 +02:00
|
|
|
has_many :pages
|
2005-11-13 14:37:47 +01:00
|
|
|
has_many :wiki_files
|
2005-08-15 21:17:32 +02:00
|
|
|
|
|
|
|
def wiki
|
|
|
|
Wiki.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings_changed?(markup, safe_mode, brackets_only)
|
|
|
|
self.markup != markup ||
|
|
|
|
self.safe_mode != safe_mode ||
|
|
|
|
self.brackets_only != brackets_only
|
|
|
|
end
|
|
|
|
|
2005-09-10 08:12:57 +02:00
|
|
|
def add_page(name, content, time, author, renderer)
|
2005-08-15 21:17:32 +02:00
|
|
|
page = page(name) || Page.new(:web => self, :name => name)
|
2005-09-10 08:12:57 +02:00
|
|
|
page.revise(content, time, author, renderer)
|
2005-08-15 21:17:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def authors
|
2005-09-27 15:46:02 +02:00
|
|
|
connection.select_all(
|
|
|
|
'SELECT DISTINCT r.author AS author ' +
|
|
|
|
'FROM revisions r ' +
|
|
|
|
'JOIN pages p ON p.id = r.page_id ' +
|
2007-01-18 14:33:52 +01:00
|
|
|
'WHERE p.web_id = ' + self.id.to_s +
|
2007-02-08 23:01:36 +01:00
|
|
|
' ORDER by 1 '
|
2007-01-18 14:33:52 +01:00
|
|
|
).collect { |row| row['author'] }
|
2005-08-15 21:17:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def categories
|
|
|
|
select.map { |page| page.categories }.flatten.uniq.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def page(name)
|
|
|
|
pages.find(:first, :conditions => ['name = ?', name])
|
|
|
|
end
|
2007-01-18 18:57:16 +01:00
|
|
|
|
|
|
|
def last_page
|
|
|
|
return Page.find(:first, :order => 'id desc', :conditions => ['web_id = ?', self.id])
|
|
|
|
end
|
2005-08-15 21:17:32 +02:00
|
|
|
|
|
|
|
def has_page?(name)
|
2007-05-08 00:46:00 +02:00
|
|
|
Page.count(:conditions => ['web_id = ? AND name = ?', id, name]) > 0
|
2005-08-15 21:17:32 +02:00
|
|
|
end
|
|
|
|
|
2005-11-13 14:37:47 +01:00
|
|
|
def has_file?(file_name)
|
|
|
|
WikiFile.find_by_file_name(file_name) != nil
|
2005-08-15 21:17:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def markup
|
|
|
|
read_attribute('markup').to_sym
|
|
|
|
end
|
|
|
|
|
2005-09-27 15:46:02 +02:00
|
|
|
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
|
|
|
|
|
2005-08-15 21:17:32 +02:00
|
|
|
def remove_pages(pages_to_be_removed)
|
|
|
|
pages_to_be_removed.each { |p| p.destroy }
|
|
|
|
end
|
|
|
|
|
|
|
|
def revised_at
|
|
|
|
select.most_recent_revision
|
|
|
|
end
|
|
|
|
|
|
|
|
def select(&condition)
|
|
|
|
PageSet.new(self, pages, condition)
|
|
|
|
end
|
|
|
|
|
2005-09-11 18:49:08 +02:00
|
|
|
def select_all
|
|
|
|
PageSet.new(self, pages, nil)
|
|
|
|
end
|
|
|
|
|
2005-10-27 07:29:24 +02:00
|
|
|
def to_param
|
|
|
|
address
|
|
|
|
end
|
|
|
|
|
2005-11-14 09:38:37 +01:00
|
|
|
def create_files_directory
|
|
|
|
return unless allow_uploads == 1
|
|
|
|
dummy_file = self.wiki_files.build(:file_name => '0', :description => '0', :content => '0')
|
|
|
|
dir = File.dirname(dummy_file.content_path)
|
|
|
|
begin
|
|
|
|
require 'fileutils'
|
|
|
|
FileUtils.mkdir_p dir
|
|
|
|
dummy_file.save
|
|
|
|
dummy_file.destroy
|
|
|
|
rescue => e
|
|
|
|
logger.error("Failed create files directory for #{self.address}: #{e}")
|
|
|
|
raise "Instiki could not create directory to store uploaded files. " +
|
|
|
|
"Please make sure that Instiki is allowed to create directory " +
|
|
|
|
"#{File.expand_path(dir)} and add files to it."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-08-15 21:17:32 +02: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
|
|
|
|
|
|
|
|
# Returns an array of all the page names on this web
|
|
|
|
def page_names
|
|
|
|
pages.map { |p| p.name }
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
before_save :sanitize_markup
|
2005-11-13 14:37:47 +01:00
|
|
|
after_save :create_files_directory
|
2005-08-15 21:17:32 +02:00
|
|
|
before_validation :validate_address
|
|
|
|
validates_uniqueness_of :address
|
|
|
|
validates_length_of :color, :in => 3..6
|
|
|
|
|
|
|
|
def sanitize_markup
|
|
|
|
self.markup = markup.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2005-11-13 14:37:47 +01:00
|
|
|
|
|
|
|
def default_web?
|
|
|
|
defined? DEFAULT_WEB and self.address == DEFAULT_WEB
|
|
|
|
end
|
|
|
|
|
|
|
|
def files_path
|
|
|
|
if default_web?
|
|
|
|
"#{RAILS_ROOT}/public/files"
|
2005-11-14 09:38:37 +01:00
|
|
|
else
|
|
|
|
"#{RAILS_ROOT}/public/#{self.address}/files"
|
2005-11-13 14:37:47 +01:00
|
|
|
end
|
|
|
|
end
|
2005-08-09 04:20:28 +02:00
|
|
|
end
|