# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/site"

module Qwik
  class SiteLog
    def initialize(config, site)
      @config = config
      @site = site
    end

    def add(t, user, cmd, pagename)
      dbkey("_SiteLog"    ).add(t, user, cmd, pagename)
      dbkey("_SiteChanged").add(t, user, cmd, pagename)
    end

    def list
      dbkey("_SiteLog").hash.sort
    end

    private

    def dbkey(key)
      page = @site[key]
      page = @site.create(key) if page.nil?
      page.wikidb
    end
  end
end

if $0 == __FILE__
  require "qwik/test-module-session"
  $debug = true
end

if $debug
  class TC_SiteLog < Test::Unit::TestCase
    include TestSession

    def test_sitelog_unit
      sitelog = @site.sitelog
      sitelog.add(0, "user@e.com", "save", "1")
      assert_equal(",0,user@e.com,save,1\n", @site["_SiteLog"].load)
      sitelog.add(0, nil, "save", "1")
      assert_equal(",0,user@e.com,save,1\n,0,,save,1\n", @site["_SiteLog"].load)

      # test_sitelog_session
      # not yet
    end
  end
end

