]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/calendar.pm
calendar: Fix midnight rebuild trigger of calendars with explicit month/year.
[ikiwiki.git] / IkiWiki / Plugin / calendar.pm
1 #! /usr/bin/perl
2 # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 require 5.002;
19 package IkiWiki::Plugin::calendar;
20
21 use warnings;
22 use strict;
23 use IkiWiki 3.00;
24 use Time::Local;
25 use POSIX;
26
27 my $time=time;
28 my @now=localtime($time);
29
30 sub import {
31         hook(type => "getsetup", id => "calendar", call => \&getsetup);
32         hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
33         hook(type => "preprocess", id => "calendar", call => \&preprocess);
34 }
35
36 sub getsetup () {
37         return
38                 plugin => {
39                         safe => 1,
40                         rebuild => undef,
41                 },
42                 archivebase => {
43                         type => "string",
44                         example => "archives",
45                         description => "base of the archives hierarchy",
46                         safe => 1,
47                         rebuild => 1,
48                 },
49 }
50
51 sub is_leap_year (@) {
52         my %params=@_;
53         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
54 }
55
56 sub month_days {
57         my %params=@_;
58         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
59         if ($params{month} == 2 && is_leap_year(%params)) {
60                 $days_in_month++;
61         }
62         return $days_in_month;
63 }
64
65 sub format_month (@) {
66         my %params=@_;
67
68         my %linkcache;
69         foreach my $p (pagespec_match_list($params{page}, $params{pages},
70                                 # add presence dependencies to update
71                                 # month calendar when pages are added/removed
72                                 deptype => deptype("presence"))) {
73                 my $mtime = $IkiWiki::pagectime{$p};
74                 my $src   = $pagesources{$p};
75                 my @date  = localtime($mtime);
76                 my $mday  = $date[3];
77                 my $month = $date[4] + 1;
78                 my $year  = $date[5] + 1900;
79                 my $mtag  = sprintf("%02d", $month);
80
81                 # Only one posting per day is being linked to.
82                 $linkcache{"$year/$mtag/$mday"} = "$src";
83         }
84
85         my @list;
86         my $calendar="\n";
87
88         # When did this month start?
89         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
90
91         my $future_dom = 0;
92         my $today      = 0;
93         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
94                 $future_dom = $now[3]+1;
95                 $today      = $now[3];
96         }
97
98         # Find out month names for this, next, and previous months
99         my $monthname=POSIX::strftime("%B", @monthstart);
100         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$params{pmonth}-1,$params{pyear}-1900)));
101         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$params{nmonth}-1,$params{nyear}-1900)));
102
103         my $archivebase = 'archives';
104         $archivebase = $config{archivebase} if defined $config{archivebase};
105         $archivebase = $params{archivebase} if defined $params{archivebase};
106   
107         # Calculate URL's for monthly archives.
108         my ($url, $purl, $nurl)=("$monthname",'','');
109         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
110                 $url = htmllink($params{page}, $params{destpage}, 
111                         "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
112                         linktext => " $monthname ");
113         }
114         add_depends($params{page}, "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
115                 deptype("presence"));
116         if (exists $pagesources{"$archivebase/$params{pyear}/$params{pmonth}"}) {
117                 $purl = htmllink($params{page}, $params{destpage}, 
118                         "$archivebase/$params{pyear}/" . sprintf("%02d", $params{pmonth}),
119                         linktext => " $pmonthname ");
120         }
121         add_depends($params{page}, "$archivebase/$params{pyear}/".sprintf("%02d", $params{pmonth}),
122                 deptype("presence"));
123         if (exists $pagesources{"$archivebase/$params{nyear}/$params{nmonth}"}) {
124                 $nurl = htmllink($params{page}, $params{destpage}, 
125                         "$archivebase/$params{nyear}/" . sprintf("%02d", $params{nmonth}),
126                         linktext => " $nmonthname ");
127         }
128         add_depends($params{page}, "$archivebase/$params{nyear}/".sprintf("%02d", $params{nmonth}),
129                 deptype("presence"));
130
131         # Start producing the month calendar
132         $calendar=<<EOF;
133 <table class="month-calendar">
134         <caption class="month-calendar-head">
135         $purl
136         $url
137         $nurl
138         </caption>
139         <tr>
140 EOF
141
142         # Suppose we want to start the week with day $week_start_day
143         # If $monthstart[6] == 1
144         my $week_start_day = $params{week_start_day};
145
146         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
147         my %downame;
148         my %dowabbr;
149         for my $dow ($week_start_day..$week_start_day+6) {
150                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
151                 my $downame = POSIX::strftime("%A", @day);
152                 my $dowabbr = POSIX::strftime("%a", @day);
153                 $downame{$dow % 7}=$downame;
154                 $dowabbr{$dow % 7}=$dowabbr;
155                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
156         }
157
158         $calendar.=<<EOF;
159         </tr>
160 EOF
161
162         my $wday;
163         # we start with a week_start_day, and skip until we get to the first
164         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
165                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
166                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
167         }
168
169         # At this point, either the first is a week_start_day, in which case
170         # nothing has been printed, or else we are in the middle of a row.
171         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
172              $day++, $wday++, $wday %= 7) {
173                 # At tihs point, on a week_start_day, we close out a row,
174                 # and start a new one -- unless it is week_start_day on the
175                 # first, where we do not close a row -- since none was started.
176                 if ($wday == $week_start_day) {
177                         $calendar.=qq{\t</tr>\n} unless $day == 1;
178                         $calendar.=qq{\t<tr>\n};
179                 }
180                 
181                 my $tag;
182                 my $mtag = sprintf("%02d", $params{month});
183                 if (defined $pagesources{"$archivebase/$params{year}/$mtag/$day"}) {
184                         if ($day == $today) {
185                                 $tag='month-calendar-day-this-day';
186                         }
187                         else {
188                                 $tag='month-calendar-day-link';
189                         }
190                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
191                         $calendar.=htmllink($params{page}, $params{destpage}, 
192                                             pagename($linkcache{"$params{year}/$mtag/$day"}),
193                                             "linktext" => "$day");
194                         push @list, pagename($linkcache{"$params{year}/$mtag/$day"});
195                         $calendar.=qq{</td>\n};
196                 }
197                 else {
198                         if ($day == $today) {
199                                 $tag='month-calendar-day-this-day';
200                         }
201                         elsif ($day == $future_dom) {
202                                 $tag='month-calendar-day-future';
203                         }
204                         else {
205                                 $tag='month-calendar-day-nolink';
206                         }
207                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
208                 }
209         }
210
211         # finish off the week
212         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
213                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
214         }
215         $calendar.=<<EOF;
216         </tr>
217 </table>
218 EOF
219
220         return $calendar;
221 }
222
223 sub format_year (@) {
224         my %params=@_;
225
226         my $calendar="\n";
227
228         my $future_month = 0;
229         $future_month = $now[4]+1 if ($params{year} == $now[5]+1900);
230
231         my $archivebase = 'archives';
232         $archivebase = $config{archivebase} if defined $config{archivebase};
233         $archivebase = $params{archivebase} if defined $params{archivebase};
234
235         # calculate URL's for previous and next years
236         my ($url, $purl, $nurl)=("$params{year}",'','');
237         if (exists $pagesources{"$archivebase/$params{year}"}) {
238                 $url = htmllink($params{page}, $params{destpage}, 
239                         "$archivebase/$params{year}",
240                         linktext => "$params{year}");
241         }
242         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
243         if (exists $pagesources{"$archivebase/$params{pyear}"}) {
244                 $purl = htmllink($params{page}, $params{destpage}, 
245                         "$archivebase/$params{pyear}",
246                         linktext => "\&larr;");
247         }
248         add_depends($params{page}, "$archivebase/$params{pyear}", deptype("presence"));
249         if (exists $pagesources{"$archivebase/$params{nyear}"}) {
250                 $nurl = htmllink($params{page}, $params{destpage}, 
251                         "$archivebase/$params{nyear}",
252                         linktext => "\&rarr;");
253         }
254         add_depends($params{page}, "$archivebase/$params{nyear}", deptype("presence"));
255
256         # Start producing the year calendar
257         $calendar=<<EOF;
258 <table class="year-calendar">
259         <caption class="year-calendar-head">
260         $purl
261         $url
262         $nurl
263         </caption>
264         <tr>
265                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
266         </tr>
267 EOF
268
269         for (my $month = 1; $month <= 12; $month++) {
270                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
271                 my $murl;
272                 my $monthname = POSIX::strftime("%B", @day);
273                 my $monthabbr = POSIX::strftime("%b", @day);
274                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
275                 my $tag;
276                 my $mtag=sprintf("%02d", $month);
277                 if ($month == $params{month}) {
278                         if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
279                                 $tag = 'this_month_link';
280                         }
281                         else {
282                                 $tag = 'this_month_nolink';
283                         }
284                 }
285                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
286                         $tag = 'month_link';
287                 } 
288                 elsif ($future_month && $month >= $future_month) {
289                         $tag = 'month_future';
290                 } 
291                 else {
292                         $tag = 'month_nolink';
293                 }
294
295                 if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
296                         $murl = htmllink($params{page}, $params{destpage}, 
297                                 "$archivebase/$params{year}/$mtag",
298                                 linktext => "$monthabbr");
299                         $calendar.=qq{\t<td class="$tag">};
300                         $calendar.=$murl;
301                         $calendar.=qq{\t</td>\n};
302                 }
303                 else {
304                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
305                 }
306                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
307                         deptype("presence"));
308
309                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
310         }
311
312         $calendar.=<<EOF;
313 </table>
314 EOF
315
316         return $calendar;
317 }
318
319 sub preprocess (@) {
320         my %params=@_;
321
322         my $thisyear=1900 + $now[5];
323         my $thismonth=1 + $now[4];
324
325         $params{pages} = "*"            unless defined $params{pages};
326         $params{type}  = "month"        unless defined $params{type};
327         $params{week_start_day} = 0     unless defined $params{week_start_day};
328         $params{months_per_row} = 3     unless defined $params{months_per_row};
329         $params{year}  = $thisyear      unless defined $params{year};
330         $params{month} = $thismonth     unless defined $params{month};
331
332         $params{month} = sprintf("%02d", $params{month});
333                         
334         if ($params{type} eq 'month' && $params{year} == $thisyear
335             && $params{month} == $thismonth) {
336                 # calendar for current month, updates next midnight
337                 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
338                         + (60 - $now[0])                # seconds
339                         + (59 - $now[1]) * 60           # minutes
340                         + (23 - $now[2]) * 60 * 60      # hours
341                 );
342         }
343         elsif ($params{type} eq 'month' &&
344                (($params{year} == $thisyear && $params{month} > $thismonth) ||
345                 $params{year} > $thisyear)) {
346                 # calendar for upcoming month, updates 1st of that month
347                 $pagestate{$params{destpage}}{calendar}{nextchange}=
348                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
349         }
350         elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
351                 # calendar for current year, updates 1st of next month
352                 $pagestate{$params{destpage}}{calendar}{nextchange}=
353                         timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
354         }
355         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
356                 # calendar for upcoming year, updates 1st of that year
357                 $pagestate{$params{destpage}}{calendar}{nextchange}=
358                         timelocal(0, 0, 0, 1, 1-1, $params{year});
359         }
360         else {
361                 # calendar for past month or year, does not need
362                 # to update any more
363                 delete $pagestate{$params{destpage}}{calendar};
364         }
365
366         # Calculate month names for next month, and previous months
367         $params{pmonth} = $params{month} - 1;
368         $params{nmonth} = $params{month} + 1;
369         $params{pyear}  = $params{year}  - 1;
370         $params{nyear}  = $params{year}  + 1;
371
372         # Adjust for January and December
373         if ($params{month} == 1) {
374                 $params{pmonth} = 12;
375                 $params{pyear}--;
376         }
377         if ($params{month} == 12) {
378                 $params{nmonth} = 1;
379                 $params{nyear}++;
380         }
381
382         my $calendar="";
383         if ($params{type} eq 'month') {
384                 $calendar=format_month(%params);
385         }
386         elsif ($params{type} eq 'year') {
387                 $calendar=format_year(%params);
388         }
389
390         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
391 } #}}
392
393 sub needsbuild (@) {
394         my $needsbuild=shift;
395         foreach my $page (keys %pagestate) {
396                 if (exists $pagestate{$page}{calendar}{nextchange}) {
397                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
398                                 # force a rebuild so the calendar shows
399                                 # the current day
400                                 push @$needsbuild, $pagesources{$page};
401                         }
402                         if (exists $pagesources{$page} && 
403                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
404                                 # remove state, will be re-added if
405                                 # the calendar is still there during the
406                                 # rebuild
407                                 delete $pagestate{$page}{calendar};
408                         }
409                 }
410         }
411 }
412
413 1