summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: aeb5f3d294693b93285fac9da87ab2564bd89add (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",'','');
  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,
  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 preprocess (@) {
  334. my %params=@_;
  335. my $thisyear=1900 + $now[5];
  336. my $thismonth=1 + $now[4];
  337. $params{pages} = "*" unless defined $params{pages};
  338. $params{type} = "month" unless defined $params{type};
  339. $params{week_start_day} = 0 unless defined $params{week_start_day};
  340. $params{months_per_row} = 3 unless defined $params{months_per_row};
  341. $params{year} = $thisyear unless defined $params{year};
  342. $params{month} = $thismonth unless defined $params{month};
  343. $params{month} = sprintf("%02d", $params{month});
  344. if ($params{type} eq 'month' && $params{year} == $thisyear
  345. && $params{month} == $thismonth) {
  346. # calendar for current month, updates next midnight
  347. $pagestate{$params{destpage}}{calendar}{nextchange}=($time
  348. + (60 - $now[0]) # seconds
  349. + (59 - $now[1]) * 60 # minutes
  350. + (23 - $now[2]) * 60 * 60 # hours
  351. );
  352. }
  353. elsif ($params{type} eq 'month' &&
  354. (($params{year} == $thisyear && $params{month} > $thismonth) ||
  355. $params{year} > $thisyear)) {
  356. # calendar for upcoming month, updates 1st of that month
  357. $pagestate{$params{destpage}}{calendar}{nextchange}=
  358. timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
  359. }
  360. elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
  361. # calendar for current year, updates 1st of next month
  362. if ($thismonth < 12) {
  363. $pagestate{$params{destpage}}{calendar}{nextchange}=
  364. timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
  365. }
  366. else {
  367. $pagestate{$params{destpage}}{calendar}{nextchange}=
  368. timelocal(0, 0, 0, 1, 1-1, $params{year}+1);
  369. }
  370. }
  371. elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
  372. # calendar for upcoming year, updates 1st of that year
  373. $pagestate{$params{destpage}}{calendar}{nextchange}=
  374. timelocal(0, 0, 0, 1, 1-1, $params{year});
  375. }
  376. else {
  377. # calendar for past month or year, does not need
  378. # to update any more
  379. delete $pagestate{$params{destpage}}{calendar};
  380. }
  381. # Calculate month names for next month, and previous months
  382. my $calendar="";
  383. if ($params{type} eq 'month') {
  384. $calendar=format_month(%params);
  385. }
  386. elsif ($params{type} eq 'year') {
  387. $calendar=format_year(%params);
  388. }
  389. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  390. } #}}
  391. sub needsbuild (@) {
  392. my $needsbuild=shift;
  393. foreach my $page (keys %pagestate) {
  394. if (exists $pagestate{$page}{calendar}{nextchange}) {
  395. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  396. # force a rebuild so the calendar shows
  397. # the current day
  398. push @$needsbuild, $pagesources{$page};
  399. }
  400. if (exists $pagesources{$page} &&
  401. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  402. # remove state, will be re-added if
  403. # the calendar is still there during the
  404. # rebuild
  405. delete $pagestate{$page}{calendar};
  406. }
  407. }
  408. }
  409. }
  410. 1