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;
226 while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
231 return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
234 return ($pyear, $pmonth, 1);
240 my $archivebase = shift;
244 while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
249 return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
252 return ($nyear, $nmonth, 1);
255 sub previousyear($$) {
257 my $archivebase = shift;
259 my $pyear = $year - 1;
260 while (not exists $pagesources{"$archivebase/$pyear"}) {
262 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
269 my $archivebase = shift;
271 my $nyear = $year + 1;
272 while (not exists $pagesources{"$archivebase/$nyear"}) {
274 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
279 sub format_month (@) {
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);
291 my $month = $date[4] + 1;
292 my $year = $date[5] + 1900;
293 my $mtag = sprintf("%02d", $month);
295 if (! $linkcache{"$year/$mtag/$mday"}) {
296 $linkcache{"$year/$mtag/$mday"} = [];
298 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
301 my $archivebase = 'archives';
302 $archivebase = $config{archivebase} if defined $config{archivebase};
303 $archivebase = $params{archivebase} if defined $params{archivebase};
305 my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
306 my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
309 $pmonth=sprintf("%02d", $pmonth);
310 $nmonth=sprintf("%02d", $nmonth);
314 # When did this month start?
315 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
319 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
320 $future_dom = $now[3]+1;
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)));
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},
336 linktext => "$monthabbrev $params{year}",
337 title => $monthname);
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",
345 linktext => "\←",
346 title => $pmonthname);
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",
354 linktext => "\→",
355 title => $nmonthname);
357 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
358 deptype("presence"));
360 # Start producing the month calendar
362 <table class="month-calendar">
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>
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};
375 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
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};
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}"> </td>\n};
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};
411 my $key="$params{year}/$params{month}/$day";
412 if (defined $linkcache{$key}) {
413 if ($day == $today) {
414 $tag='month-calendar-day-this-day';
417 $tag='month-calendar-day-link';
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
423 foreach my $page (@{$linkcache{$key}}) {
424 $calendar.= qq{\n\t\t\t<li>};
426 if (exists $pagestate{$page}{meta}{title}) {
427 $title = "$pagestate{$page}{meta}{title}";
430 $title = pagetitle(IkiWiki::basename($page));
432 $calendar.=htmllink($params{page}, $params{destpage},
439 $calendar.=qq{\n\t\t</ul>};
440 $calendar.=qq{</div></div>};
441 $calendar.=qq{</td>\n};
444 if ($day == $today) {
445 $tag='month-calendar-day-this-day';
447 elsif ($day == $future_dom) {
448 $tag='month-calendar-day-future';
451 $tag='month-calendar-day-nolink';
453 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
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}"> </td>\n};
469 sub format_year (@) {
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;
483 $post_months[$month]++;
488 my $archivebase = 'archives';
489 $archivebase = $config{archivebase} if defined $config{archivebase};
490 $archivebase = $params{archivebase} if defined $params{archivebase};
492 my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
493 my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
495 my $thisyear = $now[5]+1900;
496 my $future_month = 0;
497 $future_month = $now[4]+1 if $params{year} == $thisyear;
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}",
505 linktext => $params{year},
506 title => $params{year});
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",
513 linktext => "\←",
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",
521 linktext => "\→",
524 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
526 # Start producing the year calendar
527 my $m=$params{months_per_row}-2;
529 <table class="year-calendar">
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>
536 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
540 for (my $month = 1; $month <= 12; $month++) {
541 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
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);
547 my $mtag=sprintf("%02d", $month);
548 if ($month == $params{month} && $thisyear == $params{year}) {
549 $tag = 'year-calendar-this-month';
551 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
552 $tag = 'year-calendar-month-link';
554 elsif ($future_month && $month >= $future_month) {
555 $tag = 'year-calendar-month-future';
558 $tag = 'year-calendar-month-nolink';
561 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
562 $post_months[$mtag]) {
563 $murl = htmllink($params{page}, $params{destpage},
564 "$archivebase/$params{year}/$mtag",
566 linktext => $monthabbr,
567 title => $monthname);
568 $calendar.=qq{\t<td class="$tag">};
570 $calendar.=qq{\t</td>\n};
573 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
575 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
576 deptype("presence"));
578 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
588 sub setnextchange ($$) {
592 if (! exists $pagestate{$page}{calendar}{nextchange} ||
593 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
594 $pagestate{$page}{calendar}{nextchange}=$timestamp;
601 my $thisyear=1900 + $now[5];
602 my $thismonth=1 + $now[4];
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};
612 if ($params{year} < 1) {
614 $params{year}=$thisyear+$params{year};
617 if ($params{month} < 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;
627 $params{month} = sprintf("%02d", $params{month});
628 $changed{$params{year}}{$params{month}} = 1;
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
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}));
646 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
648 # Calendar for current year updates 1st of next month.
649 # Any calendar relative to the current month also updates
651 if ($thismonth < 12) {
652 setnextchange($params{destpage},
653 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
656 setnextchange($params{destpage},
657 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
660 elsif ($relativeyear) {
661 # Any calendar relative to the current year updates 1st
663 setnextchange($params{destpage},
664 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
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}));
672 # calendar for past month or year, does not need
674 delete $pagestate{$params{destpage}}{calendar};
678 if ($params{type} eq 'month') {
679 $calendar=format_month(%params);
681 elsif ($params{type} eq 'year') {
682 $calendar=format_year(%params);
685 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
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
695 push @$needsbuild, $pagesources{$page};
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
702 delete $pagestate{$page}{calendar};
712 my $page=$params{page};
714 return unless $config{calendar_autocreate};
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);