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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package IkiWiki::Plugin::calendar;
28 my @now=localtime($time);
31 hook(type => "getsetup", id => "calendar", call => \&getsetup);
32 hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
33 hook(type => "preprocess", id => "calendar", call => \&preprocess);
45 example => "archives",
46 description => "base of the archives hierarchy",
52 example => "page(posts/*) and !*/Discussion",
53 description => "PageSpec of pages to include in the archives; used by ikiwiki-calendar command",
54 link => 'ikiwiki/PageSpec',
60 sub is_leap_year (@) {
62 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
67 my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
68 if ($params{month} == 2 && is_leap_year(%params)) {
71 return $days_in_month;
74 sub format_month (@) {
78 foreach my $p (pagespec_match_list($params{page},
79 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
80 # add presence dependencies to update
81 # month calendar when pages are added/removed
82 deptype => deptype("presence"))) {
83 my $mtime = $IkiWiki::pagectime{$p};
84 my @date = localtime($mtime);
86 my $month = $date[4] + 1;
87 my $year = $date[5] + 1900;
88 my $mtag = sprintf("%02d", $month);
90 # Only one posting per day is being linked to.
91 $linkcache{"$year/$mtag/$mday"} = $p;
94 my $pmonth = $params{month} - 1;
95 my $nmonth = $params{month} + 1;
96 my $pyear = $params{year};
97 my $nyear = $params{year};
99 # Adjust for January and December
100 if ($params{month} == 1) {
104 if ($params{month} == 12) {
110 $pmonth=sprintf("%02d", $pmonth);
111 $nmonth=sprintf("%02d", $nmonth);
115 # When did this month start?
116 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
120 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
121 $future_dom = $now[3]+1;
125 # Find out month names for this, next, and previous months
126 my $monthabbrev=POSIX::strftime("%b", @monthstart);
127 my $monthname=POSIX::strftime("%B", @monthstart);
128 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
129 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
131 my $archivebase = 'archives';
132 $archivebase = $config{archivebase} if defined $config{archivebase};
133 $archivebase = $params{archivebase} if defined $params{archivebase};
135 # Calculate URL's for monthly archives.
136 my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
137 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
138 $url = htmllink($params{page}, $params{destpage},
139 "$archivebase/$params{year}/".$params{month},
141 linktext => "$monthabbrev $params{year}",
142 title => $monthname);
144 add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
145 deptype("presence"));
146 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
147 $purl = htmllink($params{page}, $params{destpage},
148 "$archivebase/$pyear/$pmonth",
150 linktext => "\←",
151 title => $pmonthname);
153 add_depends($params{page}, "$archivebase/$pyear/$pmonth",
154 deptype("presence"));
155 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
156 $nurl = htmllink($params{page}, $params{destpage},
157 "$archivebase/$nyear/$nmonth",
159 linktext => "\→",
160 title => $nmonthname);
162 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
163 deptype("presence"));
165 # Start producing the month calendar
167 <table class="month-calendar">
169 <th class="month-calendar-arrow">$purl</th>
170 <th class="month-calendar-head" colspan="5">$url</th>
171 <th class="month-calendar-arrow">$nurl</th>
176 # Suppose we want to start the week with day $week_start_day
177 # If $monthstart[6] == 1
178 my $week_start_day = $params{week_start_day};
180 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
183 for my $dow ($week_start_day..$week_start_day+6) {
184 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
185 my $downame = POSIX::strftime("%A", @day);
186 my $dowabbr = substr($downame, 0, 1);
187 $downame{$dow % 7}=$downame;
188 $dowabbr{$dow % 7}=$dowabbr;
189 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
197 # we start with a week_start_day, and skip until we get to the first
198 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
199 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
200 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
203 # At this point, either the first is a week_start_day, in which case
204 # nothing has been printed, or else we are in the middle of a row.
205 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
206 $day++, $wday++, $wday %= 7) {
207 # At this point, on a week_start_day, we close out a row,
208 # and start a new one -- unless it is week_start_day on the
209 # first, where we do not close a row -- since none was started.
210 if ($wday == $week_start_day) {
211 $calendar.=qq{\t</tr>\n} unless $day == 1;
212 $calendar.=qq{\t<tr>\n};
216 my $key="$params{year}/$params{month}/$day";
217 if (defined $linkcache{$key}) {
218 if ($day == $today) {
219 $tag='month-calendar-day-this-day';
222 $tag='month-calendar-day-link';
224 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
225 $calendar.=htmllink($params{page}, $params{destpage},
229 title => pagetitle(IkiWiki::basename($linkcache{$key})));
230 $calendar.=qq{</td>\n};
233 if ($day == $today) {
234 $tag='month-calendar-day-this-day';
236 elsif ($day == $future_dom) {
237 $tag='month-calendar-day-future';
240 $tag='month-calendar-day-nolink';
242 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
246 # finish off the week
247 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
248 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
258 sub format_year (@) {
262 foreach my $p (pagespec_match_list($params{page},
263 "creation_year($params{year}) and ($params{pages})",
264 # add presence dependencies to update
265 # year calendar's links to months when
266 # pages are added/removed
267 deptype => deptype("presence"))) {
268 my $mtime = $IkiWiki::pagectime{$p};
269 my @date = localtime($mtime);
270 my $month = $date[4] + 1;
272 $post_months[$month]++;
277 my $pyear = $params{year} - 1;
278 my $nyear = $params{year} + 1;
280 my $thisyear = $now[5]+1900;
281 my $future_month = 0;
282 $future_month = $now[4]+1 if $params{year} == $thisyear;
284 my $archivebase = 'archives';
285 $archivebase = $config{archivebase} if defined $config{archivebase};
286 $archivebase = $params{archivebase} if defined $params{archivebase};
288 # calculate URL's for previous and next years
289 my ($url, $purl, $nurl)=("$params{year}",'','');
290 if (exists $pagesources{"$archivebase/$params{year}"}) {
291 $url = htmllink($params{page}, $params{destpage},
292 "$archivebase/$params{year}",
294 linktext => $params{year},
295 title => $params{year});
297 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
298 if (exists $pagesources{"$archivebase/$pyear"}) {
299 $purl = htmllink($params{page}, $params{destpage},
300 "$archivebase/$pyear",
302 linktext => "\←",
305 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
306 if (exists $pagesources{"$archivebase/$nyear"}) {
307 $nurl = htmllink($params{page}, $params{destpage},
308 "$archivebase/$nyear",
310 linktext => "\→",
313 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
315 # Start producing the year calendar
316 my $m=$params{months_per_row}-2;
318 <table class="year-calendar">
320 <th class="year-calendar-arrow">$purl</th>
321 <th class="year-calendar-head" colspan="$m">$url</th>
322 <th class="year-calendar-arrow">$nurl</th>
325 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
329 for (my $month = 1; $month <= 12; $month++) {
330 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
332 my $monthname = POSIX::strftime("%B", @day);
333 my $monthabbr = POSIX::strftime("%b", @day);
334 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
336 my $mtag=sprintf("%02d", $month);
337 if ($month == $params{month} && $thisyear == $params{year}) {
338 $tag = 'year-calendar-this-month';
340 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
341 $tag = 'year-calendar-month-link';
343 elsif ($future_month && $month >= $future_month) {
344 $tag = 'year-calendar-month-future';
347 $tag = 'year-calendar-month-nolink';
350 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
351 $post_months[$mtag]) {
352 $murl = htmllink($params{page}, $params{destpage},
353 "$archivebase/$params{year}/$mtag",
355 linktext => $monthabbr,
356 title => $monthname);
357 $calendar.=qq{\t<td class="$tag">};
359 $calendar.=qq{\t</td>\n};
362 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
364 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
365 deptype("presence"));
367 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
377 sub setnextchange ($$) {
381 if (! exists $pagestate{$page}{calendar}{nextchange} ||
382 $pagestate{$page}{calendar}{nextchange} > $timestamp) {
383 $pagestate{$page}{calendar}{nextchange}=$timestamp;
390 my $thisyear=1900 + $now[5];
391 my $thismonth=1 + $now[4];
393 $params{pages} = "*" unless defined $params{pages};
394 $params{type} = "month" unless defined $params{type};
395 $params{week_start_day} = 0 unless defined $params{week_start_day};
396 $params{months_per_row} = 3 unless defined $params{months_per_row};
397 $params{year} = $thisyear unless defined $params{year};
398 $params{month} = $thismonth unless defined $params{month};
401 if ($params{year} < 1) {
403 $params{year}=$thisyear+$params{year};
406 if ($params{month} < 1) {
408 my $monthoff=$params{month};
409 $params{month}=($thismonth+$monthoff) % 12;
410 $params{month}=12 if $params{month}==0;
411 my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
412 - int($monthoff / 12);
413 $params{year}-=$yearoff;
416 $params{month} = sprintf("%02d", $params{month});
418 if ($params{type} eq 'month' && $params{year} == $thisyear
419 && $params{month} == $thismonth) {
420 # calendar for current month, updates next midnight
421 setnextchange($params{destpage}, ($time
422 + (60 - $now[0]) # seconds
423 + (59 - $now[1]) * 60 # minutes
424 + (23 - $now[2]) * 60 * 60 # hours
427 elsif ($params{type} eq 'month' &&
428 (($params{year} == $thisyear && $params{month} > $thismonth) ||
429 $params{year} > $thisyear)) {
430 # calendar for upcoming month, updates 1st of that month
431 setnextchange($params{destpage},
432 timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
434 elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
436 # Calendar for current year updates 1st of next month.
437 # Any calendar relative to the current month also updates
439 if ($thismonth < 12) {
440 setnextchange($params{destpage},
441 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
444 setnextchange($params{destpage},
445 timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
448 elsif ($relativeyear) {
449 # Any calendar relative to the current year updates 1st
451 setnextchange($params{destpage},
452 timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
454 elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
455 # calendar for upcoming year, updates 1st of that year
456 setnextchange($params{destpage},
457 timelocal(0, 0, 0, 1, 1-1, $params{year}));
460 # calendar for past month or year, does not need
462 delete $pagestate{$params{destpage}}{calendar};
466 if ($params{type} eq 'month') {
467 $calendar=format_month(%params);
469 elsif ($params{type} eq 'year') {
470 $calendar=format_year(%params);
473 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
477 my $needsbuild=shift;
478 foreach my $page (keys %pagestate) {
479 if (exists $pagestate{$page}{calendar}{nextchange}) {
480 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
481 # force a rebuild so the calendar shows
483 push @$needsbuild, $pagesources{$page};
485 if (exists $pagesources{$page} &&
486 grep { $_ eq $pagesources{$page} } @$needsbuild) {
487 # remove state, will be re-added if
488 # the calendar is still there during the
490 delete $pagestate{$page}{calendar};