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