summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: a6869d454cb90389e22a3d60adca3d0507057094 (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 $scan=shift;
  382. my $handle=sub {
  383. my $escape=shift;
  384. my $command=shift;
  385. my $params=shift;
  386. if (length $escape) {
  387. return "[[$command $params]]";
  388. }
  389. elsif (exists $hooks{preprocess}{$command}) {
  390. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  391. # Note: preserve order of params, some plugins may
  392. # consider it significant.
  393. my @params;
  394. while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
  395. my $key=$1;
  396. my $val;
  397. if (defined $2) {
  398. $val=$2;
  399. $val=~s/\r\n/\n/mg;
  400. $val=~s/^\n+//g;
  401. $val=~s/\n+$//g;
  402. }
  403. elsif (defined $3) {
  404. $val=$3;
  405. }
  406. elsif (defined $4) {
  407. $val=$4;
  408. }
  409. if (defined $key) {
  410. push @params, $key, $val;
  411. }
  412. else {
  413. push @params, $val, '';
  414. }
  415. }
  416. if ($preprocessing{$page}++ > 3) {
  417. # Avoid loops of preprocessed pages preprocessing
  418. # other pages that preprocess them, etc.
  419. return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
  420. }
  421. my $ret=$hooks{preprocess}{$command}{call}->(
  422. @params,
  423. page => $page,
  424. destpage => $destpage,
  425. );
  426. $preprocessing{$page}--;
  427. return $ret;
  428. }
  429. else {
  430. return "[[$command $params]]";
  431. }
  432. };
  433. $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
  434. return $content;
  435. } #}}}
  436. sub filter ($$) {
  437. my $page=shift;
  438. my $content=shift;
  439. run_hooks(filter => sub {
  440. $content=shift->(page => $page, content => $content);
  441. });
  442. return $content;
  443. }
  444. sub indexlink () { #{{{
  445. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  446. } #}}}
  447. sub lockwiki () { #{{{
  448. # Take an exclusive lock on the wiki to prevent multiple concurrent
  449. # run issues. The lock will be dropped on program exit.
  450. if (! -d $config{wikistatedir}) {
  451. mkdir($config{wikistatedir});
  452. }
  453. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  454. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  455. if (! flock(WIKILOCK, 2 | 4)) {
  456. debug("wiki seems to be locked, waiting for lock");
  457. my $wait=600; # arbitrary, but don't hang forever to
  458. # prevent process pileup
  459. for (1..600) {
  460. return if flock(WIKILOCK, 2 | 4);
  461. sleep 1;
  462. }
  463. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  464. }
  465. } #}}}
  466. sub unlockwiki () { #{{{
  467. close WIKILOCK;
  468. } #}}}
  469. sub loadindex () { #{{{
  470. open (IN, "$config{wikistatedir}/index") || return;
  471. while (<IN>) {
  472. $_=possibly_foolish_untaint($_);
  473. chomp;
  474. my %items;
  475. $items{link}=[];
  476. $items{dest}=[];
  477. foreach my $i (split(/ /, $_)) {
  478. my ($item, $val)=split(/=/, $i, 2);
  479. push @{$items{$item}}, decode_entities($val);
  480. }
  481. next unless exists $items{src}; # skip bad lines for now
  482. my $page=pagename($items{src}[0]);
  483. if (! $config{rebuild}) {
  484. $pagesources{$page}=$items{src}[0];
  485. $oldpagemtime{$page}=$items{mtime}[0];
  486. $oldlinks{$page}=[@{$items{link}}];
  487. $links{$page}=[@{$items{link}}];
  488. $depends{$page}=$items{depends}[0] if exists $items{depends};
  489. $renderedfiles{$page}=[@{$items{dest}}];
  490. $oldrenderedfiles{$page}=[@{$items{dest}}];
  491. $pagecase{lc $page}=$page;
  492. }
  493. $pagectime{$page}=$items{ctime}[0];
  494. }
  495. close IN;
  496. } #}}}
  497. sub saveindex () { #{{{
  498. run_hooks(savestate => sub { shift->() });
  499. if (! -d $config{wikistatedir}) {
  500. mkdir($config{wikistatedir});
  501. }
  502. open (OUT, ">$config{wikistatedir}/index") ||
  503. error("cannot write to $config{wikistatedir}/index: $!");
  504. foreach my $page (keys %oldpagemtime) {
  505. next unless $oldpagemtime{$page};
  506. my $line="mtime=$oldpagemtime{$page} ".
  507. "ctime=$pagectime{$page} ".
  508. "src=$pagesources{$page}";
  509. $line.=" dest=$_" foreach @{$renderedfiles{$page}};
  510. $line.=" link=$_" foreach @{$links{$page}};
  511. if (exists $depends{$page}) {
  512. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  513. }
  514. print OUT $line."\n";
  515. }
  516. close OUT;
  517. } #}}}
  518. sub template_params (@) { #{{{
  519. my $filename=shift;
  520. require HTML::Template;
  521. return filter => sub {
  522. my $text_ref = shift;
  523. $$text_ref=&Encode::decode_utf8($$text_ref);
  524. },
  525. filename => "$config{templatedir}/$filename",
  526. loop_context_vars => 1,
  527. die_on_bad_params => 0,
  528. @_;
  529. } #}}}
  530. sub template ($;@) { #{{{
  531. HTML::Template->new(template_params(@_));
  532. } #}}}
  533. sub misctemplate ($$;@) { #{{{
  534. my $title=shift;
  535. my $pagebody=shift;
  536. my $template=template("misc.tmpl");
  537. $template->param(
  538. title => $title,
  539. indexlink => indexlink(),
  540. wikiname => $config{wikiname},
  541. pagebody => $pagebody,
  542. baseurl => baseurl(),
  543. @_,
  544. );
  545. run_hooks(pagetemplate => sub {
  546. shift->(page => "", destpage => "", template => $template);
  547. });
  548. return $template->output;
  549. }#}}}
  550. sub hook (@) { # {{{
  551. my %param=@_;
  552. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  553. error "hook requires type, call, and id parameters";
  554. }
  555. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  556. $hooks{$param{type}}{$param{id}}=\%param;
  557. } # }}}
  558. sub run_hooks ($$) { # {{{
  559. # Calls the given sub for each hook of the given type,
  560. # passing it the hook function to call.
  561. my $type=shift;
  562. my $sub=shift;
  563. if (exists $hooks{$type}) {
  564. foreach my $id (keys %{$hooks{$type}}) {
  565. $sub->($hooks{$type}{$id}{call});
  566. }
  567. }
  568. } #}}}
  569. sub globlist_to_pagespec ($) { #{{{
  570. my @globlist=split(' ', shift);
  571. my (@spec, @skip);
  572. foreach my $glob (@globlist) {
  573. if ($glob=~/^!(.*)/) {
  574. push @skip, $glob;
  575. }
  576. else {
  577. push @spec, $glob;
  578. }
  579. }
  580. my $spec=join(" or ", @spec);
  581. if (@skip) {
  582. my $skip=join(" and ", @skip);
  583. if (length $spec) {
  584. $spec="$skip and ($spec)";
  585. }
  586. else {
  587. $spec=$skip;
  588. }
  589. }
  590. return $spec;
  591. } #}}}
  592. sub is_globlist ($) { #{{{
  593. my $s=shift;
  594. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  595. } #}}}
  596. sub safequote ($) { #{{{
  597. my $s=shift;
  598. $s=~s/[{}]//g;
  599. return "q{$s}";
  600. } #}}}
  601. sub pagespec_merge ($$) { #{{{
  602. my $a=shift;
  603. my $b=shift;
  604. return $a if $a eq $b;
  605. # Support for old-style GlobLists.
  606. if (is_globlist($a)) {
  607. $a=globlist_to_pagespec($a);
  608. }
  609. if (is_globlist($b)) {
  610. $b=globlist_to_pagespec($b);
  611. }
  612. return "($a) or ($b)";
  613. } #}}}
  614. sub pagespec_translate ($) { #{{{
  615. # This assumes that $page is in scope in the function
  616. # that evalulates the translated pagespec code.
  617. my $spec=shift;
  618. # Support for old-style GlobLists.
  619. if (is_globlist($spec)) {
  620. $spec=globlist_to_pagespec($spec);
  621. }
  622. # Convert spec to perl code.
  623. my $code="";
  624. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  625. my $word=$1;
  626. if (lc $word eq "and") {
  627. $code.=" &&";
  628. }
  629. elsif (lc $word eq "or") {
  630. $code.=" ||";
  631. }
  632. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  633. $code.=" ".$word;
  634. }
  635. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  636. $code.=" match_$1(\$page, ".safequote($2).")";
  637. }
  638. else {
  639. $code.=" match_glob(\$page, ".safequote($word).")";
  640. }
  641. }
  642. return $code;
  643. } #}}}
  644. sub add_depends ($$) { #{{{
  645. my $page=shift;
  646. my $pagespec=shift;
  647. if (! exists $depends{$page}) {
  648. $depends{$page}=$pagespec;
  649. }
  650. else {
  651. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  652. }
  653. } # }}}
  654. sub pagespec_match ($$) { #{{{
  655. my $page=shift;
  656. my $spec=shift;
  657. return eval pagespec_translate($spec);
  658. } #}}}
  659. sub match_glob ($$) { #{{{
  660. my $page=shift;
  661. my $glob=shift;
  662. # turn glob into safe regexp
  663. $glob=quotemeta($glob);
  664. $glob=~s/\\\*/.*/g;
  665. $glob=~s/\\\?/./g;
  666. return $page=~/^$glob$/i;
  667. } #}}}
  668. sub match_link ($$) { #{{{
  669. my $page=shift;
  670. my $link=lc(shift);
  671. my $links = $links{$page} or return undef;
  672. foreach my $p (@$links) {
  673. return 1 if lc $p eq $link;
  674. }
  675. return 0;
  676. } #}}}
  677. sub match_backlink ($$) { #{{{
  678. match_link(pop, pop);
  679. } #}}}
  680. sub match_created_before ($$) { #{{{
  681. my $page=shift;
  682. my $testpage=shift;
  683. if (exists $pagectime{$testpage}) {
  684. return $pagectime{$page} < $pagectime{$testpage};
  685. }
  686. else {
  687. return 0;
  688. }
  689. } #}}}
  690. sub match_created_after ($$) { #{{{
  691. my $page=shift;
  692. my $testpage=shift;
  693. if (exists $pagectime{$testpage}) {
  694. return $pagectime{$page} > $pagectime{$testpage};
  695. }
  696. else {
  697. return 0;
  698. }
  699. } #}}}
  700. sub match_creation_day ($$) { #{{{
  701. return ((gmtime($pagectime{shift()}))[3] == shift);
  702. } #}}}
  703. sub match_creation_month ($$) { #{{{
  704. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  705. } #}}}
  706. sub match_creation_year ($$) { #{{{
  707. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  708. } #}}}
  709. 1