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