summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 5c62c7f9bd5eb5deaa583228ded3040544ad0c64 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use HTML::Entities;
  7. use open qw{:utf8 :std};
  8. use vars qw{%config %links %oldlinks %oldpagemtime %pagectime %pagecase
  9. %renderedfiles %pagesources %depends %hooks %forcerebuild};
  10. use Exporter q{import};
  11. our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
  12. bestlink htmllink readfile writefile pagetype srcfile pagename
  13. displaytime
  14. %config %links %renderedfiles %pagesources);
  15. our $VERSION = 1.00;
  16. # Optimisation.
  17. use Memoize;
  18. memoize("abs2rel");
  19. memoize("pagespec_translate");
  20. my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  21. sub defaultconfig () { #{{{
  22. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.x?html?$|\.rss$|.arch-ids/|{arch}/)},
  23. wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
  24. wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
  25. verbose => 0,
  26. syslog => 0,
  27. wikiname => "wiki",
  28. default_pageext => "mdwn",
  29. cgi => 0,
  30. rcs => 'svn',
  31. notify => 0,
  32. url => '',
  33. cgiurl => '',
  34. historyurl => '',
  35. diffurl => '',
  36. anonok => 0,
  37. rss => 0,
  38. discussion => 1,
  39. rebuild => 0,
  40. refresh => 0,
  41. getctime => 0,
  42. w3mmode => 0,
  43. wrapper => undef,
  44. wrappermode => undef,
  45. svnrepo => undef,
  46. svnpath => "trunk",
  47. srcdir => undef,
  48. destdir => undef,
  49. pingurl => [],
  50. templatedir => "$installdir/share/ikiwiki/templates",
  51. underlaydir => "$installdir/share/ikiwiki/basewiki",
  52. setup => undef,
  53. adminuser => undef,
  54. adminemail => undef,
  55. plugin => [qw{mdwn inline htmlscrubber}],
  56. timeformat => '%c',
  57. locale => undef,
  58. sslcookie => 0,
  59. httpauth => 0,
  60. } #}}}
  61. sub checkconfig () { #{{{
  62. # locale stuff; avoid LC_ALL since it overrides everything
  63. if (defined $ENV{LC_ALL}) {
  64. $ENV{LANG} = $ENV{LC_ALL};
  65. delete $ENV{LC_ALL};
  66. }
  67. if (defined $config{locale}) {
  68. eval q{use POSIX};
  69. $ENV{LANG} = $config{locale}
  70. if POSIX::setlocale(&POSIX::LC_TIME, $config{locale});
  71. }
  72. if ($config{w3mmode}) {
  73. eval q{use Cwd q{abs_path}};
  74. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  75. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  76. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  77. unless $config{cgiurl} =~ m!file:///!;
  78. $config{url}="file://".$config{destdir};
  79. }
  80. if ($config{cgi} && ! length $config{url}) {
  81. error("Must specify url to wiki with --url when using --cgi\n");
  82. }
  83. if ($config{rss} && ! length $config{url}) {
  84. error("Must specify url to wiki with --url when using --rss\n");
  85. }
  86. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  87. unless exists $config{wikistatedir};
  88. if ($config{rcs}) {
  89. eval qq{require IkiWiki::Rcs::$config{rcs}};
  90. if ($@) {
  91. error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
  92. }
  93. }
  94. else {
  95. require IkiWiki::Rcs::Stub;
  96. }
  97. run_hooks(checkconfig => sub { shift->() });
  98. } #}}}
  99. sub loadplugins () { #{{{
  100. foreach my $plugin (@{$config{plugin}}) {
  101. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  102. eval qq{use $mod};
  103. if ($@) {
  104. error("Failed to load plugin $mod: $@");
  105. }
  106. }
  107. run_hooks(getopt => sub { shift->() });
  108. if (grep /^-/, @ARGV) {
  109. print STDERR "Unknown option: $_\n"
  110. foreach grep /^-/, @ARGV;
  111. usage();
  112. }
  113. } #}}}
  114. sub error ($) { #{{{
  115. if ($config{cgi}) {
  116. print "Content-type: text/html\n\n";
  117. print misctemplate("Error", "<p>Error: @_</p>");
  118. }
  119. log_message(error => @_);
  120. exit(1);
  121. } #}}}
  122. sub debug ($) { #{{{
  123. return unless $config{verbose};
  124. log_message(debug => @_);
  125. } #}}}
  126. my $log_open=0;
  127. sub log_message ($$) { #{{{
  128. my $type=shift;
  129. if ($config{syslog}) {
  130. require Sys::Syslog;
  131. unless ($log_open) {
  132. Sys::Syslog::setlogsock('unix');
  133. Sys::Syslog::openlog('ikiwiki', '', 'user');
  134. $log_open=1;
  135. }
  136. eval {
  137. Sys::Syslog::syslog($type, join(" ", @_));
  138. }
  139. }
  140. elsif (! $config{cgi}) {
  141. print "@_\n";
  142. }
  143. else {
  144. print STDERR "@_\n";
  145. }
  146. } #}}}
  147. sub possibly_foolish_untaint ($) { #{{{
  148. my $tainted=shift;
  149. my ($untainted)=$tainted=~/(.*)/;
  150. return $untainted;
  151. } #}}}
  152. sub basename ($) { #{{{
  153. my $file=shift;
  154. $file=~s!.*/+!!;
  155. return $file;
  156. } #}}}
  157. sub dirname ($) { #{{{
  158. my $file=shift;
  159. $file=~s!/*[^/]+$!!;
  160. return $file;
  161. } #}}}
  162. sub pagetype ($) { #{{{
  163. my $page=shift;
  164. if ($page =~ /\.([^.]+)$/) {
  165. return $1 if exists $hooks{htmlize}{$1};
  166. }
  167. return undef;
  168. } #}}}
  169. sub pagename ($) { #{{{
  170. my $file=shift;
  171. my $type=pagetype($file);
  172. my $page=$file;
  173. $page=~s/\Q.$type\E*$// if defined $type;
  174. return $page;
  175. } #}}}
  176. sub htmlpage ($) { #{{{
  177. my $page=shift;
  178. return $page.".html";
  179. } #}}}
  180. sub srcfile ($) { #{{{
  181. my $file=shift;
  182. return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
  183. return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
  184. error("internal error: $file cannot be found");
  185. } #}}}
  186. sub readfile ($;$) { #{{{
  187. my $file=shift;
  188. my $binary=shift;
  189. if (-l $file) {
  190. error("cannot read a symlink ($file)");
  191. }
  192. local $/=undef;
  193. open (IN, $file) || error("failed to read $file: $!");
  194. binmode(IN) if ($binary);
  195. my $ret=<IN>;
  196. close IN;
  197. return $ret;
  198. } #}}}
  199. sub writefile ($$$;$) { #{{{
  200. my $file=shift; # can include subdirs
  201. my $destdir=shift; # directory to put file in
  202. my $content=shift;
  203. my $binary=shift;
  204. my $test=$file;
  205. while (length $test) {
  206. if (-l "$destdir/$test") {
  207. error("cannot write to a symlink ($test)");
  208. }
  209. $test=dirname($test);
  210. }
  211. my $dir=dirname("$destdir/$file");
  212. if (! -d $dir) {
  213. my $d="";
  214. foreach my $s (split(m!/+!, $dir)) {
  215. $d.="$s/";
  216. if (! -d $d) {
  217. mkdir($d) || error("failed to create directory $d: $!");
  218. }
  219. }
  220. }
  221. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  222. binmode(OUT) if ($binary);
  223. print OUT $content;
  224. close OUT;
  225. } #}}}
  226. sub bestlink ($$) { #{{{
  227. my $page=shift;
  228. my $link=shift;
  229. my $cwd=$page;
  230. do {
  231. my $l=$cwd;
  232. $l.="/" if length $l;
  233. $l.=$link;
  234. if (exists $links{$l}) {
  235. return $l;
  236. }
  237. elsif (exists $pagecase{lc $l}) {
  238. return $pagecase{lc $l};
  239. }
  240. } while $cwd=~s!/?[^/]+$!!;
  241. #print STDERR "warning: page $page, broken link: $link\n";
  242. return "";
  243. } #}}}
  244. sub isinlinableimage ($) { #{{{
  245. my $file=shift;
  246. $file=~/\.(png|gif|jpg|jpeg)$/i;
  247. } #}}}
  248. sub pagetitle ($) { #{{{
  249. my $page=shift;
  250. $page=~s/__(\d+)__/&#$1;/g;
  251. $page=~y/_/ /;
  252. return $page;
  253. } #}}}
  254. sub titlepage ($) { #{{{
  255. my $title=shift;
  256. $title=~y/ /_/;
  257. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  258. return $title;
  259. } #}}}
  260. sub cgiurl (@) { #{{{
  261. my %params=@_;
  262. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  263. } #}}}
  264. sub baseurl (;$) { #{{{
  265. my $page=shift;
  266. return "$config{url}/" if ! defined $page;
  267. $page=~s/[^\/]+$//;
  268. $page=~s/[^\/]+\//..\//g;
  269. return $page;
  270. } #}}}
  271. sub abs2rel ($$) { #{{{
  272. # Work around very innefficient behavior in File::Spec if abs2rel
  273. # is passed two relative paths. It's much faster if paths are
  274. # absolute! (Debian bug #376658)
  275. my $path="/".shift;
  276. my $base="/".shift;
  277. require File::Spec;
  278. my $ret=File::Spec->abs2rel($path, $base);
  279. $ret=~s/^// if defined $ret;
  280. return $ret;
  281. } #}}}
  282. sub displaytime ($) { #{{{
  283. my $time=shift;
  284. eval q{use POSIX};
  285. # strftime doesn't know about encodings, so make sure
  286. # its output is properly treated as utf8
  287. return decode_utf8(POSIX::strftime(
  288. $config{timeformat}, localtime($time)));
  289. } #}}}
  290. sub htmllink ($$$;$$$) { #{{{
  291. my $lpage=shift; # the page doing the linking
  292. my $page=shift; # the page that will contain the link (different for inline)
  293. my $link=shift;
  294. my $noimageinline=shift; # don't turn links into inline html images
  295. my $forcesubpage=shift; # force a link to a subpage
  296. my $linktext=shift; # set to force the link text to something
  297. my $bestlink;
  298. if (! $forcesubpage) {
  299. $bestlink=bestlink($lpage, $link);
  300. }
  301. else {
  302. $bestlink="$lpage/".lc($link);
  303. }
  304. $linktext=pagetitle(basename($link)) unless defined $linktext;
  305. return "<span class=\"selflink\">$linktext</span>"
  306. if length $bestlink && $page eq $bestlink;
  307. # TODO BUG: %renderedfiles may not have it, if the linked to page
  308. # was also added and isn't yet rendered! Note that this bug is
  309. # masked by the bug that makes all new files be rendered twice.
  310. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  311. $bestlink=htmlpage($bestlink);
  312. }
  313. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  314. return "<span><a href=\"".
  315. cgiurl(do => "create", page => lc($link), from => $page).
  316. "\">?</a>$linktext</span>"
  317. }
  318. $bestlink=abs2rel($bestlink, dirname($page));
  319. if (! $noimageinline && isinlinableimage($bestlink)) {
  320. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  321. }
  322. return "<a href=\"$bestlink\">$linktext</a>";
  323. } #}}}
  324. sub htmlize ($$$) { #{{{
  325. my $page=shift;
  326. my $type=shift;
  327. my $content=shift;
  328. if (exists $hooks{htmlize}{$type}) {
  329. $content=$hooks{htmlize}{$type}{call}->(
  330. page => $page,
  331. content => $content,
  332. );
  333. }
  334. else {
  335. error("htmlization of $type not supported");
  336. }
  337. run_hooks(sanitize => sub {
  338. $content=shift->(
  339. page => $page,
  340. content => $content,
  341. );
  342. });
  343. return $content;
  344. } #}}}
  345. sub linkify ($$$) { #{{{
  346. my $lpage=shift; # the page containing the links
  347. my $page=shift; # the page the link will end up on (different for inline)
  348. my $content=shift;
  349. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  350. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
  351. : ( $1 ? "[[$3]]" : htmllink($lpage, $page, titlepage($3)))
  352. }eg;
  353. return $content;
  354. } #}}}
  355. my %preprocessing;
  356. sub preprocess ($$$) { #{{{
  357. my $page=shift; # the page the data comes from
  358. my $destpage=shift; # the page the data will appear in (different for inline)
  359. my $content=shift;
  360. my $handle=sub {
  361. my $escape=shift;
  362. my $command=shift;
  363. my $params=shift;
  364. if (length $escape) {
  365. return "[[$command $params]]";
  366. }
  367. elsif (exists $hooks{preprocess}{$command}) {
  368. # Note: preserve order of params, some plugins may
  369. # consider it significant.
  370. my @params;
  371. while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
  372. my $key=$1;
  373. my $val;
  374. if (defined $2) {
  375. $val=$2;
  376. $val=~s/\r\n/\n/mg;
  377. $val=~s/^\n+//g;
  378. $val=~s/\n+$//g;
  379. }
  380. elsif (defined $3) {
  381. $val=$3;
  382. }
  383. elsif (defined $4) {
  384. $val=$4;
  385. }
  386. if (defined $key) {
  387. push @params, $key, $val;
  388. }
  389. else {
  390. push @params, $val, '';
  391. }
  392. }
  393. if ($preprocessing{$page}++ > 3) {
  394. # Avoid loops of preprocessed pages preprocessing
  395. # other pages that preprocess them, etc.
  396. return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
  397. }
  398. my $ret=$hooks{preprocess}{$command}{call}->(
  399. @params,
  400. page => $page,
  401. destpage => $destpage,
  402. );
  403. $preprocessing{$page}--;
  404. return $ret;
  405. }
  406. else {
  407. return "[[$command $params]]";
  408. }
  409. };
  410. $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
  411. return $content;
  412. } #}}}
  413. sub filter ($$) {
  414. my $page=shift;
  415. my $content=shift;
  416. run_hooks(filter => sub {
  417. $content=shift->(page => $page, content => $content);
  418. });
  419. return $content;
  420. }
  421. sub indexlink () { #{{{
  422. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  423. } #}}}
  424. sub lockwiki () { #{{{
  425. # Take an exclusive lock on the wiki to prevent multiple concurrent
  426. # run issues. The lock will be dropped on program exit.
  427. if (! -d $config{wikistatedir}) {
  428. mkdir($config{wikistatedir});
  429. }
  430. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  431. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  432. if (! flock(WIKILOCK, 2 | 4)) {
  433. debug("wiki seems to be locked, waiting for lock");
  434. my $wait=600; # arbitrary, but don't hang forever to
  435. # prevent process pileup
  436. for (1..600) {
  437. return if flock(WIKILOCK, 2 | 4);
  438. sleep 1;
  439. }
  440. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  441. }
  442. } #}}}
  443. sub unlockwiki () { #{{{
  444. close WIKILOCK;
  445. } #}}}
  446. sub loadindex () { #{{{
  447. open (IN, "$config{wikistatedir}/index") || return;
  448. while (<IN>) {
  449. $_=possibly_foolish_untaint($_);
  450. chomp;
  451. my %items;
  452. $items{link}=[];
  453. foreach my $i (split(/ /, $_)) {
  454. my ($item, $val)=split(/=/, $i, 2);
  455. push @{$items{$item}}, decode_entities($val);
  456. }
  457. next unless exists $items{src}; # skip bad lines for now
  458. my $page=pagename($items{src}[0]);
  459. if (! $config{rebuild}) {
  460. $pagesources{$page}=$items{src}[0];
  461. $oldpagemtime{$page}=$items{mtime}[0];
  462. $oldlinks{$page}=[@{$items{link}}];
  463. $links{$page}=[@{$items{link}}];
  464. $depends{$page}=$items{depends}[0] if exists $items{depends};
  465. $renderedfiles{$page}=$items{dest}[0];
  466. $pagecase{lc $page}=$page;
  467. }
  468. $pagectime{$page}=$items{ctime}[0];
  469. }
  470. close IN;
  471. } #}}}
  472. sub saveindex () { #{{{
  473. run_hooks(savestate => sub { shift->() });
  474. if (! -d $config{wikistatedir}) {
  475. mkdir($config{wikistatedir});
  476. }
  477. open (OUT, ">$config{wikistatedir}/index") ||
  478. error("cannot write to $config{wikistatedir}/index: $!");
  479. foreach my $page (keys %oldpagemtime) {
  480. next unless $oldpagemtime{$page};
  481. my $line="mtime=$oldpagemtime{$page} ".
  482. "ctime=$pagectime{$page} ".
  483. "src=$pagesources{$page} ".
  484. "dest=$renderedfiles{$page}";
  485. $line.=" link=$_" foreach @{$links{$page}};
  486. if (exists $depends{$page}) {
  487. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  488. }
  489. print OUT $line."\n";
  490. }
  491. close OUT;
  492. } #}}}
  493. sub template_params (@) { #{{{
  494. my $filename=shift;
  495. require HTML::Template;
  496. return filter => sub {
  497. my $text_ref = shift;
  498. $$text_ref=&Encode::decode_utf8($$text_ref);
  499. },
  500. filename => "$config{templatedir}/$filename",
  501. loop_context_vars => 1,
  502. die_on_bad_params => 0,
  503. @_;
  504. } #}}}
  505. sub template ($;@) { #{{{
  506. HTML::Template->new(template_params(@_));
  507. } #}}}
  508. sub misctemplate ($$;@) { #{{{
  509. my $title=shift;
  510. my $pagebody=shift;
  511. my $template=template("misc.tmpl");
  512. $template->param(
  513. title => $title,
  514. indexlink => indexlink(),
  515. wikiname => $config{wikiname},
  516. pagebody => $pagebody,
  517. baseurl => baseurl(),
  518. @_,
  519. );
  520. run_hooks(pagetemplate => sub {
  521. shift->(page => "", destpage => "", template => $template);
  522. });
  523. return $template->output;
  524. }#}}}
  525. sub hook (@) { # {{{
  526. my %param=@_;
  527. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  528. error "hook requires type, call, and id parameters";
  529. }
  530. $hooks{$param{type}}{$param{id}}=\%param;
  531. } # }}}
  532. sub run_hooks ($$) { # {{{
  533. # Calls the given sub for each hook of the given type,
  534. # passing it the hook function to call.
  535. my $type=shift;
  536. my $sub=shift;
  537. if (exists $hooks{$type}) {
  538. foreach my $id (keys %{$hooks{$type}}) {
  539. $sub->($hooks{$type}{$id}{call});
  540. }
  541. }
  542. } #}}}
  543. sub globlist_to_pagespec ($) { #{{{
  544. my @globlist=split(' ', shift);
  545. my (@spec, @skip);
  546. foreach my $glob (@globlist) {
  547. if ($glob=~/^!(.*)/) {
  548. push @skip, $glob;
  549. }
  550. else {
  551. push @spec, $glob;
  552. }
  553. }
  554. my $spec=join(" or ", @spec);
  555. if (@skip) {
  556. my $skip=join(" and ", @skip);
  557. if (length $spec) {
  558. $spec="$skip and ($spec)";
  559. }
  560. else {
  561. $spec=$skip;
  562. }
  563. }
  564. return $spec;
  565. } #}}}
  566. sub is_globlist ($) { #{{{
  567. my $s=shift;
  568. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  569. } #}}}
  570. sub safequote ($) { #{{{
  571. my $s=shift;
  572. $s=~s/[{}]//g;
  573. return "q{$s}";
  574. } #}}}
  575. sub pagespec_merge ($$) { #{{{
  576. my $a=shift;
  577. my $b=shift;
  578. return $a if $a eq $b;
  579. # Support for old-style GlobLists.
  580. if (is_globlist($a)) {
  581. $a=globlist_to_pagespec($a);
  582. }
  583. if (is_globlist($b)) {
  584. $b=globlist_to_pagespec($b);
  585. }
  586. return "($a) or ($b)";
  587. } #}}}
  588. sub pagespec_translate ($) { #{{{
  589. # This assumes that $page is in scope in the function
  590. # that evalulates the translated pagespec code.
  591. my $spec=shift;
  592. # Support for old-style GlobLists.
  593. if (is_globlist($spec)) {
  594. $spec=globlist_to_pagespec($spec);
  595. }
  596. # Convert spec to perl code.
  597. my $code="";
  598. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  599. my $word=$1;
  600. if (lc $word eq "and") {
  601. $code.=" &&";
  602. }
  603. elsif (lc $word eq "or") {
  604. $code.=" ||";
  605. }
  606. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  607. $code.=" ".$word;
  608. }
  609. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  610. $code.=" match_$1(\$page, ".safequote($2).")";
  611. }
  612. else {
  613. $code.=" match_glob(\$page, ".safequote($word).")";
  614. }
  615. }
  616. return $code;
  617. } #}}}
  618. sub add_depends ($$) { #{{{
  619. my $page=shift;
  620. my $pagespec=shift;
  621. if (! exists $depends{$page}) {
  622. $depends{$page}=$pagespec;
  623. }
  624. else {
  625. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  626. }
  627. } # }}}
  628. sub pagespec_match ($$) { #{{{
  629. my $page=shift;
  630. my $spec=shift;
  631. return eval pagespec_translate($spec);
  632. } #}}}
  633. sub match_glob ($$) { #{{{
  634. my $page=shift;
  635. my $glob=shift;
  636. # turn glob into safe regexp
  637. $glob=quotemeta($glob);
  638. $glob=~s/\\\*/.*/g;
  639. $glob=~s/\\\?/./g;
  640. return $page=~/^$glob$/i;
  641. } #}}}
  642. sub match_link ($$) { #{{{
  643. my $page=shift;
  644. my $link=lc(shift);
  645. my $links = $links{$page} or return undef;
  646. foreach my $p (@$links) {
  647. return 1 if lc $p eq $link;
  648. }
  649. return 0;
  650. } #}}}
  651. sub match_backlink ($$) { #{{{
  652. match_link(pop, pop);
  653. } #}}}
  654. sub match_created_before ($$) { #{{{
  655. my $page=shift;
  656. my $testpage=shift;
  657. if (exists $pagectime{$testpage}) {
  658. return $pagectime{$page} < $pagectime{$testpage};
  659. }
  660. else {
  661. return 0;
  662. }
  663. } #}}}
  664. sub match_created_after ($$) { #{{{
  665. my $page=shift;
  666. my $testpage=shift;
  667. if (exists $pagectime{$testpage}) {
  668. return $pagectime{$page} > $pagectime{$testpage};
  669. }
  670. else {
  671. return 0;
  672. }
  673. } #}}}
  674. sub match_creation_day ($$) { #{{{
  675. return ((gmtime($pagectime{shift()}))[3] == shift);
  676. } #}}}
  677. sub match_creation_month ($$) { #{{{
  678. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  679. } #}}}
  680. sub match_creation_year ($$) { #{{{
  681. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  682. } #}}}
  683. 1