summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: c06751dd36bc0bf656e9b7abfc49d5c8a9455c47 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use URI::Escape q{uri_escape_utf8};
  7. use POSIX ();
  8. use Storable;
  9. use open qw{:utf8 :std};
  10. use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
  11. %pagestate %wikistate %renderedfiles %oldrenderedfiles
  12. %pagesources %delpagesources %destsources %depends %depends_simple
  13. @mass_depends %hooks %forcerebuild %loaded_plugins %typedlinks
  14. %oldtypedlinks %autofiles};
  15. use Exporter q{import};
  16. our @EXPORT = qw(hook debug error htmlpage template template_depends
  17. deptype add_depends pagespec_match pagespec_match_list bestlink
  18. htmllink readfile writefile pagetype srcfile pagename
  19. displaytime will_render gettext ngettext urlto targetpage
  20. add_underlay pagetitle titlepage linkpage newpagefile
  21. inject add_link add_autofile
  22. %config %links %pagestate %wikistate %renderedfiles
  23. %pagesources %destsources %typedlinks);
  24. our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
  25. our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
  26. our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE
  27. # Page dependency types.
  28. our $DEPEND_CONTENT=1;
  29. our $DEPEND_PRESENCE=2;
  30. our $DEPEND_LINKS=4;
  31. # Optimisation.
  32. use Memoize;
  33. memoize("abs2rel");
  34. memoize("sortspec_translate");
  35. memoize("pagespec_translate");
  36. memoize("template_file");
  37. sub getsetup () {
  38. wikiname => {
  39. type => "string",
  40. default => "wiki",
  41. description => "name of the wiki",
  42. safe => 1,
  43. rebuild => 1,
  44. },
  45. adminemail => {
  46. type => "string",
  47. default => undef,
  48. example => 'me@example.com',
  49. description => "contact email for wiki",
  50. safe => 1,
  51. rebuild => 0,
  52. },
  53. adminuser => {
  54. type => "string",
  55. default => [],
  56. description => "users who are wiki admins",
  57. safe => 1,
  58. rebuild => 0,
  59. },
  60. banned_users => {
  61. type => "string",
  62. default => [],
  63. description => "users who are banned from the wiki",
  64. safe => 1,
  65. rebuild => 0,
  66. },
  67. srcdir => {
  68. type => "string",
  69. default => undef,
  70. example => "$ENV{HOME}/wiki",
  71. description => "where the source of the wiki is located",
  72. safe => 0, # path
  73. rebuild => 1,
  74. },
  75. destdir => {
  76. type => "string",
  77. default => undef,
  78. example => "/var/www/wiki",
  79. description => "where to build the wiki",
  80. safe => 0, # path
  81. rebuild => 1,
  82. },
  83. url => {
  84. type => "string",
  85. default => '',
  86. example => "http://example.com/wiki",
  87. description => "base url to the wiki",
  88. safe => 1,
  89. rebuild => 1,
  90. },
  91. cgiurl => {
  92. type => "string",
  93. default => '',
  94. example => "http://example.com/wiki/ikiwiki.cgi",
  95. description => "url to the ikiwiki.cgi",
  96. safe => 1,
  97. rebuild => 1,
  98. },
  99. cgi_wrapper => {
  100. type => "string",
  101. default => '',
  102. example => "/var/www/wiki/ikiwiki.cgi",
  103. description => "filename of cgi wrapper to generate",
  104. safe => 0, # file
  105. rebuild => 0,
  106. },
  107. cgi_wrappermode => {
  108. type => "string",
  109. default => '06755',
  110. description => "mode for cgi_wrapper (can safely be made suid)",
  111. safe => 0,
  112. rebuild => 0,
  113. },
  114. rcs => {
  115. type => "string",
  116. default => '',
  117. description => "rcs backend to use",
  118. safe => 0, # don't allow overriding
  119. rebuild => 0,
  120. },
  121. default_plugins => {
  122. type => "internal",
  123. default => [qw{mdwn link inline meta htmlscrubber passwordauth
  124. openid signinedit lockedit conditional
  125. recentchanges parentlinks editpage}],
  126. description => "plugins to enable by default",
  127. safe => 0,
  128. rebuild => 1,
  129. },
  130. add_plugins => {
  131. type => "string",
  132. default => [],
  133. description => "plugins to add to the default configuration",
  134. safe => 1,
  135. rebuild => 1,
  136. },
  137. disable_plugins => {
  138. type => "string",
  139. default => [],
  140. description => "plugins to disable",
  141. safe => 1,
  142. rebuild => 1,
  143. },
  144. templatedir => {
  145. type => "string",
  146. default => "$installdir/share/ikiwiki/templates",
  147. description => "additional directory to search for template files",
  148. advanced => 1,
  149. safe => 0, # path
  150. rebuild => 1,
  151. },
  152. underlaydir => {
  153. type => "string",
  154. default => "$installdir/share/ikiwiki/basewiki",
  155. description => "base wiki source location",
  156. advanced => 1,
  157. safe => 0, # path
  158. rebuild => 0,
  159. },
  160. underlaydirbase => {
  161. type => "internal",
  162. default => "$installdir/share/ikiwiki",
  163. description => "parent directory containing additional underlays",
  164. safe => 0,
  165. rebuild => 0,
  166. },
  167. wrappers => {
  168. type => "internal",
  169. default => [],
  170. description => "wrappers to generate",
  171. safe => 0,
  172. rebuild => 0,
  173. },
  174. underlaydirs => {
  175. type => "internal",
  176. default => [],
  177. description => "additional underlays to use",
  178. safe => 0,
  179. rebuild => 0,
  180. },
  181. verbose => {
  182. type => "boolean",
  183. example => 1,
  184. description => "display verbose messages?",
  185. safe => 1,
  186. rebuild => 0,
  187. },
  188. syslog => {
  189. type => "boolean",
  190. example => 1,
  191. description => "log to syslog?",
  192. safe => 1,
  193. rebuild => 0,
  194. },
  195. usedirs => {
  196. type => "boolean",
  197. default => 1,
  198. description => "create output files named page/index.html?",
  199. safe => 0, # changing requires manual transition
  200. rebuild => 1,
  201. },
  202. prefix_directives => {
  203. type => "boolean",
  204. default => 1,
  205. description => "use '!'-prefixed preprocessor directives?",
  206. safe => 0, # changing requires manual transition
  207. rebuild => 1,
  208. },
  209. indexpages => {
  210. type => "boolean",
  211. default => 0,
  212. description => "use page/index.mdwn source files",
  213. safe => 1,
  214. rebuild => 1,
  215. },
  216. discussion => {
  217. type => "boolean",
  218. default => 1,
  219. description => "enable Discussion pages?",
  220. safe => 1,
  221. rebuild => 1,
  222. },
  223. discussionpage => {
  224. type => "string",
  225. default => gettext("Discussion"),
  226. description => "name of Discussion pages",
  227. safe => 1,
  228. rebuild => 1,
  229. },
  230. html5 => {
  231. type => "boolean",
  232. default => 0,
  233. description => "generate HTML5? (experimental)",
  234. advanced => 1,
  235. safe => 1,
  236. rebuild => 1,
  237. },
  238. sslcookie => {
  239. type => "boolean",
  240. default => 0,
  241. description => "only send cookies over SSL connections?",
  242. advanced => 1,
  243. safe => 1,
  244. rebuild => 0,
  245. },
  246. default_pageext => {
  247. type => "string",
  248. default => "mdwn",
  249. description => "extension to use for new pages",
  250. safe => 0, # not sanitized
  251. rebuild => 0,
  252. },
  253. htmlext => {
  254. type => "string",
  255. default => "html",
  256. description => "extension to use for html files",
  257. safe => 0, # not sanitized
  258. rebuild => 1,
  259. },
  260. timeformat => {
  261. type => "string",
  262. default => '%c',
  263. description => "strftime format string to display date",
  264. advanced => 1,
  265. safe => 1,
  266. rebuild => 1,
  267. },
  268. locale => {
  269. type => "string",
  270. default => undef,
  271. example => "en_US.UTF-8",
  272. description => "UTF-8 locale to use",
  273. advanced => 1,
  274. safe => 0,
  275. rebuild => 1,
  276. },
  277. userdir => {
  278. type => "string",
  279. default => "",
  280. example => "users",
  281. description => "put user pages below specified page",
  282. safe => 1,
  283. rebuild => 1,
  284. },
  285. numbacklinks => {
  286. type => "integer",
  287. default => 10,
  288. description => "how many backlinks to show before hiding excess (0 to show all)",
  289. safe => 1,
  290. rebuild => 1,
  291. },
  292. hardlink => {
  293. type => "boolean",
  294. default => 0,
  295. description => "attempt to hardlink source files? (optimisation for large files)",
  296. advanced => 1,
  297. safe => 0, # paranoia
  298. rebuild => 0,
  299. },
  300. umask => {
  301. type => "integer",
  302. example => "022",
  303. description => "force ikiwiki to use a particular umask",
  304. advanced => 1,
  305. safe => 0, # paranoia
  306. rebuild => 0,
  307. },
  308. wrappergroup => {
  309. type => "string",
  310. example => "ikiwiki",
  311. description => "group for wrappers to run in",
  312. advanced => 1,
  313. safe => 0, # paranoia
  314. rebuild => 0,
  315. },
  316. libdir => {
  317. type => "string",
  318. default => "",
  319. example => "$ENV{HOME}/.ikiwiki/",
  320. description => "extra library and plugin directory",
  321. advanced => 1,
  322. safe => 0, # directory
  323. rebuild => 0,
  324. },
  325. ENV => {
  326. type => "string",
  327. default => {},
  328. description => "environment variables",
  329. safe => 0, # paranoia
  330. rebuild => 0,
  331. },
  332. include => {
  333. type => "string",
  334. default => undef,
  335. example => '^\.htaccess$',
  336. description => "regexp of normally excluded files to include",
  337. advanced => 1,
  338. safe => 0, # regexp
  339. rebuild => 1,
  340. },
  341. exclude => {
  342. type => "string",
  343. default => undef,
  344. example => '^(*\.private|Makefile)$',
  345. description => "regexp of files that should be skipped",
  346. advanced => 1,
  347. safe => 0, # regexp
  348. rebuild => 1,
  349. },
  350. wiki_file_prune_regexps => {
  351. type => "internal",
  352. default => [qr/(^|\/)\.\.(\/|$)/, qr/^\//, qr/^\./, qr/\/\./,
  353. qr/\.x?html?$/, qr/\.ikiwiki-new$/,
  354. qr/(^|\/).svn\//, qr/.arch-ids\//, qr/{arch}\//,
  355. qr/(^|\/)_MTN\//, qr/(^|\/)_darcs\//,
  356. qr/(^|\/)CVS\//, qr/\.dpkg-tmp$/],
  357. description => "regexps of source files to ignore",
  358. safe => 0,
  359. rebuild => 1,
  360. },
  361. wiki_file_chars => {
  362. type => "string",
  363. description => "specifies the characters that are allowed in source filenames",
  364. default => "-[:alnum:]+/.:_",
  365. safe => 0,
  366. rebuild => 1,
  367. },
  368. wiki_file_regexp => {
  369. type => "internal",
  370. description => "regexp of legal source files",
  371. safe => 0,
  372. rebuild => 1,
  373. },
  374. web_commit_regexp => {
  375. type => "internal",
  376. default => qr/^web commit (by (.*?(?=: |$))|from ([0-9a-fA-F:.]+[0-9a-fA-F])):?(.*)/,
  377. description => "regexp to parse web commits from logs",
  378. safe => 0,
  379. rebuild => 0,
  380. },
  381. cgi => {
  382. type => "internal",
  383. default => 0,
  384. description => "run as a cgi",
  385. safe => 0,
  386. rebuild => 0,
  387. },
  388. cgi_disable_uploads => {
  389. type => "internal",
  390. default => 1,
  391. description => "whether CGI should accept file uploads",
  392. safe => 0,
  393. rebuild => 0,
  394. },
  395. post_commit => {
  396. type => "internal",
  397. default => 0,
  398. description => "run as a post-commit hook",
  399. safe => 0,
  400. rebuild => 0,
  401. },
  402. rebuild => {
  403. type => "internal",
  404. default => 0,
  405. description => "running in rebuild mode",
  406. safe => 0,
  407. rebuild => 0,
  408. },
  409. setup => {
  410. type => "internal",
  411. default => undef,
  412. description => "running in setup mode",
  413. safe => 0,
  414. rebuild => 0,
  415. },
  416. clean => {
  417. type => "internal",
  418. default => 0,
  419. description => "running in clean mode",
  420. safe => 0,
  421. rebuild => 0,
  422. },
  423. refresh => {
  424. type => "internal",
  425. default => 0,
  426. description => "running in refresh mode",
  427. safe => 0,
  428. rebuild => 0,
  429. },
  430. test_receive => {
  431. type => "internal",
  432. default => 0,
  433. description => "running in receive test mode",
  434. safe => 0,
  435. rebuild => 0,
  436. },
  437. wrapper_background_command => {
  438. type => "internal",
  439. default => '',
  440. description => "background shell command to run",
  441. safe => 0,
  442. rebuild => 0,
  443. },
  444. gettime => {
  445. type => "internal",
  446. description => "running in gettime mode",
  447. safe => 0,
  448. rebuild => 0,
  449. },
  450. w3mmode => {
  451. type => "internal",
  452. default => 0,
  453. description => "running in w3mmode",
  454. safe => 0,
  455. rebuild => 0,
  456. },
  457. wikistatedir => {
  458. type => "internal",
  459. default => undef,
  460. description => "path to the .ikiwiki directory holding ikiwiki state",
  461. safe => 0,
  462. rebuild => 0,
  463. },
  464. setupfile => {
  465. type => "internal",
  466. default => undef,
  467. description => "path to setup file",
  468. safe => 0,
  469. rebuild => 0,
  470. },
  471. setuptype => {
  472. type => "internal",
  473. default => "Standard",
  474. description => "perl class to use to dump setup file",
  475. safe => 0,
  476. rebuild => 0,
  477. },
  478. allow_symlinks_before_srcdir => {
  479. type => "boolean",
  480. default => 0,
  481. description => "allow symlinks in the path leading to the srcdir (potentially insecure)",
  482. safe => 0,
  483. rebuild => 0,
  484. },
  485. }
  486. sub defaultconfig () {
  487. my %s=getsetup();
  488. my @ret;
  489. foreach my $key (keys %s) {
  490. push @ret, $key, $s{$key}->{default};
  491. }
  492. use Data::Dumper;
  493. return @ret;
  494. }
  495. sub checkconfig () {
  496. # locale stuff; avoid LC_ALL since it overrides everything
  497. if (defined $ENV{LC_ALL}) {
  498. $ENV{LANG} = $ENV{LC_ALL};
  499. delete $ENV{LC_ALL};
  500. }
  501. if (defined $config{locale}) {
  502. if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
  503. $ENV{LANG}=$config{locale};
  504. define_gettext();
  505. }
  506. }
  507. if (! defined $config{wiki_file_regexp}) {
  508. $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
  509. }
  510. if (ref $config{ENV} eq 'HASH') {
  511. foreach my $val (keys %{$config{ENV}}) {
  512. $ENV{$val}=$config{ENV}{$val};
  513. }
  514. }
  515. if ($config{w3mmode}) {
  516. eval q{use Cwd q{abs_path}};
  517. error($@) if $@;
  518. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  519. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  520. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  521. unless $config{cgiurl} =~ m!file:///!;
  522. $config{url}="file://".$config{destdir};
  523. }
  524. if ($config{cgi} && ! length $config{url}) {
  525. error(gettext("Must specify url to wiki with --url when using --cgi"));
  526. }
  527. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  528. unless exists $config{wikistatedir} && defined $config{wikistatedir};
  529. if (defined $config{umask}) {
  530. umask(possibly_foolish_untaint($config{umask}));
  531. }
  532. run_hooks(checkconfig => sub { shift->() });
  533. return 1;
  534. }
  535. sub listplugins () {
  536. my %ret;
  537. foreach my $dir (@INC, $config{libdir}) {
  538. next unless defined $dir && length $dir;
  539. foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
  540. my ($plugin)=$file=~/.*\/(.*)\.pm$/;
  541. $ret{$plugin}=1;
  542. }
  543. }
  544. foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
  545. next unless defined $dir && length $dir;
  546. foreach my $file (glob("$dir/plugins/*")) {
  547. $ret{basename($file)}=1 if -x $file;
  548. }
  549. }
  550. return keys %ret;
  551. }
  552. sub loadplugins () {
  553. if (defined $config{libdir} && length $config{libdir}) {
  554. unshift @INC, possibly_foolish_untaint($config{libdir});
  555. }
  556. foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
  557. loadplugin($plugin);
  558. }
  559. if ($config{rcs}) {
  560. if (exists $hooks{rcs}) {
  561. error(gettext("cannot use multiple rcs plugins"));
  562. }
  563. loadplugin($config{rcs});
  564. }
  565. if (! exists $hooks{rcs}) {
  566. loadplugin("norcs");
  567. }
  568. run_hooks(getopt => sub { shift->() });
  569. if (grep /^-/, @ARGV) {
  570. print STDERR "Unknown option (or missing parameter): $_\n"
  571. foreach grep /^-/, @ARGV;
  572. usage();
  573. }
  574. return 1;
  575. }
  576. sub loadplugin ($;$) {
  577. my $plugin=shift;
  578. my $force=shift;
  579. return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
  580. foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
  581. "$installdir/lib/ikiwiki") {
  582. if (defined $dir && -x "$dir/plugins/$plugin") {
  583. eval { require IkiWiki::Plugin::external };
  584. if ($@) {
  585. my $reason=$@;
  586. error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
  587. }
  588. import IkiWiki::Plugin::external "$dir/plugins/$plugin";
  589. $loaded_plugins{$plugin}=1;
  590. return 1;
  591. }
  592. }
  593. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  594. eval qq{use $mod};
  595. if ($@) {
  596. error("Failed to load plugin $mod: $@");
  597. }
  598. $loaded_plugins{$plugin}=1;
  599. return 1;
  600. }
  601. sub error ($;$) {
  602. my $message=shift;
  603. my $cleaner=shift;
  604. log_message('err' => $message) if $config{syslog};
  605. if (defined $cleaner) {
  606. $cleaner->();
  607. }
  608. die $message."\n";
  609. }
  610. sub debug ($) {
  611. return unless $config{verbose};
  612. return log_message(debug => @_);
  613. }
  614. my $log_open=0;
  615. sub log_message ($$) {
  616. my $type=shift;
  617. if ($config{syslog}) {
  618. require Sys::Syslog;
  619. if (! $log_open) {
  620. Sys::Syslog::setlogsock('unix');
  621. Sys::Syslog::openlog('ikiwiki', '', 'user');
  622. $log_open=1;
  623. }
  624. return eval {
  625. Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
  626. };
  627. }
  628. elsif (! $config{cgi}) {
  629. return print "@_\n";
  630. }
  631. else {
  632. return print STDERR "@_\n";
  633. }
  634. }
  635. sub possibly_foolish_untaint ($) {
  636. my $tainted=shift;
  637. my ($untainted)=$tainted=~/(.*)/s;
  638. return $untainted;
  639. }
  640. sub basename ($) {
  641. my $file=shift;
  642. $file=~s!.*/+!!;
  643. return $file;
  644. }
  645. sub dirname ($) {
  646. my $file=shift;
  647. $file=~s!/*[^/]+$!!;
  648. return $file;
  649. }
  650. sub isinternal ($) {
  651. my $page=shift;
  652. return exists $pagesources{$page} &&
  653. $pagesources{$page} =~ /\._([^.]+)$/;
  654. }
  655. sub pagetype ($) {
  656. my $file=shift;
  657. if ($file =~ /\.([^.]+)$/) {
  658. return $1 if exists $hooks{htmlize}{$1};
  659. }
  660. my $base=basename($file);
  661. if (exists $hooks{htmlize}{$base} &&
  662. $hooks{htmlize}{$base}{noextension}) {
  663. return $base;
  664. }
  665. return;
  666. }
  667. my %pagename_cache;
  668. sub pagename ($) {
  669. my $file=shift;
  670. if (exists $pagename_cache{$file}) {
  671. return $pagename_cache{$file};
  672. }
  673. my $type=pagetype($file);
  674. my $page=$file;
  675. $page=~s/\Q.$type\E*$//
  676. if defined $type && !$hooks{htmlize}{$type}{keepextension}
  677. && !$hooks{htmlize}{$type}{noextension};
  678. if ($config{indexpages} && $page=~/(.*)\/index$/) {
  679. $page=$1;
  680. }
  681. $pagename_cache{$file} = $page;
  682. return $page;
  683. }
  684. sub newpagefile ($$) {
  685. my $page=shift;
  686. my $type=shift;
  687. if (! $config{indexpages} || $page eq 'index') {
  688. return $page.".".$type;
  689. }
  690. else {
  691. return $page."/index.".$type;
  692. }
  693. }
  694. sub targetpage ($$;$) {
  695. my $page=shift;
  696. my $ext=shift;
  697. my $filename=shift;
  698. if (defined $filename) {
  699. return $page."/".$filename.".".$ext;
  700. }
  701. elsif (! $config{usedirs} || $page eq 'index') {
  702. return $page.".".$ext;
  703. }
  704. else {
  705. return $page."/index.".$ext;
  706. }
  707. }
  708. sub htmlpage ($) {
  709. my $page=shift;
  710. return targetpage($page, $config{htmlext});
  711. }
  712. sub srcfile_stat {
  713. my $file=shift;
  714. my $nothrow=shift;
  715. return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
  716. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  717. return "$dir/$file", stat(_) if -e "$dir/$file";
  718. }
  719. error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
  720. return;
  721. }
  722. sub srcfile ($;$) {
  723. return (srcfile_stat(@_))[0];
  724. }
  725. sub add_underlay ($) {
  726. my $dir=shift;
  727. if ($dir !~ /^\//) {
  728. $dir="$config{underlaydirbase}/$dir";
  729. }
  730. if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
  731. unshift @{$config{underlaydirs}}, $dir;
  732. }
  733. return 1;
  734. }
  735. sub readfile ($;$$) {
  736. my $file=shift;
  737. my $binary=shift;
  738. my $wantfd=shift;
  739. if (-l $file) {
  740. error("cannot read a symlink ($file)");
  741. }
  742. local $/=undef;
  743. open (my $in, "<", $file) || error("failed to read $file: $!");
  744. binmode($in) if ($binary);
  745. return \*$in if $wantfd;
  746. my $ret=<$in>;
  747. # check for invalid utf-8, and toss it back to avoid crashes
  748. if (! utf8::valid($ret)) {
  749. $ret=encode_utf8($ret);
  750. }
  751. close $in || error("failed to read $file: $!");
  752. return $ret;
  753. }
  754. sub prep_writefile ($$) {
  755. my $file=shift;
  756. my $destdir=shift;
  757. my $test=$file;
  758. while (length $test) {
  759. if (-l "$destdir/$test") {
  760. error("cannot write to a symlink ($test)");
  761. }
  762. if (-f _ && $test ne $file) {
  763. # Remove conflicting file.
  764. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  765. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  766. if ($f eq $test) {
  767. unlink("$destdir/$test");
  768. last;
  769. }
  770. }
  771. }
  772. }
  773. $test=dirname($test);
  774. }
  775. my $dir=dirname("$destdir/$file");
  776. if (! -d $dir) {
  777. my $d="";
  778. foreach my $s (split(m!/+!, $dir)) {
  779. $d.="$s/";
  780. if (! -d $d) {
  781. mkdir($d) || error("failed to create directory $d: $!");
  782. }
  783. }
  784. }
  785. return 1;
  786. }
  787. sub writefile ($$$;$$) {
  788. my $file=shift; # can include subdirs
  789. my $destdir=shift; # directory to put file in
  790. my $content=shift;
  791. my $binary=shift;
  792. my $writer=shift;
  793. prep_writefile($file, $destdir);
  794. my $newfile="$destdir/$file.ikiwiki-new";
  795. if (-l $newfile) {
  796. error("cannot write to a symlink ($newfile)");
  797. }
  798. my $cleanup = sub { unlink($newfile) };
  799. open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
  800. binmode($out) if ($binary);
  801. if ($writer) {
  802. $writer->(\*$out, $cleanup);
  803. }
  804. else {
  805. print $out $content or error("failed writing to $newfile: $!", $cleanup);
  806. }
  807. close $out || error("failed saving $newfile: $!", $cleanup);
  808. rename($newfile, "$destdir/$file") ||
  809. error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
  810. return 1;
  811. }
  812. my %cleared;
  813. sub will_render ($$;$) {
  814. my $page=shift;
  815. my $dest=shift;
  816. my $clear=shift;
  817. # Important security check for independently created files.
  818. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  819. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
  820. my $from_other_page=0;
  821. # Expensive, but rarely runs.
  822. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  823. if (grep {
  824. $_ eq $dest ||
  825. dirname($_) eq $dest
  826. } @{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  827. $from_other_page=1;
  828. last;
  829. }
  830. }
  831. error("$config{destdir}/$dest independently created, not overwriting with version from $page")
  832. unless $from_other_page;
  833. }
  834. # If $dest exists as a directory, remove conflicting files in it
  835. # rendered from other pages.
  836. if (-d _) {
  837. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  838. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  839. if (dirname($f) eq $dest) {
  840. unlink("$config{destdir}/$f");
  841. rmdir(dirname("$config{destdir}/$f"));
  842. }
  843. }
  844. }
  845. }
  846. if (! $clear || $cleared{$page}) {
  847. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  848. }
  849. else {
  850. foreach my $old (@{$renderedfiles{$page}}) {
  851. delete $destsources{$old};
  852. }
  853. $renderedfiles{$page}=[$dest];
  854. $cleared{$page}=1;
  855. }
  856. $destsources{$dest}=$page;
  857. return 1;
  858. }
  859. sub bestlink ($$) {
  860. my $page=shift;
  861. my $link=shift;
  862. my $cwd=$page;
  863. if ($link=~s/^\/+//) {
  864. # absolute links
  865. $cwd="";
  866. }
  867. $link=~s/\/$//;
  868. do {
  869. my $l=$cwd;
  870. $l.="/" if length $l;
  871. $l.=$link;
  872. if (exists $pagesources{$l}) {
  873. return $l;
  874. }
  875. elsif (exists $pagecase{lc $l}) {
  876. return $pagecase{lc $l};
  877. }
  878. } while $cwd=~s{/?[^/]+$}{};
  879. if (length $config{userdir}) {
  880. my $l = "$config{userdir}/".lc($link);
  881. if (exists $pagesources{$l}) {
  882. return $l;
  883. }
  884. elsif (exists $pagecase{lc $l}) {
  885. return $pagecase{lc $l};
  886. }
  887. }
  888. #print STDERR "warning: page $page, broken link: $link\n";
  889. return "";
  890. }
  891. sub isinlinableimage ($) {
  892. my $file=shift;
  893. return $file =~ /\.(png|gif|jpg|jpeg)$/i;
  894. }
  895. sub pagetitle ($;$) {
  896. my $page=shift;
  897. my $unescaped=shift;
  898. if ($unescaped) {
  899. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
  900. }
  901. else {
  902. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
  903. }
  904. return $page;
  905. }
  906. sub titlepage ($) {
  907. my $title=shift;
  908. # support use w/o %config set
  909. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  910. $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  911. return $title;
  912. }
  913. sub linkpage ($) {
  914. my $link=shift;
  915. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  916. $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  917. return $link;
  918. }
  919. sub cgiurl (@) {
  920. my %params=@_;
  921. my $cgiurl=$config{cgiurl};
  922. if (exists $params{cgiurl}) {
  923. $cgiurl=$params{cgiurl};
  924. delete $params{cgiurl};
  925. }
  926. return $cgiurl."?".
  927. join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
  928. }
  929. sub baseurl (;$) {
  930. my $page=shift;
  931. return "$config{url}/" if ! defined $page;
  932. $page=htmlpage($page);
  933. $page=~s/[^\/]+$//;
  934. $page=~s/[^\/]+\//..\//g;
  935. return $page;
  936. }
  937. sub abs2rel ($$) {
  938. # Work around very innefficient behavior in File::Spec if abs2rel
  939. # is passed two relative paths. It's much faster if paths are
  940. # absolute! (Debian bug #376658; fixed in debian unstable now)
  941. my $path="/".shift;
  942. my $base="/".shift;
  943. require File::Spec;
  944. my $ret=File::Spec->abs2rel($path, $base);
  945. $ret=~s/^// if defined $ret;
  946. return $ret;
  947. }
  948. sub displaytime ($;$$) {
  949. # Plugins can override this function to mark up the time to
  950. # display.
  951. my $time=formattime($_[0], $_[1]);
  952. if ($config{html5}) {
  953. return '<time datetime="'.date_3339($_[0]).'"'.
  954. ($_[2] ? ' pubdate="pubdate"' : '').
  955. '>'.$time.'</time>';
  956. }
  957. else {
  958. return '<span class="date">'.$time.'</span>';
  959. }
  960. }
  961. sub formattime ($;$) {
  962. # Plugins can override this function to format the time.
  963. my $time=shift;
  964. my $format=shift;
  965. if (! defined $format) {
  966. $format=$config{timeformat};
  967. }
  968. # strftime doesn't know about encodings, so make sure
  969. # its output is properly treated as utf8
  970. return decode_utf8(POSIX::strftime($format, localtime($time)));
  971. }
  972. sub date_3339 ($) {
  973. my $time=shift;
  974. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  975. POSIX::setlocale(&POSIX::LC_TIME, "C");
  976. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  977. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  978. return $ret;
  979. }
  980. sub beautify_urlpath ($) {
  981. my $url=shift;
  982. # Ensure url is not an empty link, and if necessary,
  983. # add ./ to avoid colon confusion.
  984. if ($url !~ /^\// && $url !~ /^\.\.?\//) {
  985. $url="./$url";
  986. }
  987. if ($config{usedirs}) {
  988. $url =~ s!/index.$config{htmlext}$!/!;
  989. }
  990. return $url;
  991. }
  992. sub urlto ($$;$) {
  993. my $to=shift;
  994. my $from=shift;
  995. my $absolute=shift;
  996. if (! length $to) {
  997. return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
  998. }
  999. if (! $destsources{$to}) {
  1000. $to=htmlpage($to);
  1001. }
  1002. if ($absolute) {
  1003. return $config{url}.beautify_urlpath("/".$to);
  1004. }
  1005. my $link = abs2rel($to, dirname(htmlpage($from)));
  1006. return beautify_urlpath($link);
  1007. }
  1008. sub isselflink ($$) {
  1009. # Plugins can override this function to support special types
  1010. # of selflinks.
  1011. my $page=shift;
  1012. my $link=shift;
  1013. return $page eq $link;
  1014. }
  1015. sub htmllink ($$$;@) {
  1016. my $lpage=shift; # the page doing the linking
  1017. my $page=shift; # the page that will contain the link (different for inline)
  1018. my $link=shift;
  1019. my %opts=@_;
  1020. $link=~s/\/$//;
  1021. my $bestlink;
  1022. if (! $opts{forcesubpage}) {
  1023. $bestlink=bestlink($lpage, $link);
  1024. }
  1025. else {
  1026. $bestlink="$lpage/".lc($link);
  1027. }
  1028. my $linktext;
  1029. if (defined $opts{linktext}) {
  1030. $linktext=$opts{linktext};
  1031. }
  1032. else {
  1033. $linktext=pagetitle(basename($link));
  1034. }
  1035. return "<span class=\"selflink\">$linktext</span>"
  1036. if length $bestlink && isselflink($page, $bestlink) &&
  1037. ! defined $opts{anchor};
  1038. if (! $destsources{$bestlink}) {
  1039. $bestlink=htmlpage($bestlink);
  1040. if (! $destsources{$bestlink}) {
  1041. my $cgilink = "";
  1042. if (length $config{cgiurl}) {
  1043. $cgilink = "<a href=\"".
  1044. cgiurl(
  1045. do => "create",
  1046. page => lc($link),
  1047. from => $lpage
  1048. )."\" rel=\"nofollow\">?</a>";
  1049. }
  1050. return "<span class=\"createlink\">$cgilink$linktext</span>"
  1051. }
  1052. }
  1053. $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
  1054. $bestlink=beautify_urlpath($bestlink);
  1055. if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
  1056. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  1057. }
  1058. if (defined $opts{anchor}) {
  1059. $bestlink.="#".$opts{anchor};
  1060. }
  1061. my @attrs;
  1062. foreach my $attr (qw{rel class title}) {
  1063. if (defined $opts{$attr}) {
  1064. push @attrs, " $attr=\"$opts{$attr}\"";
  1065. }
  1066. }
  1067. return "<a href=\"$bestlink\"@attrs>$linktext</a>";
  1068. }
  1069. sub userpage ($) {
  1070. my $user=shift;
  1071. return length $config{userdir} ? "$config{userdir}/$user" : $user;
  1072. }
  1073. sub openiduser ($) {
  1074. my $user=shift;
  1075. if (defined $user && $user =~ m!^https?://! &&
  1076. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  1077. my $display;
  1078. if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
  1079. $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
  1080. }
  1081. else {
  1082. # backcompat with old version
  1083. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  1084. $display=$oid->display;
  1085. }
  1086. # Convert "user.somehost.com" to "user [somehost.com]"
  1087. # (also "user.somehost.co.uk")
  1088. if ($display !~ /\[/) {
  1089. $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
  1090. }
  1091. # Convert "http://somehost.com/user" to "user [somehost.com]".
  1092. # (also "https://somehost.com/user/")
  1093. if ($display !~ /\[/) {
  1094. $display=~s/^https?:\/\/(.+)\/([^\/#?]+)\/?(?:[#?].*)?$/$2 [$1]/;
  1095. }
  1096. $display=~s!^https?://!!; # make sure this is removed
  1097. eval q{use CGI 'escapeHTML'};
  1098. error($@) if $@;
  1099. return escapeHTML($display);
  1100. }
  1101. return;
  1102. }
  1103. sub htmlize ($$$$) {
  1104. my $page=shift;
  1105. my $destpage=shift;
  1106. my $type=shift;
  1107. my $content=shift;
  1108. my $oneline = $content !~ /\n/;
  1109. if (exists $hooks{htmlize}{$type}) {
  1110. $content=$hooks{htmlize}{$type}{call}->(
  1111. page => $page,
  1112. content => $content,
  1113. );
  1114. }
  1115. else {
  1116. error("htmlization of $type not supported");
  1117. }
  1118. run_hooks(sanitize => sub {
  1119. $content=shift->(
  1120. page => $page,
  1121. destpage => $destpage,
  1122. content => $content,
  1123. );
  1124. });
  1125. if ($oneline) {
  1126. # hack to get rid of enclosing junk added by markdown
  1127. # and other htmlizers/sanitizers
  1128. $content=~s/^<p>//i;
  1129. $content=~s/<\/p>\n*$//i;
  1130. }
  1131. return $content;
  1132. }
  1133. sub linkify ($$$) {
  1134. my $page=shift;
  1135. my $destpage=shift;
  1136. my $content=shift;
  1137. run_hooks(linkify => sub {
  1138. $content=shift->(
  1139. page => $page,
  1140. destpage => $destpage,
  1141. content => $content,
  1142. );
  1143. });
  1144. return $content;
  1145. }
  1146. our %preprocessing;
  1147. our $preprocess_preview=0;
  1148. sub preprocess ($$$;$$) {
  1149. my $page=shift; # the page the data comes from
  1150. my $destpage=shift; # the page the data will appear in (different for inline)
  1151. my $content=shift;
  1152. my $scan=shift;
  1153. my $preview=shift;
  1154. # Using local because it needs to be set within any nested calls
  1155. # of this function.
  1156. local $preprocess_preview=$preview if defined $preview;
  1157. my $handle=sub {
  1158. my $escape=shift;
  1159. my $prefix=shift;
  1160. my $command=shift;
  1161. my $params=shift;
  1162. $params="" if ! defined $params;
  1163. if (length $escape) {
  1164. return "[[$prefix$command $params]]";
  1165. }
  1166. elsif (exists $hooks{preprocess}{$command}) {
  1167. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  1168. # Note: preserve order of params, some plugins may
  1169. # consider it significant.
  1170. my @params;
  1171. while ($params =~ m{
  1172. (?:([-\w]+)=)? # 1: named parameter key?
  1173. (?:
  1174. """(.*?)""" # 2: triple-quoted value
  1175. |
  1176. "([^"]*?)" # 3: single-quoted value
  1177. |
  1178. (\S+) # 4: unquoted value
  1179. )
  1180. (?:\s+|$) # delimiter to next param
  1181. }sgx) {
  1182. my $key=$1;
  1183. my $val;
  1184. if (defined $2) {
  1185. $val=$2;
  1186. $val=~s/\r\n/\n/mg;
  1187. $val=~s/^\n+//g;
  1188. $val=~s/\n+$//g;
  1189. }
  1190. elsif (defined $3) {
  1191. $val=$3;
  1192. }
  1193. elsif (defined $4) {
  1194. $val=$4;
  1195. }
  1196. if (defined $key) {
  1197. push @params, $key, $val;
  1198. }
  1199. else {
  1200. push @params, $val, '';
  1201. }
  1202. }
  1203. if ($preprocessing{$page}++ > 3) {
  1204. # Avoid loops of preprocessed pages preprocessing
  1205. # other pages that preprocess them, etc.
  1206. return "[[!$command <span class=\"error\">".
  1207. sprintf(gettext("preprocessing loop detected on %s at depth %i"),
  1208. $page, $preprocessing{$page}).
  1209. "</span>]]";
  1210. }
  1211. my $ret;
  1212. if (! $scan) {
  1213. $ret=eval {
  1214. $hooks{preprocess}{$command}{call}->(
  1215. @params,
  1216. page => $page,
  1217. destpage => $destpage,
  1218. preview => $preprocess_preview,
  1219. );
  1220. };
  1221. if ($@) {
  1222. my $error=$@;
  1223. chomp $error;
  1224. $ret="[[!$command <span class=\"error\">".
  1225. gettext("Error").": $error"."</span>]]";
  1226. }
  1227. }
  1228. else {
  1229. # use void context during scan pass
  1230. eval {
  1231. $hooks{preprocess}{$command}{call}->(
  1232. @params,
  1233. page => $page,
  1234. destpage => $destpage,
  1235. preview => $preprocess_preview,
  1236. );
  1237. };
  1238. $ret="";
  1239. }
  1240. $preprocessing{$page}--;
  1241. return $ret;
  1242. }
  1243. else {
  1244. return "[[$prefix$command $params]]";
  1245. }
  1246. };
  1247. my $regex;
  1248. if ($config{prefix_directives}) {
  1249. $regex = qr{
  1250. (\\?) # 1: escape?
  1251. \[\[(!) # directive open; 2: prefix
  1252. ([-\w]+) # 3: command
  1253. ( # 4: the parameters..
  1254. \s+ # Must have space if parameters present
  1255. (?:
  1256. (?:[-\w]+=)? # named parameter key?
  1257. (?:
  1258. """.*?""" # triple-quoted value
  1259. |
  1260. "[^"]*?" # single-quoted value
  1261. |
  1262. [^"\s\]]+ # unquoted value
  1263. )
  1264. \s* # whitespace or end
  1265. # of directive
  1266. )
  1267. *)? # 0 or more parameters
  1268. \]\] # directive closed
  1269. }sx;
  1270. }
  1271. else {
  1272. $regex = qr{
  1273. (\\?) # 1: escape?
  1274. \[\[(!?) # directive open; 2: optional prefix
  1275. ([-\w]+) # 3: command
  1276. \s+
  1277. ( # 4: the parameters..
  1278. (?:
  1279. (?:[-\w]+=)? # named parameter key?
  1280. (?:
  1281. """.*?""" # triple-quoted value
  1282. |
  1283. "[^"]*?" # single-quoted value
  1284. |
  1285. [^"\s\]]+ # unquoted value
  1286. )
  1287. \s* # whitespace or end
  1288. # of directive
  1289. )
  1290. *) # 0 or more parameters
  1291. \]\] # directive closed
  1292. }sx;
  1293. }
  1294. $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
  1295. return $content;
  1296. }
  1297. sub filter ($$$) {
  1298. my $page=shift;
  1299. my $destpage=shift;
  1300. my $content=shift;
  1301. run_hooks(filter => sub {
  1302. $content=shift->(page => $page, destpage => $destpage,
  1303. content => $content);
  1304. });
  1305. return $content;
  1306. }
  1307. sub check_canedit ($$$;$) {
  1308. my $page=shift;
  1309. my $q=shift;
  1310. my $session=shift;
  1311. my $nonfatal=shift;
  1312. my $canedit;
  1313. run_hooks(canedit => sub {
  1314. return if defined $canedit;
  1315. my $ret=shift->($page, $q, $session);
  1316. if (defined $ret) {
  1317. if ($ret eq "") {
  1318. $canedit=1;
  1319. }
  1320. elsif (ref $ret eq 'CODE') {
  1321. $ret->() unless $nonfatal;
  1322. $canedit=0;
  1323. }
  1324. elsif (defined $ret) {
  1325. error($ret) unless $nonfatal;
  1326. $canedit=0;
  1327. }
  1328. }
  1329. });
  1330. return defined $canedit ? $canedit : 1;
  1331. }
  1332. sub check_content (@) {
  1333. my %params=@_;
  1334. return 1 if ! exists $hooks{checkcontent}; # optimisation
  1335. if (exists $pagesources{$params{page}}) {
  1336. my @diff;
  1337. my %old=map { $_ => 1 }
  1338. split("\n", readfile(srcfile($pagesources{$params{page}})));
  1339. foreach my $line (split("\n", $params{content})) {
  1340. push @diff, $line if ! exists $old{$line};
  1341. }
  1342. $params{diff}=join("\n", @diff);
  1343. }
  1344. my $ok;
  1345. run_hooks(checkcontent => sub {
  1346. return if defined $ok;
  1347. my $ret=shift->(%params);
  1348. if (defined $ret) {
  1349. if ($ret eq "") {
  1350. $ok=1;
  1351. }
  1352. elsif (ref $ret eq 'CODE') {
  1353. $ret->() unless $params{nonfatal};
  1354. $ok=0;
  1355. }
  1356. elsif (defined $ret) {
  1357. error($ret) unless $params{nonfatal};
  1358. $ok=0;
  1359. }
  1360. }
  1361. });
  1362. return defined $ok ? $ok : 1;
  1363. }
  1364. sub check_canchange (@) {
  1365. my %params = @_;
  1366. my $cgi = $params{cgi};
  1367. my $session = $params{session};
  1368. my @changes = @{$params{changes}};
  1369. my %newfiles;
  1370. foreach my $change (@changes) {
  1371. # This untaint is safe because we check file_pruned and
  1372. # wiki_file_regexp.
  1373. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  1374. $file=possibly_foolish_untaint($file);
  1375. if (! defined $file || ! length $file ||
  1376. file_pruned($file)) {
  1377. error(gettext("bad file name %s"), $file);
  1378. }
  1379. my $type=pagetype($file);
  1380. my $page=pagename($file) if defined $type;
  1381. if ($change->{action} eq 'add') {
  1382. $newfiles{$file}=1;
  1383. }
  1384. if ($change->{action} eq 'change' ||
  1385. $change->{action} eq 'add') {
  1386. if (defined $page) {
  1387. check_canedit($page, $cgi, $session);
  1388. next;
  1389. }
  1390. else {
  1391. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  1392. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  1393. check_canedit($file, $cgi, $session);
  1394. next;
  1395. }
  1396. }
  1397. }
  1398. elsif ($change->{action} eq 'remove') {
  1399. # check_canremove tests to see if the file is present
  1400. # on disk. This will fail when a single commit adds a
  1401. # file and then removes it again. Avoid the problem
  1402. # by not testing the removal in such pairs of changes.
  1403. # (The add is still tested, just to make sure that
  1404. # no data is added to the repo that a web edit
  1405. # could not add.)
  1406. next if $newfiles{$file};
  1407. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  1408. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  1409. check_canedit(defined $page ? $page : $file, $cgi, $session);
  1410. next;
  1411. }
  1412. }
  1413. else {
  1414. error "unknown action ".$change->{action};
  1415. }
  1416. error sprintf(gettext("you are not allowed to change %s"), $file);
  1417. }
  1418. }
  1419. my $wikilock;
  1420. sub lockwiki () {
  1421. # Take an exclusive lock on the wiki to prevent multiple concurrent
  1422. # run issues. The lock will be dropped on program exit.
  1423. if (! -d $config{wikistatedir}) {
  1424. mkdir($config{wikistatedir});
  1425. }
  1426. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  1427. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  1428. if (! flock($wikilock, 2)) { # LOCK_EX
  1429. error("failed to get lock");
  1430. }
  1431. return 1;
  1432. }
  1433. sub unlockwiki () {
  1434. POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
  1435. return close($wikilock) if $wikilock;
  1436. return;
  1437. }
  1438. my $commitlock;
  1439. sub commit_hook_enabled () {
  1440. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  1441. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1442. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  1443. close($commitlock) || error("failed closing commitlock: $!");
  1444. return 0;
  1445. }
  1446. close($commitlock) || error("failed closing commitlock: $!");
  1447. return 1;
  1448. }
  1449. sub disable_commit_hook () {
  1450. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  1451. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1452. if (! flock($commitlock, 2)) { # LOCK_EX
  1453. error("failed to get commit lock");
  1454. }
  1455. return 1;
  1456. }
  1457. sub enable_commit_hook () {
  1458. return close($commitlock) if $commitlock;
  1459. return;
  1460. }
  1461. sub loadindex () {
  1462. %oldrenderedfiles=%pagectime=();
  1463. if (! $config{rebuild}) {
  1464. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  1465. %destsources=%renderedfiles=%pagecase=%pagestate=
  1466. %depends_simple=%typedlinks=%oldtypedlinks=();
  1467. }
  1468. my $in;
  1469. if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
  1470. if (-e "$config{wikistatedir}/index") {
  1471. system("ikiwiki-transition", "indexdb", $config{srcdir});
  1472. open ($in, "<", "$config{wikistatedir}/indexdb") || return;
  1473. }
  1474. else {
  1475. $config{gettime}=1; # first build
  1476. return;
  1477. }
  1478. }
  1479. my $index=Storable::fd_retrieve($in);
  1480. if (! defined $index) {
  1481. return 0;
  1482. }
  1483. my $pages;
  1484. if (exists $index->{version} && ! ref $index->{version}) {
  1485. $pages=$index->{page};
  1486. %wikistate=%{$index->{state}};
  1487. # Handle plugins that got disabled by loading a new setup.
  1488. if (exists $config{setupfile}) {
  1489. require IkiWiki::Setup;
  1490. IkiWiki::Setup::disabled_plugins(
  1491. grep { ! $loaded_plugins{$_} } keys %wikistate);
  1492. }
  1493. }
  1494. else {
  1495. $pages=$index;
  1496. %wikistate=();
  1497. }
  1498. foreach my $src (keys %$pages) {
  1499. my $d=$pages->{$src};
  1500. my $page=pagename($src);
  1501. $pagectime{$page}=$d->{ctime};
  1502. $pagesources{$page}=$src;
  1503. if (! $config{rebuild}) {
  1504. $pagemtime{$page}=$d->{mtime};
  1505. $renderedfiles{$page}=$d->{dest};
  1506. if (exists $d->{links} && ref $d->{links}) {
  1507. $links{$page}=$d->{links};
  1508. $oldlinks{$page}=[@{$d->{links}}];
  1509. }
  1510. if (ref $d->{depends_simple} eq 'ARRAY') {
  1511. # old format
  1512. $depends_simple{$page}={
  1513. map { $_ => 1 } @{$d->{depends_simple}}
  1514. };
  1515. }
  1516. elsif (exists $d->{depends_simple}) {
  1517. $depends_simple{$page}=$d->{depends_simple};
  1518. }
  1519. if (exists $d->{dependslist}) {
  1520. # old format
  1521. $depends{$page}={
  1522. map { $_ => $DEPEND_CONTENT }
  1523. @{$d->{dependslist}}
  1524. };
  1525. }
  1526. elsif (exists $d->{depends} && ! ref $d->{depends}) {
  1527. # old format
  1528. $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
  1529. }
  1530. elsif (exists $d->{depends}) {
  1531. $depends{$page}=$d->{depends};
  1532. }
  1533. if (exists $d->{state}) {
  1534. $pagestate{$page}=$d->{state};
  1535. }
  1536. if (exists $d->{typedlinks}) {
  1537. $typedlinks{$page}=$d->{typedlinks};
  1538. while (my ($type, $links) = each %{$typedlinks{$page}}) {
  1539. next unless %$links;
  1540. $oldtypedlinks{$page}{$type} = {%$links};
  1541. }
  1542. }
  1543. }
  1544. $oldrenderedfiles{$page}=[@{$d->{dest}}];
  1545. }
  1546. foreach my $page (keys %pagesources) {
  1547. $pagecase{lc $page}=$page;
  1548. }
  1549. foreach my $page (keys %renderedfiles) {
  1550. $destsources{$_}=$page foreach @{$renderedfiles{$page}};
  1551. }
  1552. return close($in);
  1553. }
  1554. sub saveindex () {
  1555. run_hooks(savestate => sub { shift->() });
  1556. my @plugins=keys %loaded_plugins;
  1557. if (! -d $config{wikistatedir}) {
  1558. mkdir($config{wikistatedir});
  1559. }
  1560. my $newfile="$config{wikistatedir}/indexdb.new";
  1561. my $cleanup = sub { unlink($newfile) };
  1562. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  1563. my %index;
  1564. foreach my $page (keys %pagemtime) {
  1565. next unless $pagemtime{$page};
  1566. my $src=$pagesources{$page};
  1567. $index{page}{$src}={
  1568. ctime => $pagectime{$page},
  1569. mtime => $pagemtime{$page},
  1570. dest => $renderedfiles{$page},
  1571. links => $links{$page},
  1572. };
  1573. if (exists $depends{$page}) {
  1574. $index{page}{$src}{depends} = $depends{$page};
  1575. }
  1576. if (exists $depends_simple{$page}) {
  1577. $index{page}{$src}{depends_simple} = $depends_simple{$page};
  1578. }
  1579. if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
  1580. $index{page}{$src}{typedlinks} = $typedlinks{$page};
  1581. }
  1582. if (exists $pagestate{$page}) {
  1583. foreach my $id (@plugins) {
  1584. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  1585. $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
  1586. }
  1587. }
  1588. }
  1589. }
  1590. $index{state}={};
  1591. foreach my $id (@plugins) {
  1592. $index{state}{$id}={}; # used to detect disabled plugins
  1593. foreach my $key (keys %{$wikistate{$id}}) {
  1594. $index{state}{$id}{$key}=$wikistate{$id}{$key};
  1595. }
  1596. }
  1597. $index{version}="3";
  1598. my $ret=Storable::nstore_fd(\%index, $out);
  1599. return if ! defined $ret || ! $ret;
  1600. close $out || error("failed saving to $newfile: $!", $cleanup);
  1601. rename($newfile, "$config{wikistatedir}/indexdb") ||
  1602. error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
  1603. return 1;
  1604. }
  1605. sub template_file ($) {
  1606. my $name=shift;
  1607. my $tpage=($name =~ s/^\///) ? $name : "templates/$name";
  1608. my $template;
  1609. if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
  1610. $template=srcfile($pagesources{$tpage}, 1);
  1611. $name.=".tmpl";
  1612. }
  1613. else {
  1614. $template=srcfile($tpage, 1);
  1615. }
  1616. if (defined $template) {
  1617. return $template, $tpage, 1 if wantarray;
  1618. return $template;
  1619. }
  1620. else {
  1621. $name=~s:/::; # avoid path traversal
  1622. foreach my $dir ($config{templatedir},
  1623. "$installdir/share/ikiwiki/templates") {
  1624. if (-e "$dir/$name") {
  1625. $template="$dir/$name";
  1626. last;
  1627. }
  1628. }
  1629. if (defined $template) {
  1630. return $template, $tpage if wantarray;
  1631. return $template;
  1632. }
  1633. }
  1634. return;
  1635. }
  1636. sub template_depends ($$;@) {
  1637. my $name=shift;
  1638. my $page=shift;
  1639. my ($filename, $tpage, $untrusted)=template_file($name);
  1640. if (! defined $filename) {
  1641. error(sprintf(gettext("template %s not found"), $name))
  1642. }
  1643. if (defined $page && defined $tpage) {
  1644. add_depends($page, $tpage);
  1645. }
  1646. my @opts=(
  1647. filter => sub {
  1648. my $text_ref = shift;
  1649. ${$text_ref} = decode_utf8(${$text_ref});
  1650. },
  1651. loop_context_vars => 1,
  1652. die_on_bad_params => 0,
  1653. filename => $filename,
  1654. @_,
  1655. ($untrusted ? (no_includes => 1) : ()),
  1656. );
  1657. return @opts if wantarray;
  1658. require HTML::Template;
  1659. return HTML::Template->new(@opts);
  1660. }
  1661. sub template ($;@) {
  1662. template_depends(shift, undef, @_);
  1663. }
  1664. sub misctemplate ($$;@) {
  1665. my $title=shift;
  1666. my $content=shift;
  1667. my %params=@_;
  1668. my $template=template("page.tmpl");
  1669. my $page="";
  1670. if (exists $params{page}) {
  1671. $page=delete $params{page};
  1672. }
  1673. run_hooks(pagetemplate => sub {
  1674. shift->(
  1675. page => $page,
  1676. destpage => $page,
  1677. template => $template,
  1678. );
  1679. });
  1680. templateactions($template, "");
  1681. $template->param(
  1682. dynamic => 1,
  1683. title => $title,
  1684. wikiname => $config{wikiname},
  1685. content => $content,
  1686. baseurl => baseurl(),
  1687. html5 => $config{html5},
  1688. %params,
  1689. );
  1690. return $template->output;
  1691. }
  1692. sub templateactions ($$) {
  1693. my $template=shift;
  1694. my $page=shift;
  1695. my $have_actions=0;
  1696. my @actions;
  1697. run_hooks(pageactions => sub {
  1698. push @actions, map { { action => $_ } }
  1699. grep { defined } shift->(page => $page);
  1700. });
  1701. $template->param(actions => \@actions);
  1702. if ($config{cgiurl} && exists $hooks{auth}) {
  1703. $template->param(prefsurl => cgiurl(do => "prefs"));
  1704. $have_actions=1;
  1705. }
  1706. if ($have_actions || @actions) {
  1707. $template->param(have_actions => 1);
  1708. }
  1709. }
  1710. sub hook (@) {
  1711. my %param=@_;
  1712. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  1713. error 'hook requires type, call, and id parameters';
  1714. }
  1715. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  1716. $hooks{$param{type}}{$param{id}}=\%param;
  1717. return 1;
  1718. }
  1719. sub run_hooks ($$) {
  1720. # Calls the given sub for each hook of the given type,
  1721. # passing it the hook function to call.
  1722. my $type=shift;
  1723. my $sub=shift;
  1724. if (exists $hooks{$type}) {
  1725. my (@first, @middle, @last);
  1726. foreach my $id (keys %{$hooks{$type}}) {
  1727. if ($hooks{$type}{$id}{first}) {
  1728. push @first, $id;
  1729. }
  1730. elsif ($hooks{$type}{$id}{last}) {
  1731. push @last, $id;
  1732. }
  1733. else {
  1734. push @middle, $id;
  1735. }
  1736. }
  1737. foreach my $id (@first, @middle, @last) {
  1738. $sub->($hooks{$type}{$id}{call});
  1739. }
  1740. }
  1741. return 1;
  1742. }
  1743. sub rcs_update () {
  1744. $hooks{rcs}{rcs_update}{call}->(@_);
  1745. }
  1746. sub rcs_prepedit ($) {
  1747. $hooks{rcs}{rcs_prepedit}{call}->(@_);
  1748. }
  1749. sub rcs_commit (@) {
  1750. $hooks{rcs}{rcs_commit}{call}->(@_);
  1751. }
  1752. sub rcs_commit_staged (@) {
  1753. $hooks{rcs}{rcs_commit_staged}{call}->(@_);
  1754. }
  1755. sub rcs_add ($) {
  1756. $hooks{rcs}{rcs_add}{call}->(@_);
  1757. }
  1758. sub rcs_remove ($) {
  1759. $hooks{rcs}{rcs_remove}{call}->(@_);
  1760. }
  1761. sub rcs_rename ($$) {
  1762. $hooks{rcs}{rcs_rename}{call}->(@_);
  1763. }
  1764. sub rcs_recentchanges ($) {
  1765. $hooks{rcs}{rcs_recentchanges}{call}->(@_);
  1766. }
  1767. sub rcs_diff ($) {
  1768. $hooks{rcs}{rcs_diff}{call}->(@_);
  1769. }
  1770. sub rcs_getctime ($) {
  1771. $hooks{rcs}{rcs_getctime}{call}->(@_);
  1772. }
  1773. sub rcs_getmtime ($) {
  1774. $hooks{rcs}{rcs_getmtime}{call}->(@_);
  1775. }
  1776. sub rcs_receive () {
  1777. $hooks{rcs}{rcs_receive}{call}->();
  1778. }
  1779. sub add_depends ($$;$) {
  1780. my $page=shift;
  1781. my $pagespec=shift;
  1782. my $deptype=shift || $DEPEND_CONTENT;
  1783. # Is the pagespec a simple page name?
  1784. if ($pagespec =~ /$config{wiki_file_regexp}/ &&
  1785. $pagespec !~ /[\s*?()!]/) {
  1786. $depends_simple{$page}{lc $pagespec} |= $deptype;
  1787. return 1;
  1788. }
  1789. # Add explicit dependencies for influences.
  1790. my $sub=pagespec_translate($pagespec);
  1791. return unless defined $sub;
  1792. foreach my $p (keys %pagesources) {
  1793. my $r=$sub->($p, location => $page);
  1794. my $i=$r->influences;
  1795. my $static=$r->influences_static;
  1796. foreach my $k (keys %$i) {
  1797. next unless $r || $static || $k eq $page;
  1798. $depends_simple{$page}{lc $k} |= $i->{$k};
  1799. }
  1800. last if $static;
  1801. }
  1802. $depends{$page}{$pagespec} |= $deptype;
  1803. return 1;
  1804. }
  1805. sub deptype (@) {
  1806. my $deptype=0;
  1807. foreach my $type (@_) {
  1808. if ($type eq 'presence') {
  1809. $deptype |= $DEPEND_PRESENCE;
  1810. }
  1811. elsif ($type eq 'links') {
  1812. $deptype |= $DEPEND_LINKS;
  1813. }
  1814. elsif ($type eq 'content') {
  1815. $deptype |= $DEPEND_CONTENT;
  1816. }
  1817. }
  1818. return $deptype;
  1819. }
  1820. my $file_prune_regexp;
  1821. sub file_pruned ($) {
  1822. my $file=shift;
  1823. if (defined $config{include} && length $config{include}) {
  1824. return 0 if $file =~ m/$config{include}/;
  1825. }
  1826. if (! defined $file_prune_regexp) {
  1827. $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  1828. $file_prune_regexp=qr/$file_prune_regexp/;
  1829. }
  1830. return $file =~ m/$file_prune_regexp/;
  1831. }
  1832. sub define_gettext () {
  1833. # If translation is needed, redefine the gettext function to do it.
  1834. # Otherwise, it becomes a quick no-op.
  1835. my $gettext_obj;
  1836. my $getobj;
  1837. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  1838. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  1839. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  1840. $getobj=sub {
  1841. $gettext_obj=eval q{
  1842. use Locale::gettext q{textdomain};
  1843. Locale::gettext->domain('ikiwiki')
  1844. };
  1845. };
  1846. }
  1847. no warnings 'redefine';
  1848. *gettext=sub {
  1849. $getobj->() if $getobj;
  1850. if ($gettext_obj) {
  1851. $gettext_obj->get(shift);
  1852. }
  1853. else {
  1854. return shift;
  1855. }
  1856. };
  1857. *ngettext=sub {
  1858. $getobj->() if $getobj;
  1859. if ($gettext_obj) {
  1860. $gettext_obj->nget(@_);
  1861. }
  1862. else {
  1863. return ($_[2] == 1 ? $_[0] : $_[1])
  1864. }
  1865. };
  1866. }
  1867. sub gettext {
  1868. define_gettext();
  1869. gettext(@_);
  1870. }
  1871. sub ngettext {
  1872. define_gettext();
  1873. ngettext(@_);
  1874. }
  1875. sub yesno ($) {
  1876. my $val=shift;
  1877. return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
  1878. }
  1879. sub inject {
  1880. # Injects a new function into the symbol table to replace an
  1881. # exported function.
  1882. my %params=@_;
  1883. # This is deep ugly perl foo, beware.
  1884. no strict;
  1885. no warnings;
  1886. if (! defined $params{parent}) {
  1887. $params{parent}='::';
  1888. $params{old}=\&{$params{name}};
  1889. $params{name}=~s/.*:://;
  1890. }
  1891. my $parent=$params{parent};
  1892. foreach my $ns (grep /^\w+::/, keys %{$parent}) {
  1893. $ns = $params{parent} . $ns;
  1894. inject(%params, parent => $ns) unless $ns eq '::main::';
  1895. *{$ns . $params{name}} = $params{call}
  1896. if exists ${$ns}{$params{name}} &&
  1897. \&{${$ns}{$params{name}}} == $params{old};
  1898. }
  1899. use strict;
  1900. use warnings;
  1901. }
  1902. sub add_link ($$;$) {
  1903. my $page=shift;
  1904. my $link=shift;
  1905. my $type=shift;
  1906. push @{$links{$page}}, $link
  1907. unless grep { $_ eq $link } @{$links{$page}};
  1908. if (defined $type) {
  1909. $typedlinks{$page}{$type}{$link} = 1;
  1910. }
  1911. }
  1912. sub add_autofile ($$$) {
  1913. my $file=shift;
  1914. my $plugin=shift;
  1915. my $generator=shift;
  1916. $autofiles{$file}{plugin}=$plugin;
  1917. $autofiles{$file}{generator}=$generator;
  1918. }
  1919. sub sortspec_translate ($$) {
  1920. my $spec = shift;
  1921. my $reverse = shift;
  1922. my $code = "";
  1923. my @data;
  1924. while ($spec =~ m{
  1925. \s*
  1926. (-?) # group 1: perhaps negated
  1927. \s*
  1928. ( # group 2: a word
  1929. \w+\([^\)]*\) # command(params)
  1930. |
  1931. [^\s]+ # or anything else
  1932. )
  1933. \s*
  1934. }gx) {
  1935. my $negated = $1;
  1936. my $word = $2;
  1937. my $params = undef;
  1938. if ($word =~ m/^(\w+)\((.*)\)$/) {
  1939. # command with parameters
  1940. $params = $2;
  1941. $word = $1;
  1942. }
  1943. elsif ($word !~ m/^\w+$/) {
  1944. error(sprintf(gettext("invalid sort type %s"), $word));
  1945. }
  1946. if (length $code) {
  1947. $code .= " || ";
  1948. }
  1949. if ($negated) {
  1950. $code .= "-";
  1951. }
  1952. if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
  1953. if (defined $params) {
  1954. push @data, $params;
  1955. $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
  1956. }
  1957. else {
  1958. $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
  1959. }
  1960. }
  1961. else {
  1962. error(sprintf(gettext("unknown sort type %s"), $word));
  1963. }
  1964. }
  1965. if (! length $code) {
  1966. # undefined sorting method... sort arbitrarily
  1967. return sub { 0 };
  1968. }
  1969. if ($reverse) {
  1970. $code="-($code)";
  1971. }
  1972. no warnings;
  1973. return eval 'sub { '.$code.' }';
  1974. }
  1975. sub pagespec_translate ($) {
  1976. my $spec=shift;
  1977. # Convert spec to perl code.
  1978. my $code="";
  1979. my @data;
  1980. while ($spec=~m{
  1981. \s* # ignore whitespace
  1982. ( # 1: match a single word
  1983. \! # !
  1984. |
  1985. \( # (
  1986. |
  1987. \) # )
  1988. |
  1989. \w+\([^\)]*\) # command(params)
  1990. |
  1991. [^\s()]+ # any other text
  1992. )
  1993. \s* # ignore whitespace
  1994. }gx) {
  1995. my $word=$1;
  1996. if (lc $word eq 'and') {
  1997. $code.=' &';
  1998. }
  1999. elsif (lc $word eq 'or') {
  2000. $code.=' |';
  2001. }
  2002. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  2003. $code.=' '.$word;
  2004. }
  2005. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  2006. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  2007. push @data, $2;
  2008. $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
  2009. }
  2010. else {
  2011. push @data, qq{unknown function in pagespec "$word"};
  2012. $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
  2013. }
  2014. }
  2015. else {
  2016. push @data, $word;
  2017. $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
  2018. }
  2019. }
  2020. if (! length $code) {
  2021. $code="IkiWiki::FailReason->new('empty pagespec')";
  2022. }
  2023. no warnings;
  2024. return eval 'sub { my $page=shift; '.$code.' }';
  2025. }
  2026. sub pagespec_match ($$;@) {
  2027. my $page=shift;
  2028. my $spec=shift;
  2029. my @params=@_;
  2030. # Backwards compatability with old calling convention.
  2031. if (@params == 1) {
  2032. unshift @params, 'location';
  2033. }
  2034. my $sub=pagespec_translate($spec);
  2035. return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
  2036. if ! defined $sub;
  2037. return $sub->($page, @params);
  2038. }
  2039. sub pagespec_match_list ($$;@) {
  2040. my $page=shift;
  2041. my $pagespec=shift;
  2042. my %params=@_;
  2043. # Backwards compatability with old calling convention.
  2044. if (ref $page) {
  2045. print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
  2046. $params{list}=$page;
  2047. $page=$params{location}; # ugh!
  2048. }
  2049. my $sub=pagespec_translate($pagespec);
  2050. error "syntax error in pagespec \"$pagespec\""
  2051. if ! defined $sub;
  2052. my $sort=sortspec_translate($params{sort}, $params{reverse})
  2053. if defined $params{sort};
  2054. my @candidates;
  2055. if (exists $params{list}) {
  2056. @candidates=exists $params{filter}
  2057. ? grep { ! $params{filter}->($_) } @{$params{list}}
  2058. : @{$params{list}};
  2059. }
  2060. else {
  2061. @candidates=exists $params{filter}
  2062. ? grep { ! $params{filter}->($_) } keys %pagesources
  2063. : keys %pagesources;
  2064. }
  2065. # clear params, remainder is passed to pagespec
  2066. $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
  2067. my $num=$params{num};
  2068. delete @params{qw{num deptype reverse sort filter list}};
  2069. # when only the top matches will be returned, it's efficient to
  2070. # sort before matching to pagespec,
  2071. if (defined $num && defined $sort) {
  2072. @candidates=IkiWiki::SortSpec::sort_pages(
  2073. $sort, @candidates);
  2074. }
  2075. my @matches;
  2076. my $firstfail;
  2077. my $count=0;
  2078. my $accum=IkiWiki::SuccessReason->new();
  2079. foreach my $p (@candidates) {
  2080. my $r=$sub->($p, %params, location => $page);
  2081. error(sprintf(gettext("cannot match pages: %s"), $r))
  2082. if $r->isa("IkiWiki::ErrorReason");
  2083. unless ($r || $r->influences_static) {
  2084. $r->remove_influence($p);
  2085. }
  2086. $accum |= $r;
  2087. if ($r) {
  2088. push @matches, $p;
  2089. last if defined $num && ++$count == $num;
  2090. }
  2091. }
  2092. # Add simple dependencies for accumulated influences.
  2093. my $i=$accum->influences;
  2094. foreach my $k (keys %$i) {
  2095. $depends_simple{$page}{lc $k} |= $i->{$k};
  2096. }
  2097. # when all matches will be returned, it's efficient to
  2098. # sort after matching
  2099. if (! defined $num && defined $sort) {
  2100. return IkiWiki::SortSpec::sort_pages(
  2101. $sort, @matches);
  2102. }
  2103. else {
  2104. return @matches;
  2105. }
  2106. }
  2107. sub pagespec_valid ($) {
  2108. my $spec=shift;
  2109. return defined pagespec_translate($spec);
  2110. }
  2111. sub glob2re ($) {
  2112. my $re=quotemeta(shift);
  2113. $re=~s/\\\*/.*/g;
  2114. $re=~s/\\\?/./g;
  2115. return qr/^$re$/i;
  2116. }
  2117. package IkiWiki::FailReason;
  2118. use overload (
  2119. '""' => sub { $_[0][0] },
  2120. '0+' => sub { 0 },
  2121. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  2122. '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
  2123. '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
  2124. fallback => 1,
  2125. );
  2126. our @ISA = 'IkiWiki::SuccessReason';
  2127. package IkiWiki::SuccessReason;
  2128. use overload (
  2129. '""' => sub { $_[0][0] },
  2130. '0+' => sub { 1 },
  2131. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  2132. '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
  2133. '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
  2134. fallback => 1,
  2135. );
  2136. sub new {
  2137. my $class = shift;
  2138. my $value = shift;
  2139. return bless [$value, {@_}], $class;
  2140. }
  2141. sub influences {
  2142. my $this=shift;
  2143. $this->[1]={@_} if @_;
  2144. my %i=%{$this->[1]};
  2145. delete $i{""};
  2146. return \%i;
  2147. }
  2148. sub influences_static {
  2149. return ! $_[0][1]->{""};
  2150. }
  2151. sub merge_influences {
  2152. my $this=shift;
  2153. my $other=shift;
  2154. my $anded=shift;
  2155. if (! $anded || (($this || %{$this->[1]}) &&
  2156. ($other || %{$other->[1]}))) {
  2157. foreach my $influence (keys %{$other->[1]}) {
  2158. $this->[1]{$influence} |= $other->[1]{$influence};
  2159. }
  2160. }
  2161. else {
  2162. # influence blocker
  2163. $this->[1]={};
  2164. }
  2165. }
  2166. sub remove_influence {
  2167. my $this=shift;
  2168. my $torm=shift;
  2169. delete $this->[1]{$torm};
  2170. }
  2171. package IkiWiki::ErrorReason;
  2172. our @ISA = 'IkiWiki::FailReason';
  2173. package IkiWiki::PageSpec;
  2174. sub derel ($$) {
  2175. my $path=shift;
  2176. my $from=shift;
  2177. if ($path =~ m!^\.(/|$)!) {
  2178. if ($1) {
  2179. $from=~s#/?[^/]+$## if defined $from;
  2180. $path=~s#^\./##;
  2181. $path="$from/$path" if defined $from && length $from;
  2182. }
  2183. else {
  2184. $path = $from;
  2185. $path = "" unless defined $path;
  2186. }
  2187. }
  2188. return $path;
  2189. }
  2190. my %glob_cache;
  2191. sub match_glob ($$;@) {
  2192. my $page=shift;
  2193. my $glob=shift;
  2194. my %params=@_;
  2195. $glob=derel($glob, $params{location});
  2196. # Instead of converting the glob to a regex every time,
  2197. # cache the compiled regex to save time.
  2198. my $re=$glob_cache{$glob};
  2199. unless (defined $re) {
  2200. $glob_cache{$glob} = $re = IkiWiki::glob2re($glob);
  2201. }
  2202. if ($page =~ $re) {
  2203. if (! IkiWiki::isinternal($page) || $params{internal}) {
  2204. return IkiWiki::SuccessReason->new("$glob matches $page");
  2205. }
  2206. else {
  2207. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  2208. }
  2209. }
  2210. else {
  2211. return IkiWiki::FailReason->new("$glob does not match $page");
  2212. }
  2213. }
  2214. sub match_internal ($$;@) {
  2215. return match_glob(shift, shift, @_, internal => 1)
  2216. }
  2217. sub match_page ($$;@) {
  2218. my $page=shift;
  2219. my $match=match_glob($page, shift, @_);
  2220. if ($match) {
  2221. my $source=exists $IkiWiki::pagesources{$page} ?
  2222. $IkiWiki::pagesources{$page} :
  2223. $IkiWiki::delpagesources{$page};
  2224. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  2225. if (! defined $type) {
  2226. return IkiWiki::FailReason->new("$page is not a page");
  2227. }
  2228. }
  2229. return $match;
  2230. }
  2231. sub match_link ($$;@) {
  2232. my $page=shift;
  2233. my $link=lc(shift);
  2234. my %params=@_;
  2235. $link=derel($link, $params{location});
  2236. my $from=exists $params{location} ? $params{location} : '';
  2237. my $linktype=$params{linktype};
  2238. my $qualifier='';
  2239. if (defined $linktype) {
  2240. $qualifier=" with type $linktype";
  2241. }
  2242. my $links = $IkiWiki::links{$page};
  2243. return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2244. unless $links && @{$links};
  2245. my $bestlink = IkiWiki::bestlink($from, $link);
  2246. foreach my $p (@{$links}) {
  2247. next unless (! defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p});
  2248. if (length $bestlink) {
  2249. if ($bestlink eq IkiWiki::bestlink($page, $p)) {
  2250. return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2251. }
  2252. }
  2253. else {
  2254. if (match_glob($p, $link, %params)) {
  2255. return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2256. }
  2257. my ($p_rel)=$p=~/^\/?(.*)/;
  2258. $link=~s/^\///;
  2259. if (match_glob($p_rel, $link, %params)) {
  2260. return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2261. }
  2262. }
  2263. }
  2264. return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
  2265. }
  2266. sub match_backlink ($$;@) {
  2267. my $ret=match_link($_[1], $_[0], @_);
  2268. $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
  2269. return $ret;
  2270. }
  2271. sub match_created_before ($$;@) {
  2272. my $page=shift;
  2273. my $testpage=shift;
  2274. my %params=@_;
  2275. $testpage=derel($testpage, $params{location});
  2276. if (exists $IkiWiki::pagectime{$testpage}) {
  2277. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  2278. return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2279. }
  2280. else {
  2281. return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2282. }
  2283. }
  2284. else {
  2285. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2286. }
  2287. }
  2288. sub match_created_after ($$;@) {
  2289. my $page=shift;
  2290. my $testpage=shift;
  2291. my %params=@_;
  2292. $testpage=derel($testpage, $params{location});
  2293. if (exists $IkiWiki::pagectime{$testpage}) {
  2294. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  2295. return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2296. }
  2297. else {
  2298. return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2299. }
  2300. }
  2301. else {
  2302. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2303. }
  2304. }
  2305. sub match_creation_day ($$;@) {
  2306. my $page=shift;
  2307. my $d=shift;
  2308. if ($d !~ /^\d+$/) {
  2309. return IkiWiki::ErrorReason->new("invalid day $d");
  2310. }
  2311. if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
  2312. return IkiWiki::SuccessReason->new('creation_day matched');
  2313. }
  2314. else {
  2315. return IkiWiki::FailReason->new('creation_day did not match');
  2316. }
  2317. }
  2318. sub match_creation_month ($$;@) {
  2319. my $page=shift;
  2320. my $m=shift;
  2321. if ($m !~ /^\d+$/) {
  2322. return IkiWiki::ErrorReason->new("invalid month $m");
  2323. }
  2324. if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
  2325. return IkiWiki::SuccessReason->new('creation_month matched');
  2326. }
  2327. else {
  2328. return IkiWiki::FailReason->new('creation_month did not match');
  2329. }
  2330. }
  2331. sub match_creation_year ($$;@) {
  2332. my $page=shift;
  2333. my $y=shift;
  2334. if ($y !~ /^\d+$/) {
  2335. return IkiWiki::ErrorReason->new("invalid year $y");
  2336. }
  2337. if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
  2338. return IkiWiki::SuccessReason->new('creation_year matched');
  2339. }
  2340. else {
  2341. return IkiWiki::FailReason->new('creation_year did not match');
  2342. }
  2343. }
  2344. sub match_user ($$;@) {
  2345. shift;
  2346. my $user=shift;
  2347. my %params=@_;
  2348. my $regexp=IkiWiki::glob2re($user);
  2349. if (! exists $params{user}) {
  2350. return IkiWiki::ErrorReason->new("no user specified");
  2351. }
  2352. if (defined $params{user} && $params{user}=~$regexp) {
  2353. return IkiWiki::SuccessReason->new("user is $user");
  2354. }
  2355. elsif (! defined $params{user}) {
  2356. return IkiWiki::FailReason->new("not logged in");
  2357. }
  2358. else {
  2359. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  2360. }
  2361. }
  2362. sub match_admin ($$;@) {
  2363. shift;
  2364. shift;
  2365. my %params=@_;
  2366. if (! exists $params{user}) {
  2367. return IkiWiki::ErrorReason->new("no user specified");
  2368. }
  2369. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  2370. return IkiWiki::SuccessReason->new("user is an admin");
  2371. }
  2372. elsif (! defined $params{user}) {
  2373. return IkiWiki::FailReason->new("not logged in");
  2374. }
  2375. else {
  2376. return IkiWiki::FailReason->new("user is not an admin");
  2377. }
  2378. }
  2379. sub match_ip ($$;@) {
  2380. shift;
  2381. my $ip=shift;
  2382. my %params=@_;
  2383. if (! exists $params{ip}) {
  2384. return IkiWiki::ErrorReason->new("no IP specified");
  2385. }
  2386. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  2387. return IkiWiki::SuccessReason->new("IP is $ip");
  2388. }
  2389. else {
  2390. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  2391. }
  2392. }
  2393. package IkiWiki::SortSpec;
  2394. # This is in the SortSpec namespace so that the $a and $b that sort() uses
  2395. # are easily available in this namespace, for cmp functions to use them.
  2396. sub sort_pages {
  2397. my $f=shift;
  2398. sort $f @_
  2399. }
  2400. sub cmp_title {
  2401. IkiWiki::pagetitle(IkiWiki::basename($a))
  2402. cmp
  2403. IkiWiki::pagetitle(IkiWiki::basename($b))
  2404. }
  2405. sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
  2406. sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
  2407. 1