summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: db260567212d426d9b260c8e1212e49342e1c870 (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 openiduser ($) { #{{{
  516. my $user=shift;
  517. if ($user =~ m!^https?://! &&
  518. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  519. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  520. my $display=$oid->display;
  521. # Convert "user.somehost.com" to "user [somehost.com]".
  522. if ($display !~ /\[/) {
  523. $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
  524. }
  525. # Convert "http://somehost.com/user" to "user [somehost.com]".
  526. if ($display !~ /\[/) {
  527. $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
  528. }
  529. $display=~s!^https?://!!; # make sure this is removed
  530. eval q{use CGI 'escapeHTML'};
  531. error($@) if $@;
  532. return escapeHTML($display);
  533. }
  534. return;
  535. }
  536. sub userlink ($) { #{{{
  537. my $user=shift;
  538. my $oiduser=openiduser($user);
  539. if (defined $oiduser) {
  540. return "<a href=\"$user\">$oiduser</a>";
  541. }
  542. else {
  543. return htmllink("", "", escapeHTML(
  544. length $config{userdir} ? $config{userdir}."/".$user : $user
  545. ), noimageinline => 1);
  546. }
  547. } #}}}
  548. sub htmlize ($$$) { #{{{
  549. my $page=shift;
  550. my $type=shift;
  551. my $content=shift;
  552. my $oneline = $content !~ /\n/;
  553. if (exists $hooks{htmlize}{$type}) {
  554. $content=$hooks{htmlize}{$type}{call}->(
  555. page => $page,
  556. content => $content,
  557. );
  558. }
  559. else {
  560. error("htmlization of $type not supported");
  561. }
  562. run_hooks(sanitize => sub {
  563. $content=shift->(
  564. page => $page,
  565. content => $content,
  566. );
  567. });
  568. if ($oneline) {
  569. # hack to get rid of enclosing junk added by markdown
  570. # and other htmlizers
  571. $content=~s/^<p>//i;
  572. $content=~s/<\/p>$//i;
  573. chomp $content;
  574. }
  575. return $content;
  576. } #}}}
  577. sub linkify ($$$) { #{{{
  578. my $lpage=shift; # the page containing the links
  579. my $page=shift; # the page the link will end up on (different for inline)
  580. my $content=shift;
  581. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  582. defined $2
  583. ? ( $1
  584. ? "[[$2|$3".($4 ? "#$4" : "")."]]"
  585. : htmllink($lpage, $page, linkpage($3),
  586. anchor => $4, linktext => pagetitle($2)))
  587. : ( $1
  588. ? "[[$3".($4 ? "#$4" : "")."]]"
  589. : htmllink($lpage, $page, linkpage($3),
  590. anchor => $4))
  591. }eg;
  592. return $content;
  593. } #}}}
  594. my %preprocessing;
  595. our $preprocess_preview=0;
  596. sub preprocess ($$$;$$) { #{{{
  597. my $page=shift; # the page the data comes from
  598. my $destpage=shift; # the page the data will appear in (different for inline)
  599. my $content=shift;
  600. my $scan=shift;
  601. my $preview=shift;
  602. # Using local because it needs to be set within any nested calls
  603. # of this function.
  604. local $preprocess_preview=$preview if defined $preview;
  605. my $handle=sub {
  606. my $escape=shift;
  607. my $command=shift;
  608. my $params=shift;
  609. if (length $escape) {
  610. return "[[$command $params]]";
  611. }
  612. elsif (exists $hooks{preprocess}{$command}) {
  613. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  614. # Note: preserve order of params, some plugins may
  615. # consider it significant.
  616. my @params;
  617. while ($params =~ m{
  618. (?:([-\w]+)=)? # 1: named parameter key?
  619. (?:
  620. """(.*?)""" # 2: triple-quoted value
  621. |
  622. "([^"]+)" # 3: single-quoted value
  623. |
  624. (\S+) # 4: unquoted value
  625. )
  626. (?:\s+|$) # delimiter to next param
  627. }sgx) {
  628. my $key=$1;
  629. my $val;
  630. if (defined $2) {
  631. $val=$2;
  632. $val=~s/\r\n/\n/mg;
  633. $val=~s/^\n+//g;
  634. $val=~s/\n+$//g;
  635. }
  636. elsif (defined $3) {
  637. $val=$3;
  638. }
  639. elsif (defined $4) {
  640. $val=$4;
  641. }
  642. if (defined $key) {
  643. push @params, $key, $val;
  644. }
  645. else {
  646. push @params, $val, '';
  647. }
  648. }
  649. if ($preprocessing{$page}++ > 3) {
  650. # Avoid loops of preprocessed pages preprocessing
  651. # other pages that preprocess them, etc.
  652. #translators: The first parameter is a
  653. #translators: preprocessor directive name,
  654. #translators: the second a page name, the
  655. #translators: third a number.
  656. return "[[".sprintf(gettext("%s preprocessing loop detected on %s at depth %i"),
  657. $command, $page, $preprocessing{$page}).
  658. "]]";
  659. }
  660. my $ret;
  661. if (! $scan) {
  662. $ret=$hooks{preprocess}{$command}{call}->(
  663. @params,
  664. page => $page,
  665. destpage => $destpage,
  666. preview => $preprocess_preview,
  667. );
  668. }
  669. else {
  670. # use void context during scan pass
  671. $hooks{preprocess}{$command}{call}->(
  672. @params,
  673. page => $page,
  674. destpage => $destpage,
  675. preview => $preprocess_preview,
  676. );
  677. $ret="";
  678. }
  679. $preprocessing{$page}--;
  680. return $ret;
  681. }
  682. else {
  683. return "[[$command $params]]";
  684. }
  685. };
  686. $content =~ s{
  687. (\\?) # 1: escape?
  688. \[\[ # directive open
  689. ([-\w]+) # 2: command
  690. \s+
  691. ( # 3: the parameters..
  692. (?:
  693. (?:[-\w]+=)? # named parameter key?
  694. (?:
  695. """.*?""" # triple-quoted value
  696. |
  697. "[^"]+" # single-quoted value
  698. |
  699. [^\s\]]+ # unquoted value
  700. )
  701. \s* # whitespace or end
  702. # of directive
  703. )
  704. *) # 0 or more parameters
  705. \]\] # directive closed
  706. }{$handle->($1, $2, $3)}sexg;
  707. return $content;
  708. } #}}}
  709. sub filter ($$$) { #{{{
  710. my $page=shift;
  711. my $destpage=shift;
  712. my $content=shift;
  713. run_hooks(filter => sub {
  714. $content=shift->(page => $page, destpage => $destpage,
  715. content => $content);
  716. });
  717. return $content;
  718. } #}}}
  719. sub indexlink () { #{{{
  720. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  721. } #}}}
  722. my $wikilock;
  723. sub lockwiki (;$) { #{{{
  724. my $wait=@_ ? shift : 1;
  725. # Take an exclusive lock on the wiki to prevent multiple concurrent
  726. # run issues. The lock will be dropped on program exit.
  727. if (! -d $config{wikistatedir}) {
  728. mkdir($config{wikistatedir});
  729. }
  730. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  731. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  732. if (! flock($wikilock, 2 | 4)) { # LOCK_EX | LOCK_NB
  733. if ($wait) {
  734. debug("wiki seems to be locked, waiting for lock");
  735. my $wait=600; # arbitrary, but don't hang forever to
  736. # prevent process pileup
  737. for (1..$wait) {
  738. return if flock($wikilock, 2 | 4);
  739. sleep 1;
  740. }
  741. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  742. }
  743. else {
  744. return 0;
  745. }
  746. }
  747. return 1;
  748. } #}}}
  749. sub unlockwiki () { #{{{
  750. return close($wikilock) if $wikilock;
  751. return;
  752. } #}}}
  753. my $commitlock;
  754. sub commit_hook_enabled () { #{{{
  755. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  756. error("cannot write to $config{wikistatedir}/commitlock: $!");
  757. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  758. close($commitlock) || error("failed closing commitlock: $!");
  759. return 0;
  760. }
  761. close($commitlock) || error("failed closing commitlock: $!");
  762. return 1;
  763. } #}}}
  764. sub disable_commit_hook () { #{{{
  765. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  766. error("cannot write to $config{wikistatedir}/commitlock: $!");
  767. if (! flock($commitlock, 2)) { # LOCK_EX
  768. error("failed to get commit lock");
  769. }
  770. return 1;
  771. } #}}}
  772. sub enable_commit_hook () { #{{{
  773. return close($commitlock) if $commitlock;
  774. return;
  775. } #}}}
  776. sub loadindex () { #{{{
  777. %oldrenderedfiles=%pagectime=();
  778. if (! $config{rebuild}) {
  779. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  780. %destsources=%renderedfiles=%pagecase=();
  781. }
  782. open (my $in, "<", "$config{wikistatedir}/index") || return;
  783. while (<$in>) {
  784. $_=possibly_foolish_untaint($_);
  785. chomp;
  786. my %items;
  787. $items{link}=[];
  788. $items{dest}=[];
  789. foreach my $i (split(/ /, $_)) {
  790. my ($item, $val)=split(/=/, $i, 2);
  791. push @{$items{$item}}, decode_entities($val);
  792. }
  793. next unless exists $items{src}; # skip bad lines for now
  794. my $page=pagename($items{src}[0]);
  795. if (! $config{rebuild}) {
  796. $pagesources{$page}=$items{src}[0];
  797. $pagemtime{$page}=$items{mtime}[0];
  798. $oldlinks{$page}=[@{$items{link}}];
  799. $links{$page}=[@{$items{link}}];
  800. $depends{$page}=$items{depends}[0] if exists $items{depends};
  801. $destsources{$_}=$page foreach @{$items{dest}};
  802. $renderedfiles{$page}=[@{$items{dest}}];
  803. $pagecase{lc $page}=$page;
  804. foreach my $k (grep /_/, keys %items) {
  805. my ($id, $key)=split(/_/, $k, 2);
  806. $pagestate{$page}{decode_entities($id)}{decode_entities($key)}=$items{$k}[0];
  807. }
  808. }
  809. $oldrenderedfiles{$page}=[@{$items{dest}}];
  810. $pagectime{$page}=$items{ctime}[0];
  811. }
  812. return close($in);
  813. } #}}}
  814. sub saveindex () { #{{{
  815. run_hooks(savestate => sub { shift->() });
  816. my %hookids;
  817. foreach my $type (keys %hooks) {
  818. $hookids{encode_entities($_)}=1 foreach keys %{$hooks{$type}};
  819. }
  820. my @hookids=sort keys %hookids;
  821. if (! -d $config{wikistatedir}) {
  822. mkdir($config{wikistatedir});
  823. }
  824. my $newfile="$config{wikistatedir}/index.new";
  825. my $cleanup = sub { unlink($newfile) };
  826. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  827. foreach my $page (keys %pagemtime) {
  828. next unless $pagemtime{$page};
  829. my $line="mtime=$pagemtime{$page} ".
  830. "ctime=$pagectime{$page} ".
  831. "src=$pagesources{$page}";
  832. $line.=" dest=$_" foreach @{$renderedfiles{$page}};
  833. my %count;
  834. $line.=" link=$_" foreach grep { ++$count{$_} == 1 } @{$links{$page}};
  835. if (exists $depends{$page}) {
  836. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  837. }
  838. if (exists $pagestate{$page}) {
  839. foreach my $id (@hookids) {
  840. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  841. $line.=' '.$id.'_'.encode_entities($key)."=".encode_entities($pagestate{$page}{$id}{$key});
  842. }
  843. }
  844. }
  845. print $out $line."\n" || error("failed writing to $newfile: $!", $cleanup);
  846. }
  847. close $out || error("failed saving to $newfile: $!", $cleanup);
  848. rename($newfile, "$config{wikistatedir}/index") ||
  849. error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
  850. return 1;
  851. } #}}}
  852. sub template_file ($) { #{{{
  853. my $template=shift;
  854. foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
  855. return "$dir/$template" if -e "$dir/$template";
  856. }
  857. return;
  858. } #}}}
  859. sub template_params (@) { #{{{
  860. my $filename=template_file(shift);
  861. if (! defined $filename) {
  862. return if wantarray;
  863. return "";
  864. }
  865. my @ret=(
  866. filter => sub {
  867. my $text_ref = shift;
  868. ${$text_ref} = decode_utf8(${$text_ref});
  869. },
  870. filename => $filename,
  871. loop_context_vars => 1,
  872. die_on_bad_params => 0,
  873. @_
  874. );
  875. return wantarray ? @ret : {@ret};
  876. } #}}}
  877. sub template ($;@) { #{{{
  878. require HTML::Template;
  879. return HTML::Template->new(template_params(@_));
  880. } #}}}
  881. sub misctemplate ($$;@) { #{{{
  882. my $title=shift;
  883. my $pagebody=shift;
  884. my $template=template("misc.tmpl");
  885. $template->param(
  886. title => $title,
  887. indexlink => indexlink(),
  888. wikiname => $config{wikiname},
  889. pagebody => $pagebody,
  890. baseurl => baseurl(),
  891. @_,
  892. );
  893. run_hooks(pagetemplate => sub {
  894. shift->(page => "", destpage => "", template => $template);
  895. });
  896. return $template->output;
  897. }#}}}
  898. sub hook (@) { # {{{
  899. my %param=@_;
  900. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  901. error 'hook requires type, call, and id parameters';
  902. }
  903. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  904. $hooks{$param{type}}{$param{id}}=\%param;
  905. return 1;
  906. } # }}}
  907. sub run_hooks ($$) { # {{{
  908. # Calls the given sub for each hook of the given type,
  909. # passing it the hook function to call.
  910. my $type=shift;
  911. my $sub=shift;
  912. if (exists $hooks{$type}) {
  913. my @deferred;
  914. foreach my $id (keys %{$hooks{$type}}) {
  915. if ($hooks{$type}{$id}{last}) {
  916. push @deferred, $id;
  917. next;
  918. }
  919. $sub->($hooks{$type}{$id}{call});
  920. }
  921. foreach my $id (@deferred) {
  922. $sub->($hooks{$type}{$id}{call});
  923. }
  924. }
  925. return 1;
  926. } #}}}
  927. sub globlist_to_pagespec ($) { #{{{
  928. my @globlist=split(' ', shift);
  929. my (@spec, @skip);
  930. foreach my $glob (@globlist) {
  931. if ($glob=~/^!(.*)/) {
  932. push @skip, $glob;
  933. }
  934. else {
  935. push @spec, $glob;
  936. }
  937. }
  938. my $spec=join(' or ', @spec);
  939. if (@skip) {
  940. my $skip=join(' and ', @skip);
  941. if (length $spec) {
  942. $spec="$skip and ($spec)";
  943. }
  944. else {
  945. $spec=$skip;
  946. }
  947. }
  948. return $spec;
  949. } #}}}
  950. sub is_globlist ($) { #{{{
  951. my $s=shift;
  952. return ( $s =~ /[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or" );
  953. } #}}}
  954. sub safequote ($) { #{{{
  955. my $s=shift;
  956. $s=~s/[{}]//g;
  957. return "q{$s}";
  958. } #}}}
  959. sub add_depends ($$) { #{{{
  960. my $page=shift;
  961. my $pagespec=shift;
  962. if (! exists $depends{$page}) {
  963. $depends{$page}=$pagespec;
  964. }
  965. else {
  966. $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
  967. }
  968. return 1;
  969. } # }}}
  970. sub file_pruned ($$) { #{{{
  971. require File::Spec;
  972. my $file=File::Spec->canonpath(shift);
  973. my $base=File::Spec->canonpath(shift);
  974. $file =~ s#^\Q$base\E/+##;
  975. my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  976. return $file =~ m/$regexp/ && $file ne $base;
  977. } #}}}
  978. sub gettext { #{{{
  979. # Only use gettext in the rare cases it's needed.
  980. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  981. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  982. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  983. if (! $gettext_obj) {
  984. $gettext_obj=eval q{
  985. use Locale::gettext q{textdomain};
  986. Locale::gettext->domain('ikiwiki')
  987. };
  988. if ($@) {
  989. print STDERR "$@";
  990. $gettext_obj=undef;
  991. return shift;
  992. }
  993. }
  994. return $gettext_obj->get(shift);
  995. }
  996. else {
  997. return shift;
  998. }
  999. } #}}}
  1000. sub pagespec_merge ($$) { #{{{
  1001. my $a=shift;
  1002. my $b=shift;
  1003. return $a if $a eq $b;
  1004. # Support for old-style GlobLists.
  1005. if (is_globlist($a)) {
  1006. $a=globlist_to_pagespec($a);
  1007. }
  1008. if (is_globlist($b)) {
  1009. $b=globlist_to_pagespec($b);
  1010. }
  1011. return "($a) or ($b)";
  1012. } #}}}
  1013. sub pagespec_translate ($) { #{{{
  1014. # This assumes that $page is in scope in the function
  1015. # that evalulates the translated pagespec code.
  1016. my $spec=shift;
  1017. # Support for old-style GlobLists.
  1018. if (is_globlist($spec)) {
  1019. $spec=globlist_to_pagespec($spec);
  1020. }
  1021. # Convert spec to perl code.
  1022. my $code="";
  1023. while ($spec=~m{
  1024. \s* # ignore whitespace
  1025. ( # 1: match a single word
  1026. \! # !
  1027. |
  1028. \( # (
  1029. |
  1030. \) # )
  1031. |
  1032. \w+\([^\)]*\) # command(params)
  1033. |
  1034. [^\s()]+ # any other text
  1035. )
  1036. \s* # ignore whitespace
  1037. }igx) {
  1038. my $word=$1;
  1039. if (lc $word eq 'and') {
  1040. $code.=' &&';
  1041. }
  1042. elsif (lc $word eq 'or') {
  1043. $code.=' ||';
  1044. }
  1045. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  1046. $code.=' '.$word;
  1047. }
  1048. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  1049. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  1050. $code.="IkiWiki::PageSpec::match_$1(\$page, ".safequote($2).", \@params)";
  1051. }
  1052. else {
  1053. $code.=' 0';
  1054. }
  1055. }
  1056. else {
  1057. $code.=" IkiWiki::PageSpec::match_glob(\$page, ".safequote($word).", \@params)";
  1058. }
  1059. }
  1060. return $code;
  1061. } #}}}
  1062. sub pagespec_match ($$;@) { #{{{
  1063. my $page=shift;
  1064. my $spec=shift;
  1065. my @params=@_;
  1066. # Backwards compatability with old calling convention.
  1067. if (@params == 1) {
  1068. unshift @params, 'location';
  1069. }
  1070. my $ret=eval pagespec_translate($spec);
  1071. return IkiWiki::FailReason->new('syntax error') if $@;
  1072. return $ret;
  1073. } #}}}
  1074. package IkiWiki::FailReason;
  1075. use overload ( #{{{
  1076. '""' => sub { ${$_[0]} },
  1077. '0+' => sub { 0 },
  1078. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  1079. fallback => 1,
  1080. ); #}}}
  1081. sub new { #{{{
  1082. return bless \$_[1], $_[0];
  1083. } #}}}
  1084. package IkiWiki::SuccessReason;
  1085. use overload ( #{{{
  1086. '""' => sub { ${$_[0]} },
  1087. '0+' => sub { 1 },
  1088. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  1089. fallback => 1,
  1090. ); #}}}
  1091. sub new { #{{{
  1092. return bless \$_[1], $_[0];
  1093. }; #}}}
  1094. package IkiWiki::PageSpec;
  1095. sub match_glob ($$;@) { #{{{
  1096. my $page=shift;
  1097. my $glob=shift;
  1098. my %params=@_;
  1099. my $from=exists $params{location} ? $params{location} : '';
  1100. # relative matching
  1101. if ($glob =~ m!^\./!) {
  1102. $from=~s#/?[^/]+$##;
  1103. $glob=~s#^\./##;
  1104. $glob="$from/$glob" if length $from;
  1105. }
  1106. # turn glob into safe regexp
  1107. $glob=quotemeta($glob);
  1108. $glob=~s/\\\*/.*/g;
  1109. $glob=~s/\\\?/./g;
  1110. if ($page=~/^$glob$/i) {
  1111. if (! IkiWiki::isinternal($page) || $params{internal}) {
  1112. return IkiWiki::SuccessReason->new("$glob matches $page");
  1113. }
  1114. else {
  1115. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  1116. }
  1117. }
  1118. else {
  1119. return IkiWiki::FailReason->new("$glob does not match $page");
  1120. }
  1121. } #}}}
  1122. sub match_internal ($$;@) { #{{{
  1123. return match_glob($_[0], $_[1], @_, internal => 1)
  1124. } #}}}
  1125. sub match_link ($$;@) { #{{{
  1126. my $page=shift;
  1127. my $link=lc(shift);
  1128. my %params=@_;
  1129. my $from=exists $params{location} ? $params{location} : '';
  1130. # relative matching
  1131. if ($link =~ m!^\.! && defined $from) {
  1132. $from=~s#/?[^/]+$##;
  1133. $link=~s#^\./##;
  1134. $link="$from/$link" if length $from;
  1135. }
  1136. my $links = $IkiWiki::links{$page};
  1137. return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
  1138. my $bestlink = IkiWiki::bestlink($from, $link);
  1139. foreach my $p (@{$links}) {
  1140. if (length $bestlink) {
  1141. return IkiWiki::SuccessReason->new("$page links to $link")
  1142. if $bestlink eq IkiWiki::bestlink($page, $p);
  1143. }
  1144. else {
  1145. return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
  1146. if match_glob($p, $link, %params);
  1147. }
  1148. }
  1149. return IkiWiki::FailReason->new("$page does not link to $link");
  1150. } #}}}
  1151. sub match_backlink ($$;@) { #{{{
  1152. return match_link($_[1], $_[0], @_);
  1153. } #}}}
  1154. sub match_created_before ($$;@) { #{{{
  1155. my $page=shift;
  1156. my $testpage=shift;
  1157. if (exists $IkiWiki::pagectime{$testpage}) {
  1158. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  1159. return IkiWiki::SuccessReason->new("$page created before $testpage");
  1160. }
  1161. else {
  1162. return IkiWiki::FailReason->new("$page not created before $testpage");
  1163. }
  1164. }
  1165. else {
  1166. return IkiWiki::FailReason->new("$testpage has no ctime");
  1167. }
  1168. } #}}}
  1169. sub match_created_after ($$;@) { #{{{
  1170. my $page=shift;
  1171. my $testpage=shift;
  1172. if (exists $IkiWiki::pagectime{$testpage}) {
  1173. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  1174. return IkiWiki::SuccessReason->new("$page created after $testpage");
  1175. }
  1176. else {
  1177. return IkiWiki::FailReason->new("$page not created after $testpage");
  1178. }
  1179. }
  1180. else {
  1181. return IkiWiki::FailReason->new("$testpage has no ctime");
  1182. }
  1183. } #}}}
  1184. sub match_creation_day ($$;@) { #{{{
  1185. if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
  1186. return IkiWiki::SuccessReason->new('creation_day matched');
  1187. }
  1188. else {
  1189. return IkiWiki::FailReason->new('creation_day did not match');
  1190. }
  1191. } #}}}
  1192. sub match_creation_month ($$;@) { #{{{
  1193. if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
  1194. return IkiWiki::SuccessReason->new('creation_month matched');
  1195. }
  1196. else {
  1197. return IkiWiki::FailReason->new('creation_month did not match');
  1198. }
  1199. } #}}}
  1200. sub match_creation_year ($$;@) { #{{{
  1201. if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
  1202. return IkiWiki::SuccessReason->new('creation_year matched');
  1203. }
  1204. else {
  1205. return IkiWiki::FailReason->new('creation_year did not match');
  1206. }
  1207. } #}}}
  1208. 1