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