summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 80208ef2b51878bb20e9321b36a9094c8ee8b17d (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 will_render
  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. my %cleared;
  230. sub will_render ($$;$) { #{{{
  231. my $page=shift;
  232. my $dest=shift;
  233. my $clear=shift;
  234. # Important security check.
  235. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  236. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
  237. error("$config{destdir}/$dest independently created, not overwriting with version from $page");
  238. }
  239. if (! $clear || $cleared{$page}) {
  240. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  241. }
  242. else {
  243. $renderedfiles{$page}=[$dest];
  244. $cleared{$page}=1;
  245. }
  246. } #}}}
  247. sub bestlink ($$) { #{{{
  248. my $page=shift;
  249. my $link=shift;
  250. my $cwd=$page;
  251. do {
  252. my $l=$cwd;
  253. $l.="/" if length $l;
  254. $l.=$link;
  255. if (exists $links{$l}) {
  256. return $l;
  257. }
  258. elsif (exists $pagecase{lc $l}) {
  259. return $pagecase{lc $l};
  260. }
  261. } while $cwd=~s!/?[^/]+$!!;
  262. #print STDERR "warning: page $page, broken link: $link\n";
  263. return "";
  264. } #}}}
  265. sub isinlinableimage ($) { #{{{
  266. my $file=shift;
  267. $file=~/\.(png|gif|jpg|jpeg)$/i;
  268. } #}}}
  269. sub pagetitle ($) { #{{{
  270. my $page=shift;
  271. $page=~s/__(\d+)__/&#$1;/g;
  272. $page=~y/_/ /;
  273. return $page;
  274. } #}}}
  275. sub titlepage ($) { #{{{
  276. my $title=shift;
  277. $title=~y/ /_/;
  278. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  279. return $title;
  280. } #}}}
  281. sub cgiurl (@) { #{{{
  282. my %params=@_;
  283. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  284. } #}}}
  285. sub baseurl (;$) { #{{{
  286. my $page=shift;
  287. return "$config{url}/" if ! defined $page;
  288. $page=~s/[^\/]+$//;
  289. $page=~s/[^\/]+\//..\//g;
  290. return $page;
  291. } #}}}
  292. sub abs2rel ($$) { #{{{
  293. # Work around very innefficient behavior in File::Spec if abs2rel
  294. # is passed two relative paths. It's much faster if paths are
  295. # absolute! (Debian bug #376658)
  296. my $path="/".shift;
  297. my $base="/".shift;
  298. require File::Spec;
  299. my $ret=File::Spec->abs2rel($path, $base);
  300. $ret=~s/^// if defined $ret;
  301. return $ret;
  302. } #}}}
  303. sub displaytime ($) { #{{{
  304. my $time=shift;
  305. eval q{use POSIX};
  306. # strftime doesn't know about encodings, so make sure
  307. # its output is properly treated as utf8
  308. return decode_utf8(POSIX::strftime(
  309. $config{timeformat}, localtime($time)));
  310. } #}}}
  311. sub htmllink ($$$;$$$) { #{{{
  312. my $lpage=shift; # the page doing the linking
  313. my $page=shift; # the page that will contain the link (different for inline)
  314. my $link=shift;
  315. my $noimageinline=shift; # don't turn links into inline html images
  316. my $forcesubpage=shift; # force a link to a subpage
  317. my $linktext=shift; # set to force the link text to something
  318. my $bestlink;
  319. if (! $forcesubpage) {
  320. $bestlink=bestlink($lpage, $link);
  321. }
  322. else {
  323. $bestlink="$lpage/".lc($link);
  324. }
  325. $linktext=pagetitle(basename($link)) unless defined $linktext;
  326. return "<span class=\"selflink\">$linktext</span>"
  327. if length $bestlink && $page eq $bestlink;
  328. # TODO BUG: %renderedfiles may not have it, if the linked to page
  329. # was also added and isn't yet rendered! Note that this bug is
  330. # masked by the bug that makes all new files be rendered twice.
  331. if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
  332. $bestlink=htmlpage($bestlink);
  333. }
  334. if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
  335. return "<span><a href=\"".
  336. cgiurl(do => "create", page => lc($link), from => $page).
  337. "\">?</a>$linktext</span>"
  338. }
  339. $bestlink=abs2rel($bestlink, dirname($page));
  340. if (! $noimageinline && isinlinableimage($bestlink)) {
  341. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  342. }
  343. return "<a href=\"$bestlink\">$linktext</a>";
  344. } #}}}
  345. sub htmlize ($$$) { #{{{
  346. my $page=shift;
  347. my $type=shift;
  348. my $content=shift;
  349. if (exists $hooks{htmlize}{$type}) {
  350. $content=$hooks{htmlize}{$type}{call}->(
  351. page => $page,
  352. content => $content,
  353. );
  354. }
  355. else {
  356. error("htmlization of $type not supported");
  357. }
  358. run_hooks(sanitize => sub {
  359. $content=shift->(
  360. page => $page,
  361. content => $content,
  362. );
  363. });
  364. return $content;
  365. } #}}}
  366. sub linkify ($$$) { #{{{
  367. my $lpage=shift; # the page containing the links
  368. my $page=shift; # the page the link will end up on (different for inline)
  369. my $content=shift;
  370. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  371. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
  372. : ( $1 ? "[[$3]]" : htmllink($lpage, $page, titlepage($3)))
  373. }eg;
  374. return $content;
  375. } #}}}
  376. my %preprocessing;
  377. sub preprocess ($$$) { #{{{
  378. my $page=shift; # the page the data comes from
  379. my $destpage=shift; # the page the data will appear in (different for inline)
  380. my $content=shift;
  381. my $handle=sub {
  382. my $escape=shift;
  383. my $command=shift;
  384. my $params=shift;
  385. if (length $escape) {
  386. return "[[$command $params]]";
  387. }
  388. elsif (exists $hooks{preprocess}{$command}) {
  389. # Note: preserve order of params, some plugins may
  390. # consider it significant.
  391. my @params;
  392. while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
  393. my $key=$1;
  394. my $val;
  395. if (defined $2) {
  396. $val=$2;
  397. $val=~s/\r\n/\n/mg;
  398. $val=~s/^\n+//g;
  399. $val=~s/\n+$//g;
  400. }
  401. elsif (defined $3) {
  402. $val=$3;
  403. }
  404. elsif (defined $4) {
  405. $val=$4;
  406. }
  407. if (defined $key) {
  408. push @params, $key, $val;
  409. }
  410. else {
  411. push @params, $val, '';
  412. }
  413. }
  414. if ($preprocessing{$page}++ > 3) {
  415. # Avoid loops of preprocessed pages preprocessing
  416. # other pages that preprocess them, etc.
  417. return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
  418. }
  419. my $ret=$hooks{preprocess}{$command}{call}->(
  420. @params,
  421. page => $page,
  422. destpage => $destpage,
  423. );
  424. $preprocessing{$page}--;
  425. return $ret;
  426. }
  427. else {
  428. return "[[$command $params]]";
  429. }
  430. };
  431. $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
  432. return $content;
  433. } #}}}
  434. sub filter ($$) {
  435. my $page=shift;
  436. my $content=shift;
  437. run_hooks(filter => sub {
  438. $content=shift->(page => $page, content => $content);
  439. });
  440. return $content;
  441. }
  442. sub indexlink () { #{{{
  443. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  444. } #}}}
  445. sub lockwiki () { #{{{
  446. # Take an exclusive lock on the wiki to prevent multiple concurrent
  447. # run issues. The lock will be dropped on program exit.
  448. if (! -d $config{wikistatedir}) {
  449. mkdir($config{wikistatedir});
  450. }
  451. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  452. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  453. if (! flock(WIKILOCK, 2 | 4)) {
  454. debug("wiki seems to be locked, waiting for lock");
  455. my $wait=600; # arbitrary, but don't hang forever to
  456. # prevent process pileup
  457. for (1..600) {
  458. return if flock(WIKILOCK, 2 | 4);
  459. sleep 1;
  460. }
  461. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  462. }
  463. } #}}}
  464. sub unlockwiki () { #{{{
  465. close WIKILOCK;
  466. } #}}}
  467. sub loadindex () { #{{{
  468. open (IN, "$config{wikistatedir}/index") || return;
  469. while (<IN>) {
  470. $_=possibly_foolish_untaint($_);
  471. chomp;
  472. my %items;
  473. $items{link}=[];
  474. $items{dest}=[];
  475. foreach my $i (split(/ /, $_)) {
  476. my ($item, $val)=split(/=/, $i, 2);
  477. push @{$items{$item}}, decode_entities($val);
  478. }
  479. next unless exists $items{src}; # skip bad lines for now
  480. my $page=pagename($items{src}[0]);
  481. if (! $config{rebuild}) {
  482. $pagesources{$page}=$items{src}[0];
  483. $oldpagemtime{$page}=$items{mtime}[0];
  484. $oldlinks{$page}=[@{$items{link}}];
  485. $links{$page}=[@{$items{link}}];
  486. $depends{$page}=$items{depends}[0] if exists $items{depends};
  487. $renderedfiles{$page}=[@{$items{dest}}];
  488. $oldrenderedfiles{$page}=[@{$items{dest}}];
  489. $pagecase{lc $page}=$page;
  490. }
  491. $pagectime{$page}=$items{ctime}[0];
  492. }
  493. close IN;
  494. } #}}}
  495. sub saveindex () { #{{{
  496. run_hooks(savestate => sub { shift->() });
  497. if (! -d $config{wikistatedir}) {
  498. mkdir($config{wikistatedir});
  499. }
  500. open (OUT, ">$config{wikistatedir}/index") ||
  501. error("cannot write to $config{wikistatedir}/index: $!");
  502. foreach my $page (keys %oldpagemtime) {
  503. next unless $oldpagemtime{$page};
  504. my $line="mtime=$oldpagemtime{$page} ".
  505. "ctime=$pagectime{$page} ".
  506. "src=$pagesources{$page}";
  507. $line.=" dest=$_" foreach @{$renderedfiles{$page}};
  508. $line.=" link=$_" foreach @{$links{$page}};
  509. if (exists $depends{$page}) {
  510. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  511. }
  512. print OUT $line."\n";
  513. }
  514. close OUT;
  515. } #}}}
  516. sub template_params (@) { #{{{
  517. my $filename=shift;
  518. require HTML::Template;
  519. return filter => sub {
  520. my $text_ref = shift;
  521. $$text_ref=&Encode::decode_utf8($$text_ref);
  522. },
  523. filename => "$config{templatedir}/$filename",
  524. loop_context_vars => 1,
  525. die_on_bad_params => 0,
  526. @_;
  527. } #}}}
  528. sub template ($;@) { #{{{
  529. HTML::Template->new(template_params(@_));
  530. } #}}}
  531. sub misctemplate ($$;@) { #{{{
  532. my $title=shift;
  533. my $pagebody=shift;
  534. my $template=template("misc.tmpl");
  535. $template->param(
  536. title => $title,
  537. indexlink => indexlink(),
  538. wikiname => $config{wikiname},
  539. pagebody => $pagebody,
  540. baseurl => baseurl(),
  541. @_,
  542. );
  543. run_hooks(pagetemplate => sub {
  544. shift->(page => "", destpage => "", template => $template);
  545. });
  546. return $template->output;
  547. }#}}}
  548. sub hook (@) { # {{{
  549. my %param=@_;
  550. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  551. error "hook requires type, call, and id parameters";
  552. }
  553. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  554. $hooks{$param{type}}{$param{id}}=\%param;
  555. } # }}}
  556. sub run_hooks ($$) { # {{{
  557. # Calls the given sub for each hook of the given type,
  558. # passing it the hook function to call.
  559. my $type=shift;
  560. my $sub=shift;
  561. if (exists $hooks{$type}) {
  562. foreach my $id (keys %{$hooks{$type}}) {
  563. $sub->($hooks{$type}{$id}{call});
  564. }
  565. }
  566. } #}}}
  567. sub globlist_to_pagespec ($) { #{{{
  568. my @globlist=split(' ', shift);
  569. my (@spec, @skip);
  570. foreach my $glob (@globlist) {
  571. if ($glob=~/^!(.*)/) {
  572. push @skip, $glob;
  573. }
  574. else {
  575. push @spec, $glob;
  576. }
  577. }
  578. my $spec=join(" or ", @spec);
  579. if (@skip) {
  580. my $skip=join(" and ", @skip);
  581. if (length $spec) {
  582. $spec="$skip and ($spec)";
  583. }
  584. else {
  585. $spec=$skip;
  586. }
  587. }
  588. return $spec;
  589. } #}}}
  590. sub is_globlist ($) { #{{{
  591. my $s=shift;
  592. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  593. } #}}}
  594. sub safequote ($) { #{{{
  595. my $s=shift;
  596. $s=~s/[{}]//g;
  597. return "q{$s}";
  598. } #}}}
  599. sub pagespec_merge ($$) { #{{{
  600. my $a=shift;
  601. my $b=shift;
  602. return $a if $a eq $b;
  603. # Support for old-style GlobLists.
  604. if (is_globlist($a)) {
  605. $a=globlist_to_pagespec($a);
  606. }
  607. if (is_globlist($b)) {
  608. $b=globlist_to_pagespec($b);
  609. }
  610. return "($a) or ($b)";
  611. } #}}}
  612. sub pagespec_translate ($) { #{{{
  613. # This assumes that $page is in scope in the function
  614. # that evalulates the translated pagespec code.
  615. my $spec=shift;
  616. # Support for old-style GlobLists.
  617. if (is_globlist($spec)) {
  618. $spec=globlist_to_pagespec($spec);
  619. }
  620. # Convert spec to perl code.
  621. my $code="";
  622. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  623. my $word=$1;
  624. if (lc $word eq "and") {
  625. $code.=" &&";
  626. }
  627. elsif (lc $word eq "or") {
  628. $code.=" ||";
  629. }
  630. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  631. $code.=" ".$word;
  632. }
  633. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  634. $code.=" match_$1(\$page, ".safequote($2).")";
  635. }
  636. else {
  637. $code.=" match_glob(\$page, ".safequote($word).")";
  638. }
  639. }
  640. return $code;
  641. } #}}}
  642. sub add_depends ($$) { #{{{
  643. my $page=shift;
  644. my $pagespec=shift;
  645. if (! exists $depends{$page}) {
  646. $depends{$page}=$pagespec;
  647. }
  648. else {
  649. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  650. }
  651. } # }}}
  652. sub pagespec_match ($$) { #{{{
  653. my $page=shift;
  654. my $spec=shift;
  655. return eval pagespec_translate($spec);
  656. } #}}}
  657. sub match_glob ($$) { #{{{
  658. my $page=shift;
  659. my $glob=shift;
  660. # turn glob into safe regexp
  661. $glob=quotemeta($glob);
  662. $glob=~s/\\\*/.*/g;
  663. $glob=~s/\\\?/./g;
  664. return $page=~/^$glob$/i;
  665. } #}}}
  666. sub match_link ($$) { #{{{
  667. my $page=shift;
  668. my $link=lc(shift);
  669. my $links = $links{$page} or return undef;
  670. foreach my $p (@$links) {
  671. return 1 if lc $p eq $link;
  672. }
  673. return 0;
  674. } #}}}
  675. sub match_backlink ($$) { #{{{
  676. match_link(pop, pop);
  677. } #}}}
  678. sub match_created_before ($$) { #{{{
  679. my $page=shift;
  680. my $testpage=shift;
  681. if (exists $pagectime{$testpage}) {
  682. return $pagectime{$page} < $pagectime{$testpage};
  683. }
  684. else {
  685. return 0;
  686. }
  687. } #}}}
  688. sub match_created_after ($$) { #{{{
  689. my $page=shift;
  690. my $testpage=shift;
  691. if (exists $pagectime{$testpage}) {
  692. return $pagectime{$page} > $pagectime{$testpage};
  693. }
  694. else {
  695. return 0;
  696. }
  697. } #}}}
  698. sub match_creation_day ($$) { #{{{
  699. return ((gmtime($pagectime{shift()}))[3] == shift);
  700. } #}}}
  701. sub match_creation_month ($$) { #{{{
  702. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  703. } #}}}
  704. sub match_creation_year ($$) { #{{{
  705. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  706. } #}}}
  707. 1