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