summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: b6a2a3649d83a608b1ea41e4d7d46695f0219460 (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. memoize("file_pruned");
  22. my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  23. our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
  24. sub defaultconfig () { #{{{
  25. wiki_file_prune_regexps => [qr/\.\./, qr/^\./, qr/\/\./, qr/\.x?html?$/,
  26. qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//],
  27. wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
  28. wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
  29. web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
  30. verbose => 0,
  31. syslog => 0,
  32. wikiname => "wiki",
  33. default_pageext => "mdwn",
  34. cgi => 0,
  35. rcs => '',
  36. notify => 0,
  37. url => '',
  38. cgiurl => '',
  39. historyurl => '',
  40. diffurl => '',
  41. anonok => 0,
  42. rss => 0,
  43. atom => 0,
  44. discussion => 1,
  45. rebuild => 0,
  46. refresh => 0,
  47. getctime => 0,
  48. w3mmode => 0,
  49. wrapper => undef,
  50. wrappermode => undef,
  51. svnrepo => undef,
  52. svnpath => "trunk",
  53. srcdir => undef,
  54. destdir => undef,
  55. pingurl => [],
  56. templatedir => "$installdir/share/ikiwiki/templates",
  57. underlaydir => "$installdir/share/ikiwiki/basewiki",
  58. setup => undef,
  59. adminuser => undef,
  60. adminemail => undef,
  61. plugin => [qw{mdwn inline htmlscrubber passwordauth}],
  62. timeformat => '%c',
  63. locale => undef,
  64. sslcookie => 0,
  65. httpauth => 0,
  66. userdir => "",
  67. } #}}}
  68. sub checkconfig () { #{{{
  69. # locale stuff; avoid LC_ALL since it overrides everything
  70. if (defined $ENV{LC_ALL}) {
  71. $ENV{LANG} = $ENV{LC_ALL};
  72. delete $ENV{LC_ALL};
  73. }
  74. if (defined $config{locale}) {
  75. eval q{use POSIX};
  76. error($@) if $@;
  77. $ENV{LANG} = $config{locale}
  78. if POSIX::setlocale(&POSIX::LC_TIME, $config{locale});
  79. }
  80. if ($config{w3mmode}) {
  81. eval q{use Cwd q{abs_path}};
  82. error($@) if $@;
  83. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  84. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  85. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  86. unless $config{cgiurl} =~ m!file:///!;
  87. $config{url}="file://".$config{destdir};
  88. }
  89. if ($config{cgi} && ! length $config{url}) {
  90. error("Must specify url to wiki with --url when using --cgi\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. if ($link=~s/^\/+//) {
  257. # absolute links
  258. $cwd="";
  259. }
  260. do {
  261. my $l=$cwd;
  262. $l.="/" if length $l;
  263. $l.=$link;
  264. if (exists $links{$l}) {
  265. return $l;
  266. }
  267. elsif (exists $pagecase{lc $l}) {
  268. return $pagecase{lc $l};
  269. }
  270. } while $cwd=~s!/?[^/]+$!!;
  271. #print STDERR "warning: page $page, broken link: $link\n";
  272. return "";
  273. } #}}}
  274. sub isinlinableimage ($) { #{{{
  275. my $file=shift;
  276. $file=~/\.(png|gif|jpg|jpeg)$/i;
  277. } #}}}
  278. sub pagetitle ($;$) { #{{{
  279. my $page=shift;
  280. my $unescaped=shift;
  281. if ($unescaped) {
  282. $page=~s/__(\d+)__/chr($1)/eg;
  283. }
  284. else {
  285. $page=~s/__(\d+)__/&#$1;/g;
  286. }
  287. $page=~y/_/ /;
  288. return $page;
  289. } #}}}
  290. sub titlepage ($) { #{{{
  291. my $title=shift;
  292. $title=~y/ /_/;
  293. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  294. return $title;
  295. } #}}}
  296. sub cgiurl (@) { #{{{
  297. my %params=@_;
  298. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  299. } #}}}
  300. sub baseurl (;$) { #{{{
  301. my $page=shift;
  302. return "$config{url}/" if ! defined $page;
  303. $page=~s/[^\/]+$//;
  304. $page=~s/[^\/]+\//..\//g;
  305. return $page;
  306. } #}}}
  307. sub abs2rel ($$) { #{{{
  308. # Work around very innefficient behavior in File::Spec if abs2rel
  309. # is passed two relative paths. It's much faster if paths are
  310. # absolute! (Debian bug #376658; fixed in debian unstable now)
  311. my $path="/".shift;
  312. my $base="/".shift;
  313. require File::Spec;
  314. my $ret=File::Spec->abs2rel($path, $base);
  315. $ret=~s/^// if defined $ret;
  316. return $ret;
  317. } #}}}
  318. sub displaytime ($) { #{{{
  319. my $time=shift;
  320. eval q{use POSIX};
  321. error($@) if $@;
  322. # strftime doesn't know about encodings, so make sure
  323. # its output is properly treated as utf8
  324. return decode_utf8(POSIX::strftime(
  325. $config{timeformat}, localtime($time)));
  326. } #}}}
  327. sub htmllink ($$$;$$$) { #{{{
  328. my $lpage=shift; # the page doing the linking
  329. my $page=shift; # the page that will contain the link (different for inline)
  330. my $link=shift;
  331. my $noimageinline=shift; # don't turn links into inline html images
  332. my $forcesubpage=shift; # force a link to a subpage
  333. my $linktext=shift; # set to force the link text to something
  334. my $bestlink;
  335. if (! $forcesubpage) {
  336. $bestlink=bestlink($lpage, $link);
  337. }
  338. else {
  339. $bestlink="$lpage/".lc($link);
  340. }
  341. $linktext=pagetitle(basename($link)) unless defined $linktext;
  342. return "<span class=\"selflink\">$linktext</span>"
  343. if length $bestlink && $page eq $bestlink;
  344. if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
  345. $bestlink=htmlpage($bestlink);
  346. }
  347. if (! grep { $_ eq $bestlink } map { @{$_} } values %renderedfiles) {
  348. return $linktext unless length $config{cgiurl};
  349. return "<span><a href=\"".
  350. cgiurl(do => "create", page => lc($link), from => $page).
  351. "\">?</a>$linktext</span>"
  352. }
  353. $bestlink=abs2rel($bestlink, dirname($page));
  354. if (! $noimageinline && isinlinableimage($bestlink)) {
  355. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  356. }
  357. return "<a href=\"$bestlink\">$linktext</a>";
  358. } #}}}
  359. sub htmlize ($$$) { #{{{
  360. my $page=shift;
  361. my $type=shift;
  362. my $content=shift;
  363. if (exists $hooks{htmlize}{$type}) {
  364. $content=$hooks{htmlize}{$type}{call}->(
  365. page => $page,
  366. content => $content,
  367. );
  368. }
  369. else {
  370. error("htmlization of $type not supported");
  371. }
  372. run_hooks(sanitize => sub {
  373. $content=shift->(
  374. page => $page,
  375. content => $content,
  376. );
  377. });
  378. return $content;
  379. } #}}}
  380. sub linkify ($$$) { #{{{
  381. my $lpage=shift; # the page containing the links
  382. my $page=shift; # the page the link will end up on (different for inline)
  383. my $content=shift;
  384. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  385. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
  386. : ( $1 ? "[[$3]]" : htmllink($lpage, $page, titlepage($3)))
  387. }eg;
  388. return $content;
  389. } #}}}
  390. my %preprocessing;
  391. sub preprocess ($$$;$) { #{{{
  392. my $page=shift; # the page the data comes from
  393. my $destpage=shift; # the page the data will appear in (different for inline)
  394. my $content=shift;
  395. my $scan=shift;
  396. my $handle=sub {
  397. my $escape=shift;
  398. my $command=shift;
  399. my $params=shift;
  400. if (length $escape) {
  401. return "[[$command $params]]";
  402. }
  403. elsif (exists $hooks{preprocess}{$command}) {
  404. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  405. # Note: preserve order of params, some plugins may
  406. # consider it significant.
  407. my @params;
  408. while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
  409. my $key=$1;
  410. my $val;
  411. if (defined $2) {
  412. $val=$2;
  413. $val=~s/\r\n/\n/mg;
  414. $val=~s/^\n+//g;
  415. $val=~s/\n+$//g;
  416. }
  417. elsif (defined $3) {
  418. $val=$3;
  419. }
  420. elsif (defined $4) {
  421. $val=$4;
  422. }
  423. if (defined $key) {
  424. push @params, $key, $val;
  425. }
  426. else {
  427. push @params, $val, '';
  428. }
  429. }
  430. if ($preprocessing{$page}++ > 3) {
  431. # Avoid loops of preprocessed pages preprocessing
  432. # other pages that preprocess them, etc.
  433. return "[[$command preprocessing loop detected on $page at depth $preprocessing{$page}]]";
  434. }
  435. my $ret=$hooks{preprocess}{$command}{call}->(
  436. @params,
  437. page => $page,
  438. destpage => $destpage,
  439. );
  440. $preprocessing{$page}--;
  441. return $ret;
  442. }
  443. else {
  444. return "[[$command $params]]";
  445. }
  446. };
  447. $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
  448. return $content;
  449. } #}}}
  450. sub filter ($$) { #{{{
  451. my $page=shift;
  452. my $content=shift;
  453. run_hooks(filter => sub {
  454. $content=shift->(page => $page, content => $content);
  455. });
  456. return $content;
  457. } #}}}
  458. sub indexlink () { #{{{
  459. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  460. } #}}}
  461. sub lockwiki () { #{{{
  462. # Take an exclusive lock on the wiki to prevent multiple concurrent
  463. # run issues. The lock will be dropped on program exit.
  464. if (! -d $config{wikistatedir}) {
  465. mkdir($config{wikistatedir});
  466. }
  467. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  468. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  469. if (! flock(WIKILOCK, 2 | 4)) {
  470. debug("wiki seems to be locked, waiting for lock");
  471. my $wait=600; # arbitrary, but don't hang forever to
  472. # prevent process pileup
  473. for (1..600) {
  474. return if flock(WIKILOCK, 2 | 4);
  475. sleep 1;
  476. }
  477. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  478. }
  479. } #}}}
  480. sub unlockwiki () { #{{{
  481. close WIKILOCK;
  482. } #}}}
  483. sub loadindex () { #{{{
  484. open (IN, "$config{wikistatedir}/index") || return;
  485. while (<IN>) {
  486. $_=possibly_foolish_untaint($_);
  487. chomp;
  488. my %items;
  489. $items{link}=[];
  490. $items{dest}=[];
  491. foreach my $i (split(/ /, $_)) {
  492. my ($item, $val)=split(/=/, $i, 2);
  493. push @{$items{$item}}, decode_entities($val);
  494. }
  495. next unless exists $items{src}; # skip bad lines for now
  496. my $page=pagename($items{src}[0]);
  497. if (! $config{rebuild}) {
  498. $pagesources{$page}=$items{src}[0];
  499. $oldpagemtime{$page}=$items{mtime}[0];
  500. $oldlinks{$page}=[@{$items{link}}];
  501. $links{$page}=[@{$items{link}}];
  502. $depends{$page}=$items{depends}[0] if exists $items{depends};
  503. $renderedfiles{$page}=[@{$items{dest}}];
  504. $oldrenderedfiles{$page}=[@{$items{dest}}];
  505. $pagecase{lc $page}=$page;
  506. }
  507. $pagectime{$page}=$items{ctime}[0];
  508. }
  509. close IN;
  510. } #}}}
  511. sub saveindex () { #{{{
  512. run_hooks(savestate => sub { shift->() });
  513. if (! -d $config{wikistatedir}) {
  514. mkdir($config{wikistatedir});
  515. }
  516. open (OUT, ">$config{wikistatedir}/index") ||
  517. error("cannot write to $config{wikistatedir}/index: $!");
  518. foreach my $page (keys %oldpagemtime) {
  519. next unless $oldpagemtime{$page};
  520. my $line="mtime=$oldpagemtime{$page} ".
  521. "ctime=$pagectime{$page} ".
  522. "src=$pagesources{$page}";
  523. $line.=" dest=$_" foreach @{$renderedfiles{$page}};
  524. my %count;
  525. $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
  526. if (exists $depends{$page}) {
  527. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  528. }
  529. print OUT $line."\n";
  530. }
  531. close OUT;
  532. } #}}}
  533. sub template_params (@) { #{{{
  534. my $filename=shift;
  535. require HTML::Template;
  536. return filter => sub {
  537. my $text_ref = shift;
  538. $$text_ref=&Encode::decode_utf8($$text_ref);
  539. },
  540. filename => "$config{templatedir}/$filename",
  541. loop_context_vars => 1,
  542. die_on_bad_params => 0,
  543. @_;
  544. } #}}}
  545. sub template ($;@) { #{{{
  546. HTML::Template->new(template_params(@_));
  547. } #}}}
  548. sub misctemplate ($$;@) { #{{{
  549. my $title=shift;
  550. my $pagebody=shift;
  551. my $template=template("misc.tmpl");
  552. $template->param(
  553. title => $title,
  554. indexlink => indexlink(),
  555. wikiname => $config{wikiname},
  556. pagebody => $pagebody,
  557. baseurl => baseurl(),
  558. @_,
  559. );
  560. run_hooks(pagetemplate => sub {
  561. shift->(page => "", destpage => "", template => $template);
  562. });
  563. return $template->output;
  564. }#}}}
  565. sub hook (@) { # {{{
  566. my %param=@_;
  567. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  568. error "hook requires type, call, and id parameters";
  569. }
  570. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  571. $hooks{$param{type}}{$param{id}}=\%param;
  572. } # }}}
  573. sub run_hooks ($$) { # {{{
  574. # Calls the given sub for each hook of the given type,
  575. # passing it the hook function to call.
  576. my $type=shift;
  577. my $sub=shift;
  578. if (exists $hooks{$type}) {
  579. my @deferred;
  580. foreach my $id (keys %{$hooks{$type}}) {
  581. if ($hooks{$type}{$id}{last}) {
  582. push @deferred, $id;
  583. next;
  584. }
  585. $sub->($hooks{$type}{$id}{call});
  586. }
  587. foreach my $id (@deferred) {
  588. $sub->($hooks{$type}{$id}{call});
  589. }
  590. }
  591. } #}}}
  592. sub globlist_to_pagespec ($) { #{{{
  593. my @globlist=split(' ', shift);
  594. my (@spec, @skip);
  595. foreach my $glob (@globlist) {
  596. if ($glob=~/^!(.*)/) {
  597. push @skip, $glob;
  598. }
  599. else {
  600. push @spec, $glob;
  601. }
  602. }
  603. my $spec=join(" or ", @spec);
  604. if (@skip) {
  605. my $skip=join(" and ", @skip);
  606. if (length $spec) {
  607. $spec="$skip and ($spec)";
  608. }
  609. else {
  610. $spec=$skip;
  611. }
  612. }
  613. return $spec;
  614. } #}}}
  615. sub is_globlist ($) { #{{{
  616. my $s=shift;
  617. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  618. } #}}}
  619. sub safequote ($) { #{{{
  620. my $s=shift;
  621. $s=~s/[{}]//g;
  622. return "q{$s}";
  623. } #}}}
  624. sub pagespec_merge ($$) { #{{{
  625. my $a=shift;
  626. my $b=shift;
  627. return $a if $a eq $b;
  628. # Support for old-style GlobLists.
  629. if (is_globlist($a)) {
  630. $a=globlist_to_pagespec($a);
  631. }
  632. if (is_globlist($b)) {
  633. $b=globlist_to_pagespec($b);
  634. }
  635. return "($a) or ($b)";
  636. } #}}}
  637. sub pagespec_translate ($) { #{{{
  638. # This assumes that $page is in scope in the function
  639. # that evalulates the translated pagespec code.
  640. my $spec=shift;
  641. # Support for old-style GlobLists.
  642. if (is_globlist($spec)) {
  643. $spec=globlist_to_pagespec($spec);
  644. }
  645. # Convert spec to perl code.
  646. my $code="";
  647. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  648. my $word=$1;
  649. if (lc $word eq "and") {
  650. $code.=" &&";
  651. }
  652. elsif (lc $word eq "or") {
  653. $code.=" ||";
  654. }
  655. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  656. $code.=" ".$word;
  657. }
  658. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  659. $code.=" match_$1(\$page, ".safequote($2).")";
  660. }
  661. else {
  662. $code.=" match_glob(\$page, ".safequote($word).")";
  663. }
  664. }
  665. return $code;
  666. } #}}}
  667. sub add_depends ($$) { #{{{
  668. my $page=shift;
  669. my $pagespec=shift;
  670. if (! exists $depends{$page}) {
  671. $depends{$page}=$pagespec;
  672. }
  673. else {
  674. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  675. }
  676. } # }}}
  677. sub file_pruned ($$) { #{{{
  678. require File::Spec;
  679. my $file=File::Spec->canonpath(shift);
  680. my $base=File::Spec->canonpath(shift);
  681. $file=~s#^\Q$base\E/*##;
  682. my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  683. $file =~ m/$regexp/;
  684. } #}}}
  685. sub pagespec_match ($$) { #{{{
  686. my $page=shift;
  687. my $spec=shift;
  688. return eval pagespec_translate($spec);
  689. } #}}}
  690. sub match_glob ($$) { #{{{
  691. my $page=shift;
  692. my $glob=shift;
  693. # turn glob into safe regexp
  694. $glob=quotemeta($glob);
  695. $glob=~s/\\\*/.*/g;
  696. $glob=~s/\\\?/./g;
  697. return $page=~/^$glob$/i;
  698. } #}}}
  699. sub match_link ($$) { #{{{
  700. my $page=shift;
  701. my $link=lc(shift);
  702. my $links = $links{$page} or return undef;
  703. foreach my $p (@$links) {
  704. return 1 if lc $p eq $link;
  705. }
  706. return 0;
  707. } #}}}
  708. sub match_backlink ($$) { #{{{
  709. match_link(pop, pop);
  710. } #}}}
  711. sub match_created_before ($$) { #{{{
  712. my $page=shift;
  713. my $testpage=shift;
  714. if (exists $pagectime{$testpage}) {
  715. return $pagectime{$page} < $pagectime{$testpage};
  716. }
  717. else {
  718. return 0;
  719. }
  720. } #}}}
  721. sub match_created_after ($$) { #{{{
  722. my $page=shift;
  723. my $testpage=shift;
  724. if (exists $pagectime{$testpage}) {
  725. return $pagectime{$page} > $pagectime{$testpage};
  726. }
  727. else {
  728. return 0;
  729. }
  730. } #}}}
  731. sub match_creation_day ($$) { #{{{
  732. return ((gmtime($pagectime{shift()}))[3] == shift);
  733. } #}}}
  734. sub match_creation_month ($$) { #{{{
  735. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  736. } #}}}
  737. sub match_creation_year ($$) { #{{{
  738. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  739. } #}}}
  740. 1