#!/usr/bin/env ruby require 'optparse' require 'erb' require 'tdiary/view_helper' require 'tdiary/base' require 'tdiary/comment' require 'tdiary/comment_manager' require 'tdiary/referer_manager' require 'tdiary/style' require 'tdiary/cache/file' require 'ostruct' require 'tdiary/core_ext' def load_diaries(data_path, style_path) require 'tdiary/io/default' options = { 'style.path' => style_path } conf = OpenStruct.new({ data_path: "#{data_path}/", options: options }) tdiary = OpenStruct.new({conf: conf}) default_io = TDiary::IO::Default.new(tdiary) default_io.calendar.each do |y, ms| ms.each do |m| puts "loading #{y}-#{m}" month = Time.local(y.to_i, m.to_i) default_io.transaction(month) do |diaries| yield month, diaries TDiary::TDiaryBase::DIRTY_NONE end end end end def update_diaries(mongodb_io, month, new_diaries) mongodb_io.transaction(month) do |diaries| diaries.update(new_diaries) TDiary::TDiaryBase::DIRTY_DIARY | TDiary::TDiaryBase::DIRTY_COMMENT end end def store_diaries(data_path, style_path, mongo_url = nil) require 'tdiary/io/mongodb' options = {'style.path' => [style_path]} conf = OpenStruct.new({options: options, database_url: mongo_url}) tdiary = OpenStruct.new({conf: conf}) TDiary::IO::MongoDB.load_cgi_conf(conf) mongodb_io = TDiary::IO::MongoDB.new(tdiary) load_diaries(data_path, style_path) do |month, diaries| update_diaries(mongodb_io, month, diaries) end end def store_conf(conf_path, mongo_url = nil) require 'tdiary/io/mongodb' conf = OpenStruct.new({options: {}, database_url: mongo_url}) tdiary = OpenStruct.new({conf: conf}) TDiary::IO::MongoDB.save_cgi_conf(conf, open(conf_path, &:read)) end args = {} OptionParser.new do |opts| opts.banner = 'Usage: tdiary-mongodb-convert [options] ' opts.on('-c CONF', '--conf=CONF', 'store only tdiary.conf'){|v| args[:conf] = v} opts.on('-s PATH', '--style=STYLE', 'style path'){|v| args[:style] = v} opts.on('-m URL', '--mongo=URL', 'URL of mongoDB'){|v| args[:mongo] = v} opts.parse!(ARGV) args[:data] = ARGV.shift unless args[:data] || args[:conf] $stderr.print opts.help exit 1 end end if args[:conf] store_conf(args[:conf], args[:mongo]) else store_diaries(args[:data], args[:style], args[:mongo]) end