summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: 0bf933dcda9db2aedc0ccbb65a5b3f92223079e9 (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 $monthname=POSIX::strftime("%B", @monthstart);
  110. my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
  111. my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
  112. my $archivebase = 'archives';
  113. $archivebase = $config{archivebase} if defined $config{archivebase};
  114. $archivebase = $params{archivebase} if defined $params{archivebase};
  115. # Calculate URL's for monthly archives.
  116. my ($url, $purl, $nurl)=("$monthname $params{year}",'','');
  117. if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
  118. $url = htmllink($params{page}, $params{destpage},
  119. "$archivebase/$params{year}/".$params{month},
  120. noimageinline => 1,
  121. linktext => "$monthname $params{year}",
  122. title => $monthname);
  123. }
  124. add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
  125. deptype("presence"));
  126. if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
  127. $purl = htmllink($params{page}, $params{destpage},
  128. "$archivebase/$pyear/$pmonth",
  129. noimageinline => 1,
  130. linktext => "\&larr;",
  131. title => $pmonthname);
  132. }
  133. add_depends($params{page}, "$archivebase/$pyear/$pmonth",
  134. deptype("presence"));
  135. if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
  136. $nurl = htmllink($params{page}, $params{destpage},
  137. "$archivebase/$nyear/$nmonth",
  138. noimageinline => 1,
  139. linktext => "\&rarr;",
  140. title => $nmonthname);
  141. }
  142. add_depends($params{page}, "$archivebase/$nyear/$nmonth",
  143. deptype("presence"));
  144. # Start producing the month calendar
  145. $calendar=<<EOF;
  146. <table class="month-calendar">
  147. <tr>
  148. <th class="month-calendar-arrow">$purl</th>
  149. <th class="month-calendar-head" colspan="5">$url</th>
  150. <th class="month-calendar-arrow">$nurl</th>
  151. </tr>
  152. <tr>
  153. EOF
  154. # Suppose we want to start the week with day $week_start_day
  155. # If $monthstart[6] == 1
  156. my $week_start_day = $params{week_start_day};
  157. my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
  158. my %downame;
  159. my %dowabbr;
  160. for my $dow ($week_start_day..$week_start_day+6) {
  161. my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
  162. my $downame = POSIX::strftime("%A", @day);
  163. my $dowabbr = POSIX::strftime("%a", @day);
  164. $downame{$dow % 7}=$downame;
  165. $dowabbr{$dow % 7}=$dowabbr;
  166. $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
  167. }
  168. $calendar.=<<EOF;
  169. </tr>
  170. EOF
  171. my $wday;
  172. # we start with a week_start_day, and skip until we get to the first
  173. for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
  174. $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
  175. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  176. }
  177. # At this point, either the first is a week_start_day, in which case
  178. # nothing has been printed, or else we are in the middle of a row.
  179. for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
  180. $day++, $wday++, $wday %= 7) {
  181. # At this point, on a week_start_day, we close out a row,
  182. # and start a new one -- unless it is week_start_day on the
  183. # first, where we do not close a row -- since none was started.
  184. if ($wday == $week_start_day) {
  185. $calendar.=qq{\t</tr>\n} unless $day == 1;
  186. $calendar.=qq{\t<tr>\n};
  187. }
  188. my $tag;
  189. my $key="$params{year}/$params{month}/$day";
  190. if (defined $linkcache{$key}) {
  191. if ($day == $today) {
  192. $tag='month-calendar-day-this-day';
  193. }
  194. else {
  195. $tag='month-calendar-day-link';
  196. }
  197. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
  198. $calendar.=htmllink($params{page}, $params{destpage},
  199. $linkcache{$key},
  200. noimageinline => 1,
  201. linktext => $day,
  202. title => pagetitle(IkiWiki::basename($linkcache{$key})));
  203. $calendar.=qq{</td>\n};
  204. }
  205. else {
  206. if ($day == $today) {
  207. $tag='month-calendar-day-this-day';
  208. }
  209. elsif ($day == $future_dom) {
  210. $tag='month-calendar-day-future';
  211. }
  212. else {
  213. $tag='month-calendar-day-nolink';
  214. }
  215. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
  216. }
  217. }
  218. # finish off the week
  219. for (; $wday != $week_start_day; $wday++, $wday %= 7) {
  220. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  221. }
  222. $calendar.=<<EOF;
  223. </tr>
  224. </table>
  225. EOF
  226. return $calendar;
  227. }
  228. sub format_year (@) {
  229. my %params=@_;
  230. my @post_months;
  231. foreach my $p (pagespec_match_list($params{page},
  232. "creation_year($params{year}) and ($params{pages})",
  233. # add presence dependencies to update
  234. # year calendar's links to months when
  235. # pages are added/removed
  236. deptype => deptype("presence"))) {
  237. my $mtime = $IkiWiki::pagectime{$p};
  238. my @date = localtime($mtime);
  239. my $month = $date[4] + 1;
  240. $post_months[$month]++;
  241. }
  242. my $calendar="\n";
  243. my $pyear = $params{year} - 1;
  244. my $nyear = $params{year} + 1;
  245. my $thisyear = $now[5]+1900;
  246. my $future_month = 0;
  247. $future_month = $now[4]+1 if $params{year} == $thisyear;
  248. my $archivebase = 'archives';
  249. $archivebase = $config{archivebase} if defined $config{archivebase};
  250. $archivebase = $params{archivebase} if defined $params{archivebase};
  251. # calculate URL's for previous and next years
  252. my ($url, $purl, $nurl)=("$params{year}",'','');
  253. if (exists $pagesources{"$archivebase/$params{year}"}) {
  254. $url = htmllink($params{page}, $params{destpage},
  255. "$archivebase/$params{year}",
  256. noimageinline => 1,
  257. linktext => $params{year},
  258. title => $params{year});
  259. }
  260. add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
  261. if (exists $pagesources{"$archivebase/$pyear"}) {
  262. $purl = htmllink($params{page}, $params{destpage},
  263. "$archivebase/$pyear",
  264. noimageinline => 1,
  265. linktext => "\&larr;",
  266. title => $pyear);
  267. }
  268. add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
  269. if (exists $pagesources{"$archivebase/$nyear"}) {
  270. $nurl = htmllink($params{page}, $params{destpage},
  271. "$archivebase/$nyear",
  272. noimageinline => 1,
  273. linktext => "\&rarr;",
  274. title => $nyear);
  275. }
  276. add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
  277. # Start producing the year calendar
  278. my $m=$params{months_per_row}-2;
  279. $calendar=<<EOF;
  280. <table class="year-calendar">
  281. <tr>
  282. <th class="year-calendar-arrow">$purl</th>
  283. <th class="year-calendar-head" colspan="$m">$url</th>
  284. <th class="year-calendar-arrow">$nurl</th>
  285. </tr>
  286. <tr>
  287. <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
  288. </tr>
  289. EOF
  290. for (my $month = 1; $month <= 12; $month++) {
  291. my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
  292. my $murl;
  293. my $monthname = POSIX::strftime("%B", @day);
  294. my $monthabbr = POSIX::strftime("%b", @day);
  295. $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
  296. my $tag;
  297. my $mtag=sprintf("%02d", $month);
  298. if ($month == $params{month} && $thisyear == $params{year}) {
  299. $tag = 'year-calendar-this-month';
  300. }
  301. elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
  302. $tag = 'year-calendar-month-link';
  303. }
  304. elsif ($future_month && $month >= $future_month) {
  305. $tag = 'year-calendar-month-future';
  306. }
  307. else {
  308. $tag = 'year-calendar-month-nolink';
  309. }
  310. if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
  311. $post_months[$mtag]) {
  312. $murl = htmllink($params{page}, $params{destpage},
  313. "$archivebase/$params{year}/$mtag",
  314. noimageinline => 1,
  315. linktext => $monthabbr,
  316. title => $monthname);
  317. $calendar.=qq{\t<td class="$tag">};
  318. $calendar.=$murl;
  319. $calendar.=qq{\t</td>\n};
  320. }
  321. else {
  322. $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
  323. }
  324. add_depends($params{page}, "$archivebase/$params{year}/$mtag",
  325. deptype("presence"));
  326. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  327. }
  328. $calendar.=<<EOF;
  329. </table>
  330. EOF
  331. return $calendar;
  332. }
  333. sub setnextchange ($$) {
  334. my $page=shift;
  335. my $timestamp=shift;
  336. if (! exists $pagestate{$page}{calendar}{nextchange} ||
  337. $pagestate{$page}{calendar}{nextchange} > $timestamp) {
  338. $pagestate{$page}{calendar}{nextchange}=$timestamp;
  339. }
  340. }
  341. sub preprocess (@) {
  342. my %params=@_;
  343. my $thisyear=1900 + $now[5];
  344. my $thismonth=1 + $now[4];
  345. $params{pages} = "*" unless defined $params{pages};
  346. $params{type} = "month" unless defined $params{type};
  347. $params{week_start_day} = 0 unless defined $params{week_start_day};
  348. $params{months_per_row} = 3 unless defined $params{months_per_row};
  349. $params{year} = $thisyear unless defined $params{year};
  350. $params{month} = $thismonth unless defined $params{month};
  351. my $relativeyear=0;
  352. if ($params{year} < 1) {
  353. $relativeyear=1;
  354. $params{year}=$thisyear+$params{year};
  355. }
  356. my $relativemonth=0;
  357. if ($params{month} < 1) {
  358. $relativemonth=1;
  359. my $monthoff=$params{month};
  360. $params{month}=($thismonth+$monthoff) % 12;
  361. $params{month}=12 if $params{month}==0;
  362. my $yearoff=POSIX::ceil(($thismonth-$params{month}) / -12)
  363. - int($monthoff / 12);
  364. $params{year}-=$yearoff;
  365. }
  366. $params{month} = sprintf("%02d", $params{month});
  367. if ($params{type} eq 'month' && $params{year} == $thisyear
  368. && $params{month} == $thismonth) {
  369. # calendar for current month, updates next midnight
  370. setnextchange($params{destpage}, ($time
  371. + (60 - $now[0]) # seconds
  372. + (59 - $now[1]) * 60 # minutes
  373. + (23 - $now[2]) * 60 * 60 # hours
  374. ));
  375. }
  376. elsif ($params{type} eq 'month' &&
  377. (($params{year} == $thisyear && $params{month} > $thismonth) ||
  378. $params{year} > $thisyear)) {
  379. # calendar for upcoming month, updates 1st of that month
  380. setnextchange($params{destpage},
  381. timelocal(0, 0, 0, 1, $params{month}-1, $params{year}));
  382. }
  383. elsif (($params{type} eq 'year' && $params{year} == $thisyear) ||
  384. $relativemonth) {
  385. # Calendar for current year updates 1st of next month.
  386. # Any calendar relative to the current month also updates
  387. # then.
  388. if ($thismonth < 12) {
  389. setnextchange($params{destpage},
  390. timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year}));
  391. }
  392. else {
  393. setnextchange($params{destpage},
  394. timelocal(0, 0, 0, 1, 1-1, $params{year}+1));
  395. }
  396. }
  397. elsif ($relativeyear) {
  398. # Any calendar relative to the current year updates 1st
  399. # of next year.
  400. setnextchange($params{destpage},
  401. timelocal(0, 0, 0, 1, 1-1, $thisyear+1));
  402. }
  403. elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
  404. # calendar for upcoming year, updates 1st of that year
  405. setnextchange($params{destpage},
  406. timelocal(0, 0, 0, 1, 1-1, $params{year}));
  407. }
  408. else {
  409. # calendar for past month or year, does not need
  410. # to update any more
  411. delete $pagestate{$params{destpage}}{calendar};
  412. }
  413. my $calendar="";
  414. if ($params{type} eq 'month') {
  415. $calendar=format_month(%params);
  416. }
  417. elsif ($params{type} eq 'year') {
  418. $calendar=format_year(%params);
  419. }
  420. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  421. } #}}
  422. sub needsbuild (@) {
  423. my $needsbuild=shift;
  424. foreach my $page (keys %pagestate) {
  425. if (exists $pagestate{$page}{calendar}{nextchange}) {
  426. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  427. # force a rebuild so the calendar shows
  428. # the current day
  429. push @$needsbuild, $pagesources{$page};
  430. }
  431. if (exists $pagesources{$page} &&
  432. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  433. # remove state, will be re-added if
  434. # the calendar is still there during the
  435. # rebuild
  436. delete $pagestate{$page}{calendar};
  437. }
  438. }
  439. }
  440. }
  441. 1