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});
145 my $dir = $IkiWiki::Plugin::transient::transientdir;
148 writefile($pagefile, $dir, $template->output);
151 sub calendarlink($;$) {
152 my ($year, $month) = @_;
153 if (defined $month) {
154 return $config{archivebase} . "/" . $year . "/" . $month;
156 return $config{archivebase} . "/" . $year;
160 sub gencalendarmonth{
162 my $month = sprintf("%02d", shift);
164 my $page = calendarlink($year, $month);
165 my $pagefile = newpagefile($page, $config{default_pageext});
167 $pagefile, "calendar",
168 sub {return autocreate($page, $pagefile, $year, $month);}
172 sub gencalendaryear {
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 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);
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;
226 if (not exists $wikistate{calendar}{minyear}) {
227 $wikistate{calendar}{minyear} = $year;
232 while ((not exists $pagesources{"$archivebase/$pyear/" . sprintf("%02d", $pmonth)}) or ($pmonth == $month and $pyear == $year)) {
237 return ($pyear, $pmonth, 0) unless $pyear >= $wikistate{calendar}{minyear};
240 return ($pyear, $pmonth, 1);
246 my $archivebase = shift;
248 if (not exists $wikistate{calendar}{maxyear}) {
249 $wikistate{calendar}{maxyear} = $year;
254 while ((not exists $pagesources{"$archivebase/$nyear/" . sprintf("%02d", $nmonth)}) or ($nmonth == $month and $nyear == $year)) {
259 return ($nyear, $nmonth, 0) unless $nyear <= $wikistate{calendar}{maxyear};
262 return ($nyear, $nmonth, 1);
265 sub previousyear($$) {
267 my $archivebase = shift;
269 my $pyear = $year - 1;
270 while (not exists $pagesources{"$archivebase/$pyear"}) {
272 return ($pyear, 0) unless ($pyear >= $wikistate{calendar}{minyear});
279 my $archivebase = shift;
281 my $nyear = $year + 1;
282 while (not exists $pagesources{"$archivebase/$nyear"}) {
284 return ($nyear, 0) unless ($nyear <= $wikistate{calendar}{maxyear});
289 sub format_month (@) {
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);
301 my $month = $date[4] + 1;
302 my $year = $date[5] + 1900;
303 my $mtag = sprintf("%02d", $month);
305 if (! $linkcache{"$year/$mtag/$mday"}) {
306 $linkcache{"$year/$mtag/$mday"} = [];
308 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
311 my $archivebase = 'archives';
312 $archivebase = $config{archivebase} if defined $config{archivebase};
313 $archivebase = $params{archivebase} if defined $params{archivebase};
315 my ($pyear, $pmonth, $pvalid) = previousmonth($params{year}, $params{month}, $archivebase);
316 my ($nyear, $nmonth, $nvalid) = nextmonth($params{year}, $params{month}, $archivebase);
319 $pmonth=sprintf("%02d", $pmonth);
320 $nmonth=sprintf("%02d", $nmonth);
324 # When did this month start?
325 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
329 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
330 $future_dom = $now[3]+1;
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)));
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},
346 linktext => "$monthabbrev $params{year}",
347 title => $monthname);
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",
355 linktext => "\←",
356 title => $pmonthname);
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",
364 linktext => "\→",
365 title => $nmonthname);
367 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
368 deptype("presence"));
370 # Start producing the month calendar
372 <table class="month-calendar">
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>
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};
385 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
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};
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}"> </td>\n};
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};
421 my $key="$params{year}/$params{month}/$day";
422 if (defined $linkcache{$key}) {
423 if ($day == $today) {
424 $tag='month-calendar-day-this-day';
427 $tag='month-calendar-day-link';
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
433 foreach my $page (@{$linkcache{$key}}) {
434 $calendar.= qq{\n\t\t\t<li>};
436 if (exists $pagestate{$page}{meta}{title}) {
437 $title = "$pagestate{$page}{meta}{title}";
440 $title = pagetitle(IkiWiki::basename($page));
442 $calendar.=htmllink($params{page}, $params{destpage},
449 $calendar.=qq{\n\t\t</ul>};
450 $calendar.=qq{</div></div>};
451 $calendar.=qq{</td>\n};
454 if ($day == $today) {
455 $tag='month-calendar-day-this-day';
457 elsif ($day == $future_dom) {
458 $tag='month-calendar-day-future';
461 $tag='month-calendar-day-nolink';
463 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
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}"> </td>\n};
479 sub format_year (@) {
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;
493 $post_months[$month]++;
498 my $archivebase = 'archives';
499 $archivebase = $config{archivebase} if defined $config{archivebase};
500 $archivebase = $params{archivebase} if defined $params{archivebase};
502 my ($pyear, $pvalid) = previousyear($params{year}, $archivebase);
503 my ($nyear, $nvalid) = nextyear($params{year}, $archivebase);
505 my $thisyear = $now[5]+1900;
506 my $future_month = 0;
507 $future_month = $now[4]+1 if $params{year} == $thisyear;
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}",
515 linktext => $params{year},
516 title => $params{year});
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",
523 linktext => "\←",
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",
531 linktext => "\→",
534 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
536 # Start producing the year calendar
537 my $m=$params{months_per_row}-2;
539 <table class="year-calendar">
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>
546 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
550 for (my $month = 1; $month <= 12; $month++) {
551 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
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);
557 my $mtag=sprintf("%02d", $month);
558 if ($month == $params{month} && $thisyear == $params{year}) {
559 $tag = 'year-calendar-this-month';
561 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
562 $tag = 'year-calendar-month-link';
564 elsif ($future_month && $month >= $future_month) {
565 $tag = 'year-calendar-month-future';
568 $tag = 'year-calendar-month-nolink';
571 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
572 $post_months[$mtag]) {
573 $murl = htmllink($params{page}, $params{destpage},
574 "$archivebase/$params{year}/$mtag",
576 linktext => $monthabbr,
577 title => $monthname);
578 $calendar.=qq{\t<td class="$tag">};
580 $calendar.=qq{\t</td>\n};
583 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
585 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
586 deptype("presence"));
588 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
598 sub setnextchange ($$) {
602 if (! exists $pagestate{$page}{calendar}{nextchange} ||
603 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
604 $pagestate{$page}{calendar}{nextchange}=$timestamp;
611 my $thisyear=1900 + $now[5];
612 my $thismonth=1 + $now[4];
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};
622 if ($params{year} < 1) {
624 $params{year}=$thisyear+$params{year};
627 if ($params{month} < 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;
637 $params{month} = sprintf("%02d", $params{month});
638 $changed{$params{year}}{$params{month}} = 1;
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
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}));
656 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
658 # Calendar for current year updates 1st of next month.
659 # Any calendar relative to the current month also updates
661 if ($thismonth < 12) {
662 setnextchange($params{destpage},
663 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
666 setnextchange($params{destpage},
667 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
670 elsif ($relativeyear) {
671 # Any calendar relative to the current year updates 1st
673 setnextchange($params{destpage},
674 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
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}));
682 # calendar for past month or year, does not need
684 delete $pagestate{$params{destpage}}{calendar};
688 if ($params{type} eq 'month') {
689 $calendar=format_month(%params);
691 elsif ($params{type} eq 'year') {
692 $calendar=format_year(%params);
695 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
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
705 push @$needsbuild, $pagesources{$page};
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
712 delete $pagestate{$page}{calendar};
722 my $page=$params{page};
724 return unless $config{calendar_autocreate};
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);