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