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