2005-01-15 21:26:54 +01:00
|
|
|
require 'open-uri'
|
|
|
|
require 'yaml'
|
|
|
|
require 'madeleine'
|
|
|
|
require 'madeleine/automatic'
|
|
|
|
require 'madeleine/zmarshal'
|
|
|
|
|
|
|
|
require 'web'
|
|
|
|
require 'page'
|
|
|
|
require 'author'
|
2005-01-23 02:36:51 +01:00
|
|
|
require 'file_yard'
|
2005-01-15 21:26:54 +01:00
|
|
|
|
|
|
|
module AbstractWikiService
|
|
|
|
|
|
|
|
attr_reader :webs, :system
|
|
|
|
|
|
|
|
def authenticate(password)
|
|
|
|
password == (@system[:password] || 'instiki')
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_web(name, address, password = nil)
|
2005-01-23 04:27:45 +01:00
|
|
|
@webs[address] = Web.new(self, name, address, password) unless @webs[address]
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
2005-01-17 00:17:30 +01:00
|
|
|
def delete_web(address)
|
|
|
|
@webs[address] = nil
|
|
|
|
end
|
|
|
|
|
2005-01-23 02:36:51 +01:00
|
|
|
def file_yard(web)
|
|
|
|
raise "Web #{@web.name} does not belong to this wiki service" unless @webs.values.include?(web)
|
|
|
|
# TODO cache FileYards
|
|
|
|
FileYard.new("#{self.storage_path}/#{web.address}")
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
def init_wiki_service
|
|
|
|
@webs = {}
|
|
|
|
@system = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_page(web_address, page_name)
|
|
|
|
ApplicationController.logger.debug "Reading page '#{page_name}' from web '#{web_address}'"
|
|
|
|
web = @webs[web_address]
|
|
|
|
if web.nil?
|
|
|
|
ApplicationController.logger.debug "Web '#{web_address}' not found"
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
page = web.pages[page_name]
|
|
|
|
ApplicationController.logger.debug "Page '#{page_name}' #{page.nil? ? 'not' : ''} found"
|
|
|
|
return page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_orphaned_pages(web_address)
|
|
|
|
@webs[web_address].remove_pages(@webs[web_address].select.orphaned_pages)
|
|
|
|
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 setup(password, web_name, web_address)
|
|
|
|
@system[:password] = password
|
|
|
|
create_web(web_name, web_address)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup?
|
|
|
|
not (@webs.empty?)
|
|
|
|
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 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
|
2005-01-22 03:49:52 +01:00
|
|
|
|
|
|
|
def storage_path
|
|
|
|
self.class.storage_path
|
|
|
|
end
|
2005-01-15 21:26:54 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class WikiService
|
|
|
|
|
|
|
|
include AbstractWikiService
|
|
|
|
include Madeleine::Automatic::Interceptor
|
2005-01-17 00:51:59 +01:00
|
|
|
|
|
|
|
# These methods do not change the state of persistent objects, and
|
|
|
|
# should not be ogged by Madeleine
|
2005-01-23 02:36:51 +01:00
|
|
|
automatic_read_only :authenticate, :read_page, :setup?, :webs, :storage_path, :file_yard
|
2005-01-15 21:26:54 +01:00
|
|
|
|
2005-01-16 02:11:08 +01:00
|
|
|
@@storage_path = './storage/'
|
2005-01-15 21:26:54 +01:00
|
|
|
|
|
|
|
class << self
|
2005-01-22 03:49:52 +01:00
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
def storage_path=(storage_path)
|
|
|
|
@@storage_path = storage_path
|
|
|
|
end
|
|
|
|
|
2005-01-22 03:49:52 +01:00
|
|
|
def storage_path
|
|
|
|
@@storage_path
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
def clean_storage
|
|
|
|
MadeleineServer.clean_storage(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance
|
2005-01-17 00:51:59 +01:00
|
|
|
@madeleine ||= MadeleineServer.new(self)
|
|
|
|
@system = @madeleine.system
|
|
|
|
return @system
|
|
|
|
end
|
|
|
|
|
|
|
|
def snapshot
|
|
|
|
@madeleine.snapshot
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
2005-01-17 00:51:59 +01:00
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
init_wiki_service
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class MadeleineServer
|
2005-01-16 04:05:45 +01:00
|
|
|
|
|
|
|
attr_reader :storage_path
|
2005-01-15 21:26:54 +01:00
|
|
|
|
|
|
|
# Clears all the command_log and snapshot files located in the storage directory, so the
|
|
|
|
# database is essentially dropped and recreated as blank
|
|
|
|
def self.clean_storage(service)
|
|
|
|
begin
|
|
|
|
Dir.foreach(service.storage_path) do |file|
|
|
|
|
if file =~ /(command_log|snapshot)$/
|
|
|
|
File.delete(File.join(service.storage_path, file))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
Dir.mkdir(service.storage_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(service)
|
2005-01-16 04:05:45 +01:00
|
|
|
@storage_path = service.storage_path
|
2005-01-15 21:26:54 +01:00
|
|
|
@server = Madeleine::Automatic::AutomaticSnapshotMadeleine.new(service.storage_path,
|
|
|
|
Madeleine::ZMarshal.new) {
|
|
|
|
service.new
|
|
|
|
}
|
2005-01-16 04:05:45 +01:00
|
|
|
start_snapshot_thread
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
|
2005-01-16 04:05:45 +01:00
|
|
|
def command_log_present?
|
|
|
|
not Dir[storage_path + '/*.command_log'].empty?
|
|
|
|
end
|
|
|
|
|
2005-01-17 00:51:59 +01:00
|
|
|
def snapshot
|
|
|
|
@server.take_snapshot
|
|
|
|
end
|
|
|
|
|
2005-01-15 21:26:54 +01:00
|
|
|
def start_snapshot_thread
|
|
|
|
Thread.new(@server) {
|
2005-01-16 04:05:45 +01:00
|
|
|
hours_since_last_snapshot = 0
|
2005-01-15 21:26:54 +01:00
|
|
|
while true
|
2005-01-16 04:05:45 +01:00
|
|
|
begin
|
|
|
|
hours_since_last_snapshot += 1
|
|
|
|
# Take a snapshot if there is a command log, or 24 hours
|
|
|
|
# have passed since the last snapshot
|
|
|
|
if command_log_present? or hours_since_last_snapshot >= 24
|
|
|
|
ActionController::Base.logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] " +
|
|
|
|
'Taking a Madeleine snapshot'
|
2005-01-17 00:51:59 +01:00
|
|
|
snapshot
|
2005-01-16 04:05:45 +01:00
|
|
|
hours_since_last_snapshot = 0
|
|
|
|
end
|
|
|
|
sleep(1.hour)
|
|
|
|
rescue => e
|
|
|
|
ActionController::Base.logger.error(e)
|
|
|
|
# wait for a minute (not to spoof the log with the same error)
|
|
|
|
# and go back into the loop, to keep trying
|
|
|
|
sleep(1.minute)
|
|
|
|
ActionController::Base.logger.info("Retrying to save a snapshot")
|
|
|
|
end
|
2005-01-15 21:26:54 +01:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2005-01-17 00:51:59 +01:00
|
|
|
|
|
|
|
def system
|
|
|
|
@server.system
|
|
|
|
end
|
2005-01-15 21:26:54 +01:00
|
|
|
|
|
|
|
end
|