2 # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
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.
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.
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.
19 package IkiWiki::Plugin::calendar;
27 my @now=localtime($time);
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");
50 example => "archives",
51 description => "base of the archives hierarchy",
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',
63 calendar_autocreate => {
66 description => "autocreate new calendar pages?",
70 calendar_fill_gaps => {
74 description => "if set, when building calendar pages, also build pages of year and month when no pages were published (building empty calendars).",
81 if (! defined $config{calendar_autocreate}) {
82 $config{calendar_autocreate} = defined $config{archivebase};
84 if (! defined $config{archive_pagespec}) {
85 $config{archive_pagespec} = '*';
87 if (! defined $config{archivebase}) {
88 $config{archivebase} = 'archives';
90 if (! defined $config{calendar_fill_gaps}) {
91 $config{calendar_fill_gaps} = 1;
95 sub is_leap_year (@) {
97 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
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)) {
106 return $days_in_month;
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);
130 my ($page, $pagefile, $year, $month) = @_;
131 my $message=sprintf(gettext("creating calendar page %s"), $page);
135 if (defined $month) {
136 $template=template("calendarmonth.tmpl");
138 $template=template("calendaryear.tmpl");
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;
154 return $config{archivebase} . "/" . $year;
158 sub gencalendarmonth{
160 my $month = sprintf("%02d", shift);
162 my $page = calendarlink($year, $month);
163 my $pagefile = newpagefile($page, $config{default_pageext});
165 $pagefile, "calendar",
166 sub {return autocreate($page, $pagefile, $year, $month);}
170 sub gencalendaryear {
175 my $page = calendarlink($year);
176 my $pagefile = newpagefile($page, $config{default_pageext});
178 $pagefile, "calendar",
179 sub {return autocreate($page, $pagefile, $year);}
182 if (not exists $wikistate{calendar}{minyear}) {
183 $wikistate{calendar}{minyear} = $year;
185 if (not exists $wikistate{calendar}{maxyear}) {
186 $wikistate{calendar}{maxyear} = $year;
189 if ($config{calendar_fill_gaps}) {
190 # Building month pages
191 foreach my $month (1 .. 12) {
192 gencalendarmonth($year, $month);
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);
202 $wikistate{calendar}{minyear} = $year;
204 if ($wikistate{calendar}{maxyear} < $year) {
205 foreach my $other ($wikistate{calendar}{maxyear} + 1 .. $year - 1) {
206 gencalendaryear($other, norecurse => 1);
208 $wikistate{calendar}{maxyear} = $year;
211 if ($year < $wikistate{calendar}{minyear}) {
212 $wikistate{calendar}{minyear} = $year;
214 if ($year > $wikistate{calendar}{maxyear}) {
215 $wikistate{calendar}{maxyear} = $year;
219 sub previousmonth($$$) {
222 my $archivebase = shift;
224 if (not exists $wikistate{calendar}{minyear}) {
225 $wikistate{calendar}{minyear} = $year;
230 while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
235 return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
238 return ($pyear, $pmonth, 1);
244 my $archivebase = shift;
246 if (not exists $wikistate{calendar}{maxyear}) {
247 $wikistate{calendar}{maxyear} = $year;
252 while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
257 return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
260 return ($nyear, $nmonth, 1);
263 sub previousyear($$) {
265 my $archivebase = shift;
267 my $pyear = $year - 1;
268 while (not exists $pagesources{"$archivebase/$pyear"}) {
270 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
277 my $archivebase = shift;
279 my $nyear = $year + 1;
280 while (not exists $pagesources{"$archivebase/$nyear"}) {
282 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
287 sub format_month (@) {
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);
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"} = [];
306 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
309 my $archivebase = 'archives';
310 $archivebase = $config{archivebase} if defined $config{archivebase};
311 $archivebase = $params{archivebase} if defined $params{archivebase};
313 my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
314 my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
317 $pmonth=sprintf("%02d", $pmonth);
318 $nmonth=sprintf("%02d", $nmonth);
322 # When did this month start?
323 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
327 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
328 $future_dom = $now[3]+1;
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},
344 linktext => "$monthabbrev $params{year}",
345 title => $monthname);
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",
353 linktext => "\←",
354 title => $pmonthname);
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",
362 linktext => "\→",
363 title => $nmonthname);
365 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
366 deptype("presence"));
368 # Start producing the month calendar
370 <table class="month-calendar">
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>
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;
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};
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}"> </td>\n};
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};
419 my $key="$params{year}/$params{month}/$day";
420 if (defined $linkcache{$key}) {
421 if ($day == $today) {
422 $tag='month-calendar-day-this-day';
425 $tag='month-calendar-day-link';
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
431 foreach my $page (@{$linkcache{$key}}) {
432 $calendar.= qq{\n\t\t\t<li>};
434 if (exists $pagestate{$page}{meta}{title}) {
435 $title = "$pagestate{$page}{meta}{title}";
438 $title = pagetitle(IkiWiki::basename($page));
440 $calendar.=htmllink($params{page}, $params{destpage},
447 $calendar.=qq{\n\t\t</ul>};
448 $calendar.=qq{</div></div>};
449 $calendar.=qq{</td>\n};
452 if ($day == $today) {
453 $tag='month-calendar-day-this-day';
455 elsif ($day == $future_dom) {
456 $tag='month-calendar-day-future';
459 $tag='month-calendar-day-nolink';
461 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
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}"> </td>\n};
477 sub format_year (@) {
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]++;
496 my $archivebase = 'archives';
497 $archivebase = $config{archivebase} if defined $config{archivebase};
498 $archivebase = $params{archivebase} if defined $params{archivebase};
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}",
513 linktext => $params{year},
514 title => $params{year});
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",
521 linktext => "\←",
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",
529 linktext => "\→",
532 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
534 # Start producing the year calendar
535 my $m=$params{months_per_row}-2;
537 <table class="year-calendar">
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>
544 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
548 for (my $month = 1; $month <= 12; $month++) {
549 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
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);
555 my $mtag=sprintf("%02d", $month);
556 if ($month == $params{month} && $thisyear == $params{year}) {
557 $tag = 'year-calendar-this-month';
559 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
560 $tag = 'year-calendar-month-link';
562 elsif ($future_month && $month >= $future_month) {
563 $tag = 'year-calendar-month-future';
566 $tag = 'year-calendar-month-nolink';
569 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
570 $post_months[$mtag]) {
571 $murl = htmllink($params{page}, $params{destpage},
572 "$archivebase/$params{year}/$mtag",
574 linktext => $monthabbr,
575 title => $monthname);
576 $calendar.=qq{\t<td class="$tag">};
578 $calendar.=qq{\t</td>\n};
581 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
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);
596 sub setnextchange ($$) {
600 if (! exists $pagestate{$page}{calendar}{nextchange} ||
601 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
602 $pagestate{$page}{calendar}{nextchange}=$timestamp;
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};
620 if ($params{year} < 1) {
622 $params{year}=$thisyear+$params{year};
625 if ($params{month} < 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;
635 $params{month} = sprintf("%02d", $params{month});
636 $changed{$params{year}}{$params{month}} = 1;
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
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}));
654 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
656 # Calendar for current year updates 1st of next month.
657 # Any calendar relative to the current month also updates
659 if ($thismonth < 12) {
660 setnextchange($params{destpage},
661 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
664 setnextchange($params{destpage},
665 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
668 elsif ($relativeyear) {
669 # Any calendar relative to the current year updates 1st
671 setnextchange($params{destpage},
672 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
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}));
680 # calendar for past month or year, does not need
682 delete $pagestate{$params{destpage}}{calendar};
686 if ($params{type} eq 'month') {
687 $calendar=format_month(%params);
689 elsif ($params{type} eq 'year') {
690 $calendar=format_year(%params);
693 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
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
703 push @$needsbuild, $pagesources{$page};
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
710 delete $pagestate{$page}{calendar};
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);