| 1 | #!/usr/bin/env ruby |
|---|
| 2 | # estraier-register.rb $Revision: 1.3 $ |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2007 Kazuhiko <kazuhiko@fdiary.net> |
|---|
| 5 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 6 | # |
|---|
| 7 | require "estraierpure" |
|---|
| 8 | |
|---|
| 9 | mode = "" |
|---|
| 10 | if $0 == __FILE__ |
|---|
| 11 | require 'cgi' |
|---|
| 12 | ARGV << '' # dummy argument against cgi.rb offline mode. |
|---|
| 13 | @cgi = CGI::new |
|---|
| 14 | mode = "CMD" |
|---|
| 15 | else |
|---|
| 16 | mode = "PLUGIN" |
|---|
| 17 | end |
|---|
| 18 | |
|---|
| 19 | if mode == "CMD" |
|---|
| 20 | tdiary_path = "." |
|---|
| 21 | tdiary_conf = "." |
|---|
| 22 | $stdout.sync = true |
|---|
| 23 | |
|---|
| 24 | def usage |
|---|
| 25 | puts "hyper-estraier-register.rb $Revision: 1.3 $" |
|---|
| 26 | puts " register to hyper-estraier index files from tDiary's database." |
|---|
| 27 | puts " usage: ruby hyper-estraier-regiser.rb [-p <tDiary directory>] [-c <tdiary.conf directory>]" |
|---|
| 28 | exit |
|---|
| 29 | end |
|---|
| 30 | |
|---|
| 31 | require 'getoptlong' |
|---|
| 32 | parser = GetoptLong::new |
|---|
| 33 | parser.set_options(['--path', '-p', GetoptLong::REQUIRED_ARGUMENT], ['--conf', '-c', GetoptLong::REQUIRED_ARGUMENT]) |
|---|
| 34 | begin |
|---|
| 35 | parser.each do |opt, arg| |
|---|
| 36 | case opt |
|---|
| 37 | when '--path' |
|---|
| 38 | tdiary_path = arg |
|---|
| 39 | when '--conf' |
|---|
| 40 | tdiary_conf = arg |
|---|
| 41 | end |
|---|
| 42 | end |
|---|
| 43 | rescue |
|---|
| 44 | usage |
|---|
| 45 | exit( 1 ) |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | tdiary_conf = tdiary_path unless tdiary_conf |
|---|
| 49 | Dir::chdir( tdiary_conf ) |
|---|
| 50 | |
|---|
| 51 | begin |
|---|
| 52 | $:.unshift tdiary_path |
|---|
| 53 | require "#{tdiary_path}/tdiary" |
|---|
| 54 | rescue LoadError |
|---|
| 55 | $stderr.puts "hyper-estraier-register.rb: cannot load tdiary.rb. <#{tdiary_path}/tdiary>\n" |
|---|
| 56 | $stderr.puts " usage: ruby hyper-estraier-regiser.rb [-p <tDiary directory>] [-c <tdiary.conf directory>]" |
|---|
| 57 | exit( 1 ) |
|---|
| 58 | end |
|---|
| 59 | end |
|---|
| 60 | |
|---|
| 61 | module TDiary |
|---|
| 62 | # |
|---|
| 63 | # Database |
|---|
| 64 | # |
|---|
| 65 | class EstraierDB |
|---|
| 66 | attr_accessor :db |
|---|
| 67 | attr_reader :conf |
|---|
| 68 | |
|---|
| 69 | def initialize(conf) |
|---|
| 70 | @conf = conf |
|---|
| 71 | @host = @conf["estraier.host"] || "localhost" |
|---|
| 72 | @port = @conf["estraier.port"] || 1978 |
|---|
| 73 | @path = @conf["estraier.path"] || "/node/" |
|---|
| 74 | @node = @conf["estraier.node"] || "tdiary" |
|---|
| 75 | @name = @conf["estraier.name"] || "admin" |
|---|
| 76 | @password = @conf["estraier.password"] || "admin" |
|---|
| 77 | end |
|---|
| 78 | |
|---|
| 79 | def transaction |
|---|
| 80 | db = EstraierPure::Node.new |
|---|
| 81 | db.set_url("http://#{@host}:#{@port}#{@path}#{@node}") |
|---|
| 82 | db.set_auth(@name, @password) |
|---|
| 83 | |
|---|
| 84 | if db.doc_num < 0 |
|---|
| 85 | raise "Database not found : http://#{@host}:#{@port}#{@path}#{@node}" |
|---|
| 86 | end |
|---|
| 87 | @db = db |
|---|
| 88 | yield self |
|---|
| 89 | end |
|---|
| 90 | |
|---|
| 91 | def cache_path |
|---|
| 92 | @conf.cache_path || "#{@conf.data_path}cache" |
|---|
| 93 | end |
|---|
| 94 | end |
|---|
| 95 | |
|---|
| 96 | # |
|---|
| 97 | # Register |
|---|
| 98 | # |
|---|
| 99 | class EstraierRegister < TDiaryBase |
|---|
| 100 | def initialize(estraier_db, diary) |
|---|
| 101 | @db = estraier_db.db |
|---|
| 102 | super(CGI::new, 'day.rhtml', estraier_db.conf) |
|---|
| 103 | @diary = diary |
|---|
| 104 | @date = diary.date |
|---|
| 105 | @diaries = {@date.strftime('%Y%m%d') => @diary} if @diaries.empty? |
|---|
| 106 | @plugin = ::TDiary::Plugin::new( |
|---|
| 107 | 'conf' => @conf, |
|---|
| 108 | 'cgi' => @cgi, |
|---|
| 109 | 'cache_path' => cache_path, |
|---|
| 110 | 'diaries' => @diaries |
|---|
| 111 | ) |
|---|
| 112 | def @plugin.apply_plugin_alt( str, remove_tag = false ) |
|---|
| 113 | apply_plugin( str, remove_tag ) |
|---|
| 114 | end |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | def execute(force = false) |
|---|
| 118 | date = @date.strftime('%Y%m%d') |
|---|
| 119 | cond = EstraierPure::Condition.new |
|---|
| 120 | if @conf["estraier.with_user_name"] |
|---|
| 121 | cond.add_attr("@uri STRBW #{@conf.user_name}:#{date}") |
|---|
| 122 | else |
|---|
| 123 | cond.add_attr("@uri STRBW #{date}") |
|---|
| 124 | end |
|---|
| 125 | result = @db.search(cond, 0) |
|---|
| 126 | if result |
|---|
| 127 | for i in 0...result.doc_num |
|---|
| 128 | doc_id = result.get_doc(i).attr("@id") |
|---|
| 129 | @db.out_doc(doc_id) |
|---|
| 130 | end |
|---|
| 131 | end |
|---|
| 132 | return unless @diary.visible? |
|---|
| 133 | |
|---|
| 134 | # body |
|---|
| 135 | index = 0 |
|---|
| 136 | anchor = '' |
|---|
| 137 | @diary.each_section do |section| |
|---|
| 138 | index += 1 |
|---|
| 139 | @conf['apply_plugin'] = true |
|---|
| 140 | anchor = "#{date}p%02d" % index |
|---|
| 141 | title = CGI.unescapeHTML( @plugin.apply_plugin_alt( section.subtitle_to_html, true ).strip ) |
|---|
| 142 | if title.empty? |
|---|
| 143 | title = @plugin.apply_plugin_alt( section.body_to_html, true ).strip |
|---|
| 144 | title = @conf.shorten( CGI.unescapeHTML( title ), 20 ) |
|---|
| 145 | end |
|---|
| 146 | last_modified = @diary.last_modified.strftime("%FT%T") |
|---|
| 147 | body = CGI.unescapeHTML( @plugin.apply_plugin_alt( section.body_to_html, true ).strip ) |
|---|
| 148 | doc = EstraierPure::Document.new |
|---|
| 149 | doc.add_attr("@title", title) |
|---|
| 150 | if @conf["estraier.with_user_name"] |
|---|
| 151 | doc.add_attr("@uri", "#{@conf.user_name}:#{anchor}") |
|---|
| 152 | else |
|---|
| 153 | doc.add_attr("@uri", anchor) |
|---|
| 154 | end |
|---|
| 155 | doc.add_attr("@mdate", last_modified) |
|---|
| 156 | doc.add_hidden_text(title) |
|---|
| 157 | doc.add_text(body) |
|---|
| 158 | @db.put_doc(doc) |
|---|
| 159 | end |
|---|
| 160 | |
|---|
| 161 | # comment |
|---|
| 162 | @diary.each_visible_comment do |comment, index| |
|---|
| 163 | if /^(TrackBack|Pingback)$/i =~ comment.name |
|---|
| 164 | anchor = "#{date}t%02d" % index |
|---|
| 165 | title = "TrackBack (#{comment.name})" |
|---|
| 166 | else |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | anchor = "#{date}c%02d" % index |
|---|
| 170 | title = "#{@plugin.comment_description_short} (#{comment.name})" |
|---|
| 171 | end |
|---|
| 172 | body = comment.body |
|---|
| 173 | doc = EstraierPure::Document.new |
|---|
| 174 | doc.add_attr("@title", title) |
|---|
| 175 | if @conf["estraier.with_user_name"] |
|---|
| 176 | doc.add_attr("@uri", "#{@conf.user_name}:#{anchor}") |
|---|
| 177 | else |
|---|
| 178 | doc.add_attr("@uri", anchor) |
|---|
| 179 | end |
|---|
| 180 | doc.add_attr("@mdate", comment.date.strftime("%FT%T")) |
|---|
| 181 | doc.add_hidden_text(title) |
|---|
| 182 | doc.add_text(body) |
|---|
| 183 | @db.put_doc(doc) |
|---|
| 184 | end |
|---|
| 185 | end |
|---|
| 186 | |
|---|
| 187 | protected |
|---|
| 188 | |
|---|
| 189 | def mode; 'day'; end |
|---|
| 190 | def cookie_name; ''; end |
|---|
| 191 | def cookie_mail; ''; end |
|---|
| 192 | |
|---|
| 193 | def convert(str) |
|---|
| 194 | str |
|---|
| 195 | end |
|---|
| 196 | end |
|---|
| 197 | |
|---|
| 198 | # |
|---|
| 199 | # Main |
|---|
| 200 | # |
|---|
| 201 | class EstraierRegisterMain < TDiaryBase |
|---|
| 202 | def initialize(conf) |
|---|
| 203 | super(CGI::new, 'day.rhtml', conf) |
|---|
| 204 | end |
|---|
| 205 | |
|---|
| 206 | def execute(out = $stdout) |
|---|
| 207 | require 'fileutils' |
|---|
| 208 | calendar |
|---|
| 209 | db = EstraierDB.new(conf) |
|---|
| 210 | db.transaction do |estraier_db| |
|---|
| 211 | @years.keys.sort.reverse_each do |year| |
|---|
| 212 | out << "(#{year.to_s}/) " |
|---|
| 213 | @years[year.to_s].sort.reverse_each do |month| |
|---|
| 214 | @io.transaction(Time::local(year.to_i, month.to_i)) do |diaries| |
|---|
| 215 | diaries.sort.reverse_each do |day, diary| |
|---|
| 216 | EstraierRegister.new(estraier_db, diary).execute |
|---|
| 217 | out << diary.date.strftime('%m%d ') |
|---|
| 218 | end |
|---|
| 219 | false |
|---|
| 220 | end |
|---|
| 221 | end |
|---|
| 222 | end |
|---|
| 223 | end |
|---|
| 224 | end |
|---|
| 225 | end |
|---|
| 226 | end |
|---|
| 227 | |
|---|
| 228 | if mode == "CMD" |
|---|
| 229 | begin |
|---|
| 230 | require 'cgi' |
|---|
| 231 | if TDiary::Config.instance_method(:initialize).arity > 0 |
|---|
| 232 | # for tDiary 2.1 or later |
|---|
| 233 | cgi = CGI.new |
|---|
| 234 | conf = TDiary::Config::new(cgi) |
|---|
| 235 | else |
|---|
| 236 | # for tDiary 2.0 or earlier |
|---|
| 237 | conf = TDiary::Config::new |
|---|
| 238 | end |
|---|
| 239 | conf.header = '' |
|---|
| 240 | conf.footer = '' |
|---|
| 241 | conf.show_comment = true |
|---|
| 242 | conf.hide_comment_form = true |
|---|
| 243 | conf.show_nyear = false |
|---|
| 244 | def conf.bot?; true; end |
|---|
| 245 | TDiary::EstraierRegisterMain.new(conf).execute |
|---|
| 246 | rescue |
|---|
| 247 | print $!, "\n" |
|---|
| 248 | $@.each do |v| |
|---|
| 249 | print v, "\n" |
|---|
| 250 | end |
|---|
| 251 | exit( 1 ) |
|---|
| 252 | end |
|---|
| 253 | |
|---|
| 254 | puts |
|---|
| 255 | else |
|---|
| 256 | add_update_proc do |
|---|
| 257 | conf = @conf.clone |
|---|
| 258 | conf.header = '' |
|---|
| 259 | conf.footer = '' |
|---|
| 260 | conf.show_comment = true |
|---|
| 261 | conf.hide_comment_form = true |
|---|
| 262 | conf.show_nyear = false |
|---|
| 263 | def conf.bot?; true; end |
|---|
| 264 | |
|---|
| 265 | diary = @diaries[@date.strftime('%Y%m%d')] |
|---|
| 266 | TDiary::EstraierDB.new(conf).transaction do |estraier_db| |
|---|
| 267 | TDiary::EstraierRegister.new(estraier_db, diary).execute(true) |
|---|
| 268 | end |
|---|
| 269 | end |
|---|
| 270 | |
|---|
| 271 | if !@conf['estraier.hideconf'] && (@mode == 'conf' || @mode == 'saveconf') |
|---|
| 272 | args = ['estraier_register', @estraier_register_conf_label] |
|---|
| 273 | args << 'update' if TDIARY_VERSION > '2.1.3' |
|---|
| 274 | add_conf_proc(*args) do |
|---|
| 275 | str = <<-HTML |
|---|
| 276 | <h3 class="subtitle">#{@estraier_register_conf_header}</h3> |
|---|
| 277 | <p> |
|---|
| 278 | <label for="estraier_register_rebuild"><input id="estraier_register_rebuild" type="checkbox" name="estraier_register_rebuild" value="1"> |
|---|
| 279 | #{@estraier_register_conf_description}</label> |
|---|
| 280 | </p> |
|---|
| 281 | HTML |
|---|
| 282 | if @mode == 'saveconf' |
|---|
| 283 | if @cgi.valid?( 'estraier_register_rebuild' ) |
|---|
| 284 | str << '<p>The following diaries were registered.</p>' |
|---|
| 285 | out = '' |
|---|
| 286 | TDiary::EstraierRegisterMain.new(@conf).execute(out) |
|---|
| 287 | str << "<p>#{out}</p>" |
|---|
| 288 | end |
|---|
| 289 | end |
|---|
| 290 | str |
|---|
| 291 | end |
|---|
| 292 | end |
|---|
| 293 | end |
|---|