$:.unshift(File.dirname(__FILE__) + "/../../libraries/") require "madeleine_service" require "web" require "page" require "author" class WikiService < MadeleineService attr_reader :webs, :system def initialize @webs, @system = {}, {} end def setup? !@system.empty? end def authenticate(password) password == (@system["password"] || "instiki") end def setup(password, web_name, web_address) @system["password"] = password create_web(web_name, web_address) end def create_web(name, address, password = nil) @webs[address] = Web.new(name, address, password) unless @webs[address] end def update_web(old_address, new_address, name, markup, color, additional_style, safe_mode = false, password = nil, published = false, brackets_only = false, count_pages = false) if old_address != new_address @webs[new_address] = @webs[old_address] @webs.delete(old_address) @webs[new_address].address = new_address end web = @webs[new_address] web.refresh_revisions if settings_changed?(web, markup, safe_mode, brackets_only) web.name, web.markup, web.color, web.additional_style, web.safe_mode = name, markup, color, additional_style, safe_mode web.password, web.published, web.brackets_only, web.count_pages = password, published, brackets_only, count_pages end def read_page(web_address, page_name) web = @webs[web_address] web ? web.pages[page_name] : nil end def write_page(web_address, page_name, content, written_on, author) page = Page.new(@webs[web_address], page_name, content, written_on, author) @webs[web_address].add_page(page) page end def revise_page(web_address, page_name, content, revised_on, author) page = read_page(web_address, page_name) page.revise(content, revised_on, author) page end def rollback_page(web_address, page_name, revision_number, created_at, author_id = nil) page = read_page(web_address, page_name) page.rollback(revision_number, created_at, author_id) page end def remove_orphaned_pages(web_address) @webs[web_address].remove_pages(@webs[web_address].select.orphaned_pages) end private def settings_changed?(web, markup, safe_mode, brackets_only) web.markup != markup || web.safe_mode != safe_mode || web.brackets_only != brackets_only end end