summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 8172552cbe147d830d51695f5c1a2149a8274b2d (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use HTML::Entities;
  7. use URI::Escape q{uri_escape_utf8};
  8. use POSIX;
  9. use open qw{:utf8 :std};
  10. use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
  11. %pagestate %renderedfiles %oldrenderedfiles %pagesources
  12. %destsources %depends %hooks %forcerebuild $gettext_obj};
  13. use Exporter q{import};
  14. our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
  15. bestlink htmllink readfile writefile pagetype srcfile pagename
  16. displaytime will_render gettext urlto targetpage
  17. add_underlay
  18. %config %links %pagestate %renderedfiles
  19. %pagesources %destsources);
  20. our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
  21. our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
  22. my $installdir=''; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  23. # Optimisation.
  24. use Memoize;
  25. memoize("abs2rel");
  26. memoize("pagespec_translate");
  27. memoize("file_pruned");
  28. sub defaultconfig () { #{{{
  29. return
  30. wiki_file_prune_regexps => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
  31. qr/\.x?html?$/, qr/\.ikiwiki-new$/,
  32. qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
  33. qr/(^|\/)_MTN\//,
  34. qr/\.dpkg-tmp$/],
  35. wiki_link_regexp => qr{
  36. \[\[ # beginning of link
  37. (?:
  38. ([^\]\|\n\s]+) # 1: link text
  39. \| # followed by '|'
  40. )? # optional
  41. ([^\s\]#]+) # 2: page to link to
  42. (?:
  43. \# # '#', beginning of anchor
  44. ([^\s\]]+) # 3: anchor text
  45. )? # optional
  46. \]\] # end of link
  47. }x,
  48. wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
  49. web_commit_regexp => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
  50. verbose => 0,
  51. syslog => 0,
  52. wikiname => "wiki",
  53. default_pageext => "mdwn",
  54. htmlext => "html",
  55. cgi => 0,
  56. post_commit => 0,
  57. rcs => '',
  58. url => '',
  59. cgiurl => '',
  60. historyurl => '',
  61. diffurl => '',
  62. rss => 0,
  63. atom => 0,
  64. discussion => 1,
  65. rebuild => 0,
  66. refresh => 0,
  67. getctime => 0,
  68. w3mmode => 0,
  69. wrapper => undef,
  70. wrappermode => undef,
  71. svnpath => "trunk",
  72. gitorigin_branch => "origin",
  73. gitmaster_branch => "master",
  74. srcdir => undef,
  75. destdir => undef,
  76. pingurl => [],
  77. templatedir => "$installdir/share/ikiwiki/templates",
  78. underlaydir => "$installdir/share/ikiwiki/basewiki",
  79. underlaydirs => [],
  80. setup => undef,
  81. adminuser => undef,
  82. adminemail => undef,
  83. plugin => [qw{mdwn inline htmlscrubber passwordauth openid signinedit
  84. lockedit conditional recentchanges}],
  85. libdir => undef,
  86. timeformat => '%c',
  87. locale => undef,
  88. sslcookie => 0,
  89. httpauth => 0,
  90. userdir => "",
  91. usedirs => 1,
  92. numbacklinks => 10,
  93. account_creation_password => "",
  94. } #}}}
  95. sub checkconfig () { #{{{
  96. # locale stuff; avoid LC_ALL since it overrides everything
  97. if (defined $ENV{LC_ALL}) {
  98. $ENV{LANG} = $ENV{LC_ALL};
  99. delete $ENV{LC_ALL};
  100. }
  101. if (defined $config{locale}) {
  102. if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
  103. $ENV{LANG}=$config{locale};
  104. $gettext_obj=undef;
  105. }
  106. }
  107. if ($config{w3mmode}) {
  108. eval q{use Cwd q{abs_path}};
  109. error($@) if $@;
  110. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  111. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  112. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  113. unless $config{cgiurl} =~ m!file:///!;
  114. $config{url}="file://".$config{destdir};
  115. }
  116. if ($config{cgi} && ! length $config{url}) {
  117. error(gettext("Must specify url to wiki with --url when using --cgi"));
  118. }
  119. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  120. unless exists $config{wikistatedir};
  121. if ($config{rcs}) {
  122. eval qq{use IkiWiki::Rcs::$config{rcs}};
  123. if ($@) {
  124. error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
  125. }
  126. }
  127. else {
  128. require IkiWiki::Rcs::Stub;
  129. }
  130. if (exists $config{umask}) {
  131. umask(possibly_foolish_untaint($config{umask}));
  132. }
  133. run_hooks(checkconfig => sub { shift->() });
  134. return 1;
  135. } #}}}
  136. sub loadplugins () { #{{{
  137. if (defined $config{libdir}) {
  138. unshift @INC, possibly_foolish_untaint($config{libdir});
  139. }
  140. loadplugin($_) foreach @{$config{plugin}};
  141. run_hooks(getopt => sub { shift->() });
  142. if (grep /^-/, @ARGV) {
  143. print STDERR "Unknown option: $_\n"
  144. foreach grep /^-/, @ARGV;
  145. usage();
  146. }
  147. return 1;
  148. } #}}}
  149. sub loadplugin ($) { #{{{
  150. my $plugin=shift;
  151. return if grep { $_ eq $plugin} @{$config{disable_plugins}};
  152. foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
  153. "$installdir/lib/ikiwiki") {
  154. if (defined $dir && -x "$dir/plugins/$plugin") {
  155. require IkiWiki::Plugin::external;
  156. import IkiWiki::Plugin::external "$dir/plugins/$plugin";
  157. return 1;
  158. }
  159. }
  160. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  161. eval qq{use $mod};
  162. if ($@) {
  163. error("Failed to load plugin $mod: $@");
  164. }
  165. return 1;
  166. } #}}}
  167. sub error ($;$) { #{{{
  168. my $message=shift;
  169. my $cleaner=shift;
  170. if ($config{cgi}) {
  171. print "Content-type: text/html\n\n";
  172. print misctemplate(gettext("Error"),
  173. "<p>".gettext("Error").": $message</p>");
  174. }
  175. log_message('err' => $message) if $config{syslog};
  176. if (defined $cleaner) {
  177. $cleaner->();
  178. }
  179. die $message."\n";
  180. } #}}}
  181. sub debug ($) { #{{{
  182. return unless $config{verbose};
  183. return log_message(debug => @_);
  184. } #}}}
  185. my $log_open=0;
  186. sub log_message ($$) { #{{{
  187. my $type=shift;
  188. if ($config{syslog}) {
  189. require Sys::Syslog;
  190. if (! $log_open) {
  191. Sys::Syslog::setlogsock('unix');
  192. Sys::Syslog::openlog('ikiwiki', '', 'user');
  193. $log_open=1;
  194. }
  195. return eval {
  196. Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
  197. };
  198. }
  199. elsif (! $config{cgi}) {
  200. return print "@_\n";
  201. }
  202. else {
  203. return print STDERR "@_\n";
  204. }
  205. } #}}}
  206. sub possibly_foolish_untaint ($) { #{{{
  207. my $tainted=shift;
  208. my ($untainted)=$tainted=~/(.*)/s;
  209. return $untainted;
  210. } #}}}
  211. sub basename ($) { #{{{
  212. my $file=shift;
  213. $file=~s!.*/+!!;
  214. return $file;
  215. } #}}}
  216. sub dirname ($) { #{{{
  217. my $file=shift;
  218. $file=~s!/*[^/]+$!!;
  219. return $file;
  220. } #}}}
  221. sub pagetype ($) { #{{{
  222. my $page=shift;
  223. if ($page =~ /\.([^.]+)$/) {
  224. return $1 if exists $hooks{htmlize}{$1};
  225. }
  226. return;
  227. } #}}}
  228. sub isinternal ($) { #{{{
  229. my $page=shift;
  230. return exists $pagesources{$page} &&
  231. $pagesources{$page} =~ /\._([^.]+)$/;
  232. } #}}}
  233. sub pagename ($) { #{{{
  234. my $file=shift;
  235. my $type=pagetype($file);
  236. my $page=$file;
  237. $page=~s/\Q.$type\E*$// if defined $type;
  238. return $page;
  239. } #}}}
  240. sub targetpage ($$) { #{{{
  241. my $page=shift;
  242. my $ext=shift;
  243. if (! $config{usedirs} || $page =~ /^index$/ ) {
  244. return $page.".".$ext;
  245. } else {
  246. return $page."/index.".$ext;
  247. }
  248. } #}}}
  249. sub htmlpage ($) { #{{{
  250. my $page=shift;
  251. return targetpage($page, $config{htmlext});
  252. } #}}}
  253. sub srcfile ($) { #{{{
  254. my $file=shift;
  255. return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
  256. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  257. return "$dir/$file" if -e "$dir/$file";
  258. }
  259. error("internal error: $file cannot be found in $config{srcdir} or underlay");
  260. return;
  261. } #}}}
  262. sub add_underlay ($) { #{{{
  263. my $dir=shift;
  264. if ($dir=~/^\//) {
  265. unshift @{$config{underlaydirs}}, $dir;
  266. }
  267. else {
  268. unshift @{$config{underlaydirs}}, "$config{underlaydir}/../$dir";
  269. }
  270. return 1;
  271. } #}}}
  272. sub readfile ($;$$) { #{{{
  273. my $file=shift;
  274. my $binary=shift;
  275. my $wantfd=shift;
  276. if (-l $file) {
  277. error("cannot read a symlink ($file)");
  278. }
  279. local $/=undef;
  280. open (my $in, "<", $file) || error("failed to read $file: $!");
  281. binmode($in) if ($binary);
  282. return \*$in if $wantfd;
  283. my $ret=<$in>;
  284. close $in || error("failed to read $file: $!");
  285. return $ret;
  286. } #}}}
  287. sub writefile ($$$;$$) { #{{{
  288. my $file=shift; # can include subdirs
  289. my $destdir=shift; # directory to put file in
  290. my $content=shift;
  291. my $binary=shift;
  292. my $writer=shift;
  293. my $test=$file;
  294. while (length $test) {
  295. if (-l "$destdir/$test") {
  296. error("cannot write to a symlink ($test)");
  297. }
  298. $test=dirname($test);
  299. }
  300. my $newfile="$destdir/$file.ikiwiki-new";
  301. if (-l $newfile) {
  302. error("cannot write to a symlink ($newfile)");
  303. }
  304. my $dir=dirname($newfile);
  305. if (! -d $dir) {
  306. my $d="";
  307. foreach my $s (split(m!/+!, $dir)) {
  308. $d.="$s/";
  309. if (! -d $d) {
  310. mkdir($d) || error("failed to create directory $d: $!");
  311. }
  312. }
  313. }
  314. my $cleanup = sub { unlink($newfile) };
  315. open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
  316. binmode($out) if ($binary);
  317. if ($writer) {
  318. $writer->(\*$out, $cleanup);
  319. }
  320. else {
  321. print $out $content or error("failed writing to $newfile: $!", $cleanup);
  322. }
  323. close $out || error("failed saving $newfile: $!", $cleanup);
  324. rename($newfile, "$destdir/$file") ||
  325. error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
  326. return 1;
  327. } #}}}
  328. my %cleared;
  329. sub will_render ($$;$) { #{{{
  330. my $page=shift;
  331. my $dest=shift;
  332. my $clear=shift;
  333. # Important security check.
  334. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  335. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}})) {
  336. error("$config{destdir}/$dest independently created, not overwriting with version from $page");
  337. }
  338. if (! $clear || $cleared{$page}) {
  339. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  340. }
  341. else {
  342. foreach my $old (@{$renderedfiles{$page}}) {
  343. delete $destsources{$old};
  344. }
  345. $renderedfiles{$page}=[$dest];
  346. $cleared{$page}=1;
  347. }
  348. $destsources{$dest}=$page;
  349. return 1;
  350. } #}}}
  351. sub bestlink ($$) { #{{{
  352. my $page=shift;
  353. my $link=shift;
  354. my $cwd=$page;
  355. if ($link=~s/^\/+//) {
  356. # absolute links
  357. $cwd="";
  358. }
  359. $link=~s/\/$//;
  360. do {
  361. my $l=$cwd;
  362. $l.="/" if length $l;
  363. $l.=$link;
  364. if (exists $links{$l}) {
  365. return $l;
  366. }
  367. elsif (exists $pagecase{lc $l}) {
  368. return $pagecase{lc $l};
  369. }
  370. } while $cwd=~s!/?[^/]+$!!;
  371. if (length $config{userdir}) {
  372. my $l = "$config{userdir}/".lc($link);
  373. if (exists $links{$l}) {
  374. return $l;
  375. }
  376. elsif (exists $pagecase{lc $l}) {
  377. return $pagecase{lc $l};
  378. }
  379. }
  380. #print STDERR "warning: page $page, broken link: $link\n";
  381. return "";
  382. } #}}}
  383. sub isinlinableimage ($) { #{{{
  384. my $file=shift;
  385. return $file =~ /\.(png|gif|jpg|jpeg)$/i;
  386. } #}}}
  387. sub pagetitle ($;$) { #{{{
  388. my $page=shift;
  389. my $unescaped=shift;
  390. if ($unescaped) {
  391. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
  392. }
  393. else {
  394. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
  395. }
  396. return $page;
  397. } #}}}
  398. sub titlepage ($) { #{{{
  399. my $title=shift;
  400. $title=~s/([^-[:alnum:]:+\/.])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  401. return $title;
  402. } #}}}
  403. sub linkpage ($) { #{{{
  404. my $link=shift;
  405. $link=~s/([^-[:alnum:]:+\/._])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  406. return $link;
  407. } #}}}
  408. sub cgiurl (@) { #{{{
  409. my %params=@_;
  410. return $config{cgiurl}."?".
  411. join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
  412. } #}}}
  413. sub baseurl (;$) { #{{{
  414. my $page=shift;
  415. return "$config{url}/" if ! defined $page;
  416. $page=htmlpage($page);
  417. $page=~s/[^\/]+$//;
  418. $page=~s/[^\/]+\//..\//g;
  419. return $page;
  420. } #}}}
  421. sub abs2rel ($$) { #{{{
  422. # Work around very innefficient behavior in File::Spec if abs2rel
  423. # is passed two relative paths. It's much faster if paths are
  424. # absolute! (Debian bug #376658; fixed in debian unstable now)
  425. my $path="/".shift;
  426. my $base="/".shift;
  427. require File::Spec;
  428. my $ret=File::Spec->abs2rel($path, $base);
  429. $ret=~s/^// if defined $ret;
  430. return $ret;
  431. } #}}}
  432. sub displaytime ($;$) { #{{{
  433. my $time=shift;
  434. my $format=shift;
  435. if (! defined $format) {
  436. $format=$config{timeformat};
  437. }
  438. # strftime doesn't know about encodings, so make sure
  439. # its output is properly treated as utf8
  440. return decode_utf8(POSIX::strftime($format, localtime($time)));
  441. } #}}}
  442. sub beautify_url ($) { #{{{
  443. my $url=shift;
  444. if ($config{usedirs}) {
  445. $url =~ s!/index.$config{htmlext}$!/!;
  446. }
  447. $url =~ s!^$!./!; # Browsers don't like empty links...
  448. return $url;
  449. } #}}}
  450. sub urlto ($$) { #{{{
  451. my $to=shift;
  452. my $from=shift;
  453. if (! length $to) {
  454. return beautify_url(baseurl($from));
  455. }
  456. if (! $destsources{$to}) {
  457. $to=htmlpage($to);
  458. }
  459. my $link = abs2rel($to, dirname(htmlpage($from)));
  460. return beautify_url($link);
  461. } #}}}
  462. sub htmllink ($$$;@) { #{{{
  463. my $lpage=shift; # the page doing the linking
  464. my $page=shift; # the page that will contain the link (different for inline)
  465. my $link=shift;
  466. my %opts=@_;
  467. $link=~s/\/$//;
  468. my $bestlink;
  469. if (! $opts{forcesubpage}) {
  470. $bestlink=bestlink($lpage, $link);
  471. }
  472. else {
  473. $bestlink="$lpage/".lc($link);
  474. }
  475. my $linktext;
  476. if (defined $opts{linktext}) {
  477. $linktext=$opts{linktext};
  478. }
  479. else {
  480. $linktext=pagetitle(basename($link));
  481. }
  482. return "<span class=\"selflink\">$linktext</span>"
  483. if length $bestlink && $page eq $bestlink &&
  484. ! defined $opts{anchor};
  485. if (! $destsources{$bestlink}) {
  486. $bestlink=htmlpage($bestlink);
  487. if (! $destsources{$bestlink}) {
  488. return $linktext unless length $config{cgiurl};
  489. return "<span class=\"createlink\"><a href=\"".
  490. cgiurl(
  491. do => "create",
  492. page => pagetitle(lc($link), 1),
  493. from => $lpage
  494. ).
  495. "\">?</a>$linktext</span>"
  496. }
  497. }
  498. $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
  499. $bestlink=beautify_url($bestlink);
  500. if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
  501. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  502. }
  503. if (defined $opts{anchor}) {
  504. $bestlink.="#".$opts{anchor};
  505. }
  506. my @attrs;
  507. if (defined $opts{rel}) {
  508. push @attrs, ' rel="'.$opts{rel}.'"';
  509. }
  510. if (defined $opts{class}) {
  511. push @attrs, ' class="'.$opts{class}.'"';
  512. }
  513. return "<a href=\"$bestlink\"@attrs>$linktext</a>";
  514. } #}}}
  515. sub userlink ($) { #{{{
  516. my $user=shift;
  517. my $oiduser=eval { openiduser($user) };
  518. if (defined $oiduser) {
  519. return "<a href=\"$user\">$oiduser</a>";
  520. }
  521. else {
  522. return htmllink("", "", escapeHTML(
  523. length $config{userdir} ? $config{userdir}."/".$user : $user
  524. ), noimageinline => 1);
  525. }
  526. } #}}}
  527. sub htmlize ($$$) { #{{{
  528. my $page=shift;
  529. my $type=shift;
  530. my $content=shift;
  531. my $oneline = $content !~ /\n/;
  532. if (exists $hooks{htmlize}{$type}) {
  533. $content=$hooks{htmlize}{$type}{call}->(
  534. page => $page,
  535. content => $content,
  536. );
  537. }
  538. else {
  539. error("htmlization of $type not supported");
  540. }
  541. run_hooks(sanitize => sub {
  542. $content=shift->(
  543. page => $page,
  544. content => $content,
  545. );
  546. });
  547. if ($oneline) {
  548. # hack to get rid of enclosing junk added by markdown
  549. # and other htmlizers
  550. $content=~s/^<p>//i;
  551. $content=~s/<\/p>$//i;
  552. chomp $content;
  553. }
  554. return $content;
  555. } #}}}
  556. sub linkify ($$$) { #{{{
  557. my $lpage=shift; # the page containing the links
  558. my $page=shift; # the page the link will end up on (different for inline)
  559. my $content=shift;
  560. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  561. defined $2
  562. ? ( $1
  563. ? "[[$2|$3".($4 ? "#$4" : "")."]]"
  564. : htmllink($lpage, $page, linkpage($3),
  565. anchor => $4, linktext => pagetitle($2)))
  566. : ( $1
  567. ? "[[$3".($4 ? "#$4" : "")."]]"
  568. : htmllink($lpage, $page, linkpage($3),
  569. anchor => $4))
  570. }eg;
  571. return $content;
  572. } #}}}
  573. my %preprocessing;
  574. our $preprocess_preview=0;
  575. sub preprocess ($$$;$$) { #{{{
  576. my $page=shift; # the page the data comes from
  577. my $destpage=shift; # the page the data will appear in (different for inline)
  578. my $content=shift;
  579. my $scan=shift;
  580. my $preview=shift;
  581. # Using local because it needs to be set within any nested calls
  582. # of this function.
  583. local $preprocess_preview=$preview if defined $preview;
  584. my $handle=sub {
  585. my $escape=shift;
  586. my $command=shift;
  587. my $params=shift;
  588. if (length $escape) {
  589. return "[[$command $params]]";
  590. }
  591. elsif (exists $hooks{preprocess}{$command}) {
  592. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  593. # Note: preserve order of params, some plugins may
  594. # consider it significant.
  595. my @params;
  596. while ($params =~ m{
  597. (?:([-\w]+)=)? # 1: named parameter key?
  598. (?:
  599. """(.*?)""" # 2: triple-quoted value
  600. |
  601. "([^"]+)" # 3: single-quoted value
  602. |
  603. (\S+) # 4: unquoted value
  604. )
  605. (?:\s+|$) # delimiter to next param
  606. }sgx) {
  607. my $key=$1;
  608. my $val;
  609. if (defined $2) {
  610. $val=$2;
  611. $val=~s/\r\n/\n/mg;
  612. $val=~s/^\n+//g;
  613. $val=~s/\n+$//g;
  614. }
  615. elsif (defined $3) {
  616. $val=$3;
  617. }
  618. elsif (defined $4) {
  619. $val=$4;
  620. }
  621. if (defined $key) {
  622. push @params, $key, $val;
  623. }
  624. else {
  625. push @params, $val, '';
  626. }
  627. }
  628. if ($preprocessing{$page}++ > 3) {
  629. # Avoid loops of preprocessed pages preprocessing
  630. # other pages that preprocess them, etc.
  631. #translators: The first parameter is a
  632. #translators: preprocessor directive name,
  633. #translators: the second a page name, the
  634. #translators: third a number.
  635. return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
  636. $command, $page, $preprocessing{$page}).
  637. "]]";
  638. }
  639. my $ret;
  640. if (! $scan) {
  641. $ret=$hooks{preprocess}{$command}{call}->(
  642. @params,
  643. page => $page,
  644. destpage => $destpage,
  645. preview => $preprocess_preview,
  646. );
  647. }
  648. else {
  649. # use void context during scan pass
  650. $hooks{preprocess}{$command}{call}->(
  651. @params,
  652. page => $page,
  653. destpage => $destpage,
  654. preview => $preprocess_preview,
  655. );
  656. $ret="";
  657. }
  658. $preprocessing{$page}--;
  659. return $ret;
  660. }
  661. else {
  662. return "[[$command $params]]";
  663. }
  664. };
  665. $content =~ s{
  666. (\\?) # 1: escape?
  667. \[\[ # directive open
  668. ([-\w]+) # 2: command
  669. \s+
  670. ( # 3: the parameters..
  671. (?:
  672. (?:[-\w]+=)? # named parameter key?
  673. (?:
  674. """.*?""" # triple-quoted value
  675. |
  676. "[^"]+" # single-quoted value
  677. |
  678. [^\s\]]+ # unquoted value
  679. )
  680. \s* # whitespace or end
  681. # of directive
  682. )
  683. *) # 0 or more parameters
  684. \]\] # directive closed
  685. }{$handle->($1, $2, $3)}sexg;
  686. return $content;
  687. } #}}}
  688. sub filter ($$$) { #{{{
  689. my $page=shift;
  690. my $destpage=shift;
  691. my $content=shift;
  692. run_hooks(filter => sub {
  693. $content=shift->(page => $page, destpage => $destpage,
  694. content => $content);
  695. });
  696. return $content;
  697. } #}}}
  698. sub indexlink () { #{{{
  699. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  700. } #}}}
  701. my $wikilock;
  702. sub lockwiki (;$) { #{{{
  703. my $wait=@_ ? shift : 1;
  704. # Take an exclusive lock on the wiki to prevent multiple concurrent
  705. # run issues. The lock will be dropped on program exit.
  706. if (! -d $config{wikistatedir}) {
  707. mkdir($config{wikistatedir});
  708. }
  709. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  710. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  711. if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
  712. if ($wait) {
  713. debug("wiki seems to be locked, waiting for lock");
  714. my $wait=600; # arbitrary, but don't hang forever to
  715. # prevent process pileup
  716. for (1..$wait) {
  717. return if flock($wikilock, 2 | 4);
  718. sleep 1;
  719. }
  720. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  721. }
  722. else {
  723. return 0;
  724. }
  725. }
  726. return 1;
  727. } #}}}
  728. sub unlockwiki () { #{{{
  729. return close($wikilock) if $wikilock;
  730. return;
  731. } #}}}
  732. my $commitlock;
  733. sub commit_hook_enabled () { #{{{
  734. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  735. error("cannot write to $config{wikistatedir}/commitlock: $!");
  736. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  737. close($commitlock) || error("failed closing commitlock: $!");
  738. return 0;
  739. }
  740. close($commitlock) || error("failed closing commitlock: $!");
  741. return 1;
  742. } #}}}
  743. sub disable_commit_hook () { #{{{
  744. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  745. error("cannot write to $config{wikistatedir}/commitlock: $!");
  746. if (! flock($commitlock, 2)) { # LOCK_EX
  747. error("failed to get commit lock");
  748. }
  749. return 1;
  750. } #}}}
  751. sub enable_commit_hook () { #{{{
  752. return close($commitlock) if $commitlock;
  753. return;
  754. } #}}}
  755. sub loadindex () { #{{{
  756. %oldrenderedfiles=%pagectime=();
  757. if (! $config{rebuild}) {
  758. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  759. %destsources=%renderedfiles=%pagecase=%pagestate=();
  760. }
  761. open (my $in, "<", "$config{wikistatedir}/index") || return;
  762. while (<$in>) {
  763. $_=possibly_foolish_untaint($_);
  764. chomp;
  765. my %items;
  766. $items{link}=[];
  767. $items{dest}=[];
  768. foreach my $i (split(/ /, $_)) {
  769. my ($item, $val)=split(/=/, $i, 2);
  770. push @{$items{$item}}, decode_entities($val);
  771. }
  772. next unless exists $items{src}; # skip bad lines for now
  773. my $page=pagename($items{src}[0]);
  774. if (! $config{rebuild}) {
  775. $pagesources{$page}=$items{src}[0];
  776. $pagemtime{$page}=$items{mtime}[0];
  777. $oldlinks{$page}=[@{$items{link}}];
  778. $links{$page}=[@{$items{link}}];
  779. $depends{$page}=$items{depends}[0] if exists $items{depends};
  780. $destsources{$_}=$page foreach @{$items{dest}};
  781. $renderedfiles{$page}=[@{$items{dest}}];
  782. $pagecase{lc $page}=$page;
  783. foreach my $k (grep /_/, keys %items) {
  784. my ($id, $key)=split(/_/, $k, 2);
  785. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  786. }
  787. }
  788. $oldrenderedfiles{$page}=[@{$items{dest}}];
  789. $pagectime{$page}=$items{ctime}[0];
  790. }
  791. return close($in);
  792. } #}}}
  793. sub saveindex () { #{{{
  794. run_hooks(savestate => sub { shift->() });
  795. my %hookids;
  796. foreach my $type (keys %hooks) {
  797. $hookids{encode_entities($_)}=1 foreach keys %{$hooks{$type}};
  798. }
  799. my @hookids=sort keys %hookids;
  800. if (! -d $config{wikistatedir}) {
  801. mkdir($config{wikistatedir});
  802. }
  803. my $newfile="$config{wikistatedir}/index.new";
  804. my $cleanup = sub { unlink($newfile) };
  805. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  806. foreach my $page (keys %pagemtime) {
  807. next unless $pagemtime{$page};
  808. my $line="mtime=$pagemtime{$page} ".
  809. "ctime=$pagectime{$page} ".
  810. "src=$pagesources{$page}";
  811. $line.=" dest=$_" foreach @{$renderedfiles{$page}};
  812. my %count;
  813. $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
  814. if (exists $depends{$page}) {
  815. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  816. }
  817. if (exists $pagestate{$page}) {
  818. foreach my $id (@hookids) {
  819. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  820. $line.=' '.$id.'_'.encode_entities($key)."=".encode_entities($pagestate{$page}{$id}{$key}, " \t\n");
  821. }
  822. }
  823. }
  824. print $out $line."\n" || error("failed writing to $newfile: $!", $cleanup);
  825. }
  826. close $out || error("failed saving to $newfile: $!", $cleanup);
  827. rename($newfile, "$config{wikistatedir}/index") ||
  828. error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
  829. return 1;
  830. } #}}}
  831. sub template_file ($) { #{{{
  832. my $template=shift;
  833. foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
  834. return "$dir/$template" if -e "$dir/$template";
  835. }
  836. return;
  837. } #}}}
  838. sub template_params (@) { #{{{
  839. my $filename=template_file(shift);
  840. if (! defined $filename) {
  841. return if wantarray;
  842. return "";
  843. }
  844. my @ret=(
  845. filter => sub {
  846. my $text_ref = shift;
  847. ${$text_ref} = decode_utf8(${$text_ref});
  848. },
  849. filename => $filename,
  850. loop_context_vars => 1,
  851. die_on_bad_params => 0,
  852. @_
  853. );
  854. return wantarray ? @ret : {@ret};
  855. } #}}}
  856. sub template ($;@) { #{{{
  857. require HTML::Template;
  858. return HTML::Template->new(template_params(@_));
  859. } #}}}
  860. sub misctemplate ($$;@) { #{{{
  861. my $title=shift;
  862. my $pagebody=shift;
  863. my $template=template("misc.tmpl");
  864. $template->param(
  865. title => $title,
  866. indexlink => indexlink(),
  867. wikiname => $config{wikiname},
  868. pagebody => $pagebody,
  869. baseurl => baseurl(),
  870. @_,
  871. );
  872. run_hooks(pagetemplate => sub {
  873. shift->(page => "", destpage => "", template => $template);
  874. });
  875. return $template->output;
  876. }#}}}
  877. sub hook (@) { # {{{
  878. my %param=@_;
  879. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  880. error 'hook requires type, call, and id parameters';
  881. }
  882. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  883. $hooks{$param{type}}{$param{id}}=\%param;
  884. return 1;
  885. } # }}}
  886. sub run_hooks ($$) { # {{{
  887. # Calls the given sub for each hook of the given type,
  888. # passing it the hook function to call.
  889. my $type=shift;
  890. my $sub=shift;
  891. if (exists $hooks{$type}) {
  892. my @deferred;
  893. foreach my $id (keys %{$hooks{$type}}) {
  894. if ($hooks{$type}{$id}{last}) {
  895. push @deferred, $id;
  896. next;
  897. }
  898. $sub->($hooks{$type}{$id}{call});
  899. }
  900. foreach my $id (@deferred) {
  901. $sub->($hooks{$type}{$id}{call});
  902. }
  903. }
  904. return 1;
  905. } #}}}
  906. sub globlist_to_pagespec ($) { #{{{
  907. my @globlist=split(' ', shift);
  908. my (@spec, @skip);
  909. foreach my $glob (@globlist) {
  910. if ($glob=~/^!(.*)/) {
  911. push @skip, $glob;
  912. }
  913. else {
  914. push @spec, $glob;
  915. }
  916. }
  917. my $spec=join(' or ', @spec);
  918. if (@skip) {
  919. my $skip=join(' and ', @skip);
  920. if (length $spec) {
  921. $spec="$skip and ($spec)";
  922. }
  923. else {
  924. $spec=$skip;
  925. }
  926. }
  927. return $spec;
  928. } #}}}
  929. sub is_globlist ($) { #{{{
  930. my $s=shift;
  931. return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
  932. } #}}}
  933. sub safequote ($) { #{{{
  934. my $s=shift;
  935. $s=~s/[{}]//g;
  936. return "q{$s}";
  937. } #}}}
  938. sub add_depends ($$) { #{{{
  939. my $page=shift;
  940. my $pagespec=shift;
  941. if (! exists $depends{$page}) {
  942. $depends{$page}=$pagespec;
  943. }
  944. else {
  945. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  946. }
  947. return 1;
  948. } # }}}
  949. sub file_pruned ($$) { #{{{
  950. require File::Spec;
  951. my $file=File::Spec->canonpath(shift);
  952. my $base=File::Spec->canonpath(shift);
  953. $file =~ s#^\Q$base\E/+##;
  954. my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  955. return $file =~ m/$regexp/ && $file ne $base;
  956. } #}}}
  957. sub gettext { #{{{
  958. # Only use gettext in the rare cases it's needed.
  959. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  960. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  961. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  962. if (! $gettext_obj) {
  963. $gettext_obj=eval q{
  964. use Locale::gettext q{textdomain};
  965. Locale::gettext->domain('ikiwiki')
  966. };
  967. if ($@) {
  968. print STDERR "$@";
  969. $gettext_obj=undef;
  970. return shift;
  971. }
  972. }
  973. return $gettext_obj->get(shift);
  974. }
  975. else {
  976. return shift;
  977. }
  978. } #}}}
  979. sub pagespec_merge ($$) { #{{{
  980. my $a=shift;
  981. my $b=shift;
  982. return $a if $a eq $b;
  983. # Support for old-style GlobLists.
  984. if (is_globlist($a)) {
  985. $a=globlist_to_pagespec($a);
  986. }
  987. if (is_globlist($b)) {
  988. $b=globlist_to_pagespec($b);
  989. }
  990. return "($a) or ($b)";
  991. } #}}}
  992. sub pagespec_translate ($) { #{{{
  993. # This assumes that $page is in scope in the function
  994. # that evalulates the translated pagespec code.
  995. my $spec=shift;
  996. # Support for old-style GlobLists.
  997. if (is_globlist($spec)) {
  998. $spec=globlist_to_pagespec($spec);
  999. }
  1000. # Convert spec to perl code.
  1001. my $code="";
  1002. while ($spec=~m{
  1003. \s* # ignore whitespace
  1004. ( # 1: match a single word
  1005. \! # !
  1006. |
  1007. \( # (
  1008. |
  1009. \) # )
  1010. |
  1011. \w+\([^\)]*\) # command(params)
  1012. |
  1013. [^\s()]+ # any other text
  1014. )
  1015. \s* # ignore whitespace
  1016. }igx) {
  1017. my $word=$1;
  1018. if (lc $word eq 'and') {
  1019. $code.=' &&';
  1020. }
  1021. elsif (lc $word eq 'or') {
  1022. $code.=' ||';
  1023. }
  1024. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  1025. $code.=' '.$word;
  1026. }
  1027. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  1028. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  1029. $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
  1030. }
  1031. else {
  1032. $code.=' 0';
  1033. }
  1034. }
  1035. else {
  1036. $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
  1037. }
  1038. }
  1039. return $code;
  1040. } #}}}
  1041. sub pagespec_match ($$;@) { #{{{
  1042. my $page=shift;
  1043. my $spec=shift;
  1044. my @params=@_;
  1045. # Backwards compatability with old calling convention.
  1046. if (@params == 1) {
  1047. unshift @params, 'location';
  1048. }
  1049. my $ret=eval pagespec_translate($spec);
  1050. return IkiWiki::FailReason->new('syntax error') if $@;
  1051. return $ret;
  1052. } #}}}
  1053. package IkiWiki::FailReason;
  1054. use overload ( #{{{
  1055. '""' => sub { ${$_[0]} },
  1056. '0+' => sub { 0 },
  1057. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  1058. fallback => 1,
  1059. ); #}}}
  1060. sub new { #{{{
  1061. return bless \$_[1], $_[0];
  1062. } #}}}
  1063. package IkiWiki::SuccessReason;
  1064. use overload ( #{{{
  1065. '""' => sub { ${$_[0]} },
  1066. '0+' => sub { 1 },
  1067. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  1068. fallback => 1,
  1069. ); #}}}
  1070. sub new { #{{{
  1071. return bless \$_[1], $_[0];
  1072. }; #}}}
  1073. package IkiWiki::PageSpec;
  1074. sub match_glob ($$;@) { #{{{
  1075. my $page=shift;
  1076. my $glob=shift;
  1077. my %params=@_;
  1078. my $from=exists $params{location} ? $params{location} : '';
  1079. # relative matching
  1080. if ($glob =~ m!^\./!) {
  1081. $from=~s#/?[^/]+$##;
  1082. $glob=~s#^\./##;
  1083. $glob="$from/$glob" if length $from;
  1084. }
  1085. # turn glob into safe regexp
  1086. $glob=quotemeta($glob);
  1087. $glob=~s/\\\*/.*/g;
  1088. $glob=~s/\\\?/./g;
  1089. if ($page=~/^$glob$/i) {
  1090. if (! IkiWiki::isinternal($page) || $params{internal}) {
  1091. return IkiWiki::SuccessReason->new("$glob matches $page");
  1092. }
  1093. else {
  1094. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  1095. }
  1096. }
  1097. else {
  1098. return IkiWiki::FailReason->new("$glob does not match $page");
  1099. }
  1100. } #}}}
  1101. sub match_internal ($$;@) { #{{{
  1102. return match_glob($_[0], $_[1], @_, internal => 1)
  1103. } #}}}
  1104. sub match_link ($$;@) { #{{{
  1105. my $page=shift;
  1106. my $link=lc(shift);
  1107. my %params=@_;
  1108. my $from=exists $params{location} ? $params{location} : '';
  1109. # relative matching
  1110. if ($link =~ m!^\.! && defined $from) {
  1111. $from=~s#/?[^/]+$##;
  1112. $link=~s#^\./##;
  1113. $link="$from/$link" if length $from;
  1114. }
  1115. my $links = $IkiWiki::links{$page};
  1116. return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
  1117. my $bestlink = IkiWiki::bestlink($from, $link);
  1118. foreach my $p (@{$links}) {
  1119. if (length $bestlink) {
  1120. return IkiWiki::SuccessReason->new("$page links to $link")
  1121. if $bestlink eq IkiWiki::bestlink($page, $p);
  1122. }
  1123. else {
  1124. return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
  1125. if match_glob($p, $link, %params);
  1126. }
  1127. }
  1128. return IkiWiki::FailReason->new("$page does not link to $link");
  1129. } #}}}
  1130. sub match_backlink ($$;@) { #{{{
  1131. return match_link($_[1], $_[0], @_);
  1132. } #}}}
  1133. sub match_created_before ($$;@) { #{{{
  1134. my $page=shift;
  1135. my $testpage=shift;
  1136. if (exists $IkiWiki::pagectime{$testpage}) {
  1137. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  1138. return IkiWiki::SuccessReason->new("$page created before $testpage");
  1139. }
  1140. else {
  1141. return IkiWiki::FailReason->new("$page not created before $testpage");
  1142. }
  1143. }
  1144. else {
  1145. return IkiWiki::FailReason->new("$testpage has no ctime");
  1146. }
  1147. } #}}}
  1148. sub match_created_after ($$;@) { #{{{
  1149. my $page=shift;
  1150. my $testpage=shift;
  1151. if (exists $IkiWiki::pagectime{$testpage}) {
  1152. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  1153. return IkiWiki::SuccessReason->new("$page created after $testpage");
  1154. }
  1155. else {
  1156. return IkiWiki::FailReason->new("$page not created after $testpage");
  1157. }
  1158. }
  1159. else {
  1160. return IkiWiki::FailReason->new("$testpage has no ctime");
  1161. }
  1162. } #}}}
  1163. sub match_creation_day ($$;@) { #{{{
  1164. if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
  1165. return IkiWiki::SuccessReason->new('creation_day matched');
  1166. }
  1167. else {
  1168. return IkiWiki::FailReason->new('creation_day did not match');
  1169. }
  1170. } #}}}
  1171. sub match_creation_month ($$;@) { #{{{
  1172. if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
  1173. return IkiWiki::SuccessReason->new('creation_month matched');
  1174. }
  1175. else {
  1176. return IkiWiki::FailReason->new('creation_month did not match');
  1177. }
  1178. } #}}}
  1179. sub match_creation_year ($$;@) { #{{{
  1180. if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
  1181. return IkiWiki::SuccessReason->new('creation_year matched');
  1182. }
  1183. else {
  1184. return IkiWiki::FailReason->new('creation_year did not match');
  1185. }
  1186. } #}}}
  1187. 1