]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
(no commit message)
[git.ikiwiki.info.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
18 require 5.002;
19 package IkiWiki::Plugin::calendar;
21 use warnings;
22 use strict;
23 use IkiWiki 3.00;
24 use Time::Local;
25 use POSIX ();
27 my $time=time;
28 my @now=localtime($time);
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 }
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 => "page(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 }
60 sub is_leap_year (@) {
61         my %params=@_;
62         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
63 }
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 }
74 sub format_month (@) {
75         my %params=@_;
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);
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};
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         }
109         # Add padding.
110         $pmonth=sprintf("%02d", $pmonth);
111         $nmonth=sprintf("%02d", $nmonth);
113         my $calendar="\n";
115         # When did this month start?
116         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
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         }
125         # Find out month names for this, next, and previous months
126         my $monthabbrev=POSIX::strftime("%b", @monthstart);
127         my $monthname=POSIX::strftime("%B", @monthstart);
128         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
129         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
131         my $archivebase = 'archives';
132         $archivebase = $config{archivebase} if defined $config{archivebase};
133         $archivebase = $params{archivebase} if defined $params{archivebase};
134   
135         # Calculate URL's for monthly archives.
136         my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
137         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
138                 $url = htmllink($params{page}, $params{destpage}, 
139                         "$archivebase/$params{year}/".$params{month},
140                         noimageinline => 1,
141                         linktext => "$monthabbrev $params{year}",
142                         title => $monthname);
143         }
144         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
145                 deptype("presence"));
146         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
147                 $purl = htmllink($params{page}, $params{destpage}, 
148                         "$archivebase/$pyear/$pmonth",
149                         noimageinline => 1,
150                         linktext => "\&larr;",
151                         title => $pmonthname);
152         }
153         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
154                 deptype("presence"));
155         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
156                 $nurl = htmllink($params{page}, $params{destpage}, 
157                         "$archivebase/$nyear/$nmonth",
158                         noimageinline => 1,
159                         linktext => "\&rarr;",
160                         title => $nmonthname);
161         }
162         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
163                 deptype("presence"));
165         # Start producing the month calendar
166         $calendar=<<EOF;
167 <table class="month-calendar">
168         <tr>
169         <th class="month-calendar-arrow">$purl</th>
170         <th class="month-calendar-head" colspan="5">$url</th>
171         <th class="month-calendar-arrow">$nurl</th>
172         </tr>
173         <tr>
174 EOF
176         # Suppose we want to start the week with day $week_start_day
177         # If $monthstart[6] == 1
178         my $week_start_day = $params{week_start_day};
180         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
181         my %downame;
182         my %dowabbr;
183         for my $dow ($week_start_day..$week_start_day+6) {
184                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
185                 my $downame = POSIX::strftime("%A", @day);
186                 my $dowabbr = substr($downame, 0, 1);
187                 $downame{$dow % 7}=$downame;
188                 $dowabbr{$dow % 7}=$dowabbr;
189                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
190         }
192         $calendar.=<<EOF;
193         </tr>
194 EOF
196         my $wday;
197         # we start with a week_start_day, and skip until we get to the first
198         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
199                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
200                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
201         }
203         # At this point, either the first is a week_start_day, in which case
204         # nothing has been printed, or else we are in the middle of a row.
205         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
206              $day++, $wday++, $wday %= 7) {
207                 # At this point, on a week_start_day, we close out a row,
208                 # and start a new one -- unless it is week_start_day on the
209                 # first, where we do not close a row -- since none was started.
210                 if ($wday == $week_start_day) {
211                         $calendar.=qq{\t</tr>\n} unless $day == 1;
212                         $calendar.=qq{\t<tr>\n};
213                 }
214                 
215                 my $tag;
216                 my $key="$params{year}/$params{month}/$day";
217                 if (defined $linkcache{$key}) {
218                         if ($day == $today) {
219                                 $tag='month-calendar-day-this-day';
220                         }
221                         else {
222                                 $tag='month-calendar-day-link';
223                         }
224                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
225                         $calendar.=htmllink($params{page}, $params{destpage}, 
226                                 $linkcache{$key},
227                                 noimageinline => 1,
228                                 linktext => $day,
229                                 title => pagetitle(IkiWiki::basename($linkcache{$key})));
230                         $calendar.=qq{</td>\n};
231                 }
232                 else {
233                         if ($day == $today) {
234                                 $tag='month-calendar-day-this-day';
235                         }
236                         elsif ($day == $future_dom) {
237                                 $tag='month-calendar-day-future';
238                         }
239                         else {
240                                 $tag='month-calendar-day-nolink';
241                         }
242                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
243                 }
244         }
246         # finish off the week
247         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
248                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
249         }
250         $calendar.=<<EOF;
251         </tr>
252 </table>
253 EOF
255         return $calendar;
258 sub format_year (@) {
259         my %params=@_;
260         
261         my @post_months;
262         foreach my $p (pagespec_match_list($params{page}, 
263                                 "creation_year($params{year}) and ($params{pages})",
264                                 # add presence dependencies to update
265                                 # year calendar's links to months when
266                                 # pages are added/removed
267                                 deptype => deptype("presence"))) {
268                 my $mtime = $IkiWiki::pagectime{$p};
269                 my @date  = localtime($mtime);
270                 my $month = $date[4] + 1;
272                 $post_months[$month]++;
273         }
274                 
275         my $calendar="\n";
276         
277         my $pyear = $params{year}  - 1;
278         my $nyear = $params{year}  + 1;
280         my $thisyear = $now[5]+1900;
281         my $future_month = 0;
282         $future_month = $now[4]+1 if $params{year} == $thisyear;
284         my $archivebase = 'archives';
285         $archivebase = $config{archivebase} if defined $config{archivebase};
286         $archivebase = $params{archivebase} if defined $params{archivebase};
288         # calculate URL's for previous and next years
289         my ($url, $purl, $nurl)=("$params{year}",'','');
290         if (exists $pagesources{"$archivebase/$params{year}"}) {
291                 $url = htmllink($params{page}, $params{destpage}, 
292                         "$archivebase/$params{year}",
293                         noimageinline => 1,
294                         linktext => $params{year},
295                         title => $params{year});
296         }
297         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
298         if (exists $pagesources{"$archivebase/$pyear"}) {
299                 $purl = htmllink($params{page}, $params{destpage}, 
300                         "$archivebase/$pyear",
301                         noimageinline => 1,
302                         linktext => "\&larr;",
303                         title => $pyear);
304         }
305         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
306         if (exists $pagesources{"$archivebase/$nyear"}) {
307                 $nurl = htmllink($params{page}, $params{destpage}, 
308                         "$archivebase/$nyear",
309                         noimageinline => 1,
310                         linktext => "\&rarr;",
311                         title => $nyear);
312         }
313         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
315         # Start producing the year calendar
316         my $m=$params{months_per_row}-2;
317         $calendar=<<EOF;
318 <table class="year-calendar">
319         <tr>
320         <th class="year-calendar-arrow">$purl</th>
321         <th class="year-calendar-head" colspan="$m">$url</th>
322         <th class="year-calendar-arrow">$nurl</th>
323         </tr>
324         <tr>
325                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
326         </tr>
327 EOF
329         for (my $month = 1; $month <= 12; $month++) {
330                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
331                 my $murl;
332                 my $monthname = POSIX::strftime("%B", @day);
333                 my $monthabbr = POSIX::strftime("%b", @day);
334                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
335                 my $tag;
336                 my $mtag=sprintf("%02d", $month);
337                 if ($month == $params{month} && $thisyear == $params{year}) {
338                         $tag = 'year-calendar-this-month';
339                 }
340                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
341                         $tag = 'year-calendar-month-link';
342                 } 
343                 elsif ($future_month && $month >= $future_month) {
344                         $tag = 'year-calendar-month-future';
345                 } 
346                 else {
347                         $tag = 'year-calendar-month-nolink';
348                 }
350                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
351                     $post_months[$mtag]) {
352                         $murl = htmllink($params{page}, $params{destpage}, 
353                                 "$archivebase/$params{year}/$mtag",
354                                 noimageinline => 1,
355                                 linktext => $monthabbr,
356                                 title => $monthname);
357                         $calendar.=qq{\t<td class="$tag">};
358                         $calendar.=$murl;
359                         $calendar.=qq{\t</td>\n};
360                 }
361                 else {
362                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
363                 }
364                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
365                         deptype("presence"));
367                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
368         }
370         $calendar.=<<EOF;
371 </table>
372 EOF
374         return $calendar;
377 sub setnextchange ($$) {
378         my $page=shift;
379         my $timestamp=shift;
381         if (! exists $pagestate{$page}{calendar}{nextchange} ||
382             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
383                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
384         }
387 sub preprocess (@) {
388         my %params=@_;
390         my $thisyear=1900 + $now[5];
391         my $thismonth=1 + $now[4];
393         $params{pages} = "*"            unless defined $params{pages};
394         $params{type}  = "month"        unless defined $params{type};
395         $params{week_start_day} = 0     unless defined $params{week_start_day};
396         $params{months_per_row} = 3     unless defined $params{months_per_row};
397         $params{year}  = $thisyear      unless defined $params{year};
398         $params{month} = $thismonth     unless defined $params{month};
400         my $relativeyear=0;
401         if ($params{year} < 1) {
402                 $relativeyear=1;
403                 $params{year}=$thisyear+$params{year};
404         }
405         my $relativemonth=0;
406         if ($params{month} < 1) {
407                 $relativemonth=1;
408                 my $monthoff=$params{month};
409                 $params{month}=($thismonth+$monthoff) % 12;
410                 $params{month}=12 if $params{month}==0;
411                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
412                         - int($monthoff / 12);
413                 $params{year}-=$yearoff;
414         }
415         
416         $params{month} = sprintf("%02d", $params{month});
417         
418         if ($params{type} eq 'month' && $params{year} == $thisyear
419             && $params{month} == $thismonth) {
420                 # calendar for current month, updates next midnight
421                 setnextchange($params{destpage}, ($time
422                         + (60 - $now[0])                # seconds
423                         + (59 - $now[1]) * 60           # minutes
424                         + (23 - $now[2]) * 60 * 60      # hours
425                 ));
426         }
427         elsif ($params{type} eq 'month' &&
428                (($params{year} == $thisyear && $params{month} > $thismonth) ||
429                 $params{year} > $thisyear)) {
430                 # calendar for upcoming month, updates 1st of that month
431                 setnextchange($params{destpage},
432                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
433         }
434         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
435                $relativemonth) {
436                 # Calendar for current year updates 1st of next month.
437                 # Any calendar relative to the current month also updates
438                 # then.
439                 if ($thismonth < 12) {
440                         setnextchange($params{destpage},
441                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
442                 }
443                 else {
444                         setnextchange($params{destpage},
445                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
446                 }
447         }
448         elsif ($relativeyear) {
449                 # Any calendar relative to the current year updates 1st
450                 # of next year.
451                 setnextchange($params{destpage},
452                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
453         }
454         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
455                 # calendar for upcoming year, updates 1st of that year
456                 setnextchange($params{destpage},
457                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
458         }
459         else {
460                 # calendar for past month or year, does not need
461                 # to update any more
462                 delete $pagestate{$params{destpage}}{calendar};
463         }
465         my $calendar="";
466         if ($params{type} eq 'month') {
467                 $calendar=format_month(%params);
468         }
469         elsif ($params{type} eq 'year') {
470                 $calendar=format_year(%params);
471         }
473         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
474 } #}}
476 sub needsbuild (@) {
477         my $needsbuild=shift;
478         foreach my $page (keys %pagestate) {
479                 if (exists $pagestate{$page}{calendar}{nextchange}) {
480                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
481                                 # force a rebuild so the calendar shows
482                                 # the current day
483                                 push @$needsbuild, $pagesources{$page};
484                         }
485                         if (exists $pagesources{$page} && 
486                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
487                                 # remove state, will be re-added if
488                                 # the calendar is still there during the
489                                 # rebuild
490                                 delete $pagestate{$page}{calendar};
491                         }
492                 }
493         }
494         return $needsbuild;