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);
30 hook(type => "getsetup", id => "calendar", call => \&getsetup);
31 hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
32 hook(type => "preprocess", id => "calendar", call => \&preprocess);
44 example => "archives",
45 description => "base of the archives hierarchy",
51 example => "page(posts/*) and !*/Discussion",
52 description => "PageSpec of pages to include in the archives; used by ikiwiki-calendar command",
53 link => 'ikiwiki/PageSpec',
59 sub is_leap_year (@) {
61 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
66 my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
67 if ($params{month} == 2 && is_leap_year(%params)) {
70 return $days_in_month;
73 sub format_month (@) {
77 foreach my $p (pagespec_match_list($params{page},
78 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
79 # add presence dependencies to update
80 # month calendar when pages are added/removed
81 deptype => deptype("presence"))) {
82 my $mtime = $IkiWiki::pagectime{$p};
83 my @date = localtime($mtime);
85 my $month = $date[4] + 1;
86 my $year = $date[5] + 1900;
87 my $mtag = sprintf("%02d", $month);
89 if (! $linkcache{"$year/$mtag/$mday"}) {
90 $linkcache{"$year/$mtag/$mday"} = [];
92 push(@{$linkcache{"$year/$mtag/$mday"}}, $p);
95 my $pmonth = $params{month} - 1;
96 my $nmonth = $params{month} + 1;
97 my $pyear = $params{year};
98 my $nyear = $params{year};
100 # Adjust for January and December
101 if ($params{month} == 1) {
105 if ($params{month} == 12) {
111 $pmonth=sprintf("%02d", $pmonth);
112 $nmonth=sprintf("%02d", $nmonth);
116 # When did this month start?
117 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
121 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
122 $future_dom = $now[3]+1;
126 # Find out month names for this, next, and previous months
127 my $monthabbrev=strftime_utf8("%b", @monthstart);
128 my $monthname=strftime_utf8("%B", @monthstart);
129 my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
130 my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
132 my $archivebase = 'archives';
133 $archivebase = $config{archivebase} if defined $config{archivebase};
134 $archivebase = $params{archivebase} if defined $params{archivebase};
136 # Calculate URL's for monthly archives.
137 my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
138 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
139 $url = htmllink($params{page}, $params{destpage},
140 "$archivebase/$params{year}/".$params{month},
142 linktext => "$monthabbrev $params{year}",
143 title => $monthname);
145 add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
146 deptype("presence"));
147 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
148 $purl = htmllink($params{page}, $params{destpage},
149 "$archivebase/$pyear/$pmonth",
151 linktext => "\←",
152 title => $pmonthname);
154 add_depends($params{page}, "$archivebase/$pyear/$pmonth",
155 deptype("presence"));
156 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
157 $nurl = htmllink($params{page}, $params{destpage},
158 "$archivebase/$nyear/$nmonth",
160 linktext => "\→",
161 title => $nmonthname);
163 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
164 deptype("presence"));
166 # Start producing the month calendar
168 <table class="month-calendar">
170 <th class="month-calendar-arrow">$purl</th>
171 <th class="month-calendar-head" colspan="5">$url</th>
172 <th class="month-calendar-arrow">$nurl</th>
177 # Suppose we want to start the week with day $week_start_day
178 # If $monthstart[6] == 1
179 my $week_start_day = $params{week_start_day};
181 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
184 for my $dow ($week_start_day..$week_start_day+6) {
185 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
186 my $downame = strftime_utf8("%A", @day);
187 my $dowabbr = substr($downame, 0, 1);
188 $downame{$dow % 7}=$downame;
189 $dowabbr{$dow % 7}=$dowabbr;
190 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
198 # we start with a week_start_day, and skip until we get to the first
199 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
200 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
201 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
204 # At this point, either the first is a week_start_day, in which case
205 # nothing has been printed, or else we are in the middle of a row.
206 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
207 $day++, $wday++, $wday %= 7) {
208 # At this point, on a week_start_day, we close out a row,
209 # and start a new one -- unless it is week_start_day on the
210 # first, where we do not close a row -- since none was started.
211 if ($wday == $week_start_day) {
212 $calendar.=qq{\t</tr>\n} unless $day == 1;
213 $calendar.=qq{\t<tr>\n};
217 my $key="$params{year}/$params{month}/$day";
218 if (defined $linkcache{$key}) {
219 if ($day == $today) {
220 $tag='month-calendar-day-this-day';
223 $tag='month-calendar-day-link';
225 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
226 if (scalar(@{$linkcache{$key}}) == 1) {
227 # Only one posting on this page
228 my $page = $linkcache{$key}[0];
229 $calendar.=htmllink($params{page}, $params{destpage},
233 title => pagetitle(IkiWiki::basename($page)));
236 $calendar.=qq{<div class='popup'>$day<div class='balloon'>};
237 # Several postings on this page
239 foreach my $page (@{$linkcache{$key}}) {
240 $calendar.= qq{\n\t\t\t<li>};
242 if (exists $pagestate{$page}{meta}{title}) {
243 $title = "$pagestate{$page}{meta}{title}";
246 $title = pagetitle(IkiWiki::basename($page));
248 $calendar.=htmllink($params{page}, $params{destpage},
255 $calendar.=qq{\n\t\t</ul>};
256 $calendar.=qq{</div></div>};
258 $calendar.=qq{</td>\n};
261 if ($day == $today) {
262 $tag='month-calendar-day-this-day';
264 elsif ($day == $future_dom) {
265 $tag='month-calendar-day-future';
268 $tag='month-calendar-day-nolink';
270 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
274 # finish off the week
275 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
276 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
286 sub format_year (@) {
290 foreach my $p (pagespec_match_list($params{page},
291 "creation_year($params{year}) and ($params{pages})",
292 # add presence dependencies to update
293 # year calendar's links to months when
294 # pages are added/removed
295 deptype => deptype("presence"))) {
296 my $mtime = $IkiWiki::pagectime{$p};
297 my @date = localtime($mtime);
298 my $month = $date[4] + 1;
300 $post_months[$month]++;
305 my $pyear = $params{year} - 1;
306 my $nyear = $params{year} + 1;
308 my $thisyear = $now[5]+1900;
309 my $future_month = 0;
310 $future_month = $now[4]+1 if $params{year} == $thisyear;
312 my $archivebase = 'archives';
313 $archivebase = $config{archivebase} if defined $config{archivebase};
314 $archivebase = $params{archivebase} if defined $params{archivebase};
316 # calculate URL's for previous and next years
317 my ($url, $purl, $nurl)=("$params{year}",'','');
318 if (exists $pagesources{"$archivebase/$params{year}"}) {
319 $url = htmllink($params{page}, $params{destpage},
320 "$archivebase/$params{year}",
322 linktext => $params{year},
323 title => $params{year});
325 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
326 if (exists $pagesources{"$archivebase/$pyear"}) {
327 $purl = htmllink($params{page}, $params{destpage},
328 "$archivebase/$pyear",
330 linktext => "\←",
333 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
334 if (exists $pagesources{"$archivebase/$nyear"}) {
335 $nurl = htmllink($params{page}, $params{destpage},
336 "$archivebase/$nyear",
338 linktext => "\→",
341 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
343 # Start producing the year calendar
344 my $m=$params{months_per_row}-2;
346 <table class="year-calendar">
348 <th class="year-calendar-arrow">$purl</th>
349 <th class="year-calendar-head" colspan="$m">$url</th>
350 <th class="year-calendar-arrow">$nurl</th>
353 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
357 for (my $month = 1; $month <= 12; $month++) {
358 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
360 my $monthname = strftime_utf8("%B", @day);
361 my $monthabbr = strftime_utf8("%b", @day);
362 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
364 my $mtag=sprintf("%02d", $month);
365 if ($month == $params{month} && $thisyear == $params{year}) {
366 $tag = 'year-calendar-this-month';
368 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
369 $tag = 'year-calendar-month-link';
371 elsif ($future_month && $month >= $future_month) {
372 $tag = 'year-calendar-month-future';
375 $tag = 'year-calendar-month-nolink';
378 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
379 $post_months[$mtag]) {
380 $murl = htmllink($params{page}, $params{destpage},
381 "$archivebase/$params{year}/$mtag",
383 linktext => $monthabbr,
384 title => $monthname);
385 $calendar.=qq{\t<td class="$tag">};
387 $calendar.=qq{\t</td>\n};
390 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
392 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
393 deptype("presence"));
395 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
405 sub setnextchange ($$) {
409 if (! exists $pagestate{$page}{calendar}{nextchange} ||
410 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
411 $pagestate{$page}{calendar}{nextchange}=$timestamp;
418 my $thisyear=1900 + $now[5];
419 my $thismonth=1 + $now[4];
421 $params{pages} = "*" unless defined $params{pages};
422 $params{type} = "month" unless defined $params{type};
423 $params{week_start_day} = 0 unless defined $params{week_start_day};
424 $params{months_per_row} = 3 unless defined $params{months_per_row};
425 $params{year} = $thisyear unless defined $params{year};
426 $params{month} = $thismonth unless defined $params{month};
429 if ($params{year} < 1) {
431 $params{year}=$thisyear+$params{year};
434 if ($params{month} < 1) {
436 my $monthoff=$params{month};
437 $params{month}=($thismonth+$monthoff) % 12;
438 $params{month}=12 if $params{month}==0;
439 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
440 - int($monthoff / 12);
441 $params{year}-=$yearoff;
444 $params{month} = sprintf("%02d", $params{month});
446 if ($params{type} eq 'month' && $params{year} == $thisyear
447 && $params{month} == $thismonth) {
448 # calendar for current month, updates next midnight
449 setnextchange($params{destpage}, ($time
450 + (60 - $now[0]) # seconds
451 + (59 - $now[1]) * 60 # minutes
452 + (23 - $now[2]) * 60 * 60 # hours
455 elsif ($params{type} eq 'month' &&
456 (($params{year} == $thisyear && $params{month} > $thismonth) ||
457 $params{year} > $thisyear)) {
458 # calendar for upcoming month, updates 1st of that month
459 setnextchange($params{destpage},
460 timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
462 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
464 # Calendar for current year updates 1st of next month.
465 # Any calendar relative to the current month also updates
467 if ($thismonth < 12) {
468 setnextchange($params{destpage},
469 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
472 setnextchange($params{destpage},
473 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
476 elsif ($relativeyear) {
477 # Any calendar relative to the current year updates 1st
479 setnextchange($params{destpage},
480 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
482 elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
483 # calendar for upcoming year, updates 1st of that year
484 setnextchange($params{destpage},
485 timelocal(0, 0, 0, 1, 1-1, $params{year}));
488 # calendar for past month or year, does not need
490 delete $pagestate{$params{destpage}}{calendar};
494 if ($params{type} eq 'month') {
495 $calendar=format_month(%params);
497 elsif ($params{type} eq 'year') {
498 $calendar=format_year(%params);
501 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
505 my $needsbuild=shift;
506 foreach my $page (keys %pagestate) {
507 if (exists $pagestate{$page}{calendar}{nextchange}) {
508 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
509 # force a rebuild so the calendar shows
511 push @$needsbuild, $pagesources{$page};
513 if (exists $pagesources{$page} &&
514 grep { $_ eq $pagesources{$page} } @$needsbuild) {
515 # remove state, will be re-added if
516 # the calendar is still there during the
518 delete $pagestate{$page}{calendar};