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