summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: 359c9b86141541549a1f42a32fa0ef869ec08987 (plain)
  1. #! /usr/bin/perl
  2. # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
  3. #
  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.
  8. #
  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.
  13. #
  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
  17. require 5.002;
  18. package IkiWiki::Plugin::calendar;
  19. use warnings;
  20. use strict;
  21. use IkiWiki 3.00;
  22. use Time::Local;
  23. use POSIX ();
  24. my $time=time;
  25. my @now=localtime($time);
  26. sub import {
  27. hook(type => "getsetup", id => "calendar", call => \&getsetup);
  28. hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
  29. hook(type => "preprocess", id => "calendar", call => \&preprocess);
  30. }
  31. sub getsetup () {
  32. return
  33. plugin => {
  34. safe => 1,
  35. rebuild => undef,
  36. section => "widget",
  37. },
  38. archivebase => {
  39. type => "string",
  40. example => "archives",
  41. description => "base of the archives hierarchy",
  42. safe => 1,
  43. rebuild => 1,
  44. },
  45. archive_pagespec => {
  46. type => "pagespec",
  47. example => "posts/* and !*/Discussion",
  48. description => "PageSpec of pages to include in the archives; used by ikiwiki-calendar command",
  49. link => 'ikiwiki/PageSpec',
  50. safe => 1,
  51. rebuild => 0,
  52. },
  53. }
  54. sub is_leap_year (@) {
  55. my %params=@_;
  56. return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
  57. }
  58. sub month_days {
  59. my %params=@_;
  60. my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
  61. if ($params{month} == 2 && is_leap_year(%params)) {
  62. $days_in_month++;
  63. }
  64. return $days_in_month;
  65. }
  66. sub format_month (@) {
  67. my %params=@_;
  68. my %linkcache;
  69. foreach my $p (pagespec_match_list($params{page},
  70. "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
  71. # add presence dependencies to update
  72. # month calendar when pages are added/removed
  73. deptype => deptype("presence"))) {
  74. my $mtime = $IkiWiki::pagectime{$p};
  75. my @date = localtime($mtime);
  76. my $mday = $date[3];
  77. my $month = $date[4] + 1;
  78. my $year = $date[5] + 1900;
  79. my $mtag = sprintf("%02d", $month);
  80. # Only one posting per day is being linked to.
  81. $linkcache{"$year/$mtag/$mday"} = $p;
  82. }
  83. my $pmonth = $params{month} - 1;
  84. my $nmonth = $params{month} + 1;
  85. my $pyear = $params{year};
  86. my $nyear = $params{year};
  87. # Adjust for January and December
  88. if ($params{month} == 1) {
  89. $pmonth = 12;
  90. $pyear--;
  91. }
  92. if ($params{month} == 12) {
  93. $nmonth = 1;
  94. $nyear++;
  95. }
  96. # Add padding.
  97. $pmonth=sprintf("%02d", $pmonth);
  98. $nmonth=sprintf("%02d", $nmonth);
  99. my $calendar="\n";
  100. # When did this month start?
  101. my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
  102. my $future_dom = 0;
  103. my $today = 0;
  104. if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
  105. $future_dom = $now[3]+1;
  106. $today = $now[3];
  107. }
  108. # Find out month names for this, next, and previous months
  109. my $monthabbrev=POSIX::strftime("%b", @monthstart);
  110. my $monthname=POSIX::strftime("%B", @monthstart);
  111. my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
  112. my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
  113. my $archivebase = 'archives';
  114. $archivebase = $config{archivebase} if defined $config{archivebase};
  115. $archivebase = $params{archivebase} if defined $params{archivebase};
  116. # Calculate URL's for monthly archives.
  117. my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
  118. if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
  119. $url = htmllink($params{page}, $params{destpage},
  120. "$archivebase/$params{year}/".$params{month},
  121. noimageinline => 1,
  122. linktext => "$monthabbrev $params{year}",
  123. title => $monthname);
  124. }
  125. add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
  126. deptype("presence"));
  127. if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
  128. $purl = htmllink($params{page}, $params{destpage},
  129. "$archivebase/$pyear/$pmonth",
  130. noimageinline => 1,
  131. linktext => "\&larr;",
  132. title => $pmonthname);
  133. }
  134. add_depends($params{page}, "$archivebase/$pyear/$pmonth",
  135. deptype("presence"));
  136. if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
  137. $nurl = htmllink($params{page}, $params{destpage},
  138. "$archivebase/$nyear/$nmonth",
  139. noimageinline => 1,
  140. linktext => "\&rarr;",
  141. title => $nmonthname);
  142. }
  143. add_depends($params{page}, "$archivebase/$nyear/$nmonth",
  144. deptype("presence"));
  145. # Start producing the month calendar
  146. $calendar=<<EOF;
  147. <table class="month-calendar">
  148. <tr>
  149. <th class="month-calendar-arrow">$purl</th>
  150. <th class="month-calendar-head" colspan="5">$url</th>
  151. <th class="month-calendar-arrow">$nurl</th>
  152. </tr>
  153. <tr>
  154. EOF
  155. # Suppose we want to start the week with day $week_start_day
  156. # If $monthstart[6] == 1
  157. my $week_start_day = $params{week_start_day};
  158. my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
  159. my %downame;
  160. my %dowabbr;
  161. for my $dow ($week_start_day..$week_start_day+6) {
  162. my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
  163. my $downame = POSIX::strftime("%A", @day);
  164. my $dowabbr = substr($downame, 0, 1);
  165. $downame{$dow % 7}=$downame;
  166. $dowabbr{$dow % 7}=$dowabbr;
  167. $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
  168. }
  169. $calendar.=<<EOF;
  170. </tr>
  171. EOF
  172. my $wday;
  173. # we start with a week_start_day, and skip until we get to the first
  174. for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
  175. $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
  176. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  177. }
  178. # At this point, either the first is a week_start_day, in which case
  179. # nothing has been printed, or else we are in the middle of a row.
  180. for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
  181. $day++, $wday++, $wday %= 7) {
  182. # At this point, on a week_start_day, we close out a row,
  183. # and start a new one -- unless it is week_start_day on the
  184. # first, where we do not close a row -- since none was started.
  185. if ($wday == $week_start_day) {
  186. $calendar.=qq{\t</tr>\n} unless $day == 1;
  187. $calendar.=qq{\t<tr>\n};
  188. }
  189. my $tag;
  190. my $key="$params{year}/$params{month}/$day";
  191. if (defined $linkcache{$key}) {
  192. if ($day == $today) {
  193. $tag='month-calendar-day-this-day';
  194. }
  195. else {
  196. $tag='month-calendar-day-link';
  197. }
  198. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
  199. $calendar.=htmllink($params{page}, $params{destpage},
  200. $linkcache{$key},
  201. noimageinline => 1,
  202. linktext => $day,
  203. title => pagetitle(IkiWiki::basename($linkcache{$key})));
  204. $calendar.=qq{</td>\n};
  205. }
  206. else {
  207. if ($day == $today) {
  208. $tag='month-calendar-day-this-day';
  209. }
  210. elsif ($day == $future_dom) {
  211. $tag='month-calendar-day-future';
  212. }
  213. else {
  214. $tag='month-calendar-day-nolink';
  215. }
  216. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
  217. }
  218. }
  219. # finish off the week
  220. for (; $wday != $week_start_day; $wday++, $wday %= 7) {
  221. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  222. }
  223. $calendar.=<<EOF;
  224. </tr>
  225. </table>
  226. EOF
  227. return $calendar;
  228. }
  229. sub format_year (@) {
  230. my %params=@_;
  231. my @post_months;
  232. foreach my $p (pagespec_match_list($params{page},
  233. "creation_year($params{year}) and ($params{pages})",
  234. # add presence dependencies to update
  235. # year calendar's links to months when
  236. # pages are added/removed
  237. deptype => deptype("presence"))) {
  238. my $mtime = $IkiWiki::pagectime{$p};
  239. my @date = localtime($mtime);
  240. my $month = $date[4] + 1;
  241. $post_months[$month]++;
  242. }
  243. my $calendar="\n";
  244. my $pyear = $params{year} - 1;
  245. my $nyear = $params{year} + 1;
  246. my $thisyear = $now[5]+1900;
  247. my $future_month = 0;
  248. $future_month = $now[4]+1 if $params{year} == $thisyear;
  249. my $archivebase = 'archives';
  250. $archivebase = $config{archivebase} if defined $config{archivebase};
  251. $archivebase = $params{archivebase} if defined $params{archivebase};
  252. # calculate URL's for previous and next years
  253. my ($url, $purl, $nurl)=("$params{year}",'','');
  254. if (exists $pagesources{"$archivebase/$params{year}"}) {
  255. $url = htmllink($params{page}, $params{destpage},
  256. "$archivebase/$params{year}",
  257. noimageinline => 1,
  258. linktext => $params{year},
  259. title => $params{year});
  260. }
  261. add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
  262. if (exists $pagesources{"$archivebase/$pyear"}) {
  263. $purl = htmllink($params{page}, $params{destpage},
  264. "$archivebase/$pyear",
  265. noimageinline => 1,
  266. linktext => "\&larr;",
  267. title => $pyear);
  268. }
  269. add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
  270. if (exists $pagesources{"$archivebase/$nyear"}) {
  271. $nurl = htmllink($params{page}, $params{destpage},
  272. "$archivebase/$nyear",
  273. noimageinline => 1,
  274. linktext => "\&rarr;",
  275. title => $nyear);
  276. }
  277. add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
  278. # Start producing the year calendar
  279. my $m=$params{months_per_row}-2;
  280. $calendar=<<EOF;
  281. <table class="year-calendar">
  282. <tr>
  283. <th class="year-calendar-arrow">$purl</th>
  284. <th class="year-calendar-head" colspan="$m">$url</th>
  285. <th class="year-calendar-arrow">$nurl</th>
  286. </tr>
  287. <tr>
  288. <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
  289. </tr>
  290. EOF
  291. for (my $month = 1; $month <= 12; $month++) {
  292. my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
  293. my $murl;
  294. my $monthname = POSIX::strftime("%B", @day);
  295. my $monthabbr = POSIX::strftime("%b", @day);
  296. $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
  297. my $tag;
  298. my $mtag=sprintf("%02d", $month);
  299. if ($month == $params{month} && $thisyear == $params{year}) {
  300. $tag = 'year-calendar-this-month';
  301. }
  302. elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
  303. $tag = 'year-calendar-month-link';
  304. }
  305. elsif ($future_month && $month >= $future_month) {
  306. $tag = 'year-calendar-month-future';
  307. }
  308. else {
  309. $tag = 'year-calendar-month-nolink';
  310. }
  311. if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
  312. $post_months[$mtag]) {
  313. $murl = htmllink($params{page}, $params{destpage},
  314. "$archivebase/$params{year}/$mtag",
  315. noimageinline => 1,
  316. linktext => $monthabbr,
  317. title => $monthname);
  318. $calendar.=qq{\t<td class="$tag">};
  319. $calendar.=$murl;
  320. $calendar.=qq{\t</td>\n};
  321. }
  322. else {
  323. $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
  324. }
  325. add_depends($params{page}, "$archivebase/$params{year}/$mtag",
  326. deptype("presence"));
  327. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  328. }
  329. $calendar.=<<EOF;
  330. </table>
  331. EOF
  332. return $calendar;
  333. }
  334. sub setnextchange ($$) {
  335. my $page=shift;
  336. my $timestamp=shift;
  337. if (! exists $pagestate{$page}{calendar}{nextchange} ||
  338. $pagestate{$page}{calendar}{nextchange} > $timestamp) {
  339. $pagestate{$page}{calendar}{nextchange}=$timestamp;
  340. }
  341. }
  342. sub preprocess (@) {
  343. my %params=@_;
  344. my $thisyear=1900 + $now[5];
  345. my $thismonth=1 + $now[4];
  346. $params{pages} = "*" unless defined $params{pages};
  347. $params{type} = "month" unless defined $params{type};
  348. $params{week_start_day} = 0 unless defined $params{week_start_day};
  349. $params{months_per_row} = 3 unless defined $params{months_per_row};
  350. $params{year} = $thisyear unless defined $params{year};
  351. $params{month} = $thismonth unless defined $params{month};
  352. my $relativeyear=0;
  353. if ($params{year} < 1) {
  354. $relativeyear=1;
  355. $params{year}=$thisyear+$params{year};
  356. }
  357. my $relativemonth=0;
  358. if ($params{month} < 1) {
  359. $relativemonth=1;
  360. my $monthoff=$params{month};
  361. $params{month}=($thismonth+$monthoff) % 12;
  362. $params{month}=12 if $params{month}==0;
  363. my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
  364. - int($monthoff / 12);
  365. $params{year}-=$yearoff;
  366. }
  367. $params{month} = sprintf("%02d", $params{month});
  368. if ($params{type} eq 'month' && $params{year} == $thisyear
  369. && $params{month} == $thismonth) {
  370. # calendar for current month, updates next midnight
  371. setnextchange($params{destpage}, ($time
  372. + (60 - $now[0]) # seconds
  373. + (59 - $now[1]) * 60 # minutes
  374. + (23 - $now[2]) * 60 * 60 # hours
  375. ));
  376. }
  377. elsif ($params{type} eq 'month' &&
  378. (($params{year} == $thisyear && $params{month} > $thismonth) ||
  379. $params{year} > $thisyear)) {
  380. # calendar for upcoming month, updates 1st of that month
  381. setnextchange($params{destpage},
  382. timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
  383. }
  384. elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
  385. $relativemonth) {
  386. # Calendar for current year updates 1st of next month.
  387. # Any calendar relative to the current month also updates
  388. # then.
  389. if ($thismonth < 12) {
  390. setnextchange($params{destpage},
  391. timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
  392. }
  393. else {
  394. setnextchange($params{destpage},
  395. timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
  396. }
  397. }
  398. elsif ($relativeyear) {
  399. # Any calendar relative to the current year updates 1st
  400. # of next year.
  401. setnextchange($params{destpage},
  402. timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
  403. }
  404. elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
  405. # calendar for upcoming year, updates 1st of that year
  406. setnextchange($params{destpage},
  407. timelocal(0, 0, 0, 1, 1-1, $params{year}));
  408. }
  409. else {
  410. # calendar for past month or year, does not need
  411. # to update any more
  412. delete $pagestate{$params{destpage}}{calendar};
  413. }
  414. my $calendar="";
  415. if ($params{type} eq 'month') {
  416. $calendar=format_month(%params);
  417. }
  418. elsif ($params{type} eq 'year') {
  419. $calendar=format_year(%params);
  420. }
  421. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  422. } #}}
  423. sub needsbuild (@) {
  424. my $needsbuild=shift;
  425. foreach my $page (keys %pagestate) {
  426. if (exists $pagestate{$page}{calendar}{nextchange}) {
  427. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  428. # force a rebuild so the calendar shows
  429. # the current day
  430. push @$needsbuild, $pagesources{$page};
  431. }
  432. if (exists $pagesources{$page} &&
  433. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  434. # remove state, will be re-added if
  435. # the calendar is still there during the
  436. # rebuild
  437. delete $pagestate{$page}{calendar};
  438. }
  439. }
  440. }
  441. }
  442. 1