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