summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 453bc9f8ca0898b97c2242d36367ec2ecb077b13 (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 Storable;
  10. use open qw{:utf8 :std};
  11. use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
  12. %pagestate %wikistate %renderedfiles %oldrenderedfiles
  13. %pagesources %destsources %depends %depends_simple %hooks
  14. %forcerebuild %loaded_plugins};
  15. use Exporter q{import};
  16. our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
  17. pagespec_match_list bestlink htmllink readfile writefile
  18. pagetype srcfile pagename displaytime will_render gettext urlto
  19. targetpage add_underlay pagetitle titlepage linkpage
  20. newpagefile inject add_link
  21. %config %links %pagestate %wikistate %renderedfiles
  22. %pagesources %destsources);
  23. our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
  24. our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
  25. our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  26. # Optimisation.
  27. use Memoize;
  28. memoize("abs2rel");
  29. memoize("pagespec_translate");
  30. memoize("file_pruned");
  31. sub getsetup () {
  32. wikiname => {
  33. type => "string",
  34. default => "wiki",
  35. description => "name of the wiki",
  36. safe => 1,
  37. rebuild => 1,
  38. },
  39. adminemail => {
  40. type => "string",
  41. default => undef,
  42. example => 'me@example.com',
  43. description => "contact email for wiki",
  44. safe => 1,
  45. rebuild => 0,
  46. },
  47. adminuser => {
  48. type => "string",
  49. default => [],
  50. description => "users who are wiki admins",
  51. safe => 1,
  52. rebuild => 0,
  53. },
  54. banned_users => {
  55. type => "string",
  56. default => [],
  57. description => "users who are banned from the wiki",
  58. safe => 1,
  59. rebuild => 0,
  60. },
  61. srcdir => {
  62. type => "string",
  63. default => undef,
  64. example => "$ENV{HOME}/wiki",
  65. description => "where the source of the wiki is located",
  66. safe => 0, # path
  67. rebuild => 1,
  68. },
  69. destdir => {
  70. type => "string",
  71. default => undef,
  72. example => "/var/www/wiki",
  73. description => "where to build the wiki",
  74. safe => 0, # path
  75. rebuild => 1,
  76. },
  77. url => {
  78. type => "string",
  79. default => '',
  80. example => "http://example.com/wiki",
  81. description => "base url to the wiki",
  82. safe => 1,
  83. rebuild => 1,
  84. },
  85. cgiurl => {
  86. type => "string",
  87. default => '',
  88. example => "http://example.com/wiki/ikiwiki.cgi",
  89. description => "url to the ikiwiki.cgi",
  90. safe => 1,
  91. rebuild => 1,
  92. },
  93. cgi_wrapper => {
  94. type => "string",
  95. default => '',
  96. example => "/var/www/wiki/ikiwiki.cgi",
  97. description => "filename of cgi wrapper to generate",
  98. safe => 0, # file
  99. rebuild => 0,
  100. },
  101. cgi_wrappermode => {
  102. type => "string",
  103. default => '06755',
  104. description => "mode for cgi_wrapper (can safely be made suid)",
  105. safe => 0,
  106. rebuild => 0,
  107. },
  108. rcs => {
  109. type => "string",
  110. default => '',
  111. description => "rcs backend to use",
  112. safe => 0, # don't allow overriding
  113. rebuild => 0,
  114. },
  115. default_plugins => {
  116. type => "internal",
  117. default => [qw{mdwn link inline meta htmlscrubber passwordauth
  118. openid signinedit lockedit conditional
  119. recentchanges parentlinks editpage}],
  120. description => "plugins to enable by default",
  121. safe => 0,
  122. rebuild => 1,
  123. },
  124. add_plugins => {
  125. type => "string",
  126. default => [],
  127. description => "plugins to add to the default configuration",
  128. safe => 1,
  129. rebuild => 1,
  130. },
  131. disable_plugins => {
  132. type => "string",
  133. default => [],
  134. description => "plugins to disable",
  135. safe => 1,
  136. rebuild => 1,
  137. },
  138. templatedir => {
  139. type => "string",
  140. default => "$installdir/share/ikiwiki/templates",
  141. description => "location of template files",
  142. advanced => 1,
  143. safe => 0, # path
  144. rebuild => 1,
  145. },
  146. underlaydir => {
  147. type => "string",
  148. default => "$installdir/share/ikiwiki/basewiki",
  149. description => "base wiki source location",
  150. advanced => 1,
  151. safe => 0, # path
  152. rebuild => 0,
  153. },
  154. underlaydirbase => {
  155. type => "internal",
  156. default => "$installdir/share/ikiwiki",
  157. description => "parent directory containing additional underlays",
  158. safe => 0,
  159. rebuild => 0,
  160. },
  161. wrappers => {
  162. type => "internal",
  163. default => [],
  164. description => "wrappers to generate",
  165. safe => 0,
  166. rebuild => 0,
  167. },
  168. underlaydirs => {
  169. type => "internal",
  170. default => [],
  171. description => "additional underlays to use",
  172. safe => 0,
  173. rebuild => 0,
  174. },
  175. verbose => {
  176. type => "boolean",
  177. example => 1,
  178. description => "display verbose messages?",
  179. safe => 1,
  180. rebuild => 0,
  181. },
  182. syslog => {
  183. type => "boolean",
  184. example => 1,
  185. description => "log to syslog?",
  186. safe => 1,
  187. rebuild => 0,
  188. },
  189. usedirs => {
  190. type => "boolean",
  191. default => 1,
  192. description => "create output files named page/index.html?",
  193. safe => 0, # changing requires manual transition
  194. rebuild => 1,
  195. },
  196. prefix_directives => {
  197. type => "boolean",
  198. default => 1,
  199. description => "use '!'-prefixed preprocessor directives?",
  200. safe => 0, # changing requires manual transition
  201. rebuild => 1,
  202. },
  203. indexpages => {
  204. type => "boolean",
  205. default => 0,
  206. description => "use page/index.mdwn source files",
  207. safe => 1,
  208. rebuild => 1,
  209. },
  210. discussion => {
  211. type => "boolean",
  212. default => 1,
  213. description => "enable Discussion pages?",
  214. safe => 1,
  215. rebuild => 1,
  216. },
  217. discussionpage => {
  218. type => "string",
  219. default => gettext("Discussion"),
  220. description => "name of Discussion pages",
  221. safe => 1,
  222. rebuild => 1,
  223. },
  224. sslcookie => {
  225. type => "boolean",
  226. default => 0,
  227. description => "only send cookies over SSL connections?",
  228. advanced => 1,
  229. safe => 1,
  230. rebuild => 0,
  231. },
  232. default_pageext => {
  233. type => "string",
  234. default => "mdwn",
  235. description => "extension to use for new pages",
  236. safe => 0, # not sanitized
  237. rebuild => 0,
  238. },
  239. htmlext => {
  240. type => "string",
  241. default => "html",
  242. description => "extension to use for html files",
  243. safe => 0, # not sanitized
  244. rebuild => 1,
  245. },
  246. timeformat => {
  247. type => "string",
  248. default => '%c',
  249. description => "strftime format string to display date",
  250. advanced => 1,
  251. safe => 1,
  252. rebuild => 1,
  253. },
  254. locale => {
  255. type => "string",
  256. default => undef,
  257. example => "en_US.UTF-8",
  258. description => "UTF-8 locale to use",
  259. advanced => 1,
  260. safe => 0,
  261. rebuild => 1,
  262. },
  263. userdir => {
  264. type => "string",
  265. default => "",
  266. example => "users",
  267. description => "put user pages below specified page",
  268. safe => 1,
  269. rebuild => 1,
  270. },
  271. numbacklinks => {
  272. type => "integer",
  273. default => 10,
  274. description => "how many backlinks to show before hiding excess (0 to show all)",
  275. safe => 1,
  276. rebuild => 1,
  277. },
  278. hardlink => {
  279. type => "boolean",
  280. default => 0,
  281. description => "attempt to hardlink source files? (optimisation for large files)",
  282. advanced => 1,
  283. safe => 0, # paranoia
  284. rebuild => 0,
  285. },
  286. umask => {
  287. type => "integer",
  288. example => "022",
  289. description => "force ikiwiki to use a particular umask",
  290. advanced => 1,
  291. safe => 0, # paranoia
  292. rebuild => 0,
  293. },
  294. wrappergroup => {
  295. type => "string",
  296. example => "ikiwiki",
  297. description => "group for wrappers to run in",
  298. advanced => 1,
  299. safe => 0, # paranoia
  300. rebuild => 0,
  301. },
  302. libdir => {
  303. type => "string",
  304. default => "",
  305. example => "$ENV{HOME}/.ikiwiki/",
  306. description => "extra library and plugin directory",
  307. advanced => 1,
  308. safe => 0, # directory
  309. rebuild => 0,
  310. },
  311. ENV => {
  312. type => "string",
  313. default => {},
  314. description => "environment variables",
  315. safe => 0, # paranoia
  316. rebuild => 0,
  317. },
  318. exclude => {
  319. type => "string",
  320. default => undef,
  321. example => '\.wav$',
  322. description => "regexp of source files to ignore",
  323. advanced => 1,
  324. safe => 0, # regexp
  325. rebuild => 1,
  326. },
  327. wiki_file_prune_regexps => {
  328. type => "internal",
  329. default => [qr/(^|\/)\.\.(\/|$)/, qr/^\./, qr/\/\./,
  330. qr/\.x?html?$/, qr/\.ikiwiki-new$/,
  331. qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
  332. qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
  333. qr/(^|\/)CVS\//, qr/\.dpkg-tmp$/],
  334. description => "regexps of source files to ignore",
  335. safe => 0,
  336. rebuild => 1,
  337. },
  338. wiki_file_chars => {
  339. type => "string",
  340. description => "specifies the characters that are allowed in source filenames",
  341. default => "-[:alnum:]+/.:_",
  342. safe => 0,
  343. rebuild => 1,
  344. },
  345. wiki_file_regexp => {
  346. type => "internal",
  347. description => "regexp of legal source files",
  348. safe => 0,
  349. rebuild => 1,
  350. },
  351. web_commit_regexp => {
  352. type => "internal",
  353. default => qr/^web commit (by (.*?(?=: |$))|from (\d+\.\d+\.\d+\.\d+)):?(.*)/,
  354. description => "regexp to parse web commits from logs",
  355. safe => 0,
  356. rebuild => 0,
  357. },
  358. cgi => {
  359. type => "internal",
  360. default => 0,
  361. description => "run as a cgi",
  362. safe => 0,
  363. rebuild => 0,
  364. },
  365. cgi_disable_uploads => {
  366. type => "internal",
  367. default => 1,
  368. description => "whether CGI should accept file uploads",
  369. safe => 0,
  370. rebuild => 0,
  371. },
  372. post_commit => {
  373. type => "internal",
  374. default => 0,
  375. description => "run as a post-commit hook",
  376. safe => 0,
  377. rebuild => 0,
  378. },
  379. rebuild => {
  380. type => "internal",
  381. default => 0,
  382. description => "running in rebuild mode",
  383. safe => 0,
  384. rebuild => 0,
  385. },
  386. setup => {
  387. type => "internal",
  388. default => undef,
  389. description => "running in setup mode",
  390. safe => 0,
  391. rebuild => 0,
  392. },
  393. refresh => {
  394. type => "internal",
  395. default => 0,
  396. description => "running in refresh mode",
  397. safe => 0,
  398. rebuild => 0,
  399. },
  400. test_receive => {
  401. type => "internal",
  402. default => 0,
  403. description => "running in receive test mode",
  404. safe => 0,
  405. rebuild => 0,
  406. },
  407. getctime => {
  408. type => "internal",
  409. default => 0,
  410. description => "running in getctime mode",
  411. safe => 0,
  412. rebuild => 0,
  413. },
  414. w3mmode => {
  415. type => "internal",
  416. default => 0,
  417. description => "running in w3mmode",
  418. safe => 0,
  419. rebuild => 0,
  420. },
  421. wikistatedir => {
  422. type => "internal",
  423. default => undef,
  424. description => "path to the .ikiwiki directory holding ikiwiki state",
  425. safe => 0,
  426. rebuild => 0,
  427. },
  428. setupfile => {
  429. type => "internal",
  430. default => undef,
  431. description => "path to setup file",
  432. safe => 0,
  433. rebuild => 0,
  434. },
  435. allow_symlinks_before_srcdir => {
  436. type => "boolean",
  437. default => 0,
  438. description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
  439. safe => 0,
  440. rebuild => 0,
  441. },
  442. }
  443. sub defaultconfig () {
  444. my %s=getsetup();
  445. my @ret;
  446. foreach my $key (keys %s) {
  447. push @ret, $key, $s{$key}->{default};
  448. }
  449. use Data::Dumper;
  450. return @ret;
  451. }
  452. sub checkconfig () {
  453. # locale stuff; avoid LC_ALL since it overrides everything
  454. if (defined $ENV{LC_ALL}) {
  455. $ENV{LANG} = $ENV{LC_ALL};
  456. delete $ENV{LC_ALL};
  457. }
  458. if (defined $config{locale}) {
  459. if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
  460. $ENV{LANG}=$config{locale};
  461. define_gettext();
  462. }
  463. }
  464. if (! defined $config{wiki_file_regexp}) {
  465. $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
  466. }
  467. if (ref $config{ENV} eq 'HASH') {
  468. foreach my $val (keys %{$config{ENV}}) {
  469. $ENV{$val}=$config{ENV}{$val};
  470. }
  471. }
  472. if ($config{w3mmode}) {
  473. eval q{use Cwd q{abs_path}};
  474. error($@) if $@;
  475. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  476. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  477. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  478. unless $config{cgiurl} =~ m!file:///!;
  479. $config{url}="file://".$config{destdir};
  480. }
  481. if ($config{cgi} && ! length $config{url}) {
  482. error(gettext("Must specify url to wiki with --url when using --cgi"));
  483. }
  484. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  485. unless exists $config{wikistatedir} && defined $config{wikistatedir};
  486. if (defined $config{umask}) {
  487. umask(possibly_foolish_untaint($config{umask}));
  488. }
  489. run_hooks(checkconfig => sub { shift->() });
  490. return 1;
  491. }
  492. sub listplugins () {
  493. my %ret;
  494. foreach my $dir (@INC, $config{libdir}) {
  495. next unless defined $dir && length $dir;
  496. foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
  497. my ($plugin)=$file=~/.*\/(.*)\.pm$/;
  498. $ret{$plugin}=1;
  499. }
  500. }
  501. foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
  502. next unless defined $dir && length $dir;
  503. foreach my $file (glob("$dir/plugins/*")) {
  504. $ret{basename($file)}=1 if -x $file;
  505. }
  506. }
  507. return keys %ret;
  508. }
  509. sub loadplugins () {
  510. if (defined $config{libdir} && length $config{libdir}) {
  511. unshift @INC, possibly_foolish_untaint($config{libdir});
  512. }
  513. foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
  514. loadplugin($plugin);
  515. }
  516. if ($config{rcs}) {
  517. if (exists $IkiWiki::hooks{rcs}) {
  518. error(gettext("cannot use multiple rcs plugins"));
  519. }
  520. loadplugin($config{rcs});
  521. }
  522. if (! exists $IkiWiki::hooks{rcs}) {
  523. loadplugin("norcs");
  524. }
  525. run_hooks(getopt => sub { shift->() });
  526. if (grep /^-/, @ARGV) {
  527. print STDERR "Unknown option (or missing parameter): $_\n"
  528. foreach grep /^-/, @ARGV;
  529. usage();
  530. }
  531. return 1;
  532. }
  533. sub loadplugin ($) {
  534. my $plugin=shift;
  535. return if grep { $_ eq $plugin} @{$config{disable_plugins}};
  536. foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
  537. "$installdir/lib/ikiwiki") {
  538. if (defined $dir && -x "$dir/plugins/$plugin") {
  539. eval { require IkiWiki::Plugin::external };
  540. if ($@) {
  541. my $reason=$@;
  542. error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
  543. }
  544. import IkiWiki::Plugin::external "$dir/plugins/$plugin";
  545. $loaded_plugins{$plugin}=1;
  546. return 1;
  547. }
  548. }
  549. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  550. eval qq{use $mod};
  551. if ($@) {
  552. error("Failed to load plugin $mod: $@");
  553. }
  554. $loaded_plugins{$plugin}=1;
  555. return 1;
  556. }
  557. sub error ($;$) {
  558. my $message=shift;
  559. my $cleaner=shift;
  560. log_message('err' => $message) if $config{syslog};
  561. if (defined $cleaner) {
  562. $cleaner->();
  563. }
  564. die $message."\n";
  565. }
  566. sub debug ($) {
  567. return unless $config{verbose};
  568. return log_message(debug => @_);
  569. }
  570. my $log_open=0;
  571. sub log_message ($$) {
  572. my $type=shift;
  573. if ($config{syslog}) {
  574. require Sys::Syslog;
  575. if (! $log_open) {
  576. Sys::Syslog::setlogsock('unix');
  577. Sys::Syslog::openlog('ikiwiki', '', 'user');
  578. $log_open=1;
  579. }
  580. return eval {
  581. Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
  582. };
  583. }
  584. elsif (! $config{cgi}) {
  585. return print "@_\n";
  586. }
  587. else {
  588. return print STDERR "@_\n";
  589. }
  590. }
  591. sub possibly_foolish_untaint ($) {
  592. my $tainted=shift;
  593. my ($untainted)=$tainted=~/(.*)/s;
  594. return $untainted;
  595. }
  596. sub basename ($) {
  597. my $file=shift;
  598. $file=~s!.*/+!!;
  599. return $file;
  600. }
  601. sub dirname ($) {
  602. my $file=shift;
  603. $file=~s!/*[^/]+$!!;
  604. return $file;
  605. }
  606. sub isinternal ($) {
  607. my $page=shift;
  608. return exists $pagesources{$page} &&
  609. $pagesources{$page} =~ /\._([^.]+)$/;
  610. }
  611. sub pagetype ($) {
  612. my $file=shift;
  613. if ($file =~ /\.([^.]+)$/) {
  614. return $1 if exists $hooks{htmlize}{$1};
  615. }
  616. my $base=basename($file);
  617. if (exists $hooks{htmlize}{$base} &&
  618. $hooks{htmlize}{$base}{noextension}) {
  619. return $base;
  620. }
  621. return;
  622. }
  623. my %pagename_cache;
  624. sub pagename ($) {
  625. my $file=shift;
  626. if (exists $pagename_cache{$file}) {
  627. return $pagename_cache{$file};
  628. }
  629. my $type=pagetype($file);
  630. my $page=$file;
  631. $page=~s/\Q.$type\E*$//
  632. if defined $type && !$hooks{htmlize}{$type}{keepextension}
  633. && !$hooks{htmlize}{$type}{noextension};
  634. if ($config{indexpages} && $page=~/(.*)\/index$/) {
  635. $page=$1;
  636. }
  637. $pagename_cache{$file} = $page;
  638. return $page;
  639. }
  640. sub newpagefile ($$) {
  641. my $page=shift;
  642. my $type=shift;
  643. if (! $config{indexpages} || $page eq 'index') {
  644. return $page.".".$type;
  645. }
  646. else {
  647. return $page."/index.".$type;
  648. }
  649. }
  650. sub targetpage ($$;$) {
  651. my $page=shift;
  652. my $ext=shift;
  653. my $filename=shift;
  654. if (defined $filename) {
  655. return $page."/".$filename.".".$ext;
  656. }
  657. elsif (! $config{usedirs} || $page eq 'index') {
  658. return $page.".".$ext;
  659. }
  660. else {
  661. return $page."/index.".$ext;
  662. }
  663. }
  664. sub htmlpage ($) {
  665. my $page=shift;
  666. return targetpage($page, $config{htmlext});
  667. }
  668. sub srcfile_stat {
  669. my $file=shift;
  670. my $nothrow=shift;
  671. return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
  672. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  673. return "$dir/$file", stat(_) if -e "$dir/$file";
  674. }
  675. error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
  676. return;
  677. }
  678. sub srcfile ($;$) {
  679. return (srcfile_stat(@_))[0];
  680. }
  681. sub add_underlay ($) {
  682. my $dir=shift;
  683. if ($dir !~ /^\//) {
  684. $dir="$config{underlaydirbase}/$dir";
  685. }
  686. if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
  687. unshift @{$config{underlaydirs}}, $dir;
  688. }
  689. return 1;
  690. }
  691. sub readfile ($;$$) {
  692. my $file=shift;
  693. my $binary=shift;
  694. my $wantfd=shift;
  695. if (-l $file) {
  696. error("cannot read a symlink ($file)");
  697. }
  698. local $/=undef;
  699. open (my $in, "<", $file) || error("failed to read $file: $!");
  700. binmode($in) if ($binary);
  701. return \*$in if $wantfd;
  702. my $ret=<$in>;
  703. # check for invalid utf-8, and toss it back to avoid crashes
  704. if (! utf8::valid($ret)) {
  705. $ret=encode_utf8($ret);
  706. }
  707. close $in || error("failed to read $file: $!");
  708. return $ret;
  709. }
  710. sub prep_writefile ($$) {
  711. my $file=shift;
  712. my $destdir=shift;
  713. my $test=$file;
  714. while (length $test) {
  715. if (-l "$destdir/$test") {
  716. error("cannot write to a symlink ($test)");
  717. }
  718. $test=dirname($test);
  719. }
  720. my $dir=dirname("$destdir/$file");
  721. if (! -d $dir) {
  722. my $d="";
  723. foreach my $s (split(m!/+!, $dir)) {
  724. $d.="$s/";
  725. if (! -d $d) {
  726. mkdir($d) || error("failed to create directory $d: $!");
  727. }
  728. }
  729. }
  730. return 1;
  731. }
  732. sub writefile ($$$;$$) {
  733. my $file=shift; # can include subdirs
  734. my $destdir=shift; # directory to put file in
  735. my $content=shift;
  736. my $binary=shift;
  737. my $writer=shift;
  738. prep_writefile($file, $destdir);
  739. my $newfile="$destdir/$file.ikiwiki-new";
  740. if (-l $newfile) {
  741. error("cannot write to a symlink ($newfile)");
  742. }
  743. my $cleanup = sub { unlink($newfile) };
  744. open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
  745. binmode($out) if ($binary);
  746. if ($writer) {
  747. $writer->(\*$out, $cleanup);
  748. }
  749. else {
  750. print $out $content or error("failed writing to $newfile: $!", $cleanup);
  751. }
  752. close $out || error("failed saving $newfile: $!", $cleanup);
  753. rename($newfile, "$destdir/$file") ||
  754. error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
  755. return 1;
  756. }
  757. my %cleared;
  758. sub will_render ($$;$) {
  759. my $page=shift;
  760. my $dest=shift;
  761. my $clear=shift;
  762. # Important security check.
  763. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  764. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
  765. error("$config{destdir}/$dest independently created, not overwriting with version from $page");
  766. }
  767. if (! $clear || $cleared{$page}) {
  768. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  769. }
  770. else {
  771. foreach my $old (@{$renderedfiles{$page}}) {
  772. delete $destsources{$old};
  773. }
  774. $renderedfiles{$page}=[$dest];
  775. $cleared{$page}=1;
  776. }
  777. $destsources{$dest}=$page;
  778. return 1;
  779. }
  780. sub bestlink ($$) {
  781. my $page=shift;
  782. my $link=shift;
  783. my $cwd=$page;
  784. if ($link=~s/^\/+//) {
  785. # absolute links
  786. $cwd="";
  787. }
  788. $link=~s/\/$//;
  789. do {
  790. my $l=$cwd;
  791. $l.="/" if length $l;
  792. $l.=$link;
  793. if (exists $links{$l}) {
  794. return $l;
  795. }
  796. elsif (exists $pagecase{lc $l}) {
  797. return $pagecase{lc $l};
  798. }
  799. } while $cwd=~s{/?[^/]+$}{};
  800. if (length $config{userdir}) {
  801. my $l = "$config{userdir}/".lc($link);
  802. if (exists $links{$l}) {
  803. return $l;
  804. }
  805. elsif (exists $pagecase{lc $l}) {
  806. return $pagecase{lc $l};
  807. }
  808. }
  809. #print STDERR "warning: page $page, broken link: $link\n";
  810. return "";
  811. }
  812. sub isinlinableimage ($) {
  813. my $file=shift;
  814. return $file =~ /\.(png|gif|jpg|jpeg)$/i;
  815. }
  816. sub pagetitle ($;$) {
  817. my $page=shift;
  818. my $unescaped=shift;
  819. if ($unescaped) {
  820. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
  821. }
  822. else {
  823. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
  824. }
  825. return $page;
  826. }
  827. sub titlepage ($) {
  828. my $title=shift;
  829. # support use w/o %config set
  830. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  831. $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  832. return $title;
  833. }
  834. sub linkpage ($) {
  835. my $link=shift;
  836. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  837. $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  838. return $link;
  839. }
  840. sub cgiurl (@) {
  841. my %params=@_;
  842. return $config{cgiurl}."?".
  843. join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
  844. }
  845. sub baseurl (;$) {
  846. my $page=shift;
  847. return "$config{url}/" if ! defined $page;
  848. $page=htmlpage($page);
  849. $page=~s/[^\/]+$//;
  850. $page=~s/[^\/]+\//..\//g;
  851. return $page;
  852. }
  853. sub abs2rel ($$) {
  854. # Work around very innefficient behavior in File::Spec if abs2rel
  855. # is passed two relative paths. It's much faster if paths are
  856. # absolute! (Debian bug #376658; fixed in debian unstable now)
  857. my $path="/".shift;
  858. my $base="/".shift;
  859. require File::Spec;
  860. my $ret=File::Spec->abs2rel($path, $base);
  861. $ret=~s/^// if defined $ret;
  862. return $ret;
  863. }
  864. sub displaytime ($;$) {
  865. # Plugins can override this function to mark up the time to
  866. # display.
  867. return '<span class="date">'.formattime(@_).'</span>';
  868. }
  869. sub formattime ($;$) {
  870. # Plugins can override this function to format the time.
  871. my $time=shift;
  872. my $format=shift;
  873. if (! defined $format) {
  874. $format=$config{timeformat};
  875. }
  876. # strftime doesn't know about encodings, so make sure
  877. # its output is properly treated as utf8
  878. return decode_utf8(POSIX::strftime($format, localtime($time)));
  879. }
  880. sub beautify_urlpath ($) {
  881. my $url=shift;
  882. # Ensure url is not an empty link, and if necessary,
  883. # add ./ to avoid colon confusion.
  884. if ($url !~ /^\// && $url !~ /^\.\.?\//) {
  885. $url="./$url";
  886. }
  887. if ($config{usedirs}) {
  888. $url =~ s!/index.$config{htmlext}$!/!;
  889. }
  890. return $url;
  891. }
  892. sub urlto ($$;$) {
  893. my $to=shift;
  894. my $from=shift;
  895. my $absolute=shift;
  896. if (! length $to) {
  897. return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
  898. }
  899. if (! $destsources{$to}) {
  900. $to=htmlpage($to);
  901. }
  902. if ($absolute) {
  903. return $config{url}.beautify_urlpath("/".$to);
  904. }
  905. my $link = abs2rel($to, dirname(htmlpage($from)));
  906. return beautify_urlpath($link);
  907. }
  908. sub htmllink ($$$;@) {
  909. my $lpage=shift; # the page doing the linking
  910. my $page=shift; # the page that will contain the link (different for inline)
  911. my $link=shift;
  912. my %opts=@_;
  913. $link=~s/\/$//;
  914. my $bestlink;
  915. if (! $opts{forcesubpage}) {
  916. $bestlink=bestlink($lpage, $link);
  917. }
  918. else {
  919. $bestlink="$lpage/".lc($link);
  920. }
  921. my $linktext;
  922. if (defined $opts{linktext}) {
  923. $linktext=$opts{linktext};
  924. }
  925. else {
  926. $linktext=pagetitle(basename($link));
  927. }
  928. return "<span class=\"selflink\">$linktext</span>"
  929. if length $bestlink && $page eq $bestlink &&
  930. ! defined $opts{anchor};
  931. if (! $destsources{$bestlink}) {
  932. $bestlink=htmlpage($bestlink);
  933. if (! $destsources{$bestlink}) {
  934. return $linktext unless length $config{cgiurl};
  935. return "<span class=\"createlink\"><a href=\"".
  936. cgiurl(
  937. do => "create",
  938. page => lc($link),
  939. from => $lpage
  940. ).
  941. "\" rel=\"nofollow\">?</a>$linktext</span>"
  942. }
  943. }
  944. $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
  945. $bestlink=beautify_urlpath($bestlink);
  946. if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
  947. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  948. }
  949. if (defined $opts{anchor}) {
  950. $bestlink.="#".$opts{anchor};
  951. }
  952. my @attrs;
  953. if (defined $opts{rel}) {
  954. push @attrs, ' rel="'.$opts{rel}.'"';
  955. }
  956. if (defined $opts{class}) {
  957. push @attrs, ' class="'.$opts{class}.'"';
  958. }
  959. return "<a href=\"$bestlink\"@attrs>$linktext</a>";
  960. }
  961. sub openiduser ($) {
  962. my $user=shift;
  963. if ($user =~ m!^https?://! &&
  964. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  965. my $display;
  966. if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
  967. # this works in at least 2.x
  968. $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
  969. }
  970. else {
  971. # this only works in 1.x
  972. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  973. $display=$oid->display;
  974. }
  975. # Convert "user.somehost.com" to "user [somehost.com]"
  976. # (also "user.somehost.co.uk")
  977. if ($display !~ /\[/) {
  978. $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
  979. }
  980. # Convert "http://somehost.com/user" to "user [somehost.com]".
  981. # (also "https://somehost.com/user/")
  982. if ($display !~ /\[/) {
  983. $display=~s/^https?:\/\/(.+)\/([^\/]+)\/?$/$2 [$1]/;
  984. }
  985. $display=~s!^https?://!!; # make sure this is removed
  986. eval q{use CGI 'escapeHTML'};
  987. error($@) if $@;
  988. return escapeHTML($display);
  989. }
  990. return;
  991. }
  992. sub userlink ($) {
  993. my $user=shift;
  994. my $oiduser=eval { openiduser($user) };
  995. if (defined $oiduser) {
  996. return "<a href=\"$user\">$oiduser</a>";
  997. }
  998. else {
  999. eval q{use CGI 'escapeHTML'};
  1000. error($@) if $@;
  1001. return htmllink("", "", escapeHTML(
  1002. length $config{userdir} ? $config{userdir}."/".$user : $user
  1003. ), noimageinline => 1);
  1004. }
  1005. }
  1006. sub htmlize ($$$$) {
  1007. my $page=shift;
  1008. my $destpage=shift;
  1009. my $type=shift;
  1010. my $content=shift;
  1011. my $oneline = $content !~ /\n/;
  1012. if (exists $hooks{htmlize}{$type}) {
  1013. $content=$hooks{htmlize}{$type}{call}->(
  1014. page => $page,
  1015. content => $content,
  1016. );
  1017. }
  1018. else {
  1019. error("htmlization of $type not supported");
  1020. }
  1021. run_hooks(sanitize => sub {
  1022. $content=shift->(
  1023. page => $page,
  1024. destpage => $destpage,
  1025. content => $content,
  1026. );
  1027. });
  1028. if ($oneline) {
  1029. # hack to get rid of enclosing junk added by markdown
  1030. # and other htmlizers
  1031. $content=~s/^<p>//i;
  1032. $content=~s/<\/p>$//i;
  1033. chomp $content;
  1034. }
  1035. return $content;
  1036. }
  1037. sub linkify ($$$) {
  1038. my $page=shift;
  1039. my $destpage=shift;
  1040. my $content=shift;
  1041. run_hooks(linkify => sub {
  1042. $content=shift->(
  1043. page => $page,
  1044. destpage => $destpage,
  1045. content => $content,
  1046. );
  1047. });
  1048. return $content;
  1049. }
  1050. our %preprocessing;
  1051. our $preprocess_preview=0;
  1052. sub preprocess ($$$;$$) {
  1053. my $page=shift; # the page the data comes from
  1054. my $destpage=shift; # the page the data will appear in (different for inline)
  1055. my $content=shift;
  1056. my $scan=shift;
  1057. my $preview=shift;
  1058. # Using local because it needs to be set within any nested calls
  1059. # of this function.
  1060. local $preprocess_preview=$preview if defined $preview;
  1061. my $handle=sub {
  1062. my $escape=shift;
  1063. my $prefix=shift;
  1064. my $command=shift;
  1065. my $params=shift;
  1066. $params="" if ! defined $params;
  1067. if (length $escape) {
  1068. return "[[$prefix$command $params]]";
  1069. }
  1070. elsif (exists $hooks{preprocess}{$command}) {
  1071. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  1072. # Note: preserve order of params, some plugins may
  1073. # consider it significant.
  1074. my @params;
  1075. while ($params =~ m{
  1076. (?:([-\w]+)=)? # 1: named parameter key?
  1077. (?:
  1078. """(.*?)""" # 2: triple-quoted value
  1079. |
  1080. "([^"]+)" # 3: single-quoted value
  1081. |
  1082. (\S+) # 4: unquoted value
  1083. )
  1084. (?:\s+|$) # delimiter to next param
  1085. }sgx) {
  1086. my $key=$1;
  1087. my $val;
  1088. if (defined $2) {
  1089. $val=$2;
  1090. $val=~s/\r\n/\n/mg;
  1091. $val=~s/^\n+//g;
  1092. $val=~s/\n+$//g;
  1093. }
  1094. elsif (defined $3) {
  1095. $val=$3;
  1096. }
  1097. elsif (defined $4) {
  1098. $val=$4;
  1099. }
  1100. if (defined $key) {
  1101. push @params, $key, $val;
  1102. }
  1103. else {
  1104. push @params, $val, '';
  1105. }
  1106. }
  1107. if ($preprocessing{$page}++ > 3) {
  1108. # Avoid loops of preprocessed pages preprocessing
  1109. # other pages that preprocess them, etc.
  1110. return "[[!$command <span class=\"error\">".
  1111. sprintf(gettext("preprocessing loop detected on %s at depth %i"),
  1112. $page, $preprocessing{$page}).
  1113. "</span>]]";
  1114. }
  1115. my $ret;
  1116. if (! $scan) {
  1117. $ret=eval {
  1118. $hooks{preprocess}{$command}{call}->(
  1119. @params,
  1120. page => $page,
  1121. destpage => $destpage,
  1122. preview => $preprocess_preview,
  1123. );
  1124. };
  1125. if ($@) {
  1126. my $error=$@;
  1127. chomp $error;
  1128. $ret="[[!$command <span class=\"error\">".
  1129. gettext("Error").": $error"."</span>]]";
  1130. }
  1131. }
  1132. else {
  1133. # use void context during scan pass
  1134. eval {
  1135. $hooks{preprocess}{$command}{call}->(
  1136. @params,
  1137. page => $page,
  1138. destpage => $destpage,
  1139. preview => $preprocess_preview,
  1140. );
  1141. };
  1142. $ret="";
  1143. }
  1144. $preprocessing{$page}--;
  1145. return $ret;
  1146. }
  1147. else {
  1148. return "[[$prefix$command $params]]";
  1149. }
  1150. };
  1151. my $regex;
  1152. if ($config{prefix_directives}) {
  1153. $regex = qr{
  1154. (\\?) # 1: escape?
  1155. \[\[(!) # directive open; 2: prefix
  1156. ([-\w]+) # 3: command
  1157. ( # 4: the parameters..
  1158. \s+ # Must have space if parameters present
  1159. (?:
  1160. (?:[-\w]+=)? # named parameter key?
  1161. (?:
  1162. """.*?""" # triple-quoted value
  1163. |
  1164. "[^"]+" # single-quoted value
  1165. |
  1166. [^"\s\]]+ # unquoted value
  1167. )
  1168. \s* # whitespace or end
  1169. # of directive
  1170. )
  1171. *)? # 0 or more parameters
  1172. \]\] # directive closed
  1173. }sx;
  1174. }
  1175. else {
  1176. $regex = qr{
  1177. (\\?) # 1: escape?
  1178. \[\[(!?) # directive open; 2: optional prefix
  1179. ([-\w]+) # 3: command
  1180. \s+
  1181. ( # 4: the parameters..
  1182. (?:
  1183. (?:[-\w]+=)? # named parameter key?
  1184. (?:
  1185. """.*?""" # triple-quoted value
  1186. |
  1187. "[^"]+" # single-quoted value
  1188. |
  1189. [^"\s\]]+ # unquoted value
  1190. )
  1191. \s* # whitespace or end
  1192. # of directive
  1193. )
  1194. *) # 0 or more parameters
  1195. \]\] # directive closed
  1196. }sx;
  1197. }
  1198. $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
  1199. return $content;
  1200. }
  1201. sub filter ($$$) {
  1202. my $page=shift;
  1203. my $destpage=shift;
  1204. my $content=shift;
  1205. run_hooks(filter => sub {
  1206. $content=shift->(page => $page, destpage => $destpage,
  1207. content => $content);
  1208. });
  1209. return $content;
  1210. }
  1211. sub indexlink () {
  1212. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  1213. }
  1214. sub check_canedit ($$$;$) {
  1215. my $page=shift;
  1216. my $q=shift;
  1217. my $session=shift;
  1218. my $nonfatal=shift;
  1219. my $canedit;
  1220. run_hooks(canedit => sub {
  1221. return if defined $canedit;
  1222. my $ret=shift->($page, $q, $session);
  1223. if (defined $ret) {
  1224. if ($ret eq "") {
  1225. $canedit=1;
  1226. }
  1227. elsif (ref $ret eq 'CODE') {
  1228. $ret->() unless $nonfatal;
  1229. $canedit=0;
  1230. }
  1231. elsif (defined $ret) {
  1232. error($ret) unless $nonfatal;
  1233. $canedit=0;
  1234. }
  1235. }
  1236. });
  1237. return defined $canedit ? $canedit : 1;
  1238. }
  1239. sub check_content (@) {
  1240. my %params=@_;
  1241. return 1 if ! exists $hooks{checkcontent}; # optimisation
  1242. if (exists $pagesources{$params{page}}) {
  1243. my @diff;
  1244. my %old=map { $_ => 1 }
  1245. split("\n", readfile(srcfile($pagesources{$params{page}})));
  1246. foreach my $line (split("\n", $params{content})) {
  1247. push @diff, $line if ! exists $old{$_};
  1248. }
  1249. $params{diff}=join("\n", @diff);
  1250. }
  1251. my $ok;
  1252. run_hooks(checkcontent => sub {
  1253. return if defined $ok;
  1254. my $ret=shift->(%params);
  1255. if (defined $ret) {
  1256. if ($ret eq "") {
  1257. $ok=1;
  1258. }
  1259. elsif (ref $ret eq 'CODE') {
  1260. $ret->() unless $params{nonfatal};
  1261. $ok=0;
  1262. }
  1263. elsif (defined $ret) {
  1264. error($ret) unless $params{nonfatal};
  1265. $ok=0;
  1266. }
  1267. }
  1268. });
  1269. return defined $ok ? $ok : 1;
  1270. }
  1271. my $wikilock;
  1272. sub lockwiki () {
  1273. # Take an exclusive lock on the wiki to prevent multiple concurrent
  1274. # run issues. The lock will be dropped on program exit.
  1275. if (! -d $config{wikistatedir}) {
  1276. mkdir($config{wikistatedir});
  1277. }
  1278. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  1279. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  1280. if (! flock($wikilock, 2)) { # LOCK_EX
  1281. error("failed to get lock");
  1282. }
  1283. return 1;
  1284. }
  1285. sub unlockwiki () {
  1286. POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
  1287. return close($wikilock) if $wikilock;
  1288. return;
  1289. }
  1290. my $commitlock;
  1291. sub commit_hook_enabled () {
  1292. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  1293. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1294. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  1295. close($commitlock) || error("failed closing commitlock: $!");
  1296. return 0;
  1297. }
  1298. close($commitlock) || error("failed closing commitlock: $!");
  1299. return 1;
  1300. }
  1301. sub disable_commit_hook () {
  1302. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  1303. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1304. if (! flock($commitlock, 2)) { # LOCK_EX
  1305. error("failed to get commit lock");
  1306. }
  1307. return 1;
  1308. }
  1309. sub enable_commit_hook () {
  1310. return close($commitlock) if $commitlock;
  1311. return;
  1312. }
  1313. sub loadindex () {
  1314. %oldrenderedfiles=%pagectime=();
  1315. if (! $config{rebuild}) {
  1316. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  1317. %destsources=%renderedfiles=%pagecase=%pagestate=
  1318. %depends_simple=();
  1319. }
  1320. my $in;
  1321. if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
  1322. if (-e "$config{wikistatedir}/index") {
  1323. system("ikiwiki-transition", "indexdb", $config{srcdir});
  1324. open ($in, "<", "$config{wikistatedir}/indexdb") || return;
  1325. }
  1326. else {
  1327. return;
  1328. }
  1329. }
  1330. my $index=Storable::fd_retrieve($in);
  1331. if (! defined $index) {
  1332. return 0;
  1333. }
  1334. my $pages;
  1335. if (exists $index->{version} && ! ref $index->{version}) {
  1336. $pages=$index->{page};
  1337. %wikistate=%{$index->{state}};
  1338. }
  1339. else {
  1340. $pages=$index;
  1341. %wikistate=();
  1342. }
  1343. foreach my $src (keys %$pages) {
  1344. my $d=$pages->{$src};
  1345. my $page=pagename($src);
  1346. $pagectime{$page}=$d->{ctime};
  1347. if (! $config{rebuild}) {
  1348. $pagesources{$page}=$src;
  1349. $pagemtime{$page}=$d->{mtime};
  1350. $renderedfiles{$page}=$d->{dest};
  1351. if (exists $d->{links} && ref $d->{links}) {
  1352. $links{$page}=$d->{links};
  1353. $oldlinks{$page}=[@{$d->{links}}];
  1354. }
  1355. if (exists $d->{depends_simple}) {
  1356. $depends_simple{$page}={
  1357. map { $_ => 1 } @{$d->{depends_simple}}
  1358. };
  1359. }
  1360. if (exists $d->{dependslist}) {
  1361. $depends{$page}={
  1362. map { $_ => 1 } @{$d->{dependslist}}
  1363. };
  1364. }
  1365. elsif (exists $d->{depends}) {
  1366. $depends{$page}={$d->{depends} => 1};
  1367. }
  1368. if (exists $d->{state}) {
  1369. $pagestate{$page}=$d->{state};
  1370. }
  1371. }
  1372. $oldrenderedfiles{$page}=[@{$d->{dest}}];
  1373. }
  1374. foreach my $page (keys %pagesources) {
  1375. $pagecase{lc $page}=$page;
  1376. }
  1377. foreach my $page (keys %renderedfiles) {
  1378. $destsources{$_}=$page foreach @{$renderedfiles{$page}};
  1379. }
  1380. return close($in);
  1381. }
  1382. sub saveindex () {
  1383. run_hooks(savestate => sub { shift->() });
  1384. my %hookids;
  1385. foreach my $type (keys %hooks) {
  1386. $hookids{$_}=1 foreach keys %{$hooks{$type}};
  1387. }
  1388. my @hookids=keys %hookids;
  1389. if (! -d $config{wikistatedir}) {
  1390. mkdir($config{wikistatedir});
  1391. }
  1392. my $newfile="$config{wikistatedir}/indexdb.new";
  1393. my $cleanup = sub { unlink($newfile) };
  1394. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  1395. my %index;
  1396. foreach my $page (keys %pagemtime) {
  1397. next unless $pagemtime{$page};
  1398. my $src=$pagesources{$page};
  1399. $index{page}{$src}={
  1400. ctime => $pagectime{$page},
  1401. mtime => $pagemtime{$page},
  1402. dest => $renderedfiles{$page},
  1403. links => $links{$page},
  1404. };
  1405. if (exists $depends{$page}) {
  1406. $index{page}{$src}{dependslist} = [ keys %{$depends{$page}} ];
  1407. }
  1408. if (exists $depends_simple{$page}) {
  1409. $index{page}{$src}{depends_simple} = [ keys %{$depends_simple{$page}} ];
  1410. }
  1411. if (exists $pagestate{$page}) {
  1412. foreach my $id (@hookids) {
  1413. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  1414. $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
  1415. }
  1416. }
  1417. }
  1418. }
  1419. $index{state}={};
  1420. foreach my $id (@hookids) {
  1421. foreach my $key (keys %{$wikistate{$id}}) {
  1422. $index{state}{$id}{$key}=$wikistate{$id}{$key};
  1423. }
  1424. }
  1425. $index{version}="3";
  1426. my $ret=Storable::nstore_fd(\%index, $out);
  1427. return if ! defined $ret || ! $ret;
  1428. close $out || error("failed saving to $newfile: $!", $cleanup);
  1429. rename($newfile, "$config{wikistatedir}/indexdb") ||
  1430. error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
  1431. return 1;
  1432. }
  1433. sub template_file ($) {
  1434. my $template=shift;
  1435. foreach my $dir ($config{templatedir}, "$installdir/share/ikiwiki/templates") {
  1436. return "$dir/$template" if -e "$dir/$template";
  1437. }
  1438. return;
  1439. }
  1440. sub template_params (@) {
  1441. my $filename=template_file(shift);
  1442. if (! defined $filename) {
  1443. return if wantarray;
  1444. return "";
  1445. }
  1446. my @ret=(
  1447. filter => sub {
  1448. my $text_ref = shift;
  1449. ${$text_ref} = decode_utf8(${$text_ref});
  1450. },
  1451. filename => $filename,
  1452. loop_context_vars => 1,
  1453. die_on_bad_params => 0,
  1454. @_
  1455. );
  1456. return wantarray ? @ret : {@ret};
  1457. }
  1458. sub template ($;@) {
  1459. require HTML::Template;
  1460. return HTML::Template->new(template_params(@_));
  1461. }
  1462. sub misctemplate ($$;@) {
  1463. my $title=shift;
  1464. my $pagebody=shift;
  1465. my $template=template("misc.tmpl");
  1466. $template->param(
  1467. title => $title,
  1468. indexlink => indexlink(),
  1469. wikiname => $config{wikiname},
  1470. pagebody => $pagebody,
  1471. baseurl => baseurl(),
  1472. @_,
  1473. );
  1474. run_hooks(pagetemplate => sub {
  1475. shift->(page => "", destpage => "", template => $template);
  1476. });
  1477. return $template->output;
  1478. }
  1479. sub hook (@) {
  1480. my %param=@_;
  1481. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  1482. error 'hook requires type, call, and id parameters';
  1483. }
  1484. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  1485. $hooks{$param{type}}{$param{id}}=\%param;
  1486. return 1;
  1487. }
  1488. sub run_hooks ($$) {
  1489. # Calls the given sub for each hook of the given type,
  1490. # passing it the hook function to call.
  1491. my $type=shift;
  1492. my $sub=shift;
  1493. if (exists $hooks{$type}) {
  1494. my (@first, @middle, @last);
  1495. foreach my $id (keys %{$hooks{$type}}) {
  1496. if ($hooks{$type}{$id}{first}) {
  1497. push @first, $id;
  1498. }
  1499. elsif ($hooks{$type}{$id}{last}) {
  1500. push @last, $id;
  1501. }
  1502. else {
  1503. push @middle, $id;
  1504. }
  1505. }
  1506. foreach my $id (@first, @middle, @last) {
  1507. $sub->($hooks{$type}{$id}{call});
  1508. }
  1509. }
  1510. return 1;
  1511. }
  1512. sub rcs_update () {
  1513. $hooks{rcs}{rcs_update}{call}->(@_);
  1514. }
  1515. sub rcs_prepedit ($) {
  1516. $hooks{rcs}{rcs_prepedit}{call}->(@_);
  1517. }
  1518. sub rcs_commit ($$$;$$) {
  1519. $hooks{rcs}{rcs_commit}{call}->(@_);
  1520. }
  1521. sub rcs_commit_staged ($$$) {
  1522. $hooks{rcs}{rcs_commit_staged}{call}->(@_);
  1523. }
  1524. sub rcs_add ($) {
  1525. $hooks{rcs}{rcs_add}{call}->(@_);
  1526. }
  1527. sub rcs_remove ($) {
  1528. $hooks{rcs}{rcs_remove}{call}->(@_);
  1529. }
  1530. sub rcs_rename ($$) {
  1531. $hooks{rcs}{rcs_rename}{call}->(@_);
  1532. }
  1533. sub rcs_recentchanges ($) {
  1534. $hooks{rcs}{rcs_recentchanges}{call}->(@_);
  1535. }
  1536. sub rcs_diff ($) {
  1537. $hooks{rcs}{rcs_diff}{call}->(@_);
  1538. }
  1539. sub rcs_getctime ($) {
  1540. $hooks{rcs}{rcs_getctime}{call}->(@_);
  1541. }
  1542. sub rcs_receive () {
  1543. $hooks{rcs}{rcs_receive}{call}->();
  1544. }
  1545. sub add_depends ($$) {
  1546. my $page=shift;
  1547. my $pagespec=shift;
  1548. if ($pagespec =~ /$config{wiki_file_regexp}/ &&
  1549. $pagespec !~ /[\s*?()!]/) {
  1550. # a simple dependency, which can be matched by string eq
  1551. $depends_simple{$page}{lc $pagespec} = 1;
  1552. return 1;
  1553. }
  1554. return unless pagespec_valid($pagespec);
  1555. $depends{$page}{$pagespec} = 1;
  1556. return 1;
  1557. }
  1558. sub file_pruned ($$) {
  1559. require File::Spec;
  1560. my $file=File::Spec->canonpath(shift);
  1561. my $base=File::Spec->canonpath(shift);
  1562. $file =~ s#^\Q$base\E/+##;
  1563. my $regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  1564. return $file =~ m/$regexp/ && $file ne $base;
  1565. }
  1566. sub define_gettext () {
  1567. # If translation is needed, redefine the gettext function to do it.
  1568. # Otherwise, it becomes a quick no-op.
  1569. no warnings 'redefine';
  1570. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  1571. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  1572. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  1573. *gettext=sub {
  1574. my $gettext_obj=eval q{
  1575. use Locale::gettext q{textdomain};
  1576. Locale::gettext->domain('ikiwiki')
  1577. };
  1578. if ($gettext_obj) {
  1579. $gettext_obj->get(shift);
  1580. }
  1581. else {
  1582. return shift;
  1583. }
  1584. };
  1585. }
  1586. else {
  1587. *gettext=sub { return shift };
  1588. }
  1589. }
  1590. sub gettext {
  1591. define_gettext();
  1592. gettext(@_);
  1593. }
  1594. sub yesno ($) {
  1595. my $val=shift;
  1596. return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
  1597. }
  1598. sub inject {
  1599. # Injects a new function into the symbol table to replace an
  1600. # exported function.
  1601. my %params=@_;
  1602. # This is deep ugly perl foo, beware.
  1603. no strict;
  1604. no warnings;
  1605. if (! defined $params{parent}) {
  1606. $params{parent}='::';
  1607. $params{old}=\&{$params{name}};
  1608. $params{name}=~s/.*:://;
  1609. }
  1610. my $parent=$params{parent};
  1611. foreach my $ns (grep /^\w+::/, keys %{$parent}) {
  1612. $ns = $params{parent} . $ns;
  1613. inject(%params, parent => $ns) unless $ns eq '::main::';
  1614. *{$ns . $params{name}} = $params{call}
  1615. if exists ${$ns}{$params{name}} &&
  1616. \&{${$ns}{$params{name}}} == $params{old};
  1617. }
  1618. use strict;
  1619. use warnings;
  1620. }
  1621. sub add_link ($$) {
  1622. my $page=shift;
  1623. my $link=shift;
  1624. push @{$links{$page}}, $link
  1625. unless grep { $_ eq $link } @{$links{$page}};
  1626. }
  1627. sub pagespec_translate ($) {
  1628. my $spec=shift;
  1629. # Convert spec to perl code.
  1630. my $code="";
  1631. my @data;
  1632. while ($spec=~m{
  1633. \s* # ignore whitespace
  1634. ( # 1: match a single word
  1635. \! # !
  1636. |
  1637. \( # (
  1638. |
  1639. \) # )
  1640. |
  1641. \w+\([^\)]*\) # command(params)
  1642. |
  1643. [^\s()]+ # any other text
  1644. )
  1645. \s* # ignore whitespace
  1646. }igx) {
  1647. my $word=$1;
  1648. if (lc $word eq 'and') {
  1649. $code.=' &&';
  1650. }
  1651. elsif (lc $word eq 'or') {
  1652. $code.=' ||';
  1653. }
  1654. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  1655. $code.=' '.$word;
  1656. }
  1657. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  1658. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  1659. push @data, $2;
  1660. $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
  1661. }
  1662. else {
  1663. push @data, qq{unknown function in pagespec "$word"};
  1664. $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
  1665. }
  1666. }
  1667. else {
  1668. push @data, $word;
  1669. $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
  1670. }
  1671. }
  1672. if (! length $code) {
  1673. $code="IkiWiki::FailReason->new('empty pagespec')";
  1674. }
  1675. no warnings;
  1676. return eval 'sub { my $page=shift; '.$code.' }';
  1677. }
  1678. sub pagespec_match ($$;@) {
  1679. my $page=shift;
  1680. my $spec=shift;
  1681. my @params=@_;
  1682. # Backwards compatability with old calling convention.
  1683. if (@params == 1) {
  1684. unshift @params, 'location';
  1685. }
  1686. my $sub=pagespec_translate($spec);
  1687. return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
  1688. if $@ || ! defined $sub;
  1689. return $sub->($page, @params);
  1690. }
  1691. sub pagespec_match_list ($$;@) {
  1692. my $pages=shift;
  1693. my $spec=shift;
  1694. my @params=@_;
  1695. my $sub=pagespec_translate($spec);
  1696. error "syntax error in pagespec \"$spec\""
  1697. if $@ || ! defined $sub;
  1698. my @ret;
  1699. my $r;
  1700. foreach my $page (@$pages) {
  1701. $r=$sub->($page, @params);
  1702. push @ret, $page if $r;
  1703. }
  1704. if (! @ret && defined $r && $r->isa("IkiWiki::ErrorReason")) {
  1705. error(sprintf(gettext("cannot match pages: %s"), $r));
  1706. }
  1707. else {
  1708. return @ret;
  1709. }
  1710. }
  1711. sub pagespec_valid ($) {
  1712. my $spec=shift;
  1713. my $sub=pagespec_translate($spec);
  1714. return ! $@;
  1715. }
  1716. sub glob2re ($) {
  1717. my $re=quotemeta(shift);
  1718. $re=~s/\\\*/.*/g;
  1719. $re=~s/\\\?/./g;
  1720. return $re;
  1721. }
  1722. package IkiWiki::FailReason;
  1723. use overload (
  1724. '""' => sub { ${$_[0]} },
  1725. '0+' => sub { 0 },
  1726. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  1727. fallback => 1,
  1728. );
  1729. sub new {
  1730. my $class = shift;
  1731. my $value = shift;
  1732. return bless \$value, $class;
  1733. }
  1734. package IkiWiki::ErrorReason;
  1735. our @ISA = 'IkiWiki::FailReason';
  1736. package IkiWiki::SuccessReason;
  1737. use overload (
  1738. '""' => sub { ${$_[0]} },
  1739. '0+' => sub { 1 },
  1740. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  1741. fallback => 1,
  1742. );
  1743. sub new {
  1744. my $class = shift;
  1745. my $value = shift;
  1746. return bless \$value, $class;
  1747. };
  1748. package IkiWiki::PageSpec;
  1749. sub derel ($$) {
  1750. my $path=shift;
  1751. my $from=shift;
  1752. if ($path =~ m!^\./!) {
  1753. $from=~s#/?[^/]+$## if defined $from;
  1754. $path=~s#^\./##;
  1755. $path="$from/$path" if length $from;
  1756. }
  1757. return $path;
  1758. }
  1759. sub match_glob ($$;@) {
  1760. my $page=shift;
  1761. my $glob=shift;
  1762. my %params=@_;
  1763. $glob=derel($glob, $params{location});
  1764. my $regexp=IkiWiki::glob2re($glob);
  1765. if ($page=~/^$regexp$/i) {
  1766. if (! IkiWiki::isinternal($page) || $params{internal}) {
  1767. return IkiWiki::SuccessReason->new("$glob matches $page");
  1768. }
  1769. else {
  1770. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  1771. }
  1772. }
  1773. else {
  1774. return IkiWiki::FailReason->new("$glob does not match $page");
  1775. }
  1776. }
  1777. sub match_internal ($$;@) {
  1778. return match_glob($_[0], $_[1], @_, internal => 1)
  1779. }
  1780. sub match_link ($$;@) {
  1781. my $page=shift;
  1782. my $link=lc(shift);
  1783. my %params=@_;
  1784. $link=derel($link, $params{location});
  1785. my $from=exists $params{location} ? $params{location} : '';
  1786. my $links = $IkiWiki::links{$page};
  1787. return IkiWiki::FailReason->new("$page has no links") unless $links && @{$links};
  1788. my $bestlink = IkiWiki::bestlink($from, $link);
  1789. foreach my $p (@{$links}) {
  1790. if (length $bestlink) {
  1791. return IkiWiki::SuccessReason->new("$page links to $link")
  1792. if $bestlink eq IkiWiki::bestlink($page, $p);
  1793. }
  1794. else {
  1795. return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
  1796. if match_glob($p, $link, %params);
  1797. $p=~s/^\///;
  1798. $link=~s/^\///;
  1799. return IkiWiki::SuccessReason->new("$page links to page $p matching $link")
  1800. if match_glob($p, $link, %params);
  1801. }
  1802. }
  1803. return IkiWiki::FailReason->new("$page does not link to $link");
  1804. }
  1805. sub match_backlink ($$;@) {
  1806. return match_link($_[1], $_[0], @_);
  1807. }
  1808. sub match_created_before ($$;@) {
  1809. my $page=shift;
  1810. my $testpage=shift;
  1811. my %params=@_;
  1812. $testpage=derel($testpage, $params{location});
  1813. if (exists $IkiWiki::pagectime{$testpage}) {
  1814. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  1815. return IkiWiki::SuccessReason->new("$page created before $testpage");
  1816. }
  1817. else {
  1818. return IkiWiki::FailReason->new("$page not created before $testpage");
  1819. }
  1820. }
  1821. else {
  1822. return IkiWiki::ErrorReason->new("$testpage does not exist");
  1823. }
  1824. }
  1825. sub match_created_after ($$;@) {
  1826. my $page=shift;
  1827. my $testpage=shift;
  1828. my %params=@_;
  1829. $testpage=derel($testpage, $params{location});
  1830. if (exists $IkiWiki::pagectime{$testpage}) {
  1831. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  1832. return IkiWiki::SuccessReason->new("$page created after $testpage");
  1833. }
  1834. else {
  1835. return IkiWiki::FailReason->new("$page not created after $testpage");
  1836. }
  1837. }
  1838. else {
  1839. return IkiWiki::ErrorReason->new("$testpage does not exist");
  1840. }
  1841. }
  1842. sub match_creation_day ($$;@) {
  1843. if ((gmtime($IkiWiki::pagectime{shift()}))[3] == shift) {
  1844. return IkiWiki::SuccessReason->new('creation_day matched');
  1845. }
  1846. else {
  1847. return IkiWiki::FailReason->new('creation_day did not match');
  1848. }
  1849. }
  1850. sub match_creation_month ($$;@) {
  1851. if ((gmtime($IkiWiki::pagectime{shift()}))[4] + 1 == shift) {
  1852. return IkiWiki::SuccessReason->new('creation_month matched');
  1853. }
  1854. else {
  1855. return IkiWiki::FailReason->new('creation_month did not match');
  1856. }
  1857. }
  1858. sub match_creation_year ($$;@) {
  1859. if ((gmtime($IkiWiki::pagectime{shift()}))[5] + 1900 == shift) {
  1860. return IkiWiki::SuccessReason->new('creation_year matched');
  1861. }
  1862. else {
  1863. return IkiWiki::FailReason->new('creation_year did not match');
  1864. }
  1865. }
  1866. sub match_user ($$;@) {
  1867. shift;
  1868. my $user=shift;
  1869. my %params=@_;
  1870. if (! exists $params{user}) {
  1871. return IkiWiki::ErrorReason->new("no user specified");
  1872. }
  1873. if (defined $params{user} && lc $params{user} eq lc $user) {
  1874. return IkiWiki::SuccessReason->new("user is $user");
  1875. }
  1876. elsif (! defined $params{user}) {
  1877. return IkiWiki::FailReason->new("not logged in");
  1878. }
  1879. else {
  1880. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  1881. }
  1882. }
  1883. sub match_admin ($$;@) {
  1884. shift;
  1885. shift;
  1886. my %params=@_;
  1887. if (! exists $params{user}) {
  1888. return IkiWiki::ErrorReason->new("no user specified");
  1889. }
  1890. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  1891. return IkiWiki::SuccessReason->new("user is an admin");
  1892. }
  1893. elsif (! defined $params{user}) {
  1894. return IkiWiki::FailReason->new("not logged in");
  1895. }
  1896. else {
  1897. return IkiWiki::FailReason->new("user is not an admin");
  1898. }
  1899. }
  1900. sub match_ip ($$;@) {
  1901. shift;
  1902. my $ip=shift;
  1903. my %params=@_;
  1904. if (! exists $params{ip}) {
  1905. return IkiWiki::ErrorReason->new("no IP specified");
  1906. }
  1907. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  1908. return IkiWiki::SuccessReason->new("IP is $ip");
  1909. }
  1910. else {
  1911. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  1912. }
  1913. }
  1914. 1