]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
9639f6d08ea360fcf0d1865bb8277beb69dfb167
[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;
485                 $post_months[$month]++;
486         }
487                 
488         my $calendar="\n";
490         my $archivebase = 'archives';
491         $archivebase = $config{archivebase} if defined $config{archivebase};
492         $archivebase = $params{archivebase} if defined $params{archivebase};
493         
494         my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
495         my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
497         my $thisyear = $now[5]+1900;
498         my $future_month = 0;
499         $future_month = $now[4]+1 if $params{year} == $thisyear;
501         # calculate URL's for previous and next years
502         my ($url, $purl, $nurl)=("$params{year}",'','');
503         if (exists $pagesources{"$archivebase/$params{year}"}) {
504                 $url = htmllink($params{page}, $params{destpage}, 
505                         "$archivebase/$params{year}",
506                         noimageinline => 1,
507                         linktext => $params{year},
508                         title => $params{year});
509         }
510         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
511         if (exists $pagesources{"$archivebase/$pyear"}) {
512                 $purl = htmllink($params{page}, $params{destpage}, 
513                         "$archivebase/$pyear",
514                         noimageinline => 1,
515                         linktext => "\&larr;",
516                         title => $pyear);
517         }
518         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
519         if (exists $pagesources{"$archivebase/$nyear"}) {
520                 $nurl = htmllink($params{page}, $params{destpage}, 
521                         "$archivebase/$nyear",
522                         noimageinline => 1,
523                         linktext => "\&rarr;",
524                         title => $nyear);
525         }
526         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
528         # Start producing the year calendar
529         my $m=$params{months_per_row}-2;
530         $calendar=<<EOF;
531 <table class="year-calendar">
532         <tr>
533         <th class="year-calendar-arrow">$purl</th>
534         <th class="year-calendar-head" colspan="$m">$url</th>
535         <th class="year-calendar-arrow">$nurl</th>
536         </tr>
537         <tr>
538                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
539         </tr>
540 EOF
542         for (my $month = 1; $month <= 12; $month++) {
543                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
544                 my $murl;
545                 my $monthname = strftime_utf8("%B", @day);
546                 my $monthabbr = strftime_utf8("%b", @day);
547                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
548                 my $tag;
549                 my $mtag=sprintf("%02d", $month);
550                 if ($month == $params{month} && $thisyear == $params{year}) {
551                         $tag = 'year-calendar-this-month';
552                 }
553                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
554                         $tag = 'year-calendar-month-link';
555                 } 
556                 elsif ($future_month && $month >= $future_month) {
557                         $tag = 'year-calendar-month-future';
558                 } 
559                 else {
560                         $tag = 'year-calendar-month-nolink';
561                 }
563                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
564                     $post_months[$mtag]) {
565                         $murl = htmllink($params{page}, $params{destpage}, 
566                                 "$archivebase/$params{year}/$mtag",
567                                 noimageinline => 1,
568                                 linktext => $monthabbr,
569                                 title => $monthname);
570                         $calendar.=qq{\t<td class="$tag">};
571                         $calendar.=$murl;
572                         $calendar.=qq{\t</td>\n};
573                 }
574                 else {
575                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
576                 }
577                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
578                         deptype("presence"));
580                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
581         }
583         $calendar.=<<EOF;
584 </table>
585 EOF
587         return $calendar;
590 sub setnextchange ($$) {
591         my $page=shift;
592         my $timestamp=shift;
594         if (! exists $pagestate{$page}{calendar}{nextchange} ||
595             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
596                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
597         }
600 sub preprocess (@) {
601         my %params=@_;
603         my $thisyear=1900 + $now[5];
604         my $thismonth=1 + $now[4];
606         $params{pages} = "*"            unless defined $params{pages};
607         $params{type}  = "month"        unless defined $params{type};
608         $params{week_start_day} = 0     unless defined $params{week_start_day};
609         $params{months_per_row} = 3     unless defined $params{months_per_row};
610         $params{year}  = $thisyear      unless defined $params{year};
611         $params{month} = $thismonth     unless defined $params{month};
613         my $relativeyear=0;
614         if ($params{year} < 1) {
615                 $relativeyear=1;
616                 $params{year}=$thisyear+$params{year};
617         }
618         my $relativemonth=0;
619         if ($params{month} < 1) {
620                 $relativemonth=1;
621                 my $monthoff=$params{month};
622                 $params{month}=($thismonth+$monthoff) % 12;
623                 $params{month}=12 if $params{month}==0;
624                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
625                         - int($monthoff / 12);
626                 $params{year}-=$yearoff;
627         }
628         
629         $params{month} = sprintf("%02d", $params{month});
630         $changed{$params{year}}{$params{month}} = 1;
631         
632         if ($params{type} eq 'month' && $params{year} == $thisyear
633             && $params{month} == $thismonth) {
634                 # calendar for current month, updates next midnight
635                 setnextchange($params{destpage}, ($time
636                         + (60 - $now[0])                # seconds
637                         + (59 - $now[1]) * 60           # minutes
638                         + (23 - $now[2]) * 60 * 60      # hours
639                 ));
640         }
641         elsif ($params{type} eq 'month' &&
642                (($params{year} == $thisyear && $params{month} > $thismonth) ||
643                 $params{year} > $thisyear)) {
644                 # calendar for upcoming month, updates 1st of that month
645                 setnextchange($params{destpage},
646                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
647         }
648         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
649                $relativemonth) {
650                 # Calendar for current year updates 1st of next month.
651                 # Any calendar relative to the current month also updates
652                 # then.
653                 if ($thismonth < 12) {
654                         setnextchange($params{destpage},
655                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
656                 }
657                 else {
658                         setnextchange($params{destpage},
659                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
660                 }
661         }
662         elsif ($relativeyear) {
663                 # Any calendar relative to the current year updates 1st
664                 # of next year.
665                 setnextchange($params{destpage},
666                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
667         }
668         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
669                 # calendar for upcoming year, updates 1st of that year
670                 setnextchange($params{destpage},
671                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
672         }
673         else {
674                 # calendar for past month or year, does not need
675                 # to update any more
676                 delete $pagestate{$params{destpage}}{calendar};
677         }
679         my $calendar="";
680         if ($params{type} eq 'month') {
681                 $calendar=format_month(%params);
682         }
683         elsif ($params{type} eq 'year') {
684                 $calendar=format_year(%params);
685         }
687         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
688 } #}}
690 sub needsbuild (@) {
691         my $needsbuild=shift;
692         foreach my $page (keys %pagestate) {
693                 if (exists $pagestate{$page}{calendar}{nextchange}) {
694                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
695                                 # force a rebuild so the calendar shows
696                                 # the current day
697                                 push @$needsbuild, $pagesources{$page};
698                         }
699                         if (exists $pagesources{$page} && 
700                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
701                                 # remove state, will be re-added if
702                                 # the calendar is still there during the
703                                 # rebuild
704                                 delete $pagestate{$page}{calendar};
705                         }
706                 }
707         }
709         return $needsbuild;
712 sub scan (@) {
713         my %params=@_;
714         my $page=$params{page};
716         # Check if year pages have to be generated
717         if (pagespec_match($page, $config{archive_pagespec})) {
718                 my @ctime = localtime($IkiWiki::pagectime{$page});
719                 gencalendaryear($ctime[5]+1900);
720                 gencalendarmonth($ctime[5]+1900, $ctime[4]+1);
721         }