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 {
174 return unless $config{calendar_autocreate};
177 my $page = calendarlink($year);
178 my $pagefile = newpagefile($page, $config{default_pageext});
180 $pagefile, "calendar",
181 sub {return autocreate($page, $pagefile, $year);}
184 if (not exists $wikistate{calendar}{minyear}) {
185 $wikistate{calendar}{minyear} = $year;
187 if (not exists $wikistate{calendar}{maxyear}) {
188 $wikistate{calendar}{maxyear} = $year;
191 if ($config{calendar_fill_gaps}) {
192 # Building month pages
193 foreach my $month (1 .. 12) {
194 gencalendarmonth($year, $month);
197 # Filling potential gaps in years (e.g. calendar goes from 2010 to 2014,
198 # and we just added year 2005. We have to had 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);
204 $wikistate{calendar}{minyear} = $year;
206 if ($wikistate{calendar}{maxyear} < $year) {
207 foreach my $other ($wikistate{calendar}{maxyear} + 1 .. $year - 1) {
208 gencalendaryear($other, norecurse => 1);
210 $wikistate{calendar}{maxyear} = $year;
213 if ($year < $wikistate{calendar}{minyear}) {
214 $wikistate{calendar}{minyear} = $year;
216 if ($year > $wikistate{calendar}{maxyear}) {
217 $wikistate{calendar}{maxyear} = $year;
221 sub previousmonth($$$) {
224 my $archivebase = shift;
228 while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
233 return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
236 return ($pyear, $pmonth, 1);
242 my $archivebase = shift;
246 while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
251 return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
254 return ($nyear, $nmonth, 1);
257 sub previousyear($$) {
259 my $archivebase = shift;
261 my $pyear = $year - 1;
262 while (not exists $pagesources{"$archivebase/$pyear"}) {
264 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
271 my $archivebase = shift;
273 my $nyear = $year + 1;
274 while (not exists $pagesources{"$archivebase/$nyear"}) {
276 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
281 sub format_month (@) {
285 foreach my $p (pagespec_match_list($params{page},
286 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
287 # add presence dependencies to update
288 # month calendar when pages are added/removed
289 deptype => deptype("presence"))) {
290 my $mtime = $IkiWiki::pagectime{$p};
291 my @date = localtime($mtime);
293 my $month = $date[4] + 1;
294 my $year = $date[5] + 1900;
295 my $mtag = sprintf("%02d", $month);
297 if (! $linkcache{"$year/$mtag/$mday"}) {
298 $linkcache{"$year/$mtag/$mday"} = [];
300 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
303 my $archivebase = 'archives';
304 $archivebase = $config{archivebase} if defined $config{archivebase};
305 $archivebase = $params{archivebase} if defined $params{archivebase};
307 my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
308 my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
311 $pmonth=sprintf("%02d", $pmonth);
312 $nmonth=sprintf("%02d", $nmonth);
316 # When did this month start?
317 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
321 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
322 $future_dom = $now[3]+1;
326 # Find out month names for this, next, and previous months
327 my $monthabbrev=strftime_utf8("%b", @monthstart);
328 my $monthname=strftime_utf8("%B", @monthstart);
329 my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
330 my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
332 # Calculate URL's for monthly archives.
333 my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
334 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
335 $url = htmllink($params{page}, $params{destpage},
336 "$archivebase/$params{year}/".$params{month},
338 linktext => "$monthabbrev $params{year}",
339 title => $monthname);
341 add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
342 deptype("presence"));
343 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
344 $purl = htmllink($params{page}, $params{destpage},
345 "$archivebase/$pyear/$pmonth",
347 linktext => "\←",
348 title => $pmonthname);
350 add_depends($params{page}, "$archivebase/$pyear/$pmonth",
351 deptype("presence"));
352 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
353 $nurl = htmllink($params{page}, $params{destpage},
354 "$archivebase/$nyear/$nmonth",
356 linktext => "\→",
357 title => $nmonthname);
359 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
360 deptype("presence"));
362 # Start producing the month calendar
364 <table class="month-calendar">
366 <th class="month-calendar-arrow">$purl</th>
367 <th class="month-calendar-head" colspan="5">$url</th>
368 <th class="month-calendar-arrow">$nurl</th>
373 # Suppose we want to start the week with day $week_start_day
374 # If $monthstart[6] == 1
375 my $week_start_day = $params{week_start_day};
377 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
380 for my $dow ($week_start_day..$week_start_day+6) {
381 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
382 my $downame = strftime_utf8("%A", @day);
383 my $dowabbr = substr($downame, 0, 1);
384 $downame{$dow % 7}=$downame;
385 $dowabbr{$dow % 7}=$dowabbr;
386 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
394 # we start with a week_start_day, and skip until we get to the first
395 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
396 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
397 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
400 # At this point, either the first is a week_start_day, in which case
401 # nothing has been printed, or else we are in the middle of a row.
402 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
403 $day++, $wday++, $wday %= 7) {
404 # At this point, on a week_start_day, we close out a row,
405 # and start a new one -- unless it is week_start_day on the
406 # first, where we do not close a row -- since none was started.
407 if ($wday == $week_start_day) {
408 $calendar.=qq{\t</tr>\n} unless $day == 1;
409 $calendar.=qq{\t<tr>\n};
413 my $key="$params{year}/$params{month}/$day";
414 if (defined $linkcache{$key}) {
415 if ($day == $today) {
416 $tag='month-calendar-day-this-day';
419 $tag='month-calendar-day-link';
421 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
422 $calendar.=qq{<div class='popup'>$day<div class='balloon'>};
423 # Several postings on this page
425 foreach my $page (@{$linkcache{$key}}) {
426 $calendar.= qq{\n\t\t\t<li>};
428 if (exists $pagestate{$page}{meta}{title}) {
429 $title = "$pagestate{$page}{meta}{title}";
432 $title = pagetitle(IkiWiki::basename($page));
434 $calendar.=htmllink($params{page}, $params{destpage},
441 $calendar.=qq{\n\t\t</ul>};
442 $calendar.=qq{</div></div>};
443 $calendar.=qq{</td>\n};
446 if ($day == $today) {
447 $tag='month-calendar-day-this-day';
449 elsif ($day == $future_dom) {
450 $tag='month-calendar-day-future';
453 $tag='month-calendar-day-nolink';
455 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
459 # finish off the week
460 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
461 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
471 sub format_year (@) {
475 foreach my $p (pagespec_match_list($params{page},
476 "creation_year($params{year}) and ($params{pages})",
477 # add presence dependencies to update
478 # year calendar's links to months when
479 # pages are added/removed
480 deptype => deptype("presence"))) {
481 my $mtime = $IkiWiki::pagectime{$p};
482 my @date = localtime($mtime);
483 my $month = $date[4] + 1;
484 my $year = $date[5] + 1900;
486 $post_months[$month]++;
491 my $archivebase = 'archives';
492 $archivebase = $config{archivebase} if defined $config{archivebase};
493 $archivebase = $params{archivebase} if defined $params{archivebase};
495 my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
496 my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
498 my $thisyear = $now[5]+1900;
499 my $future_month = 0;
500 $future_month = $now[4]+1 if $params{year} == $thisyear;
502 # calculate URL's for previous and next years
503 my ($url, $purl, $nurl)=("$params{year}",'','');
504 if (exists $pagesources{"$archivebase/$params{year}"}) {
505 $url = htmllink($params{page}, $params{destpage},
506 "$archivebase/$params{year}",
508 linktext => $params{year},
509 title => $params{year});
511 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
512 if (exists $pagesources{"$archivebase/$pyear"}) {
513 $purl = htmllink($params{page}, $params{destpage},
514 "$archivebase/$pyear",
516 linktext => "\←",
519 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
520 if (exists $pagesources{"$archivebase/$nyear"}) {
521 $nurl = htmllink($params{page}, $params{destpage},
522 "$archivebase/$nyear",
524 linktext => "\→",
527 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
529 # Start producing the year calendar
530 my $m=$params{months_per_row}-2;
532 <table class="year-calendar">
534 <th class="year-calendar-arrow">$purl</th>
535 <th class="year-calendar-head" colspan="$m">$url</th>
536 <th class="year-calendar-arrow">$nurl</th>
539 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
543 for (my $month = 1; $month <= 12; $month++) {
544 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
546 my $monthname = strftime_utf8("%B", @day);
547 my $monthabbr = strftime_utf8("%b", @day);
548 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
550 my $mtag=sprintf("%02d", $month);
551 if ($month == $params{month} && $thisyear == $params{year}) {
552 $tag = 'year-calendar-this-month';
554 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
555 $tag = 'year-calendar-month-link';
557 elsif ($future_month && $month >= $future_month) {
558 $tag = 'year-calendar-month-future';
561 $tag = 'year-calendar-month-nolink';
564 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
565 $post_months[$mtag]) {
566 $murl = htmllink($params{page}, $params{destpage},
567 "$archivebase/$params{year}/$mtag",
569 linktext => $monthabbr,
570 title => $monthname);
571 $calendar.=qq{\t<td class="$tag">};
573 $calendar.=qq{\t</td>\n};
576 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
578 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
579 deptype("presence"));
581 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
591 sub setnextchange ($$) {
595 if (! exists $pagestate{$page}{calendar}{nextchange} ||
596 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
597 $pagestate{$page}{calendar}{nextchange}=$timestamp;
604 my $thisyear=1900 + $now[5];
605 my $thismonth=1 + $now[4];
607 $params{pages} = "*" unless defined $params{pages};
608 $params{type} = "month" unless defined $params{type};
609 $params{week_start_day} = 0 unless defined $params{week_start_day};
610 $params{months_per_row} = 3 unless defined $params{months_per_row};
611 $params{year} = $thisyear unless defined $params{year};
612 $params{month} = $thismonth unless defined $params{month};
615 if ($params{year} < 1) {
617 $params{year}=$thisyear+$params{year};
620 if ($params{month} < 1) {
622 my $monthoff=$params{month};
623 $params{month}=($thismonth+$monthoff) % 12;
624 $params{month}=12 if $params{month}==0;
625 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
626 - int($monthoff / 12);
627 $params{year}-=$yearoff;
630 $params{month} = sprintf("%02d", $params{month});
631 if (not exists $changed{$params{year}}) {
632 $changed{$params{year}} = ();
634 $changed{$params{year}}{$params{month}} = 1;
636 if ($params{type} eq 'month' && $params{year} == $thisyear
637 && $params{month} == $thismonth) {
638 # calendar for current month, updates next midnight
639 setnextchange($params{destpage}, ($time
640 + (60 - $now[0]) # seconds
641 + (59 - $now[1]) * 60 # minutes
642 + (23 - $now[2]) * 60 * 60 # hours
645 elsif ($params{type} eq 'month' &&
646 (($params{year} == $thisyear && $params{month} > $thismonth) ||
647 $params{year} > $thisyear)) {
648 # calendar for upcoming month, updates 1st of that month
649 setnextchange($params{destpage},
650 timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
652 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
654 # Calendar for current year updates 1st of next month.
655 # Any calendar relative to the current month also updates
657 if ($thismonth < 12) {
658 setnextchange($params{destpage},
659 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
662 setnextchange($params{destpage},
663 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
666 elsif ($relativeyear) {
667 # Any calendar relative to the current year updates 1st
669 setnextchange($params{destpage},
670 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
672 elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
673 # calendar for upcoming year, updates 1st of that year
674 setnextchange($params{destpage},
675 timelocal(0, 0, 0, 1, 1-1, $params{year}));
678 # calendar for past month or year, does not need
680 delete $pagestate{$params{destpage}}{calendar};
684 if ($params{type} eq 'month') {
685 $calendar=format_month(%params);
687 elsif ($params{type} eq 'year') {
688 $calendar=format_year(%params);
691 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
695 my $needsbuild=shift;
696 foreach my $page (keys %pagestate) {
697 if (exists $pagestate{$page}{calendar}{nextchange}) {
698 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
699 # force a rebuild so the calendar shows
701 push @$needsbuild, $pagesources{$page};
703 if (exists $pagesources{$page} &&
704 grep { $_ eq $pagesources{$page} } @$needsbuild) {
705 # remove state, will be re-added if
706 # the calendar is still there during the
708 delete $pagestate{$page}{calendar};
718 my $page=$params{page};
720 # Check if year pages have to be generated
721 if (pagespec_match($page, $config{archive_pagespec})) {
722 my @ctime = localtime($IkiWiki::pagectime{$page});
723 gencalendaryear($ctime[5]+1900);
724 gencalendarmonth($ctime[5]+1900, $ctime[4]+1);