Skip to content

Commit 54dc7f7

Browse files
machutdtds
authored andcommitted
adapt for plugin storage (#10)
* adapt for plugin storage: use TDiary::Plugin#transaction instead of PStore * PStore.new(file).transaction -> transaction(plugin_name) * db[key] -> JSON.load(db.get(key)) * db[key] = object -> db.set(key, object.to_json) * create rdf file when startup
1 parent 26d3162 commit 54dc7f7

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

plugin/blog-category.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
# Distributed under the GPL
1111
#
1212

13-
require 'pstore'
14-
1513
def blog_category
1614
cat = @cgi.params['blogcategory'][0]
1715
if cat and !cat.empty?
@@ -46,7 +44,7 @@ def stripped_title
4644
return '' unless @title
4745
stripped = @title.sub(/^(\[(.*?)\])+\s*/,'')
4846
end
49-
47+
5048
def categories
5149
return [] unless @title
5250
cat = /^(\[([^\[]+?)\])+/.match(@title).to_a[0]
@@ -107,27 +105,24 @@ def navi_user
107105
end
108106

109107
def blog_category_cache
110-
"#{@cache_path}/blog_category"
108+
"blog_category"
111109
end
112110

113111
def blog_category_cache_add(diary)
114-
PStore.new(blog_category_cache).transaction do |db|
115-
db['blog_category'] = Hash.new unless db.root?('blog_category')
112+
transaction(blog_category_cache) do |db|
113+
cache = JSON.load(db.get('blog_category')) || Hash.new
116114
diary.categories.each do |c|
117-
db['blog_category'][c] = Hash.new unless db['blog_category'][c]
118-
db['blog_category'][c][diary.date.strftime('%Y%m%d')] = diary.stripped_title
115+
cache[c] ||= Hash.new
116+
cache[c][diary.date.strftime('%Y%m%d')] = diary.stripped_title
119117
end
118+
db.set('blog_category', cache.to_json)
120119
end
121120
end
122121

123122
def blog_category_cache_restore
124-
return nil unless File.exist?(blog_category_cache)
125-
cache = {}
126-
PStore.new(blog_category_cache).transaction do |db|
127-
cache.update(db['blog_category'])
128-
db.abort
123+
transaction(blog_category_cache) do |db|
124+
JSON.load(db.get('blog_category'))
129125
end
130-
cache
131126
end
132127

133128
def blog_category_entry(limit = 20)
@@ -179,20 +174,21 @@ def blog_category_cache_initialize
179174
cgi = CGI::new
180175
def cgi.referer; nil; end
181176

182-
PStore.new(blog_category_cache).transaction do |db|
183-
db['blog_category'] = Hash.new
177+
transaction(blog_category_cache) do |db|
178+
cache = Hash.new
184179
@years.each do |y, ms|
185180
ms.each do |m|
186181
cgi.params['date'] = ["#{y}#{m}"]
187182
m = TDiaryMonth.new(cgi, '', @conf)
188183
m.diaries.each do |k, diary|
189184
diary.categories.each do |c|
190-
db['blog_category'][c] = Hash.new unless db['blog_category'][c]
191-
db['blog_category'][c][diary.date.strftime('%Y%m%d')] = diary.stripped_title
185+
cache[c] ||= Hash.new
186+
cache[c][diary.date.strftime('%Y%m%d')] = diary.stripped_title
192187
end
193188
end
194189
end
195190
end
191+
db.set('blog_category', cache.to_json)
196192
end
197193
end
198194

plugin/whatsnew-list.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
# Copyright (c) 2002 by TADA Tadashi <sho@spc.gr.jp>
2525
# Distributed under the GPL
2626
#
27-
require 'pstore'
2827

2928
def whatsnew_list( max = 5, limit = 20 )
3029
return 'DO NOT USE IN SECURE MODE' if @conf.secure
3130

3231
max = max.to_i
3332
limit = limit.to_i
34-
33+
3534
wl = "#{@cache_path}/whatsnew-list"
3635
begin
3736
if @mode == 'latest' then
@@ -42,15 +41,14 @@ def whatsnew_list( max = 5, limit = 20 )
4241
end
4342

4443
r = "<ul>\n"
45-
PStore::new( wl ).transaction do |db|
44+
transaction( 'whatsnew-list' ) do |db|
4645
begin
47-
wn = db['whatsnew']
46+
wn = JSON.load( db.get( 'whatsnew' ) ) || []
4847
wn.each_with_index do |item,i|
4948
break if i >= max
5049
title = @conf.shorten( apply_plugin( item[1] ).gsub( /^(\[.*?\])+\s*/, '' ).gsub( /<.*?>/, '' ), limit )
5150
r << %Q|<li><a href="#{h @index}#{anchor item[0]}">#{title}</a></li>\n|
5251
end
53-
db.abort
5452
rescue
5553
end
5654
end
@@ -70,7 +68,7 @@ def whatsnew_list( max = 5, limit = 20 )
7068
rescue Errno::EACCES
7169
@conf['whatsnew_list.rdf.out'] = false
7270
error_message = %Q|<p class="message">#{rdf}#{@whatsnew_list_msg_access}</p>|
73-
71+
7472
end
7573
else
7674
@conf['whatsnew_list.rdf.out'] = false
@@ -209,17 +207,14 @@ def whatsnew_list_update
209207
@whatsnew_list_in_feed = false
210208

211209
new_item = [diary.date.strftime('%Y%m%d'), title, Time::now.strftime("%Y-%m-%dT%H:%M:%S#{zone}"), desc]
212-
PStore::new( "#{@cache_path}/whatsnew-list" ).transaction do |db|
210+
transaction( 'whatsnew-list' ) do |db|
213211
wn = []
214-
begin
215-
(db['whatsnew'] || []).each_with_index do |item, i|
216-
wn << item unless item[0] == new_item[0]
217-
break if i > 15
218-
end
219-
rescue PStore::Error
212+
(JSON.load( db.get( 'whatsnew' ) ) || []).each_with_index do |item, i|
213+
wn << item unless item[0] == new_item[0]
214+
break if i > 15
220215
end
221216
wn.unshift new_item if diary.visible?
222-
db['whatsnew'] = wn
217+
db.set( 'whatsnew', wn.to_json )
223218

224219
rdf = whatsnew_list_rdf_file
225220
if @conf['whatsnew_list.rdf.out'] then
@@ -253,6 +248,10 @@ def whatsnew_list_update
253248
HTML
254249
end
255250

251+
add_startup_proc do
252+
whatsnew_list_update
253+
end
254+
256255
# Local Variables:
257256
# mode: ruby
258257
# indent-tabs-mode: t

0 commit comments

Comments
 (0)