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