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