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