summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 73bc747f9b443c6aee916b057b3f21c9d61c3c66 (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. my %count;
  511. $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
  512. if (exists $depends{$page}) {
  513. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  514. }
  515. print OUT $line."\n";
  516. }
  517. close OUT;
  518. } #}}}
  519. sub template_params (@) { #{{{
  520. my $filename=shift;
  521. require HTML::Template;
  522. return filter => sub {
  523. my $text_ref = shift;
  524. $$text_ref=&Encode::decode_utf8($$text_ref);
  525. },
  526. filename => "$config{templatedir}/$filename",
  527. loop_context_vars => 1,
  528. die_on_bad_params => 0,
  529. @_;
  530. } #}}}
  531. sub template ($;@) { #{{{
  532. HTML::Template->new(template_params(@_));
  533. } #}}}
  534. sub misctemplate ($$;@) { #{{{
  535. my $title=shift;
  536. my $pagebody=shift;
  537. my $template=template("misc.tmpl");
  538. $template->param(
  539. title => $title,
  540. indexlink => indexlink(),
  541. wikiname => $config{wikiname},
  542. pagebody => $pagebody,
  543. baseurl => baseurl(),
  544. @_,
  545. );
  546. run_hooks(pagetemplate => sub {
  547. shift->(page => "", destpage => "", template => $template);
  548. });
  549. return $template->output;
  550. }#}}}
  551. sub hook (@) { # {{{
  552. my %param=@_;
  553. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  554. error "hook requires type, call, and id parameters";
  555. }
  556. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  557. $hooks{$param{type}}{$param{id}}=\%param;
  558. } # }}}
  559. sub run_hooks ($$) { # {{{
  560. # Calls the given sub for each hook of the given type,
  561. # passing it the hook function to call.
  562. my $type=shift;
  563. my $sub=shift;
  564. if (exists $hooks{$type}) {
  565. foreach my $id (keys %{$hooks{$type}}) {
  566. $sub->($hooks{$type}{$id}{call});
  567. }
  568. }
  569. } #}}}
  570. sub globlist_to_pagespec ($) { #{{{
  571. my @globlist=split(' ', shift);
  572. my (@spec, @skip);
  573. foreach my $glob (@globlist) {
  574. if ($glob=~/^!(.*)/) {
  575. push @skip, $glob;
  576. }
  577. else {
  578. push @spec, $glob;
  579. }
  580. }
  581. my $spec=join(" or ", @spec);
  582. if (@skip) {
  583. my $skip=join(" and ", @skip);
  584. if (length $spec) {
  585. $spec="$skip and ($spec)";
  586. }
  587. else {
  588. $spec=$skip;
  589. }
  590. }
  591. return $spec;
  592. } #}}}
  593. sub is_globlist ($) { #{{{
  594. my $s=shift;
  595. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  596. } #}}}
  597. sub safequote ($) { #{{{
  598. my $s=shift;
  599. $s=~s/[{}]//g;
  600. return "q{$s}";
  601. } #}}}
  602. sub pagespec_merge ($$) { #{{{
  603. my $a=shift;
  604. my $b=shift;
  605. return $a if $a eq $b;
  606. # Support for old-style GlobLists.
  607. if (is_globlist($a)) {
  608. $a=globlist_to_pagespec($a);
  609. }
  610. if (is_globlist($b)) {
  611. $b=globlist_to_pagespec($b);
  612. }
  613. return "($a) or ($b)";
  614. } #}}}
  615. sub pagespec_translate ($) { #{{{
  616. # This assumes that $page is in scope in the function
  617. # that evalulates the translated pagespec code.
  618. my $spec=shift;
  619. # Support for old-style GlobLists.
  620. if (is_globlist($spec)) {
  621. $spec=globlist_to_pagespec($spec);
  622. }
  623. # Convert spec to perl code.
  624. my $code="";
  625. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  626. my $word=$1;
  627. if (lc $word eq "and") {
  628. $code.=" &&";
  629. }
  630. elsif (lc $word eq "or") {
  631. $code.=" ||";
  632. }
  633. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  634. $code.=" ".$word;
  635. }
  636. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  637. $code.=" match_$1(\$page, ".safequote($2).")";
  638. }
  639. else {
  640. $code.=" match_glob(\$page, ".safequote($word).")";
  641. }
  642. }
  643. return $code;
  644. } #}}}
  645. sub add_depends ($$) { #{{{
  646. my $page=shift;
  647. my $pagespec=shift;
  648. if (! exists $depends{$page}) {
  649. $depends{$page}=$pagespec;
  650. }
  651. else {
  652. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  653. }
  654. } # }}}
  655. sub pagespec_match ($$) { #{{{
  656. my $page=shift;
  657. my $spec=shift;
  658. return eval pagespec_translate($spec);
  659. } #}}}
  660. sub match_glob ($$) { #{{{
  661. my $page=shift;
  662. my $glob=shift;
  663. # turn glob into safe regexp
  664. $glob=quotemeta($glob);
  665. $glob=~s/\\\*/.*/g;
  666. $glob=~s/\\\?/./g;
  667. return $page=~/^$glob$/i;
  668. } #}}}
  669. sub match_link ($$) { #{{{
  670. my $page=shift;
  671. my $link=lc(shift);
  672. my $links = $links{$page} or return undef;
  673. foreach my $p (@$links) {
  674. return 1 if lc $p eq $link;
  675. }
  676. return 0;
  677. } #}}}
  678. sub match_backlink ($$) { #{{{
  679. match_link(pop, pop);
  680. } #}}}
  681. sub match_created_before ($$) { #{{{
  682. my $page=shift;
  683. my $testpage=shift;
  684. if (exists $pagectime{$testpage}) {
  685. return $pagectime{$page} < $pagectime{$testpage};
  686. }
  687. else {
  688. return 0;
  689. }
  690. } #}}}
  691. sub match_created_after ($$) { #{{{
  692. my $page=shift;
  693. my $testpage=shift;
  694. if (exists $pagectime{$testpage}) {
  695. return $pagectime{$page} > $pagectime{$testpage};
  696. }
  697. else {
  698. return 0;
  699. }
  700. } #}}}
  701. sub match_creation_day ($$) { #{{{
  702. return ((gmtime($pagectime{shift()}))[3] == shift);
  703. } #}}}
  704. sub match_creation_month ($$) { #{{{
  705. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  706. } #}}}
  707. sub match_creation_year ($$) { #{{{
  708. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  709. } #}}}
  710. 1