]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/calendar.pm
Use xhtml friendly pubdate setting.
[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                         section => "widget",
42                 },
43                 archivebase => {
44                         type => "string",
45                         example => "archives",
46                         description => "base of the archives hierarchy",
47                         safe => 1,
48                         rebuild => 1,
49                 },
50                 archive_pagespec => {
51                         type => "pagespec",
52                         example => "posts/* and !*/Discussion",
53                         description => "PageSpec of pages to include in the archives; used by ikiwiki-calendar command",
54                         link => 'ikiwiki/PageSpec',
55                         safe => 1,
56                         rebuild => 0,
57                 },
58 }
59
60 sub is_leap_year (@) {
61         my %params=@_;
62         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
63 }
64
65 sub month_days {
66         my %params=@_;
67         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
68         if ($params{month} == 2 && is_leap_year(%params)) {
69                 $days_in_month++;
70         }
71         return $days_in_month;
72 }
73
74 sub format_month (@) {
75         my %params=@_;
76
77         my %linkcache;
78         foreach my $p (pagespec_match_list($params{page}, 
79                                 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
80                                 # add presence dependencies to update
81                                 # month calendar when pages are added/removed
82                                 deptype => deptype("presence"))) {
83                 my $mtime = $IkiWiki::pagectime{$p};
84                 my @date  = localtime($mtime);
85                 my $mday  = $date[3];
86                 my $month = $date[4] + 1;
87                 my $year  = $date[5] + 1900;
88                 my $mtag  = sprintf("%02d", $month);
89
90                 # Only one posting per day is being linked to.
91                 $linkcache{"$year/$mtag/$mday"} = $p;
92         }
93                 
94         my $pmonth = $params{month} - 1;
95         my $nmonth = $params{month} + 1;
96         my $pyear  = $params{year};
97         my $nyear  = $params{year};
98
99         # Adjust for January and December
100         if ($params{month} == 1) {
101                 $pmonth = 12;
102                 $pyear--;
103         }
104         if ($params{month} == 12) {
105                 $nmonth = 1;
106                 $nyear++;
107         }
108
109         # Add padding.
110         $pmonth=sprintf("%02d", $pmonth);
111         $nmonth=sprintf("%02d", $nmonth);
112
113         my $calendar="\n";
114
115         # When did this month start?
116         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
117
118         my $future_dom = 0;
119         my $today      = 0;
120         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
121                 $future_dom = $now[3]+1;
122                 $today      = $now[3];
123         }
124
125         # Find out month names for this, next, and previous months
126         my $monthname=POSIX::strftime("%B", @monthstart);
127         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
128         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
129
130         my $archivebase = 'archives';
131         $archivebase = $config{archivebase} if defined $config{archivebase};
132         $archivebase = $params{archivebase} if defined $params{archivebase};
133   
134         # Calculate URL's for monthly archives.
135         my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
136         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
137                 $url = htmllink($params{page}, $params{destpage}, 
138                         "$archivebase/$params{year}/".$params{month},
139                         noimageinline => 1,
140                         linktext => "$monthname $params{year}",
141                         title => $monthname);
142         }
143         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
144                 deptype("presence"));
145         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
146                 $purl = htmllink($params{page}, $params{destpage}, 
147                         "$archivebase/$pyear/$pmonth",
148                         noimageinline => 1,
149                         linktext => "\&larr;",
150                         title => $pmonthname);
151         }
152         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
153                 deptype("presence"));
154         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
155                 $nurl = htmllink($params{page}, $params{destpage}, 
156                         "$archivebase/$nyear/$nmonth",
157                         noimageinline => 1,
158                         linktext => "\&rarr;",
159                         title => $nmonthname);
160         }
161         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
162                 deptype("presence"));
163
164         # Start producing the month calendar
165         $calendar=<<EOF;
166 <table class="month-calendar">
167         <tr>
168         <th class="month-calendar-arrow">$purl</th>
169         <th class="month-calendar-head" colspan="5">$url</th>
170         <th class="month-calendar-arrow">$nurl</th>
171         </tr>
172         <tr>
173 EOF
174
175         # Suppose we want to start the week with day $week_start_day
176         # If $monthstart[6] == 1
177         my $week_start_day = $params{week_start_day};
178
179         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
180         my %downame;
181         my %dowabbr;
182         for my $dow ($week_start_day..$week_start_day+6) {
183                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
184                 my $downame = POSIX::strftime("%A", @day);
185                 my $dowabbr = POSIX::strftime("%a", @day);
186                 $downame{$dow % 7}=$downame;
187                 $dowabbr{$dow % 7}=$dowabbr;
188                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
189         }
190
191         $calendar.=<<EOF;
192         </tr>
193 EOF
194
195         my $wday;
196         # we start with a week_start_day, and skip until we get to the first
197         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
198                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
199                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
200         }
201
202         # At this point, either the first is a week_start_day, in which case
203         # nothing has been printed, or else we are in the middle of a row.
204         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
205              $day++, $wday++, $wday %= 7) {
206                 # At this point, on a week_start_day, we close out a row,
207                 # and start a new one -- unless it is week_start_day on the
208                 # first, where we do not close a row -- since none was started.
209                 if ($wday == $week_start_day) {
210                         $calendar.=qq{\t</tr>\n} unless $day == 1;
211                         $calendar.=qq{\t<tr>\n};
212                 }
213                 
214                 my $tag;
215                 my $key="$params{year}/$params{month}/$day";
216                 if (defined $linkcache{$key}) {
217                         if ($day == $today) {
218                                 $tag='month-calendar-day-this-day';
219                         }
220                         else {
221                                 $tag='month-calendar-day-link';
222                         }
223                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
224                         $calendar.=htmllink($params{page}, $params{destpage}, 
225                                 $linkcache{$key},
226                                 noimageinline => 1,
227                                 linktext => $day,
228                                 title => pagetitle(IkiWiki::basename($linkcache{$key})));
229                         $calendar.=qq{</td>\n};
230                 }
231                 else {
232                         if ($day == $today) {
233                                 $tag='month-calendar-day-this-day';
234                         }
235                         elsif ($day == $future_dom) {
236                                 $tag='month-calendar-day-future';
237                         }
238                         else {
239                                 $tag='month-calendar-day-nolink';
240                         }
241                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
242                 }
243         }
244
245         # finish off the week
246         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
247                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
248         }
249         $calendar.=<<EOF;
250         </tr>
251 </table>
252 EOF
253
254         return $calendar;
255 }
256
257 sub format_year (@) {
258         my %params=@_;
259         
260         my @post_months;
261         foreach my $p (pagespec_match_list($params{page}, 
262                                 "creation_year($params{year}) and ($params{pages})",
263                                 # add presence dependencies to update
264                                 # year calendar's links to months when
265                                 # pages are added/removed
266                                 deptype => deptype("presence"))) {
267                 my $mtime = $IkiWiki::pagectime{$p};
268                 my @date  = localtime($mtime);
269                 my $month = $date[4] + 1;
270
271                 $post_months[$month]++;
272         }
273                 
274         my $calendar="\n";
275         
276         my $pyear = $params{year}  - 1;
277         my $nyear = $params{year}  + 1;
278
279         my $thisyear = $now[5]+1900;
280         my $future_month = 0;
281         $future_month = $now[4]+1 if $params{year} == $thisyear;
282
283         my $archivebase = 'archives';
284         $archivebase = $config{archivebase} if defined $config{archivebase};
285         $archivebase = $params{archivebase} if defined $params{archivebase};
286
287         # calculate URL's for previous and next years
288         my ($url, $purl, $nurl)=("$params{year}",'','');
289         if (exists $pagesources{"$archivebase/$params{year}"}) {
290                 $url = htmllink($params{page}, $params{destpage}, 
291                         "$archivebase/$params{year}",
292                         noimageinline => 1,
293                         linktext => $params{year},
294                         title => $params{year});
295         }
296         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
297         if (exists $pagesources{"$archivebase/$pyear"}) {
298                 $purl = htmllink($params{page}, $params{destpage}, 
299                         "$archivebase/$pyear",
300                         noimageinline => 1,
301                         linktext => "\&larr;",
302                         title => $pyear);
303         }
304         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
305         if (exists $pagesources{"$archivebase/$nyear"}) {
306                 $nurl = htmllink($params{page}, $params{destpage}, 
307                         "$archivebase/$nyear",
308                         noimageinline => 1,
309                         linktext => "\&rarr;",
310                         title => $nyear);
311         }
312         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
313
314         # Start producing the year calendar
315         my $m=$params{months_per_row}-2;
316         $calendar=<<EOF;
317 <table class="year-calendar">
318         <tr>
319         <th class="year-calendar-arrow">$purl</th>
320         <th class="year-calendar-head" colspan="$m">$url</th>
321         <th class="year-calendar-arrow">$nurl</th>
322         </tr>
323         <tr>
324                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
325         </tr>
326 EOF
327
328         for (my $month = 1; $month <= 12; $month++) {
329                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
330                 my $murl;
331                 my $monthname = POSIX::strftime("%B", @day);
332                 my $monthabbr = POSIX::strftime("%b", @day);
333                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
334                 my $tag;
335                 my $mtag=sprintf("%02d", $month);
336                 if ($month == $params{month} && $thisyear == $params{year}) {
337                         $tag = 'year-calendar-this-month';
338                 }
339                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
340                         $tag = 'year-calendar-month-link';
341                 } 
342                 elsif ($future_month && $month >= $future_month) {
343                         $tag = 'year-calendar-month-future';
344                 } 
345                 else {
346                         $tag = 'year-calendar-month-nolink';
347                 }
348
349                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
350                     $post_months[$mtag]) {
351                         $murl = htmllink($params{page}, $params{destpage}, 
352                                 "$archivebase/$params{year}/$mtag",
353                                 noimageinline => 1,
354                                 linktext => $monthabbr,
355                                 title => $monthname);
356                         $calendar.=qq{\t<td class="$tag">};
357                         $calendar.=$murl;
358                         $calendar.=qq{\t</td>\n};
359                 }
360                 else {
361                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
362                 }
363                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
364                         deptype("presence"));
365
366                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
367         }
368
369         $calendar.=<<EOF;
370 </table>
371 EOF
372
373         return $calendar;
374 }
375
376 sub setnextchange ($$) {
377         my $page=shift;
378         my $timestamp=shift;
379
380         if (! exists $pagestate{$page}{calendar}{nextchange} ||
381             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
382                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
383         }
384 }
385
386 sub preprocess (@) {
387         my %params=@_;
388
389         my $thisyear=1900 + $now[5];
390         my $thismonth=1 + $now[4];
391
392         $params{pages} = "*"            unless defined $params{pages};
393         $params{type}  = "month"        unless defined $params{type};
394         $params{week_start_day} = 0     unless defined $params{week_start_day};
395         $params{months_per_row} = 3     unless defined $params{months_per_row};
396         $params{year}  = $thisyear      unless defined $params{year};
397         $params{month} = $thismonth     unless defined $params{month};
398
399         my $relativeyear=0;
400         if ($params{year} < 1) {
401                 $relativeyear=1;
402                 $params{year}=$thisyear+$params{year};
403         }
404         my $relativemonth=0;
405         if ($params{month} < 1) {
406                 $relativemonth=1;
407                 my $monthoff=$params{month};
408                 $params{month}=($thismonth+$monthoff) % 12;
409                 $params{month}=12 if $params{month}==0;
410                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
411                         - int($monthoff / 12);
412                 $params{year}-=$yearoff;
413         }
414         
415         $params{month} = sprintf("%02d", $params{month});
416         
417         if ($params{type} eq 'month' && $params{year} == $thisyear
418             && $params{month} == $thismonth) {
419                 # calendar for current month, updates next midnight
420                 setnextchange($params{destpage}, ($time
421                         + (60 - $now[0])                # seconds
422                         + (59 - $now[1]) * 60           # minutes
423                         + (23 - $now[2]) * 60 * 60      # hours
424                 ));
425         }
426         elsif ($params{type} eq 'month' &&
427                (($params{year} == $thisyear && $params{month} > $thismonth) ||
428                 $params{year} > $thisyear)) {
429                 # calendar for upcoming month, updates 1st of that month
430                 setnextchange($params{destpage},
431                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
432         }
433         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
434                $relativemonth) {
435                 # Calendar for current year updates 1st of next month.
436                 # Any calendar relative to the current month also updates
437                 # then.
438                 if ($thismonth < 12) {
439                         setnextchange($params{destpage},
440                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
441                 }
442                 else {
443                         setnextchange($params{destpage},
444                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
445                 }
446         }
447         elsif ($relativeyear) {
448                 # Any calendar relative to the current year updates 1st
449                 # of next year.
450                 setnextchange($params{destpage},
451                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
452         }
453         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
454                 # calendar for upcoming year, updates 1st of that year
455                 setnextchange($params{destpage},
456                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
457         }
458         else {
459                 # calendar for past month or year, does not need
460                 # to update any more
461                 delete $pagestate{$params{destpage}}{calendar};
462         }
463
464         my $calendar="";
465         if ($params{type} eq 'month') {
466                 $calendar=format_month(%params);
467         }
468         elsif ($params{type} eq 'year') {
469                 $calendar=format_year(%params);
470         }
471
472         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
473 } #}}
474
475 sub needsbuild (@) {
476         my $needsbuild=shift;
477         foreach my $page (keys %pagestate) {
478                 if (exists $pagestate{$page}{calendar}{nextchange}) {
479                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
480                                 # force a rebuild so the calendar shows
481                                 # the current day
482                                 push @$needsbuild, $pagesources{$page};
483                         }
484                         if (exists $pagesources{$page} && 
485                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
486                                 # remove state, will be re-added if
487                                 # the calendar is still there during the
488                                 # rebuild
489                                 delete $pagestate{$page}{calendar};
490                         }
491                 }
492         }
493 }
494
495 1