instiki/app/models/wiki.rb

98 lines
3.2 KiB
Ruby
Raw Normal View History

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
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
end
def read_page(web_address, page_name)
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?
ApplicationController.logger.debug "Web '#{web_address}' not found"
2007-01-22 14:43:50 +01:00
return nil
else
page = web.pages.first(:conditions => ['name = ?', page_name])
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
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
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)
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
@system ||= (System.first() || System.create)
2007-01-22 14:43:50 +01:00
end
def setup?
Web.count > 0
end
def webs
@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
end