summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: 0f0e9518adf27b0ca3f912622d89b71ea13f5aa6 (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. <caption class="month-calendar-head">
  148. $purl
  149. $url
  150. $nurl
  151. </caption>
  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. $calendar=<<EOF;
  279. <table class="year-calendar">
  280. <caption class="year-calendar-head">
  281. $purl
  282. $url
  283. $nurl
  284. </caption>
  285. <tr>
  286. <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
  287. </tr>
  288. EOF
  289. for (my $month = 1; $month <= 12; $month++) {
  290. my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
  291. my $murl;
  292. my $monthname = POSIX::strftime("%B", @day);
  293. my $monthabbr = POSIX::strftime("%b", @day);
  294. $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
  295. my $tag;
  296. my $mtag=sprintf("%02d", $month);
  297. if ($month == $params{month} && $thisyear == $params{year}) {
  298. $tag = 'year-calendar-this-month';
  299. }
  300. elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
  301. $tag = 'year-calendar-month-link';
  302. }
  303. elsif ($future_month && $month >= $future_month) {
  304. $tag = 'year-calendar-month-future';
  305. }
  306. else {
  307. $tag = 'year-calendar-month-nolink';
  308. }
  309. if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
  310. $post_months[$mtag]) {
  311. $murl = htmllink($params{page}, $params{destpage},
  312. "$archivebase/$params{year}/$mtag",
  313. noimageinline => 1,
  314. linktext => $monthabbr,
  315. title => $monthname);
  316. $calendar.=qq{\t<td class="$tag">};
  317. $calendar.=$murl;
  318. $calendar.=qq{\t</td>\n};
  319. }
  320. else {
  321. $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
  322. }
  323. add_depends($params{page}, "$archivebase/$params{year}/$mtag",
  324. deptype("presence"));
  325. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  326. }
  327. $calendar.=<<EOF;
  328. </table>
  329. EOF
  330. return $calendar;
  331. }
  332. sub preprocess (@) {
  333. my %params=@_;
  334. my $thisyear=1900 + $now[5];
  335. my $thismonth=1 + $now[4];
  336. $params{pages} = "*" unless defined $params{pages};
  337. $params{type} = "month" unless defined $params{type};
  338. $params{week_start_day} = 0 unless defined $params{week_start_day};
  339. $params{months_per_row} = 3 unless defined $params{months_per_row};
  340. $params{year} = $thisyear unless defined $params{year};
  341. $params{month} = $thismonth unless defined $params{month};
  342. $params{month} = sprintf("%02d", $params{month});
  343. if ($params{type} eq 'month' && $params{year} == $thisyear
  344. && $params{month} == $thismonth) {
  345. # calendar for current month, updates next midnight
  346. $pagestate{$params{destpage}}{calendar}{nextchange}=($time
  347. + (60 - $now[0]) # seconds
  348. + (59 - $now[1]) * 60 # minutes
  349. + (23 - $now[2]) * 60 * 60 # hours
  350. );
  351. }
  352. elsif ($params{type} eq 'month' &&
  353. (($params{year} == $thisyear && $params{month} > $thismonth) ||
  354. $params{year} > $thisyear)) {
  355. # calendar for upcoming month, updates 1st of that month
  356. $pagestate{$params{destpage}}{calendar}{nextchange}=
  357. timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
  358. }
  359. elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
  360. # calendar for current year, updates 1st of next month
  361. if ($thismonth < 12) {
  362. $pagestate{$params{destpage}}{calendar}{nextchange}=
  363. timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
  364. }
  365. else {
  366. $pagestate{$params{destpage}}{calendar}{nextchange}=
  367. timelocal(0, 0, 0, 1, 1-1, $params{year}+1);
  368. }
  369. }
  370. elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
  371. # calendar for upcoming year, updates 1st of that year
  372. $pagestate{$params{destpage}}{calendar}{nextchange}=
  373. timelocal(0, 0, 0, 1, 1-1, $params{year});
  374. }
  375. else {
  376. # calendar for past month or year, does not need
  377. # to update any more
  378. delete $pagestate{$params{destpage}}{calendar};
  379. }
  380. # Calculate month names for next month, and previous months
  381. my $calendar="";
  382. if ($params{type} eq 'month') {
  383. $calendar=format_month(%params);
  384. }
  385. elsif ($params{type} eq 'year') {
  386. $calendar=format_year(%params);
  387. }
  388. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  389. } #}}
  390. sub needsbuild (@) {
  391. my $needsbuild=shift;
  392. foreach my $page (keys %pagestate) {
  393. if (exists $pagestate{$page}{calendar}{nextchange}) {
  394. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  395. # force a rebuild so the calendar shows
  396. # the current day
  397. push @$needsbuild, $pagesources{$page};
  398. }
  399. if (exists $pagesources{$page} &&
  400. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  401. # remove state, will be re-added if
  402. # the calendar is still there during the
  403. # rebuild
  404. delete $pagestate{$page}{calendar};
  405. }
  406. }
  407. }
  408. }
  409. 1