tdiary 4.0.1.20130929 → 4.0.1.20131102

Sign up to get free protection for your applications and to get access to all the features.
data/tdiary/io/rdb.rb DELETED
@@ -1,213 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # rdb.rb: RDB IO for tDiary 3.x
4
- #
5
- # NAME rdb
6
- #
7
- # DESCRIPTION RDB 向け tDiary IO クラス
8
- # 日記データは RDB に保存、リンク元の記録には未対応
9
- #
10
- # Copyright (C) 2003 ma2tak <ma2tak@ma2tak.dyndns.org>
11
- # (C) 2004 moonwolf <moonwolf@mooonwolf.com>
12
- # (C) 2005 Kazuhiko <kazuhiko@fdiary.net>
13
- # (C) 2012 hsbt <shibata.hiroshi@gmail.com>
14
- #
15
- # You can distribute this under GPL.
16
- require 'tdiary/io/base'
17
- require 'tempfile'
18
- require 'sequel'
19
-
20
- module TDiary
21
- module CommentIO
22
- def restore_comment(diaries)
23
- diaries.each do |date, diary_object|
24
- db[:comments].filter(:diary_id => date).order_by(:no).select(:name, :mail, :last_modified, :visible, :comment).each do |row|
25
- comment = Comment.new(row[:name], row[:mail], row[:comment], Time.at(row[:last_modified].to_i))
26
- comment.show = row[:visible]
27
- diary_object.add_comment(comment)
28
- end
29
- end
30
- end
31
-
32
- def store_comment(diaries)
33
- diaries.each do |diary_id, diary|
34
- no = 0
35
- diary.each_comment(diary.count_comments(true)) do |com|
36
- no += 1
37
- date = {
38
- :diary_id => diary_id,
39
- :no => no
40
- }
41
- body = {
42
- :name => com.name,
43
- :mail => com.mail,
44
- :last_modified => com.date.to_i,
45
- :visible => com.visible?,
46
- :comment => com.body
47
- }
48
- comment = db[:comments].filter(date)
49
- if comment.count > 0
50
- comment.update(body)
51
- else
52
- db[:comments].insert(date.merge(body))
53
- end
54
- end
55
- end
56
- end
57
- end
58
-
59
- module RefererIO
60
- def restore_referer(diaries)
61
- # not implemented yet
62
- return
63
- end
64
-
65
- def store_referer(diaries)
66
- # not implemented yet
67
- return
68
- end
69
- end
70
-
71
- class RdbIO < BaseIO
72
- include CommentIO
73
- include RefererIO
74
- include CacheIO
75
-
76
- class << self
77
- def load_cgi_conf(conf)
78
- if cgi_conf = db(conf)[:conf].select(:body).first
79
- cgi_conf[:body]
80
- else
81
- ""
82
- end
83
- end
84
-
85
- def save_cgi_conf(conf, result)
86
- if db(conf)[:conf].count > 0
87
- db(conf)[:conf].update(:body => result)
88
- else
89
- db(conf)[:conf].insert(:body => result)
90
- end
91
- end
92
-
93
- def db(conf)
94
- @@_db ||= Sequel.connect(conf.database_url || ENV['DATABASE_URL'])
95
-
96
- @@_db.create_table :conf do
97
- String :body, :text => true
98
- end unless @@_db.table_exists?(:conf)
99
-
100
- @@_db.create_table :diaries do
101
- String :diary_id, :size => 8
102
- String :year, :size => 4
103
- String :month, :size => 2
104
- String :day, :size => 2
105
- String :title, :text => true
106
- String :body, :text => true
107
- String :style, :text => true
108
- Fixnum :last_modified
109
- TrueClass :visible
110
- primary_key :diary_id
111
- end unless @@_db.table_exists?(:diaries)
112
-
113
- @@_db.create_table :comments do
114
- String :diary_id, :size => 8
115
- Fixnum :no
116
- String :name, :text => true
117
- String :mail, :text => true
118
- String :comment, :text => true
119
- Fixnum :last_modified
120
- TrueClass :visible
121
- primary_key [:diary_id, :no]
122
- end unless @@_db.table_exists?(:comments)
123
-
124
- @@_db
125
- end
126
- end
127
-
128
- #
129
- # block must be return boolean which dirty diaries.
130
- #
131
- def transaction(date)
132
- diaries = {}
133
-
134
- if cache = restore_parser_cache(date)
135
- diaries.update(cache)
136
- else
137
- restore(date.strftime("%Y%m%d"), diaries)
138
- restore_comment(diaries)
139
- end
140
-
141
- dirty = yield(diaries) if iterator?
142
-
143
- store(diaries) if (dirty & TDiary::TDiaryBase::DIRTY_DIARY) != 0
144
- store_comment(diaries) if (dirty & TDiary::TDiaryBase::DIRTY_COMMENT) != 0
145
-
146
- store_parser_cache(date, diaries) if dirty || !cache
147
- end
148
-
149
- def calendar
150
- calendar = Hash.new{|hash, key| hash[key] = []}
151
- db[:diaries].select(:year, :month).group_by(:year, :month).order_by(:year, :month).each do |row|
152
- calendar[row[:year]] << row[:month]
153
- end
154
- calendar
155
- end
156
-
157
- def cache_dir
158
- @tdiary.conf.cache_path || "#{Dir.tmpdir}/cache"
159
- end
160
-
161
- private
162
-
163
- def restore(date, diaries, month = true)
164
- query = db[:diaries].select(:diary_id, :title, :last_modified, :visible, :body, :style)
165
- query = if month && /(\d\d\d\d)(\d\d)(\d\d)/ =~ date
166
- query.filter(:year => $1, :month => $2)
167
- else
168
- query.filter(:diary_id => date)
169
- end
170
- query.each do |row|
171
- style = if row[:style].nil? || row[:style].empty?
172
- 'wiki'
173
- else
174
- row[:style].downcase
175
- end
176
- diary = eval("#{style(style)}::new(row[:diary_id], row[:title], row[:body], Time::at(row[:last_modified].to_i))")
177
- diary.show(row[:visible])
178
- diaries[row[:diary_id]] = diary
179
- end
180
- end
181
-
182
- def store(diaries)
183
- diaries.each do |diary_id, diary|
184
- date = if /(\d\d\d\d)(\d\d)(\d\d)/ =~ diary_id
185
- {
186
- :year => $1,
187
- :month => $2,
188
- :day => $3,
189
- :diary_id => diary_id
190
- }
191
- end
192
- body = {
193
- :title => diary.title,
194
- :last_modified => diary.last_modified.to_i,
195
- :style => diary.style,
196
- :visible => diary.visible?,
197
- :body => diary.to_src
198
- }
199
-
200
- entry = db[:diaries].filter(date)
201
- if entry.count > 0
202
- entry.update(body)
203
- else
204
- db[:diaries].insert(date.merge(body))
205
- end
206
- end
207
- end
208
-
209
- def db
210
- self.class.db(@tdiary.conf)
211
- end
212
- end
213
- end