2007-03-21 21:37:29 +01:00
|
|
|
class Wiki
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
cattr_accessor :storage_path, :logger
|
2009-11-22 08:32:58 +01:00
|
|
|
self.storage_path = Rails.root.join('storage')
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def authenticate(password)
|
|
|
|
password == (system.password || 'instiki')
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_web(name, address, password = nil)
|
|
|
|
@webs = nil
|
|
|
|
Web.create(:name => name, :address => address, :password => password)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_web(address)
|
|
|
|
web = Web.find_by_address(address)
|
|
|
|
unless web.nil?
|
|
|
|
web.destroy
|
|
|
|
@webs = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_web(old_address, new_address, name, markup, color, additional_style, safe_mode = false,
|
|
|
|
password = nil, published = false, brackets_only = false, count_pages = false,
|
|
|
|
allow_uploads = true, max_upload_size = nil)
|
|
|
|
|
|
|
|
if not (web = Web.find_by_address(old_address))
|
|
|
|
raise Instiki::ValidationError.new("Web with address '#{old_address}' does not exist")
|
|
|
|
end
|
2009-12-27 06:43:18 +01:00
|
|
|
old_files_path = web.files_path
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
web.update_attributes(:address => new_address, :name => name, :markup => markup, :color => color,
|
|
|
|
:additional_style => additional_style, :safe_mode => safe_mode, :password => password, :published => published,
|
|
|
|
:brackets_only => brackets_only, :count_pages => count_pages, :allow_uploads => allow_uploads, :max_upload_size => max_upload_size)
|
|
|
|
@webs = nil
|
|
|
|
raise Instiki::ValidationError.new("There is already a web with address '#{new_address}'") unless web.errors.on(:address).nil?
|
|
|
|
web
|
2009-12-27 06:43:18 +01:00
|
|
|
move_files(old_files_path, web.files_path)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2009-12-27 06:43:18 +01:00
|
|
|
|
|
|
|
def move_files(old_path, new_path)
|
|
|
|
return if new_path == old_path
|
|
|
|
default_path = Rails.root.join("webs", "files")
|
2009-12-27 07:41:04 +01:00
|
|
|
FileUtils.rmdir(new_path) if File.exist?(new_path)
|
2009-12-27 06:43:18 +01:00
|
|
|
if [old_path, new_path].include? default_path
|
|
|
|
File.rename(old_path, new_path)
|
|
|
|
FileUtils.rmdir(old_path.parent) unless old_path == default_path
|
|
|
|
else
|
|
|
|
File.rename(old_path.parent, new_path.parent)
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
def read_page(web_address, page_name)
|
2007-03-21 21:37:29 +01:00
|
|
|
ApplicationController.logger.debug "Reading page '#{page_name}' from web '#{web_address}'"
|
2007-01-22 14:43:50 +01:00
|
|
|
web = Web.find_by_address(web_address)
|
|
|
|
if web.nil?
|
2007-03-21 21:37:29 +01:00
|
|
|
ApplicationController.logger.debug "Web '#{web_address}' not found"
|
2007-01-22 14:43:50 +01:00
|
|
|
return nil
|
|
|
|
else
|
2009-03-06 04:42:41 +01:00
|
|
|
page = web.pages.first(:conditions => ['name = ?', page_name])
|
2007-03-21 21:37:29 +01:00
|
|
|
ApplicationController.logger.debug "Page '#{page_name}' #{page.nil? ? 'not' : ''} found"
|
2007-01-22 14:43:50 +01:00
|
|
|
return page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_orphaned_pages(web_address)
|
|
|
|
web = Web.find_by_address(web_address)
|
|
|
|
web.remove_pages(web.select.orphaned_pages)
|
|
|
|
end
|
|
|
|
|
2008-12-06 23:11:47 +01:00
|
|
|
def remove_orphaned_pages_in_category(web_address,category)
|
|
|
|
web = Web.find_by_address(web_address)
|
|
|
|
pages_in_category = PageSet.new(web, web.select.pages_in_category(category))
|
|
|
|
web.remove_pages(pages_in_category.orphaned_pages)
|
|
|
|
end
|
|
|
|
|
2009-06-03 05:17:15 +02:00
|
|
|
def revise_page(web_address, page_name, new_name, content, revised_at, author, renderer)
|
2007-01-22 14:43:50 +01:00
|
|
|
page = read_page(web_address, page_name)
|
2009-06-03 05:17:15 +02:00
|
|
|
page.revise(content, new_name, revised_at, author, renderer)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_page(web_address, page_name, revision_number, time, author_id = nil)
|
|
|
|
page = read_page(web_address, page_name)
|
|
|
|
page.rollback(revision_number, time, author_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup(password, web_name, web_address)
|
|
|
|
system.update_attribute(:password, password)
|
|
|
|
create_web(web_name, web_address)
|
|
|
|
end
|
|
|
|
|
|
|
|
def system
|
2009-03-06 04:42:41 +01:00
|
|
|
@system ||= (System.first() || System.create)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup?
|
|
|
|
Web.count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def webs
|
2009-03-06 04:42:41 +01:00
|
|
|
@webs ||= Web.all.inject({}) { |webs, web| webs.merge(web.address => web) }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def storage_path
|
|
|
|
self.class.storage_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_page(web_address, page_name, content, written_on, author, renderer)
|
|
|
|
Web.find_by_address(web_address).add_page(page_name, content, written_on, author, renderer)
|
|
|
|
end
|
2007-03-13 20:54:43 +01:00
|
|
|
end
|