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