]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
Don't memoize ensure_committer
[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         # Building year page
175         my $page = calendarlink($year);
176         my $pagefile = newpagefile($page, $config{default_pageext});
177         add_autofile(
178                 $pagefile, "calendar",
179                 sub {return autocreate($page, $pagefile, $year);}
180         );
182         if (not exists $wikistate{calendar}{minyear}) {
183                 $wikistate{calendar}{minyear} = $year;
184         }
185         if (not exists $wikistate{calendar}{maxyear}) {
186                 $wikistate{calendar}{maxyear} = $year;
187         }
189         if ($config{calendar_fill_gaps}) {
190                 # Building month pages
191                 foreach my $month (1 .. 12) {
192                         gencalendarmonth($year, $month);
193                 }
195                 # Filling potential gaps in years (e.g. calendar goes from 2010 to 2014,
196                 # and we just added year 2005. We have to add years 2006 to 2009).
197                 return if $params{norecurse};
198                 if ($wikistate{calendar}{minyear} > $year) {
199                         foreach my $other ($year + 1 .. $wikistate{calendar}{minyear} - 1) {
200                                 gencalendaryear($other, norecurse => 1);
201                         }
202                         $wikistate{calendar}{minyear} = $year;
203                 }
204                 if ($wikistate{calendar}{maxyear} < $year) {
205                         foreach my $other ($wikistate{calendar}{maxyear} + 1 .. $year - 1) {
206                                 gencalendaryear($other, norecurse => 1);
207                         }
208                         $wikistate{calendar}{maxyear} = $year;
209                 }
210         }
211         if ($year < $wikistate{calendar}{minyear}) {
212                 $wikistate{calendar}{minyear} = $year;
213         }
214         if ($year >  $wikistate{calendar}{maxyear}) {
215                 $wikistate{calendar}{maxyear} = $year;
216         }
219 sub previousmonth($$$) {
220         my $year = shift;
221         my $month = shift;
222         my $archivebase = shift;
224         if (not exists $wikistate{calendar}{minyear}) {
225                 $wikistate{calendar}{minyear} = $year;
226         }
228         my $pmonth = $month;
229         my $pyear  = $year;
230         while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
231                 $pmonth -= 1;
232                 if ($pmonth == 0) {
233                         $pyear -= 1;
234                         $pmonth = 12;
235                         return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
236                 }
237         }
238         return ($pyear, $pmonth, 1);
241 sub nextmonth($$$) {
242         my $year = shift;
243         my $month = shift;
244         my $archivebase = shift;
246         if (not exists $wikistate{calendar}{maxyear}) {
247                 $wikistate{calendar}{maxyear} = $year;
248         }
250         my $nmonth = $month;
251         my $nyear  = $year;
252         while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
253                 $nmonth += 1;
254                 if ($nmonth == 13) {
255                         $nyear += 1;
256                         $nmonth = 1;
257                         return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
258                 }
259         }
260         return ($nyear, $nmonth, 1);
263 sub previousyear($$) {
264         my $year = shift;
265         my $archivebase = shift;
267         my $pyear = $year - 1;
268         while (not exists $pagesources{"$archivebase/$pyear"}) {
269                 $pyear -= 1;
270                 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
271         }
272         return ($pyear, 1);
275 sub nextyear($$) {
276         my $year = shift;
277         my $archivebase = shift;
279         my $nyear = $year + 1;
280         while (not exists $pagesources{"$archivebase/$nyear"}) {
281                 $nyear += 1;
282                 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
283         }
284         return ($nyear, 1);
287 sub format_month (@) {
288         my %params=@_;
290         my %linkcache;
291         foreach my $p (pagespec_match_list($params{page}, 
292                                 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
293                                 # add presence dependencies to update
294                                 # month calendar when pages are added/removed
295                                 deptype => deptype("presence"))) {
296                 my $mtime = $IkiWiki::pagectime{$p};
297                 my @date  = localtime($mtime);
298                 my $mday  = $date[3];
299                 my $month = $date[4] + 1;
300                 my $year  = $date[5] + 1900;
301                 my $mtag  = sprintf("%02d", $month);
303                 if (! $linkcache{"$year/$mtag/$mday"}) {
304                         $linkcache{"$year/$mtag/$mday"} = [];
305                 }
306                 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
307         }
308                 
309         my $archivebase = 'archives';
310         $archivebase = $config{archivebase} if defined $config{archivebase};
311         $archivebase = $params{archivebase} if defined $params{archivebase};
312         
313         my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
314         my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
316         # Add padding.
317         $pmonth=sprintf("%02d", $pmonth);
318         $nmonth=sprintf("%02d", $nmonth);
320         my $calendar="\n";
322         # When did this month start?
323         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
325         my $future_dom = 0;
326         my $today      = 0;
327         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
328                 $future_dom = $now[3]+1;
329                 $today      = $now[3];
330         }
332         # Find out month names for this, next, and previous months
333         my $monthabbrev=strftime_utf8("%b", @monthstart);
334         my $monthname=strftime_utf8("%B", @monthstart);
335         my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
336         my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
338         # Calculate URL's for monthly archives.
339         my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
340         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
341                 $url = htmllink($params{page}, $params{destpage}, 
342                         "$archivebase/$params{year}/".$params{month},
343                         noimageinline => 1,
344                         linktext => "$monthabbrev $params{year}",
345                         title => $monthname);
346         }
347         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
348                 deptype("presence"));
349         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
350                 $purl = htmllink($params{page}, $params{destpage}, 
351                         "$archivebase/$pyear/$pmonth",
352                         noimageinline => 1,
353                         linktext => "\&larr;",
354                         title => $pmonthname);
355         }
356         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
357                 deptype("presence"));
358         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
359                 $nurl = htmllink($params{page}, $params{destpage}, 
360                         "$archivebase/$nyear/$nmonth",
361                         noimageinline => 1,
362                         linktext => "\&rarr;",
363                         title => $nmonthname);
364         }
365         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
366                 deptype("presence"));
368         # Start producing the month calendar
369         $calendar=<<EOF;
370 <table class="month-calendar">
371         <tr>
372         <th class="month-calendar-arrow">$purl</th>
373         <th class="month-calendar-head" colspan="5">$url</th>
374         <th class="month-calendar-arrow">$nurl</th>
375         </tr>
376         <tr>
377 EOF
379         # Suppose we want to start the week with day $week_start_day
380         # If $monthstart[6] == 1
381         my $week_start_day = $params{week_start_day};
383         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
384         my %downame;
385         my %dowabbr;
386         for my $dow ($week_start_day..$week_start_day+6) {
387                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
388                 my $downame = strftime_utf8("%A", @day);
389                 my $dowabbr = substr($downame, 0, 1);
390                 $downame{$dow % 7}=$downame;
391                 $dowabbr{$dow % 7}=$dowabbr;
392                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
393         }
395         $calendar.=<<EOF;
396         </tr>
397 EOF
399         my $wday;
400         # we start with a week_start_day, and skip until we get to the first
401         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
402                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
403                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
404         }
406         # At this point, either the first is a week_start_day, in which case
407         # nothing has been printed, or else we are in the middle of a row.
408         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
409              $day++, $wday++, $wday %= 7) {
410                 # At this point, on a week_start_day, we close out a row,
411                 # and start a new one -- unless it is week_start_day on the
412                 # first, where we do not close a row -- since none was started.
413                 if ($wday == $week_start_day) {
414                         $calendar.=qq{\t</tr>\n} unless $day == 1;
415                         $calendar.=qq{\t<tr>\n};
416                 }
417                 
418                 my $tag;
419                 my $key="$params{year}/$params{month}/$day";
420                 if (defined $linkcache{$key}) {
421                         if ($day == $today) {
422                                 $tag='month-calendar-day-this-day';
423                         }
424                         else {
425                                 $tag='month-calendar-day-link';
426                         }
427                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
428                         $calendar.=qq{<div class='popup'>$day<div class='balloon'>};
429                         # Several postings on this page
430                         $calendar.=qq{<ul>};
431                         foreach my $page (@{$linkcache{$key}}) {
432                                 $calendar.= qq{\n\t\t\t<li>};
433                                 my $title;
434                                 if (exists $pagestate{$page}{meta}{title}) {
435                                         $title = "$pagestate{$page}{meta}{title}";
436                                 }
437                                 else {
438                                         $title = pagetitle(IkiWiki::basename($page));
439                                 }
440                                 $calendar.=htmllink($params{page}, $params{destpage}, 
441                                         $page,
442                                         noimageinline => 1,
443                                         linktext => $title,
444                                         title => $title);
445                                 $calendar.= '</li>';
446                         }
447                         $calendar.=qq{\n\t\t</ul>};
448                         $calendar.=qq{</div></div>};
449                         $calendar.=qq{</td>\n};
450                 }
451                 else {
452                         if ($day == $today) {
453                                 $tag='month-calendar-day-this-day';
454                         }
455                         elsif ($day == $future_dom) {
456                                 $tag='month-calendar-day-future';
457                         }
458                         else {
459                                 $tag='month-calendar-day-nolink';
460                         }
461                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
462                 }
463         }
465         # finish off the week
466         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
467                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
468         }
469         $calendar.=<<EOF;
470         </tr>
471 </table>
472 EOF
474         return $calendar;
477 sub format_year (@) {
478         my %params=@_;
480         my @post_months;
481         foreach my $p (pagespec_match_list($params{page}, 
482                                 "creation_year($params{year}) and ($params{pages})",
483                                 # add presence dependencies to update
484                                 # year calendar's links to months when
485                                 # pages are added/removed
486                                 deptype => deptype("presence"))) {
487                 my $mtime = $IkiWiki::pagectime{$p};
488                 my @date  = localtime($mtime);
489                 my $month = $date[4] + 1;
491                 $post_months[$month]++;
492         }
493                 
494         my $calendar="\n";
496         my $archivebase = 'archives';
497         $archivebase = $config{archivebase} if defined $config{archivebase};
498         $archivebase = $params{archivebase} if defined $params{archivebase};
499         
500         my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
501         my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
503         my $thisyear = $now[5]+1900;
504         my $future_month = 0;
505         $future_month = $now[4]+1 if $params{year} == $thisyear;
507         # calculate URL's for previous and next years
508         my ($url, $purl, $nurl)=("$params{year}",'','');
509         if (exists $pagesources{"$archivebase/$params{year}"}) {
510                 $url = htmllink($params{page}, $params{destpage}, 
511                         "$archivebase/$params{year}",
512                         noimageinline => 1,
513                         linktext => $params{year},
514                         title => $params{year});
515         }
516         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
517         if (exists $pagesources{"$archivebase/$pyear"}) {
518                 $purl = htmllink($params{page}, $params{destpage}, 
519                         "$archivebase/$pyear",
520                         noimageinline => 1,
521                         linktext => "\&larr;",
522                         title => $pyear);
523         }
524         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
525         if (exists $pagesources{"$archivebase/$nyear"}) {
526                 $nurl = htmllink($params{page}, $params{destpage}, 
527                         "$archivebase/$nyear",
528                         noimageinline => 1,
529                         linktext => "\&rarr;",
530                         title => $nyear);
531         }
532         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
534         # Start producing the year calendar
535         my $m=$params{months_per_row}-2;
536         $calendar=<<EOF;
537 <table class="year-calendar">
538         <tr>
539         <th class="year-calendar-arrow">$purl</th>
540         <th class="year-calendar-head" colspan="$m">$url</th>
541         <th class="year-calendar-arrow">$nurl</th>
542         </tr>
543         <tr>
544                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
545         </tr>
546 EOF
548         for (my $month = 1; $month <= 12; $month++) {
549                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
550                 my $murl;
551                 my $monthname = strftime_utf8("%B", @day);
552                 my $monthabbr = strftime_utf8("%b", @day);
553                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
554                 my $tag;
555                 my $mtag=sprintf("%02d", $month);
556                 if ($month == $params{month} && $thisyear == $params{year}) {
557                         $tag = 'year-calendar-this-month';
558                 }
559                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
560                         $tag = 'year-calendar-month-link';
561                 } 
562                 elsif ($future_month && $month >= $future_month) {
563                         $tag = 'year-calendar-month-future';
564                 } 
565                 else {
566                         $tag = 'year-calendar-month-nolink';
567                 }
569                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
570                     $post_months[$mtag]) {
571                         $murl = htmllink($params{page}, $params{destpage}, 
572                                 "$archivebase/$params{year}/$mtag",
573                                 noimageinline => 1,
574                                 linktext => $monthabbr,
575                                 title => $monthname);
576                         $calendar.=qq{\t<td class="$tag">};
577                         $calendar.=$murl;
578                         $calendar.=qq{\t</td>\n};
579                 }
580                 else {
581                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
582                 }
583                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
584                         deptype("presence"));
586                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
587         }
589         $calendar.=<<EOF;
590 </table>
591 EOF
593         return $calendar;
596 sub setnextchange ($$) {
597         my $page=shift;
598         my $timestamp=shift;
600         if (! exists $pagestate{$page}{calendar}{nextchange} ||
601             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
602                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
603         }
606 sub preprocess (@) {
607         my %params=@_;
609         my $thisyear=1900 + $now[5];
610         my $thismonth=1 + $now[4];
612         $params{pages} = "*"            unless defined $params{pages};
613         $params{type}  = "month"        unless defined $params{type};
614         $params{week_start_day} = 0     unless defined $params{week_start_day};
615         $params{months_per_row} = 3     unless defined $params{months_per_row};
616         $params{year}  = $thisyear      unless defined $params{year};
617         $params{month} = $thismonth     unless defined $params{month};
619         my $relativeyear=0;
620         if ($params{year} < 1) {
621                 $relativeyear=1;
622                 $params{year}=$thisyear+$params{year};
623         }
624         my $relativemonth=0;
625         if ($params{month} < 1) {
626                 $relativemonth=1;
627                 my $monthoff=$params{month};
628                 $params{month}=($thismonth+$monthoff) % 12;
629                 $params{month}=12 if $params{month}==0;
630                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
631                         - int($monthoff / 12);
632                 $params{year}-=$yearoff;
633         }
634         
635         $params{month} = sprintf("%02d", $params{month});
636         $changed{$params{year}}{$params{month}} = 1;
637         
638         if ($params{type} eq 'month' && $params{year} == $thisyear
639             && $params{month} == $thismonth) {
640                 # calendar for current month, updates next midnight
641                 setnextchange($params{destpage}, ($time
642                         + (60 - $now[0])                # seconds
643                         + (59 - $now[1]) * 60           # minutes
644                         + (23 - $now[2]) * 60 * 60      # hours
645                 ));
646         }
647         elsif ($params{type} eq 'month' &&
648                (($params{year} == $thisyear && $params{month} > $thismonth) ||
649                 $params{year} > $thisyear)) {
650                 # calendar for upcoming month, updates 1st of that month
651                 setnextchange($params{destpage},
652                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
653         }
654         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
655                $relativemonth) {
656                 # Calendar for current year updates 1st of next month.
657                 # Any calendar relative to the current month also updates
658                 # then.
659                 if ($thismonth < 12) {
660                         setnextchange($params{destpage},
661                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
662                 }
663                 else {
664                         setnextchange($params{destpage},
665                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
666                 }
667         }
668         elsif ($relativeyear) {
669                 # Any calendar relative to the current year updates 1st
670                 # of next year.
671                 setnextchange($params{destpage},
672                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
673         }
674         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
675                 # calendar for upcoming year, updates 1st of that year
676                 setnextchange($params{destpage},
677                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
678         }
679         else {
680                 # calendar for past month or year, does not need
681                 # to update any more
682                 delete $pagestate{$params{destpage}}{calendar};
683         }
685         my $calendar="";
686         if ($params{type} eq 'month') {
687                 $calendar=format_month(%params);
688         }
689         elsif ($params{type} eq 'year') {
690                 $calendar=format_year(%params);
691         }
693         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
694 } #}}
696 sub needsbuild (@) {
697         my $needsbuild=shift;
698         foreach my $page (keys %pagestate) {
699                 if (exists $pagestate{$page}{calendar}{nextchange}) {
700                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
701                                 # force a rebuild so the calendar shows
702                                 # the current day
703                                 push @$needsbuild, $pagesources{$page};
704                         }
705                         if (exists $pagesources{$page} && 
706                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
707                                 # remove state, will be re-added if
708                                 # the calendar is still there during the
709                                 # rebuild
710                                 delete $pagestate{$page}{calendar};
711                         }
712                 }
713         }
715         return $needsbuild;
718 sub scan (@) {
719         my %params=@_;
720         my $page=$params{page};
722         return unless $config{calendar_autocreate};
724         # Check if year pages have to be generated
725         if (pagespec_match($page, $config{archive_pagespec})) {
726                 my @ctime = localtime($IkiWiki::pagectime{$page});
727                 gencalendaryear($ctime[5]+1900);
728                 gencalendarmonth($ctime[5]+1900, $ctime[4]+1);
729         }