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