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