]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
83d65d0c3d07c05d08c959b4f5ab4e3674d97d2b
[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);
28 my %changed;
30 sub import {
31         hook(type => "checkconfig", id => "calendar", call => \&checkconfig);
32         hook(type => "getsetup", id => "calendar", call => \&getsetup);
33         hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
34         hook(type => "preprocess", id => "calendar", call => \&preprocess);
35         hook(type => "scan", id => "calendar", call => \&scan);
36         hook(type => "build_affected", id => "calendar", call => \&build_affected);
38         IkiWiki::loadplugin("transient");
39 }
41 sub getsetup () {
42         return
43                 plugin => {
44                         safe => 1,
45                         rebuild => undef,
46                         section => "widget",
47                 },
48                 archivebase => {
49                         type => "string",
50                         example => "archives",
51                         description => "base of the archives hierarchy",
52                         safe => 1,
53                         rebuild => 1,
54                 },
55                 archive_pagespec => {
56                         type => "pagespec",
57                         example => "page(posts/*) and !*/Discussion",
58                         description => "PageSpec of pages to include in the archives, if option `calendar_autocreate` is true.",
59                         link => 'ikiwiki/PageSpec',
60                         safe => 1,
61                         rebuild => 0,
62                 },
63                 calendar_autocreate => {
64                         type => "boolean",
65                         example => 1,
66                         description => "autocreate new calendar pages?",
67                         safe => 1,
68                         rebuild => undef,
69                 },
70                 calendar_fill_gaps => {
71                         type => "boolean",
72                         example => 1,
73                         default => 1,
74                         description => "if set, when building calendar pages, also build pages of year and month when no pages were published (building empty calendars).",
75                         safe => 1,
76                         rebuild => 0,
77                 },
78 }
80 sub checkconfig () {
81         if (! defined $config{calendar_autocreate}) {
82                 $config{calendar_autocreate} = defined $config{archivebase};
83         }
84         if (! defined $config{archive_pagespec}) {
85                 $config{archive_pagespec} = '*';
86         }
87         if (! defined $config{archivebase}) {
88                 $config{archivebase} = 'archives';
89         }
90         if (! defined $config{calendar_fill_gaps}) {
91                 $config{calendar_fill_gaps} = 1;
92         }
93 }
95 sub is_leap_year (@) {
96         my %params=@_;
97         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
98 }
100 sub month_days {
101         my %params=@_;
102         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
103         if ($params{month} == 2 && is_leap_year(%params)) {
104                 $days_in_month++;
105         }
106         return $days_in_month;
109 sub build_affected {
110         my %affected;
111         my ($ayear, $amonth, $valid);
113         foreach my $year (keys %changed) {
114                 ($ayear, $valid) = nextyear($year, $config{archivebase});
115                 $affected{calendarlink($ayear)} = sprintf(gettext("building calendar for %s, its previous or next year has changed"), $ayear) if ($valid);
116                 ($ayear, $valid) = previousyear($year, $config{archivebase});
117                 $affected{calendarlink($ayear)} = sprintf(gettext("building calendar for %s, its previous or next year has changed"), $ayear) if ($valid);
118                 foreach my $month (keys $changed{$year}) {
119                         ($ayear, $amonth, $valid) = nextmonth($year, $month, $config{archivebase});
120                         $affected{calendarlink($ayear, sprintf("%02d", $amonth))} = sprintf(gettext("building calendar for %s/%s, its previous or next month has changed"), $amonth, $ayear) if ($valid);
121                         ($ayear, $amonth, $valid) = previousmonth($year, $month, $config{archivebase});
122                         $affected{calendarlink($ayear, sprintf("%02d", $amonth))} = sprintf(gettext("building calendar for %s/%s, its previous or next month has changed"), $amonth, $ayear) if ($valid);
123                 }
124         }
126         return %affected;
129 sub autocreate {
130         my ($page, $pagefile, $year, $month) = @_;
131         my $message=sprintf(gettext("creating calendar page %s"), $page);
132         debug($message);
134         my $template;
135         if (defined $month) {
136                 $template=template("calendarmonth.tmpl");
137         } else {
138                 $template=template("calendaryear.tmpl");
139         }
140         $template->param(year => $year);
141         $template->param(month => $month) if defined $month;
142         $template->param(pagespec => $config{archive_pagespec});
144         my $dir = $IkiWiki::Plugin::transient::transientdir;
146         writefile($pagefile, $dir, $template->output);
149 sub calendarlink($;$) {
150         my ($year, $month) = @_;
151         if (defined $month) {
152                 return $config{archivebase} . "/" . $year . "/" . $month;
153         } else {
154                 return $config{archivebase} . "/" . $year;
155         }
158 sub gencalendarmonth{
159         my $year = shift;
160         my $month = sprintf("%02d", shift);
162         my $page = calendarlink($year, $month);
163         my $pagefile = newpagefile($page, $config{default_pageext});
164         add_autofile(
165                 $pagefile, "calendar",
166                 sub {return autocreate($page, $pagefile, $year, $month);}
167         );
170 sub gencalendaryear {
171         my $year = shift;
172         my %params = @_;
174         return unless $config{calendar_autocreate};
176         # Building year page
177         my $page = calendarlink($year);
178         my $pagefile = newpagefile($page, $config{default_pageext});
179         add_autofile(
180                 $pagefile, "calendar",
181                 sub {return autocreate($page, $pagefile, $year);}
182         );
184         if (not exists $wikistate{calendar}{minyear}) {
185                 $wikistate{calendar}{minyear} = $year;
186         }
187         if (not exists $wikistate{calendar}{maxyear}) {
188                 $wikistate{calendar}{maxyear} = $year;
189         }
191         if ($config{calendar_fill_gaps}) {
192                 # Building month pages
193                 foreach my $month (1 .. 12) {
194                         gencalendarmonth($year, $month);
195                 }
197                 # Filling potential gaps in years (e.g. calendar goes from 2010 to 2014,
198                 # and we just added year 2005. We have to had years 2006 to 2009).
199                 return if $params{norecurse};
200                 if ($wikistate{calendar}{minyear} > $year) {
201                         foreach my $other ($year + 1 .. $wikistate{calendar}{minyear} - 1) {
202                                 gencalendaryear($other, norecurse => 1);
203                         }
204                         $wikistate{calendar}{minyear} = $year;
205                 }
206                 if ($wikistate{calendar}{maxyear} < $year) {
207                         foreach my $other ($wikistate{calendar}{maxyear} + 1 .. $year - 1) {
208                                 gencalendaryear($other, norecurse => 1);
209                         }
210                         $wikistate{calendar}{maxyear} = $year;
211                 }
212         }
213         if ($year < $wikistate{calendar}{minyear}) {
214                 $wikistate{calendar}{minyear} = $year;
215         }
216         if ($year >  $wikistate{calendar}{maxyear}) {
217                 $wikistate{calendar}{maxyear} = $year;
218         }
221 sub previousmonth($$$) {
222         my $year = shift;
223         my $month = shift;
224         my $archivebase = shift;
226         my $pmonth = $month;
227         my $pyear  = $year;
228         while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
229                 $pmonth -= 1;
230                 if ($pmonth == 0) {
231                         $pyear -= 1;
232                         $pmonth = 12;
233                         return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
234                 }
235         }
236         return ($pyear, $pmonth, 1);
239 sub nextmonth($$$) {
240         my $year = shift;
241         my $month = shift;
242         my $archivebase = shift;
244         my $nmonth = $month;
245         my $nyear  = $year;
246         while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
247                 $nmonth += 1;
248                 if ($nmonth == 13) {
249                         $nyear += 1;
250                         $nmonth = 1;
251                         return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
252                 }
253         }
254         return ($nyear, $nmonth, 1);
257 sub previousyear($$) {
258         my $year = shift;
259         my $archivebase = shift;
261         my $pyear = $year - 1;
262         while (not exists $pagesources{"$archivebase/$pyear"}) {
263                 $pyear -= 1;
264                 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
265         }
266         return ($pyear, 1);
269 sub nextyear($$) {
270         my $year = shift;
271         my $archivebase = shift;
273         my $nyear = $year + 1;
274         while (not exists $pagesources{"$archivebase/$nyear"}) {
275                 $nyear += 1;
276                 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
277         }
278         return ($nyear, 1);
281 sub format_month (@) {
282         my %params=@_;
284         my %linkcache;
285         foreach my $p (pagespec_match_list($params{page}, 
286                                 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
287                                 # add presence dependencies to update
288                                 # month calendar when pages are added/removed
289                                 deptype => deptype("presence"))) {
290                 my $mtime = $IkiWiki::pagectime{$p};
291                 my @date  = localtime($mtime);
292                 my $mday  = $date[3];
293                 my $month = $date[4] + 1;
294                 my $year  = $date[5] + 1900;
295                 my $mtag  = sprintf("%02d", $month);
297                 if (! $linkcache{"$year/$mtag/$mday"}) {
298                         $linkcache{"$year/$mtag/$mday"} = [];
299                 }
300                 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
301         }
302                 
303         my $archivebase = 'archives';
304         $archivebase = $config{archivebase} if defined $config{archivebase};
305         $archivebase = $params{archivebase} if defined $params{archivebase};
306         
307         my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
308         my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
310         # Add padding.
311         $pmonth=sprintf("%02d", $pmonth);
312         $nmonth=sprintf("%02d", $nmonth);
314         my $calendar="\n";
316         # When did this month start?
317         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
319         my $future_dom = 0;
320         my $today      = 0;
321         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
322                 $future_dom = $now[3]+1;
323                 $today      = $now[3];
324         }
326         # Find out month names for this, next, and previous months
327         my $monthabbrev=strftime_utf8("%b", @monthstart);
328         my $monthname=strftime_utf8("%B", @monthstart);
329         my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
330         my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
332         # Calculate URL's for monthly archives.
333         my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
334         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
335                 $url = htmllink($params{page}, $params{destpage}, 
336                         "$archivebase/$params{year}/".$params{month},
337                         noimageinline => 1,
338                         linktext => "$monthabbrev $params{year}",
339                         title => $monthname);
340         }
341         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
342                 deptype("presence"));
343         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
344                 $purl = htmllink($params{page}, $params{destpage}, 
345                         "$archivebase/$pyear/$pmonth",
346                         noimageinline => 1,
347                         linktext => "\&larr;",
348                         title => $pmonthname);
349         }
350         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
351                 deptype("presence"));
352         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
353                 $nurl = htmllink($params{page}, $params{destpage}, 
354                         "$archivebase/$nyear/$nmonth",
355                         noimageinline => 1,
356                         linktext => "\&rarr;",
357                         title => $nmonthname);
358         }
359         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
360                 deptype("presence"));
362         # Start producing the month calendar
363         $calendar=<<EOF;
364 <table class="month-calendar">
365         <tr>
366         <th class="month-calendar-arrow">$purl</th>
367         <th class="month-calendar-head" colspan="5">$url</th>
368         <th class="month-calendar-arrow">$nurl</th>
369         </tr>
370         <tr>
371 EOF
373         # Suppose we want to start the week with day $week_start_day
374         # If $monthstart[6] == 1
375         my $week_start_day = $params{week_start_day};
377         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
378         my %downame;
379         my %dowabbr;
380         for my $dow ($week_start_day..$week_start_day+6) {
381                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
382                 my $downame = strftime_utf8("%A", @day);
383                 my $dowabbr = substr($downame, 0, 1);
384                 $downame{$dow % 7}=$downame;
385                 $dowabbr{$dow % 7}=$dowabbr;
386                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
387         }
389         $calendar.=<<EOF;
390         </tr>
391 EOF
393         my $wday;
394         # we start with a week_start_day, and skip until we get to the first
395         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
396                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
397                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
398         }
400         # At this point, either the first is a week_start_day, in which case
401         # nothing has been printed, or else we are in the middle of a row.
402         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
403              $day++, $wday++, $wday %= 7) {
404                 # At this point, on a week_start_day, we close out a row,
405                 # and start a new one -- unless it is week_start_day on the
406                 # first, where we do not close a row -- since none was started.
407                 if ($wday == $week_start_day) {
408                         $calendar.=qq{\t</tr>\n} unless $day == 1;
409                         $calendar.=qq{\t<tr>\n};
410                 }
411                 
412                 my $tag;
413                 my $key="$params{year}/$params{month}/$day";
414                 if (defined $linkcache{$key}) {
415                         if ($day == $today) {
416                                 $tag='month-calendar-day-this-day';
417                         }
418                         else {
419                                 $tag='month-calendar-day-link';
420                         }
421                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
422                         $calendar.=qq{<div class='popup'>$day<div class='balloon'>};
423                         # Several postings on this page
424                         $calendar.=qq{<ul>};
425                         foreach my $page (@{$linkcache{$key}}) {
426                                 $calendar.= qq{\n\t\t\t<li>};
427                                 my $title;
428                                 if (exists $pagestate{$page}{meta}{title}) {
429                                         $title = "$pagestate{$page}{meta}{title}";
430                                 }
431                                 else {
432                                         $title = pagetitle(IkiWiki::basename($page));
433                                 }
434                                 $calendar.=htmllink($params{page}, $params{destpage}, 
435                                         $page,
436                                         noimageinline => 1,
437                                         linktext => $title,
438                                         title => $title);
439                                 $calendar.= '</li>';
440                         }
441                         $calendar.=qq{\n\t\t</ul>};
442                         $calendar.=qq{</div></div>};
443                         $calendar.=qq{</td>\n};
444                 }
445                 else {
446                         if ($day == $today) {
447                                 $tag='month-calendar-day-this-day';
448                         }
449                         elsif ($day == $future_dom) {
450                                 $tag='month-calendar-day-future';
451                         }
452                         else {
453                                 $tag='month-calendar-day-nolink';
454                         }
455                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
456                 }
457         }
459         # finish off the week
460         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
461                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
462         }
463         $calendar.=<<EOF;
464         </tr>
465 </table>
466 EOF
468         return $calendar;
471 sub format_year (@) {
472         my %params=@_;
474         my @post_months;
475         foreach my $p (pagespec_match_list($params{page}, 
476                                 "creation_year($params{year}) and ($params{pages})",
477                                 # add presence dependencies to update
478                                 # year calendar's links to months when
479                                 # pages are added/removed
480                                 deptype => deptype("presence"))) {
481                 my $mtime = $IkiWiki::pagectime{$p};
482                 my @date  = localtime($mtime);
483                 my $month = $date[4] + 1;
484                 my $year  = $date[5] + 1900;
486                 $post_months[$month]++;
487         }
488                 
489         my $calendar="\n";
491         my $archivebase = 'archives';
492         $archivebase = $config{archivebase} if defined $config{archivebase};
493         $archivebase = $params{archivebase} if defined $params{archivebase};
494         
495         my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
496         my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
498         my $thisyear = $now[5]+1900;
499         my $future_month = 0;
500         $future_month = $now[4]+1 if $params{year} == $thisyear;
502         # calculate URL's for previous and next years
503         my ($url, $purl, $nurl)=("$params{year}",'','');
504         if (exists $pagesources{"$archivebase/$params{year}"}) {
505                 $url = htmllink($params{page}, $params{destpage}, 
506                         "$archivebase/$params{year}",
507                         noimageinline => 1,
508                         linktext => $params{year},
509                         title => $params{year});
510         }
511         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
512         if (exists $pagesources{"$archivebase/$pyear"}) {
513                 $purl = htmllink($params{page}, $params{destpage}, 
514                         "$archivebase/$pyear",
515                         noimageinline => 1,
516                         linktext => "\&larr;",
517                         title => $pyear);
518         }
519         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
520         if (exists $pagesources{"$archivebase/$nyear"}) {
521                 $nurl = htmllink($params{page}, $params{destpage}, 
522                         "$archivebase/$nyear",
523                         noimageinline => 1,
524                         linktext => "\&rarr;",
525                         title => $nyear);
526         }
527         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
529         # Start producing the year calendar
530         my $m=$params{months_per_row}-2;
531         $calendar=<<EOF;
532 <table class="year-calendar">
533         <tr>
534         <th class="year-calendar-arrow">$purl</th>
535         <th class="year-calendar-head" colspan="$m">$url</th>
536         <th class="year-calendar-arrow">$nurl</th>
537         </tr>
538         <tr>
539                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
540         </tr>
541 EOF
543         for (my $month = 1; $month <= 12; $month++) {
544                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
545                 my $murl;
546                 my $monthname = strftime_utf8("%B", @day);
547                 my $monthabbr = strftime_utf8("%b", @day);
548                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
549                 my $tag;
550                 my $mtag=sprintf("%02d", $month);
551                 if ($month == $params{month} && $thisyear == $params{year}) {
552                         $tag = 'year-calendar-this-month';
553                 }
554                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
555                         $tag = 'year-calendar-month-link';
556                 } 
557                 elsif ($future_month && $month >= $future_month) {
558                         $tag = 'year-calendar-month-future';
559                 } 
560                 else {
561                         $tag = 'year-calendar-month-nolink';
562                 }
564                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
565                     $post_months[$mtag]) {
566                         $murl = htmllink($params{page}, $params{destpage}, 
567                                 "$archivebase/$params{year}/$mtag",
568                                 noimageinline => 1,
569                                 linktext => $monthabbr,
570                                 title => $monthname);
571                         $calendar.=qq{\t<td class="$tag">};
572                         $calendar.=$murl;
573                         $calendar.=qq{\t</td>\n};
574                 }
575                 else {
576                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
577                 }
578                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
579                         deptype("presence"));
581                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
582         }
584         $calendar.=<<EOF;
585 </table>
586 EOF
588         return $calendar;
591 sub setnextchange ($$) {
592         my $page=shift;
593         my $timestamp=shift;
595         if (! exists $pagestate{$page}{calendar}{nextchange} ||
596             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
597                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
598         }
601 sub preprocess (@) {
602         my %params=@_;
604         my $thisyear=1900 + $now[5];
605         my $thismonth=1 + $now[4];
607         $params{pages} = "*"            unless defined $params{pages};
608         $params{type}  = "month"        unless defined $params{type};
609         $params{week_start_day} = 0     unless defined $params{week_start_day};
610         $params{months_per_row} = 3     unless defined $params{months_per_row};
611         $params{year}  = $thisyear      unless defined $params{year};
612         $params{month} = $thismonth     unless defined $params{month};
614         my $relativeyear=0;
615         if ($params{year} < 1) {
616                 $relativeyear=1;
617                 $params{year}=$thisyear+$params{year};
618         }
619         my $relativemonth=0;
620         if ($params{month} < 1) {
621                 $relativemonth=1;
622                 my $monthoff=$params{month};
623                 $params{month}=($thismonth+$monthoff) % 12;
624                 $params{month}=12 if $params{month}==0;
625                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
626                         - int($monthoff / 12);
627                 $params{year}-=$yearoff;
628         }
629         
630         $params{month} = sprintf("%02d", $params{month});
631         if (not exists $changed{$params{year}}) {
632                 $changed{$params{year}} = ();
633         }
634         $changed{$params{year}}{$params{month}} = 1;
635         
636         if ($params{type} eq 'month' && $params{year} == $thisyear
637             && $params{month} == $thismonth) {
638                 # calendar for current month, updates next midnight
639                 setnextchange($params{destpage}, ($time
640                         + (60 - $now[0])                # seconds
641                         + (59 - $now[1]) * 60           # minutes
642                         + (23 - $now[2]) * 60 * 60      # hours
643                 ));
644         }
645         elsif ($params{type} eq 'month' &&
646                (($params{year} == $thisyear && $params{month} > $thismonth) ||
647                 $params{year} > $thisyear)) {
648                 # calendar for upcoming month, updates 1st of that month
649                 setnextchange($params{destpage},
650                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
651         }
652         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
653                $relativemonth) {
654                 # Calendar for current year updates 1st of next month.
655                 # Any calendar relative to the current month also updates
656                 # then.
657                 if ($thismonth < 12) {
658                         setnextchange($params{destpage},
659                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
660                 }
661                 else {
662                         setnextchange($params{destpage},
663                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
664                 }
665         }
666         elsif ($relativeyear) {
667                 # Any calendar relative to the current year updates 1st
668                 # of next year.
669                 setnextchange($params{destpage},
670                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
671         }
672         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
673                 # calendar for upcoming year, updates 1st of that year
674                 setnextchange($params{destpage},
675                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
676         }
677         else {
678                 # calendar for past month or year, does not need
679                 # to update any more
680                 delete $pagestate{$params{destpage}}{calendar};
681         }
683         my $calendar="";
684         if ($params{type} eq 'month') {
685                 $calendar=format_month(%params);
686         }
687         elsif ($params{type} eq 'year') {
688                 $calendar=format_year(%params);
689         }
691         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
692 } #}}
694 sub needsbuild (@) {
695         my $needsbuild=shift;
696         foreach my $page (keys %pagestate) {
697                 if (exists $pagestate{$page}{calendar}{nextchange}) {
698                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
699                                 # force a rebuild so the calendar shows
700                                 # the current day
701                                 push @$needsbuild, $pagesources{$page};
702                         }
703                         if (exists $pagesources{$page} && 
704                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
705                                 # remove state, will be re-added if
706                                 # the calendar is still there during the
707                                 # rebuild
708                                 delete $pagestate{$page}{calendar};
709                         }
710                 }
711         }
713         return $needsbuild;
716 sub scan (@) {
717         my %params=@_;
718         my $page=$params{page};
720         # Check if year pages have to be generated
721         if (pagespec_match($page, $config{archive_pagespec})) {
722                 my @ctime = localtime($IkiWiki::pagectime{$page});
723                 gencalendaryear($ctime[5]+1900);
724                 gencalendarmonth($ctime[5]+1900, $ctime[4]+1);
725         }