f0ac9acf9a334a09e6ae83f506db7824aee57378
[ikiwiki.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.
17
18 require 5.002;
19 package IkiWiki::Plugin::calendar;
20
21 use warnings;
22 use strict;
23 use IkiWiki 3.00;
24 use Time::Local;
25
26 my $time=time;
27 my @now=localtime($time);
28 my %changed;
29
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);
37
38         IkiWiki::loadplugin("transient");
39 }
40
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 }
79
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 }
94
95 sub is_leap_year (@) {
96         my %params=@_;
97         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
98 }
99
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;
107 }
108
109 sub build_affected {
110         my %affected;
111         my ($ayear, $amonth, $valid);
112
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         }
125
126         return %affected;
127 }
128
129 sub autocreate {
130         my ($page, $pagefile, $year, $month) = @_;
131         my $message=sprintf(gettext("creating calendar page %s"), $page);
132         debug($message);
133
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});
143
144         my $dir = $IkiWiki::Plugin::transient::transientdir;
145
146         writefile($pagefile, $dir, $template->output);
147 }
148
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         }
156 }
157
158 sub gencalendarmonth{
159         my $year = shift;
160         my $month = sprintf("%02d", shift);
161
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         );
168 }
169
170 sub gencalendaryear {
171         my $year = shift;
172         my %params = @_;
173
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         );
181
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         }
188
189         if ($config{calendar_fill_gaps}) {
190                 # Building month pages
191                 foreach my $month (1 .. 12) {
192                         gencalendarmonth($year, $month);
193                 }
194
195                 # Filling potential gaps in years (e.g. calendar goes from 2010 to 2014,
196                 # and we just added year 2005. We have to had 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         }
217 }
218
219 sub previousmonth($$$) {
220         my $year = shift;
221         my $month = shift;
222         my $archivebase = shift;
223
224         my $pmonth = $month;
225         my $pyear  = $year;
226         while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
227                 $pmonth -= 1;
228                 if ($pmonth == 0) {
229                         $pyear -= 1;
230                         $pmonth = 12;
231                         return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
232                 }
233         }
234         return ($pyear, $pmonth, 1);
235 }
236
237 sub nextmonth($$$) {
238         my $year = shift;
239         my $month = shift;
240         my $archivebase = shift;
241
242         my $nmonth = $month;
243         my $nyear  = $year;
244         while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
245                 $nmonth += 1;
246                 if ($nmonth == 13) {
247                         $nyear += 1;
248                         $nmonth = 1;
249                         return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
250                 }
251         }
252         return ($nyear, $nmonth, 1);
253 }
254
255 sub previousyear($$) {
256         my $year = shift;
257         my $archivebase = shift;
258
259         my $pyear = $year - 1;
260         while (not exists $pagesources{"$archivebase/$pyear"}) {
261                 $pyear -= 1;
262                 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
263         }
264         return ($pyear, 1);
265 }
266
267 sub nextyear($$) {
268         my $year = shift;
269         my $archivebase = shift;
270
271         my $nyear = $year + 1;
272         while (not exists $pagesources{"$archivebase/$nyear"}) {
273                 $nyear += 1;
274                 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
275         }
276         return ($nyear, 1);
277 }
278
279 sub format_month (@) {
280         my %params=@_;
281
282         my %linkcache;
283         foreach my $p (pagespec_match_list($params{page}, 
284                                 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
285                                 # add presence dependencies to update
286                                 # month calendar when pages are added/removed
287                                 deptype => deptype("presence"))) {
288                 my $mtime = $IkiWiki::pagectime{$p};
289                 my @date  = localtime($mtime);
290                 my $mday  = $date[3];
291                 my $month = $date[4] + 1;
292                 my $year  = $date[5] + 1900;
293                 my $mtag  = sprintf("%02d", $month);
294
295                 if (! $linkcache{"$year/$mtag/$mday"}) {
296                         $linkcache{"$year/$mtag/$mday"} = [];
297                 }
298                 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
299         }
300                 
301         my $archivebase = 'archives';
302         $archivebase = $config{archivebase} if defined $config{archivebase};
303         $archivebase = $params{archivebase} if defined $params{archivebase};
304         
305         my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
306         my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
307
308         # Add padding.
309         $pmonth=sprintf("%02d", $pmonth);
310         $nmonth=sprintf("%02d", $nmonth);
311
312         my $calendar="\n";
313
314         # When did this month start?
315         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
316
317         my $future_dom = 0;
318         my $today      = 0;
319         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
320                 $future_dom = $now[3]+1;
321                 $today      = $now[3];
322         }
323
324         # Find out month names for this, next, and previous months
325         my $monthabbrev=strftime_utf8("%b", @monthstart);
326         my $monthname=strftime_utf8("%B", @monthstart);
327         my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
328         my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
329
330         # Calculate URL's for monthly archives.
331         my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
332         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
333                 $url = htmllink($params{page}, $params{destpage}, 
334                         "$archivebase/$params{year}/".$params{month},
335                         noimageinline => 1,
336                         linktext => "$monthabbrev $params{year}",
337                         title => $monthname);
338         }
339         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
340                 deptype("presence"));
341         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
342                 $purl = htmllink($params{page}, $params{destpage}, 
343                         "$archivebase/$pyear/$pmonth",
344                         noimageinline => 1,
345                         linktext => "\&larr;",
346                         title => $pmonthname);
347         }
348         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
349                 deptype("presence"));
350         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
351                 $nurl = htmllink($params{page}, $params{destpage}, 
352                         "$archivebase/$nyear/$nmonth",
353                         noimageinline => 1,
354                         linktext => "\&rarr;",
355                         title => $nmonthname);
356         }
357         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
358                 deptype("presence"));
359
360         # Start producing the month calendar
361         $calendar=<<EOF;
362 <table class="month-calendar">
363         <tr>
364         <th class="month-calendar-arrow">$purl</th>
365         <th class="month-calendar-head" colspan="5">$url</th>
366         <th class="month-calendar-arrow">$nurl</th>
367         </tr>
368         <tr>
369 EOF
370
371         # Suppose we want to start the week with day $week_start_day
372         # If $monthstart[6] == 1
373         my $week_start_day = $params{week_start_day};
374
375         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
376         my %downame;
377         my %dowabbr;
378         for my $dow ($week_start_day..$week_start_day+6) {
379                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
380                 my $downame = strftime_utf8("%A", @day);
381                 my $dowabbr = substr($downame, 0, 1);
382                 $downame{$dow % 7}=$downame;
383                 $dowabbr{$dow % 7}=$dowabbr;
384                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
385         }
386
387         $calendar.=<<EOF;
388         </tr>
389 EOF
390
391         my $wday;
392         # we start with a week_start_day, and skip until we get to the first
393         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
394                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
395                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
396         }
397
398         # At this point, either the first is a week_start_day, in which case
399         # nothing has been printed, or else we are in the middle of a row.
400         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
401              $day++, $wday++, $wday %= 7) {
402                 # At this point, on a week_start_day, we close out a row,
403                 # and start a new one -- unless it is week_start_day on the
404                 # first, where we do not close a row -- since none was started.
405                 if ($wday == $week_start_day) {
406                         $calendar.=qq{\t</tr>\n} unless $day == 1;
407                         $calendar.=qq{\t<tr>\n};
408                 }
409                 
410                 my $tag;
411                 my $key="$params{year}/$params{month}/$day";
412                 if (defined $linkcache{$key}) {
413                         if ($day == $today) {
414                                 $tag='month-calendar-day-this-day';
415                         }
416                         else {
417                                 $tag='month-calendar-day-link';
418                         }
419                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
420                         $calendar.=qq{<div class='popup'>$day<div class='balloon'>};
421                         # Several postings on this page
422                         $calendar.=qq{<ul>};
423                         foreach my $page (@{$linkcache{$key}}) {
424                                 $calendar.= qq{\n\t\t\t<li>};
425                                 my $title;
426                                 if (exists $pagestate{$page}{meta}{title}) {
427                                         $title = "$pagestate{$page}{meta}{title}";
428                                 }
429                                 else {
430                                         $title = pagetitle(IkiWiki::basename($page));
431                                 }
432                                 $calendar.=htmllink($params{page}, $params{destpage}, 
433                                         $page,
434                                         noimageinline => 1,
435                                         linktext => $title,
436                                         title => $title);
437                                 $calendar.= '</li>';
438                         }
439                         $calendar.=qq{\n\t\t</ul>};
440                         $calendar.=qq{</div></div>};
441                         $calendar.=qq{</td>\n};
442                 }
443                 else {
444                         if ($day == $today) {
445                                 $tag='month-calendar-day-this-day';
446                         }
447                         elsif ($day == $future_dom) {
448                                 $tag='month-calendar-day-future';
449                         }
450                         else {
451                                 $tag='month-calendar-day-nolink';
452                         }
453                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
454                 }
455         }
456
457         # finish off the week
458         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
459                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
460         }
461         $calendar.=<<EOF;
462         </tr>
463 </table>
464 EOF
465
466         return $calendar;
467 }
468
469 sub format_year (@) {
470         my %params=@_;
471
472         my @post_months;
473         foreach my $p (pagespec_match_list($params{page}, 
474                                 "creation_year($params{year}) and ($params{pages})",
475                                 # add presence dependencies to update
476                                 # year calendar's links to months when
477                                 # pages are added/removed
478                                 deptype => deptype("presence"))) {
479                 my $mtime = $IkiWiki::pagectime{$p};
480                 my @date  = localtime($mtime);
481                 my $month = $date[4] + 1;
482
483                 $post_months[$month]++;
484         }
485                 
486         my $calendar="\n";
487
488         my $archivebase = 'archives';
489         $archivebase = $config{archivebase} if defined $config{archivebase};
490         $archivebase = $params{archivebase} if defined $params{archivebase};
491         
492         my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
493         my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
494
495         my $thisyear = $now[5]+1900;
496         my $future_month = 0;
497         $future_month = $now[4]+1 if $params{year} == $thisyear;
498
499         # calculate URL's for previous and next years
500         my ($url, $purl, $nurl)=("$params{year}",'','');
501         if (exists $pagesources{"$archivebase/$params{year}"}) {
502                 $url = htmllink($params{page}, $params{destpage}, 
503                         "$archivebase/$params{year}",
504                         noimageinline => 1,
505                         linktext => $params{year},
506                         title => $params{year});
507         }
508         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
509         if (exists $pagesources{"$archivebase/$pyear"}) {
510                 $purl = htmllink($params{page}, $params{destpage}, 
511                         "$archivebase/$pyear",
512                         noimageinline => 1,
513                         linktext => "\&larr;",
514                         title => $pyear);
515         }
516         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
517         if (exists $pagesources{"$archivebase/$nyear"}) {
518                 $nurl = htmllink($params{page}, $params{destpage}, 
519                         "$archivebase/$nyear",
520                         noimageinline => 1,
521                         linktext => "\&rarr;",
522                         title => $nyear);
523         }
524         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
525
526         # Start producing the year calendar
527         my $m=$params{months_per_row}-2;
528         $calendar=<<EOF;
529 <table class="year-calendar">
530         <tr>
531         <th class="year-calendar-arrow">$purl</th>
532         <th class="year-calendar-head" colspan="$m">$url</th>
533         <th class="year-calendar-arrow">$nurl</th>
534         </tr>
535         <tr>
536                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
537         </tr>
538 EOF
539
540         for (my $month = 1; $month <= 12; $month++) {
541                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
542                 my $murl;
543                 my $monthname = strftime_utf8("%B", @day);
544                 my $monthabbr = strftime_utf8("%b", @day);
545                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
546                 my $tag;
547                 my $mtag=sprintf("%02d", $month);
548                 if ($month == $params{month} && $thisyear == $params{year}) {
549                         $tag = 'year-calendar-this-month';
550                 }
551                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
552                         $tag = 'year-calendar-month-link';
553                 } 
554                 elsif ($future_month && $month >= $future_month) {
555                         $tag = 'year-calendar-month-future';
556                 } 
557                 else {
558                         $tag = 'year-calendar-month-nolink';
559                 }
560
561                 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
562                     $post_months[$mtag]) {
563                         $murl = htmllink($params{page}, $params{destpage}, 
564                                 "$archivebase/$params{year}/$mtag",
565                                 noimageinline => 1,
566                                 linktext => $monthabbr,
567                                 title => $monthname);
568                         $calendar.=qq{\t<td class="$tag">};
569                         $calendar.=$murl;
570                         $calendar.=qq{\t</td>\n};
571                 }
572                 else {
573                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
574                 }
575                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
576                         deptype("presence"));
577
578                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
579         }
580
581         $calendar.=<<EOF;
582 </table>
583 EOF
584
585         return $calendar;
586 }
587
588 sub setnextchange ($$) {
589         my $page=shift;
590         my $timestamp=shift;
591
592         if (! exists $pagestate{$page}{calendar}{nextchange} ||
593             $pagestate{$page}{calendar}{nextchange} > $timestamp) {
594                 $pagestate{$page}{calendar}{nextchange}=$timestamp;
595         }
596 }
597
598 sub preprocess (@) {
599         my %params=@_;
600
601         my $thisyear=1900 + $now[5];
602         my $thismonth=1 + $now[4];
603
604         $params{pages} = "*"            unless defined $params{pages};
605         $params{type}  = "month"        unless defined $params{type};
606         $params{week_start_day} = 0     unless defined $params{week_start_day};
607         $params{months_per_row} = 3     unless defined $params{months_per_row};
608         $params{year}  = $thisyear      unless defined $params{year};
609         $params{month} = $thismonth     unless defined $params{month};
610
611         my $relativeyear=0;
612         if ($params{year} < 1) {
613                 $relativeyear=1;
614                 $params{year}=$thisyear+$params{year};
615         }
616         my $relativemonth=0;
617         if ($params{month} < 1) {
618                 $relativemonth=1;
619                 my $monthoff=$params{month};
620                 $params{month}=($thismonth+$monthoff) % 12;
621                 $params{month}=12 if $params{month}==0;
622                 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
623                         - int($monthoff / 12);
624                 $params{year}-=$yearoff;
625         }
626         
627         $params{month} = sprintf("%02d", $params{month});
628         $changed{$params{year}}{$params{month}} = 1;
629         
630         if ($params{type} eq 'month' && $params{year} == $thisyear
631             && $params{month} == $thismonth) {
632                 # calendar for current month, updates next midnight
633                 setnextchange($params{destpage}, ($time
634                         + (60 - $now[0])                # seconds
635                         + (59 - $now[1]) * 60           # minutes
636                         + (23 - $now[2]) * 60 * 60      # hours
637                 ));
638         }
639         elsif ($params{type} eq 'month' &&
640                (($params{year} == $thisyear && $params{month} > $thismonth) ||
641                 $params{year} > $thisyear)) {
642                 # calendar for upcoming month, updates 1st of that month
643                 setnextchange($params{destpage},
644                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
645         }
646         elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
647                $relativemonth) {
648                 # Calendar for current year updates 1st of next month.
649                 # Any calendar relative to the current month also updates
650                 # then.
651                 if ($thismonth < 12) {
652                         setnextchange($params{destpage},
653                                 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
654                 }
655                 else {
656                         setnextchange($params{destpage},
657                                 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
658                 }
659         }
660         elsif ($relativeyear) {
661                 # Any calendar relative to the current year updates 1st
662                 # of next year.
663                 setnextchange($params{destpage},
664                         timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
665         }
666         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
667                 # calendar for upcoming year, updates 1st of that year
668                 setnextchange($params{destpage},
669                         timelocal(0, 0, 0, 1, 1-1, $params{year}));
670         }
671         else {
672                 # calendar for past month or year, does not need
673                 # to update any more
674                 delete $pagestate{$params{destpage}}{calendar};
675         }
676
677         my $calendar="";
678         if ($params{type} eq 'month') {
679                 $calendar=format_month(%params);
680         }
681         elsif ($params{type} eq 'year') {
682                 $calendar=format_year(%params);
683         }
684
685         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
686 } #}}
687
688 sub needsbuild (@) {
689         my $needsbuild=shift;
690         foreach my $page (keys %pagestate) {
691                 if (exists $pagestate{$page}{calendar}{nextchange}) {
692                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
693                                 # force a rebuild so the calendar shows
694                                 # the current day
695                                 push @$needsbuild, $pagesources{$page};
696                         }
697                         if (exists $pagesources{$page} && 
698                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
699                                 # remove state, will be re-added if
700                                 # the calendar is still there during the
701                                 # rebuild
702                                 delete $pagestate{$page}{calendar};
703                         }
704                 }
705         }
706
707         return $needsbuild;
708 }
709
710 sub scan (@) {
711         my %params=@_;
712         my $page=$params{page};
713
714         return unless $config{calendar_autocreate};
715
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         }
722 }
723
724 1