zundaです
出張帰りtDiary、職場で使ってる日記で欲しい機能ですので実装してみました。下記
にパッチを添付します。うまく当たるといいですが…。
正または負のdays=パラメータが指定されていて、
* date=YYYYMMDDが指定されていたらその日から
* date=todayが指定されていたら今日から
* dateが指定されていなかったら最新の日記から、
過去(days<0)あるいは未来(days>0)にdays.abs日分の日記を、
* daysが正なら古いものから、
* daysが負なら新しいものから、
Latestモードとして表示するようになっています。
キャッシュはこれまでと同じ最新表示(daysもdateも指定されていない)の場合以外は
作りません。
.htaccessに
RewriteRule ^([0-9]{8}|today|)([+-][0-9]{1,})\.html index.rb?date=$1;days=$2 [L]
という行を加えて、例えば、
http://www.example.com/today-2.html #=> 今日から遡って2日分を表示
http://www.example.com/today+2.html #=> 今日の予定と次の予定を表示
http://www.example.com/19930224+2.html #=> Rubyの誕生日と次の日を表示
http://www.example.com/19930224-7.html #=> Rubyの誕生日前一週間の行動を表示
などができます。
問題点は、プラグインからは最新表示とn日表示の区別がつかないため、navi_userな
どが、最新の日記へのリンクなど、適切なリンクを表示できないことです。機能とし
てはまったくいっしょのn日表示用のクラスを作って index.rb で振り分ける方が適
当かもしれません。
以上、参考まで。
--- Keiichiro SAKURAI <kei-tdiary@ksakurai.nwr.jp> からのメッセージ:
> さくらぃですー。
>
> > 「過去n日分」リンクでも、URLを日付入りで生成すればOKだと思います。
> > たとえば「4月3日から 7 日分」ならこんなURLで。
> > http://my.domain/tdiary/?startdate=20050403;days=7
> > n のデフォルトは予め設定(latest_limit流用で可?)。
>
> これ、キャッシュに一工夫いりますね。システム負荷を抑える観点からは、
>
> ・通常は n を固定にして
> http://my.domain/tdiary/?startdate=20050403
> という形式のURLを利用する。「過去n日分」のリンクもこの形式で生成。
> ・日記が更新された場合は、その日の前後n日の「まとめ表示」のcacheもクリア。
> (TDiary::TDiaryUpdate::do_eval_rhtml() 内で当該cacheを探してclear?)
> ・days が明示的に指定されたらキャッシュしない。
>
> という仕様がよさそうですね。
Index: ChangeLog
===================================================================
RCS file: /cvsroot/tdiary/core/ChangeLog,v
retrieving revision 1.382
diff -u -r1.382 ChangeLog
--- ChangeLog 8 Apr 2005 08:38:53 -0000 1.382
+++ ChangeLog 13 Apr 2005 23:31:27 -0000
@@ -1,3 +1,6 @@
+2005-04-13 zunda <zunda at freeshell.org>
+ * index.rb, tdiary.rb: accepts days= parameter
+
2005-04-08 TADA Tadashi <sho@spc.gr.jp>
* tdiary.rb, plugin/00default.rb, misc/theme_convert/theme_convert.rb:
remove using ERbLight. (only support ruby 1.8 or later.)
Index: tdiary.rb
===================================================================
RCS file: /cvsroot/tdiary/core/tdiary.rb,v
retrieving revision 1.209
diff -u -r1.209 tdiary.rb
--- tdiary.rb 8 Apr 2005 08:38:54 -0000 1.209
+++ tdiary.rb 13 Apr 2005 23:31:27 -0000
@@ -7,7 +7,7 @@
You can redistribute it and/or modify it under GPL2.
=end
-TDIARY_VERSION = '2.1.0.20050408'
+TDIARY_VERSION = '2.1.0.20050413'
require 'cgi'
begin
@@ -15,6 +15,7 @@
rescue LoadError
require 'erb'
end
+require 'date'
=begin
== String class
@@ -1573,31 +1574,63 @@
class TDiaryLatest < TDiaryView
def initialize( cgi, rhtml, conf )
super
- ym = latest_month
+
+ @default_latest = true
+
+ if @cgi.valid?( 'date' ) then
+ @default_latest = false
+ date = @cgi.params['date'][0]
+ if date == 'today' then
+ @date = Time.now
+ elsif date =~ /\A\d{8}\Z/
+ @date = Time::local( *(date.scan(/^(\d{4})(\d\d)(\d\d)$/)[0]) )
+ end
+ end
unless @date then
+ ym = latest_month
@date = ym ? Time::local( ym[0], ym[1] ) : Time::now
@io.transaction( @date ) do |diaries|
- @diaries = diaries
- @diary = @diaries[@diaries.keys.sort.reverse[0]]
+ @date = Time::local( *(diaries.keys.sort[-1].scan(/^(\d{4})(\d\d)(\d\d)$/)[0]) ) if diaries
DIRTY_NONE
end
end
-
- if ym then
- y = ym[0].to_i
- m = ym[1].to_i
- oldest = oldest_month
- calc_diaries_size
- while ( oldest and @diaries_size < @conf.latest_limit )
- date = if m == 1 then
- Time::local( y -= 1, m = 12 )
- else
- Time::local( y, m -= 1 )
- end
- break if date < Time::local( *oldest )
+
+ if @cgi.valid?( 'days' ) then
+ @default_latest = false
+ @days = @cgi.params['days'][0].to_i
+ else
+ @days = -1 * @conf.latest_limit
+ end
+ if @days.abs > 2 * @conf.latest_limit then
+ @days = (@days < 0 ? -1 : 1 ) * 2 * @conf.latest_limit
+ end
+
+ @diaries = {}
+ oldest = oldest_month
+ latest = latest_month
+ if oldest and latest then
+ date = @date
+ oldestdate = Time.local( *oldest )
+ latestdate = Time.local( latest[0], latest[1], last_day( *latest ) )
+ i = 0
+ while( oldestdate <= date and date <= latestdate and i < @days.abs )
+ ym = date.year.to_s + '%02d' % date.month
@io.transaction( date ) do |diaries|
- @diaries.update( diaries )
- calc_diaries_size
+ if diaries.empty? then
+ date = next_month( date, @days )
+ else
+ begin
+ ymd = ym + '%02d' % date.day
+ diary = diaries[ymd]
+ if diary and diary.visible? then
+ @diaries[ymd] = diary
+ i += 1
+ break if i >= @days.abs
+ end
+ date = next_day( date, @days )
+ ymnew = date.year.to_s + '%02d' % date.month
+ end while ym == ymnew
+ end
DIRTY_NONE
end
end
@@ -1605,26 +1638,53 @@
end
def latest( limit = 5 )
- idx = 0
- @diaries.keys.sort.reverse_each do |date|
- break if idx >= limit
- diary = @diaries[date]
- next unless diary.visible?
- yield diary
- idx += 1
+ if @days < 0 then
+ @diaries.keys.sort.reverse_each do |date|
+ yield @diaries[date]
+ end
+ else
+ @diaries.keys.sort.each do |date|
+ yield @diaries[date]
+ end
end
end
protected
- def calc_diaries_size
- @diaries_size = 0
- @diaries.each_value do |diary|
- @diaries_size += 1 if diary.visible?
+ def last_day( y, m )
+ Date.new( y.to_i, m.to_i, -1 ).day
+ end
+
+ def next_month( date, direction )
+ y = date.year
+ m = date.month
+ if direction < 0 then
+ m -= 1
+ if m == 0 then
+ y -= 1
+ m = 12
+ end
+ d = last_day( y, m )
+ else
+ m += 1
+ if m > 12 then
+ y += 1
+ m = 1
+ end
+ d = 1
end
+ Time.local( y, m, d )
end
-
+
+ def next_day( date, direction )
+ date + ( direction < 0 ? -86400 : 86400 )
+ end
+
def cache_file( prefix )
- "#{prefix}#{@rhtml.sub( /\.rhtml$/, '.rb' )}"
+ if @default_latest then
+ "#{prefix}#{@rhtml.sub( /\.rhtml$/, '.rb' )}"
+ else
+ nil
+ end
end
end
@@ -1799,4 +1859,4 @@
end
end
-# vim: ts=3
+# vim: ts=3 sw=3
Index: index.rb
===================================================================
RCS file: /cvsroot/tdiary/core/index.rb,v
retrieving revision 1.28
diff -u -r1.28 index.rb
--- index.rb 13 Oct 2004 18:05:58 -0000 1.28
+++ index.rb 13 Apr 2005 23:31:27 -0000
@@ -32,11 +32,17 @@
elsif @cgi.valid?( 'date' )
date = @cgi.params['date'][0]
if /^\d{8}$/ =~ date then
- tdiary = TDiary::TDiaryDay::new( @cgi, "day.rhtml", conf )
+ if @cgi.valid?( 'days' ) then
+ tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf )
+ else
+ tdiary = TDiary::TDiaryDay::new( @cgi, "day.rhtml", conf )
+ end
elsif /^\d{6}$/ =~ date then
tdiary = TDiary::TDiaryMonth::new( @cgi, "month.rhtml", conf )
elsif /^\d{4}$/ =~ date then
tdiary = TDiary::TDiaryNYear::new( @cgi, "month.rhtml", conf )
+ elsif 'today' == date then
+ tdiary = TDiary::TDiaryLatest::new( @cgi, "latest.rhtml", conf )
end
elsif @cgi.valid?( 'category' )
tdiary = TDiary::TDiaryCategoryView::new( @cgi, "category.rhtml", conf )
__________________________________
Do You Yahoo!?
Upgrade Your Life
http://bb.yahoo.co.jp/
|