summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: a4afef8e0381f337d9cf8c26fe961210a2f5aaf0 (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. # URL to top of wiki as a path starting with /, valid from any wiki page or
  496. # the CGI; if that's not possible, an absolute URL. Either way, it ends with /
  497. my $local_url;
  498. # URL to CGI script, similar to $local_url
  499. my $local_cgiurl;
  500. sub checkconfig () {
  501. # locale stuff; avoid LC_ALL since it overrides everything
  502. if (defined $ENV{LC_ALL}) {
  503. $ENV{LANG} = $ENV{LC_ALL};
  504. delete $ENV{LC_ALL};
  505. }
  506. if (defined $config{locale}) {
  507. if (POSIX::setlocale(&POSIX::LC_ALL, $config{locale})) {
  508. $ENV{LANG}=$config{locale};
  509. define_gettext();
  510. }
  511. }
  512. if (! defined $config{wiki_file_regexp}) {
  513. $config{wiki_file_regexp}=qr/(^[$config{wiki_file_chars}]+$)/;
  514. }
  515. if (ref $config{ENV} eq 'HASH') {
  516. foreach my $val (keys %{$config{ENV}}) {
  517. $ENV{$val}=$config{ENV}{$val};
  518. }
  519. }
  520. if ($config{w3mmode}) {
  521. eval q{use Cwd q{abs_path}};
  522. error($@) if $@;
  523. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  524. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  525. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  526. unless $config{cgiurl} =~ m!file:///!;
  527. $config{url}="file://".$config{destdir};
  528. }
  529. if ($config{cgi} && ! length $config{url}) {
  530. error(gettext("Must specify url to wiki with --url when using --cgi"));
  531. }
  532. if (length $config{url}) {
  533. eval q{use URI};
  534. my $baseurl = URI->new($config{url});
  535. $local_url = $baseurl->path . "/";
  536. $local_cgiurl = undef;
  537. if (length $config{cgiurl}) {
  538. my $cgiurl = URI->new($config{cgiurl});
  539. $local_cgiurl = $cgiurl->path;
  540. if ($cgiurl->scheme ne $baseurl->scheme or
  541. $cgiurl->authority ne $baseurl->authority) {
  542. # too far apart, fall back to absolute URLs
  543. $local_url = "$config{url}/";
  544. $local_cgiurl = $config{cgiurl};
  545. }
  546. }
  547. $local_url =~ s{//$}{/};
  548. }
  549. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  550. unless exists $config{wikistatedir} && defined $config{wikistatedir};
  551. if (defined $config{umask}) {
  552. umask(possibly_foolish_untaint($config{umask}));
  553. }
  554. run_hooks(checkconfig => sub { shift->() });
  555. return 1;
  556. }
  557. sub listplugins () {
  558. my %ret;
  559. foreach my $dir (@INC, $config{libdir}) {
  560. next unless defined $dir && length $dir;
  561. foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
  562. my ($plugin)=$file=~/.*\/(.*)\.pm$/;
  563. $ret{$plugin}=1;
  564. }
  565. }
  566. foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
  567. next unless defined $dir && length $dir;
  568. foreach my $file (glob("$dir/plugins/*")) {
  569. $ret{basename($file)}=1 if -x $file;
  570. }
  571. }
  572. return keys %ret;
  573. }
  574. sub loadplugins () {
  575. if (defined $config{libdir} && length $config{libdir}) {
  576. unshift @INC, possibly_foolish_untaint($config{libdir});
  577. }
  578. foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
  579. loadplugin($plugin);
  580. }
  581. if ($config{rcs}) {
  582. if (exists $hooks{rcs}) {
  583. error(gettext("cannot use multiple rcs plugins"));
  584. }
  585. loadplugin($config{rcs});
  586. }
  587. if (! exists $hooks{rcs}) {
  588. loadplugin("norcs");
  589. }
  590. run_hooks(getopt => sub { shift->() });
  591. if (grep /^-/, @ARGV) {
  592. print STDERR "Unknown option (or missing parameter): $_\n"
  593. foreach grep /^-/, @ARGV;
  594. usage();
  595. }
  596. return 1;
  597. }
  598. sub loadplugin ($;$) {
  599. my $plugin=shift;
  600. my $force=shift;
  601. return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
  602. foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
  603. "$installdir/lib/ikiwiki") {
  604. if (defined $dir && -x "$dir/plugins/$plugin") {
  605. eval { require IkiWiki::Plugin::external };
  606. if ($@) {
  607. my $reason=$@;
  608. error(sprintf(gettext("failed to load external plugin needed for %s plugin: %s"), $plugin, $reason));
  609. }
  610. import IkiWiki::Plugin::external "$dir/plugins/$plugin";
  611. $loaded_plugins{$plugin}=1;
  612. return 1;
  613. }
  614. }
  615. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  616. eval qq{use $mod};
  617. if ($@) {
  618. error("Failed to load plugin $mod: $@");
  619. }
  620. $loaded_plugins{$plugin}=1;
  621. return 1;
  622. }
  623. sub error ($;$) {
  624. my $message=shift;
  625. my $cleaner=shift;
  626. log_message('err' => $message) if $config{syslog};
  627. if (defined $cleaner) {
  628. $cleaner->();
  629. }
  630. die $message."\n";
  631. }
  632. sub debug ($) {
  633. return unless $config{verbose};
  634. return log_message(debug => @_);
  635. }
  636. my $log_open=0;
  637. sub log_message ($$) {
  638. my $type=shift;
  639. if ($config{syslog}) {
  640. require Sys::Syslog;
  641. if (! $log_open) {
  642. Sys::Syslog::setlogsock('unix');
  643. Sys::Syslog::openlog('ikiwiki', '', 'user');
  644. $log_open=1;
  645. }
  646. return eval {
  647. Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
  648. };
  649. }
  650. elsif (! $config{cgi}) {
  651. return print "@_\n";
  652. }
  653. else {
  654. return print STDERR "@_\n";
  655. }
  656. }
  657. sub possibly_foolish_untaint ($) {
  658. my $tainted=shift;
  659. my ($untainted)=$tainted=~/(.*)/s;
  660. return $untainted;
  661. }
  662. sub basename ($) {
  663. my $file=shift;
  664. $file=~s!.*/+!!;
  665. return $file;
  666. }
  667. sub dirname ($) {
  668. my $file=shift;
  669. $file=~s!/*[^/]+$!!;
  670. return $file;
  671. }
  672. sub isinternal ($) {
  673. my $page=shift;
  674. return exists $pagesources{$page} &&
  675. $pagesources{$page} =~ /\._([^.]+)$/;
  676. }
  677. sub pagetype ($) {
  678. my $file=shift;
  679. if ($file =~ /\.([^.]+)$/) {
  680. return $1 if exists $hooks{htmlize}{$1};
  681. }
  682. my $base=basename($file);
  683. if (exists $hooks{htmlize}{$base} &&
  684. $hooks{htmlize}{$base}{noextension}) {
  685. return $base;
  686. }
  687. return;
  688. }
  689. my %pagename_cache;
  690. sub pagename ($) {
  691. my $file=shift;
  692. if (exists $pagename_cache{$file}) {
  693. return $pagename_cache{$file};
  694. }
  695. my $type=pagetype($file);
  696. my $page=$file;
  697. $page=~s/\Q.$type\E*$//
  698. if defined $type && !$hooks{htmlize}{$type}{keepextension}
  699. && !$hooks{htmlize}{$type}{noextension};
  700. if ($config{indexpages} && $page=~/(.*)\/index$/) {
  701. $page=$1;
  702. }
  703. $pagename_cache{$file} = $page;
  704. return $page;
  705. }
  706. sub newpagefile ($$) {
  707. my $page=shift;
  708. my $type=shift;
  709. if (! $config{indexpages} || $page eq 'index') {
  710. return $page.".".$type;
  711. }
  712. else {
  713. return $page."/index.".$type;
  714. }
  715. }
  716. sub targetpage ($$;$) {
  717. my $page=shift;
  718. my $ext=shift;
  719. my $filename=shift;
  720. if (defined $filename) {
  721. return $page."/".$filename.".".$ext;
  722. }
  723. elsif (! $config{usedirs} || $page eq 'index') {
  724. return $page.".".$ext;
  725. }
  726. else {
  727. return $page."/index.".$ext;
  728. }
  729. }
  730. sub htmlpage ($) {
  731. my $page=shift;
  732. return targetpage($page, $config{htmlext});
  733. }
  734. sub srcfile_stat {
  735. my $file=shift;
  736. my $nothrow=shift;
  737. return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
  738. foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
  739. return "$dir/$file", stat(_) if -e "$dir/$file";
  740. }
  741. error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
  742. return;
  743. }
  744. sub srcfile ($;$) {
  745. return (srcfile_stat(@_))[0];
  746. }
  747. sub add_underlay ($) {
  748. my $dir=shift;
  749. if ($dir !~ /^\//) {
  750. $dir="$config{underlaydirbase}/$dir";
  751. }
  752. if (! grep { $_ eq $dir } @{$config{underlaydirs}}) {
  753. unshift @{$config{underlaydirs}}, $dir;
  754. }
  755. return 1;
  756. }
  757. sub readfile ($;$$) {
  758. my $file=shift;
  759. my $binary=shift;
  760. my $wantfd=shift;
  761. if (-l $file) {
  762. error("cannot read a symlink ($file)");
  763. }
  764. local $/=undef;
  765. open (my $in, "<", $file) || error("failed to read $file: $!");
  766. binmode($in) if ($binary);
  767. return \*$in if $wantfd;
  768. my $ret=<$in>;
  769. # check for invalid utf-8, and toss it back to avoid crashes
  770. if (! utf8::valid($ret)) {
  771. $ret=encode_utf8($ret);
  772. }
  773. close $in || error("failed to read $file: $!");
  774. return $ret;
  775. }
  776. sub prep_writefile ($$) {
  777. my $file=shift;
  778. my $destdir=shift;
  779. my $test=$file;
  780. while (length $test) {
  781. if (-l "$destdir/$test") {
  782. error("cannot write to a symlink ($test)");
  783. }
  784. if (-f _ && $test ne $file) {
  785. # Remove conflicting file.
  786. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  787. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  788. if ($f eq $test) {
  789. unlink("$destdir/$test");
  790. last;
  791. }
  792. }
  793. }
  794. }
  795. $test=dirname($test);
  796. }
  797. my $dir=dirname("$destdir/$file");
  798. if (! -d $dir) {
  799. my $d="";
  800. foreach my $s (split(m!/+!, $dir)) {
  801. $d.="$s/";
  802. if (! -d $d) {
  803. mkdir($d) || error("failed to create directory $d: $!");
  804. }
  805. }
  806. }
  807. return 1;
  808. }
  809. sub writefile ($$$;$$) {
  810. my $file=shift; # can include subdirs
  811. my $destdir=shift; # directory to put file in
  812. my $content=shift;
  813. my $binary=shift;
  814. my $writer=shift;
  815. prep_writefile($file, $destdir);
  816. my $newfile="$destdir/$file.ikiwiki-new";
  817. if (-l $newfile) {
  818. error("cannot write to a symlink ($newfile)");
  819. }
  820. my $cleanup = sub { unlink($newfile) };
  821. open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
  822. binmode($out) if ($binary);
  823. if ($writer) {
  824. $writer->(\*$out, $cleanup);
  825. }
  826. else {
  827. print $out $content or error("failed writing to $newfile: $!", $cleanup);
  828. }
  829. close $out || error("failed saving $newfile: $!", $cleanup);
  830. rename($newfile, "$destdir/$file") ||
  831. error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
  832. return 1;
  833. }
  834. my %cleared;
  835. sub will_render ($$;$) {
  836. my $page=shift;
  837. my $dest=shift;
  838. my $clear=shift;
  839. # Important security check for independently created files.
  840. if (-e "$config{destdir}/$dest" && ! $config{rebuild} &&
  841. ! grep { $_ eq $dest } (@{$renderedfiles{$page}}, @{$oldrenderedfiles{$page}}, @{$wikistate{editpage}{previews}})) {
  842. my $from_other_page=0;
  843. # Expensive, but rarely runs.
  844. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  845. if (grep {
  846. $_ eq $dest ||
  847. dirname($_) eq $dest
  848. } @{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  849. $from_other_page=1;
  850. last;
  851. }
  852. }
  853. error("$config{destdir}/$dest independently created, not overwriting with version from $page")
  854. unless $from_other_page;
  855. }
  856. # If $dest exists as a directory, remove conflicting files in it
  857. # rendered from other pages.
  858. if (-d _) {
  859. foreach my $p (keys %renderedfiles, keys %oldrenderedfiles) {
  860. foreach my $f (@{$renderedfiles{$p}}, @{$oldrenderedfiles{$p}}) {
  861. if (dirname($f) eq $dest) {
  862. unlink("$config{destdir}/$f");
  863. rmdir(dirname("$config{destdir}/$f"));
  864. }
  865. }
  866. }
  867. }
  868. if (! $clear || $cleared{$page}) {
  869. $renderedfiles{$page}=[$dest, grep { $_ ne $dest } @{$renderedfiles{$page}}];
  870. }
  871. else {
  872. foreach my $old (@{$renderedfiles{$page}}) {
  873. delete $destsources{$old};
  874. }
  875. $renderedfiles{$page}=[$dest];
  876. $cleared{$page}=1;
  877. }
  878. $destsources{$dest}=$page;
  879. return 1;
  880. }
  881. sub bestlink ($$) {
  882. my $page=shift;
  883. my $link=shift;
  884. my $cwd=$page;
  885. if ($link=~s/^\/+//) {
  886. # absolute links
  887. $cwd="";
  888. }
  889. $link=~s/\/$//;
  890. do {
  891. my $l=$cwd;
  892. $l.="/" if length $l;
  893. $l.=$link;
  894. if (exists $pagesources{$l}) {
  895. return $l;
  896. }
  897. elsif (exists $pagecase{lc $l}) {
  898. return $pagecase{lc $l};
  899. }
  900. } while $cwd=~s{/?[^/]+$}{};
  901. if (length $config{userdir}) {
  902. my $l = "$config{userdir}/".lc($link);
  903. if (exists $pagesources{$l}) {
  904. return $l;
  905. }
  906. elsif (exists $pagecase{lc $l}) {
  907. return $pagecase{lc $l};
  908. }
  909. }
  910. #print STDERR "warning: page $page, broken link: $link\n";
  911. return "";
  912. }
  913. sub isinlinableimage ($) {
  914. my $file=shift;
  915. return $file =~ /\.(png|gif|jpg|jpeg)$/i;
  916. }
  917. sub pagetitle ($;$) {
  918. my $page=shift;
  919. my $unescaped=shift;
  920. if ($unescaped) {
  921. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : chr($2)/eg;
  922. }
  923. else {
  924. $page=~s/(__(\d+)__|_)/$1 eq '_' ? ' ' : "&#$2;"/eg;
  925. }
  926. return $page;
  927. }
  928. sub titlepage ($) {
  929. my $title=shift;
  930. # support use w/o %config set
  931. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  932. $title=~s/([^$chars]|_)/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  933. return $title;
  934. }
  935. sub linkpage ($) {
  936. my $link=shift;
  937. my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
  938. $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
  939. return $link;
  940. }
  941. sub cgiurl (@) {
  942. my %params=@_;
  943. my $cgiurl=$local_cgiurl;
  944. if (exists $params{cgiurl}) {
  945. $cgiurl=$params{cgiurl};
  946. delete $params{cgiurl};
  947. }
  948. unless (%params) {
  949. return $cgiurl;
  950. }
  951. return $cgiurl."?".
  952. join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
  953. }
  954. sub baseurl (;$) {
  955. my $page=shift;
  956. return $local_url if ! defined $page;
  957. $page=htmlpage($page);
  958. $page=~s/[^\/]+$//;
  959. $page=~s/[^\/]+\//..\//g;
  960. return $page;
  961. }
  962. sub abs2rel ($$) {
  963. # Work around very innefficient behavior in File::Spec if abs2rel
  964. # is passed two relative paths. It's much faster if paths are
  965. # absolute! (Debian bug #376658; fixed in debian unstable now)
  966. my $path="/".shift;
  967. my $base="/".shift;
  968. require File::Spec;
  969. my $ret=File::Spec->abs2rel($path, $base);
  970. $ret=~s/^// if defined $ret;
  971. return $ret;
  972. }
  973. sub displaytime ($;$$) {
  974. # Plugins can override this function to mark up the time to
  975. # display.
  976. my $time=formattime($_[0], $_[1]);
  977. if ($config{html5}) {
  978. return '<time datetime="'.date_3339($_[0]).'"'.
  979. ($_[2] ? ' pubdate="pubdate"' : '').
  980. '>'.$time.'</time>';
  981. }
  982. else {
  983. return '<span class="date">'.$time.'</span>';
  984. }
  985. }
  986. sub formattime ($;$) {
  987. # Plugins can override this function to format the time.
  988. my $time=shift;
  989. my $format=shift;
  990. if (! defined $format) {
  991. $format=$config{timeformat};
  992. }
  993. # strftime doesn't know about encodings, so make sure
  994. # its output is properly treated as utf8
  995. return decode_utf8(POSIX::strftime($format, localtime($time)));
  996. }
  997. sub date_3339 ($) {
  998. my $time=shift;
  999. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  1000. POSIX::setlocale(&POSIX::LC_TIME, "C");
  1001. my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
  1002. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  1003. return $ret;
  1004. }
  1005. sub beautify_urlpath ($) {
  1006. my $url=shift;
  1007. # Ensure url is not an empty link, and if necessary,
  1008. # add ./ to avoid colon confusion.
  1009. if ($url !~ /^\// && $url !~ /^\.\.?\//) {
  1010. $url="./$url";
  1011. }
  1012. if ($config{usedirs}) {
  1013. $url =~ s!/index.$config{htmlext}$!/!;
  1014. }
  1015. return $url;
  1016. }
  1017. sub urlto ($$;$) {
  1018. my $to=shift;
  1019. my $from=shift;
  1020. my $absolute=shift;
  1021. if (! length $to) {
  1022. return beautify_urlpath(baseurl($from)."index.$config{htmlext}");
  1023. }
  1024. if (! $destsources{$to}) {
  1025. $to=htmlpage($to);
  1026. }
  1027. if ($absolute) {
  1028. return $config{url}.beautify_urlpath("/".$to);
  1029. }
  1030. my $link = abs2rel($to, dirname(htmlpage($from)));
  1031. return beautify_urlpath($link);
  1032. }
  1033. sub isselflink ($$) {
  1034. # Plugins can override this function to support special types
  1035. # of selflinks.
  1036. my $page=shift;
  1037. my $link=shift;
  1038. return $page eq $link;
  1039. }
  1040. sub htmllink ($$$;@) {
  1041. my $lpage=shift; # the page doing the linking
  1042. my $page=shift; # the page that will contain the link (different for inline)
  1043. my $link=shift;
  1044. my %opts=@_;
  1045. $link=~s/\/$//;
  1046. my $bestlink;
  1047. if (! $opts{forcesubpage}) {
  1048. $bestlink=bestlink($lpage, $link);
  1049. }
  1050. else {
  1051. $bestlink="$lpage/".lc($link);
  1052. }
  1053. my $linktext;
  1054. if (defined $opts{linktext}) {
  1055. $linktext=$opts{linktext};
  1056. }
  1057. else {
  1058. $linktext=pagetitle(basename($link));
  1059. }
  1060. return "<span class=\"selflink\">$linktext</span>"
  1061. if length $bestlink && isselflink($page, $bestlink) &&
  1062. ! defined $opts{anchor};
  1063. if (! $destsources{$bestlink}) {
  1064. $bestlink=htmlpage($bestlink);
  1065. if (! $destsources{$bestlink}) {
  1066. my $cgilink = "";
  1067. if (length $config{cgiurl}) {
  1068. $cgilink = "<a href=\"".
  1069. cgiurl(
  1070. do => "create",
  1071. page => lc($link),
  1072. from => $lpage
  1073. )."\" rel=\"nofollow\">?</a>";
  1074. }
  1075. return "<span class=\"createlink\">$cgilink$linktext</span>"
  1076. }
  1077. }
  1078. $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
  1079. $bestlink=beautify_urlpath($bestlink);
  1080. if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
  1081. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  1082. }
  1083. if (defined $opts{anchor}) {
  1084. $bestlink.="#".$opts{anchor};
  1085. }
  1086. my @attrs;
  1087. foreach my $attr (qw{rel class title}) {
  1088. if (defined $opts{$attr}) {
  1089. push @attrs, " $attr=\"$opts{$attr}\"";
  1090. }
  1091. }
  1092. return "<a href=\"$bestlink\"@attrs>$linktext</a>";
  1093. }
  1094. sub userpage ($) {
  1095. my $user=shift;
  1096. return length $config{userdir} ? "$config{userdir}/$user" : $user;
  1097. }
  1098. sub openiduser ($) {
  1099. my $user=shift;
  1100. if ($user =~ m!^https?://! &&
  1101. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  1102. my $display;
  1103. if (Net::OpenID::VerifiedIdentity->can("DisplayOfURL")) {
  1104. $display = Net::OpenID::VerifiedIdentity::DisplayOfURL($user);
  1105. }
  1106. else {
  1107. # backcompat with old version
  1108. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  1109. $display=$oid->display;
  1110. }
  1111. # Convert "user.somehost.com" to "user [somehost.com]"
  1112. # (also "user.somehost.co.uk")
  1113. if ($display !~ /\[/) {
  1114. $display=~s/^([-a-zA-Z0-9]+?)\.([-.a-zA-Z0-9]+\.[a-z]+)$/$1 [$2]/;
  1115. }
  1116. # Convert "http://somehost.com/user" to "user [somehost.com]".
  1117. # (also "https://somehost.com/user/")
  1118. if ($display !~ /\[/) {
  1119. $display=~s/^https?:\/\/(.+)\/([^\/#?]+)\/?(?:[#?].*)?$/$2 [$1]/;
  1120. }
  1121. $display=~s!^https?://!!; # make sure this is removed
  1122. eval q{use CGI 'escapeHTML'};
  1123. error($@) if $@;
  1124. return escapeHTML($display);
  1125. }
  1126. return;
  1127. }
  1128. sub htmlize ($$$$) {
  1129. my $page=shift;
  1130. my $destpage=shift;
  1131. my $type=shift;
  1132. my $content=shift;
  1133. my $oneline = $content !~ /\n/;
  1134. if (exists $hooks{htmlize}{$type}) {
  1135. $content=$hooks{htmlize}{$type}{call}->(
  1136. page => $page,
  1137. content => $content,
  1138. );
  1139. }
  1140. else {
  1141. error("htmlization of $type not supported");
  1142. }
  1143. run_hooks(sanitize => sub {
  1144. $content=shift->(
  1145. page => $page,
  1146. destpage => $destpage,
  1147. content => $content,
  1148. );
  1149. });
  1150. if ($oneline) {
  1151. # hack to get rid of enclosing junk added by markdown
  1152. # and other htmlizers/sanitizers
  1153. $content=~s/^<p>//i;
  1154. $content=~s/<\/p>\n*$//i;
  1155. }
  1156. return $content;
  1157. }
  1158. sub linkify ($$$) {
  1159. my $page=shift;
  1160. my $destpage=shift;
  1161. my $content=shift;
  1162. run_hooks(linkify => sub {
  1163. $content=shift->(
  1164. page => $page,
  1165. destpage => $destpage,
  1166. content => $content,
  1167. );
  1168. });
  1169. return $content;
  1170. }
  1171. our %preprocessing;
  1172. our $preprocess_preview=0;
  1173. sub preprocess ($$$;$$) {
  1174. my $page=shift; # the page the data comes from
  1175. my $destpage=shift; # the page the data will appear in (different for inline)
  1176. my $content=shift;
  1177. my $scan=shift;
  1178. my $preview=shift;
  1179. # Using local because it needs to be set within any nested calls
  1180. # of this function.
  1181. local $preprocess_preview=$preview if defined $preview;
  1182. my $handle=sub {
  1183. my $escape=shift;
  1184. my $prefix=shift;
  1185. my $command=shift;
  1186. my $params=shift;
  1187. $params="" if ! defined $params;
  1188. if (length $escape) {
  1189. return "[[$prefix$command $params]]";
  1190. }
  1191. elsif (exists $hooks{preprocess}{$command}) {
  1192. return "" if $scan && ! $hooks{preprocess}{$command}{scan};
  1193. # Note: preserve order of params, some plugins may
  1194. # consider it significant.
  1195. my @params;
  1196. while ($params =~ m{
  1197. (?:([-\w]+)=)? # 1: named parameter key?
  1198. (?:
  1199. """(.*?)""" # 2: triple-quoted value
  1200. |
  1201. "([^"]*?)" # 3: single-quoted value
  1202. |
  1203. (\S+) # 4: unquoted value
  1204. )
  1205. (?:\s+|$) # delimiter to next param
  1206. }sgx) {
  1207. my $key=$1;
  1208. my $val;
  1209. if (defined $2) {
  1210. $val=$2;
  1211. $val=~s/\r\n/\n/mg;
  1212. $val=~s/^\n+//g;
  1213. $val=~s/\n+$//g;
  1214. }
  1215. elsif (defined $3) {
  1216. $val=$3;
  1217. }
  1218. elsif (defined $4) {
  1219. $val=$4;
  1220. }
  1221. if (defined $key) {
  1222. push @params, $key, $val;
  1223. }
  1224. else {
  1225. push @params, $val, '';
  1226. }
  1227. }
  1228. if ($preprocessing{$page}++ > 3) {
  1229. # Avoid loops of preprocessed pages preprocessing
  1230. # other pages that preprocess them, etc.
  1231. return "[[!$command <span class=\"error\">".
  1232. sprintf(gettext("preprocessing loop detected on %s at depth %i"),
  1233. $page, $preprocessing{$page}).
  1234. "</span>]]";
  1235. }
  1236. my $ret;
  1237. if (! $scan) {
  1238. $ret=eval {
  1239. $hooks{preprocess}{$command}{call}->(
  1240. @params,
  1241. page => $page,
  1242. destpage => $destpage,
  1243. preview => $preprocess_preview,
  1244. );
  1245. };
  1246. if ($@) {
  1247. my $error=$@;
  1248. chomp $error;
  1249. $ret="[[!$command <span class=\"error\">".
  1250. gettext("Error").": $error"."</span>]]";
  1251. }
  1252. }
  1253. else {
  1254. # use void context during scan pass
  1255. eval {
  1256. $hooks{preprocess}{$command}{call}->(
  1257. @params,
  1258. page => $page,
  1259. destpage => $destpage,
  1260. preview => $preprocess_preview,
  1261. );
  1262. };
  1263. $ret="";
  1264. }
  1265. $preprocessing{$page}--;
  1266. return $ret;
  1267. }
  1268. else {
  1269. return "[[$prefix$command $params]]";
  1270. }
  1271. };
  1272. my $regex;
  1273. if ($config{prefix_directives}) {
  1274. $regex = qr{
  1275. (\\?) # 1: escape?
  1276. \[\[(!) # directive open; 2: prefix
  1277. ([-\w]+) # 3: command
  1278. ( # 4: the parameters..
  1279. \s+ # Must have space if parameters present
  1280. (?:
  1281. (?:[-\w]+=)? # named parameter key?
  1282. (?:
  1283. """.*?""" # triple-quoted value
  1284. |
  1285. "[^"]*?" # single-quoted value
  1286. |
  1287. [^"\s\]]+ # unquoted value
  1288. )
  1289. \s* # whitespace or end
  1290. # of directive
  1291. )
  1292. *)? # 0 or more parameters
  1293. \]\] # directive closed
  1294. }sx;
  1295. }
  1296. else {
  1297. $regex = qr{
  1298. (\\?) # 1: escape?
  1299. \[\[(!?) # directive open; 2: optional prefix
  1300. ([-\w]+) # 3: command
  1301. \s+
  1302. ( # 4: the parameters..
  1303. (?:
  1304. (?:[-\w]+=)? # named parameter key?
  1305. (?:
  1306. """.*?""" # triple-quoted value
  1307. |
  1308. "[^"]*?" # single-quoted value
  1309. |
  1310. [^"\s\]]+ # unquoted value
  1311. )
  1312. \s* # whitespace or end
  1313. # of directive
  1314. )
  1315. *) # 0 or more parameters
  1316. \]\] # directive closed
  1317. }sx;
  1318. }
  1319. $content =~ s{$regex}{$handle->($1, $2, $3, $4)}eg;
  1320. return $content;
  1321. }
  1322. sub filter ($$$) {
  1323. my $page=shift;
  1324. my $destpage=shift;
  1325. my $content=shift;
  1326. run_hooks(filter => sub {
  1327. $content=shift->(page => $page, destpage => $destpage,
  1328. content => $content);
  1329. });
  1330. return $content;
  1331. }
  1332. sub check_canedit ($$$;$) {
  1333. my $page=shift;
  1334. my $q=shift;
  1335. my $session=shift;
  1336. my $nonfatal=shift;
  1337. my $canedit;
  1338. run_hooks(canedit => sub {
  1339. return if defined $canedit;
  1340. my $ret=shift->($page, $q, $session);
  1341. if (defined $ret) {
  1342. if ($ret eq "") {
  1343. $canedit=1;
  1344. }
  1345. elsif (ref $ret eq 'CODE') {
  1346. $ret->() unless $nonfatal;
  1347. $canedit=0;
  1348. }
  1349. elsif (defined $ret) {
  1350. error($ret) unless $nonfatal;
  1351. $canedit=0;
  1352. }
  1353. }
  1354. });
  1355. return defined $canedit ? $canedit : 1;
  1356. }
  1357. sub check_content (@) {
  1358. my %params=@_;
  1359. return 1 if ! exists $hooks{checkcontent}; # optimisation
  1360. if (exists $pagesources{$params{page}}) {
  1361. my @diff;
  1362. my %old=map { $_ => 1 }
  1363. split("\n", readfile(srcfile($pagesources{$params{page}})));
  1364. foreach my $line (split("\n", $params{content})) {
  1365. push @diff, $line if ! exists $old{$line};
  1366. }
  1367. $params{diff}=join("\n", @diff);
  1368. }
  1369. my $ok;
  1370. run_hooks(checkcontent => sub {
  1371. return if defined $ok;
  1372. my $ret=shift->(%params);
  1373. if (defined $ret) {
  1374. if ($ret eq "") {
  1375. $ok=1;
  1376. }
  1377. elsif (ref $ret eq 'CODE') {
  1378. $ret->() unless $params{nonfatal};
  1379. $ok=0;
  1380. }
  1381. elsif (defined $ret) {
  1382. error($ret) unless $params{nonfatal};
  1383. $ok=0;
  1384. }
  1385. }
  1386. });
  1387. return defined $ok ? $ok : 1;
  1388. }
  1389. sub check_canchange (@) {
  1390. my %params = @_;
  1391. my $cgi = $params{cgi};
  1392. my $session = $params{session};
  1393. my @changes = @{$params{changes}};
  1394. my %newfiles;
  1395. foreach my $change (@changes) {
  1396. # This untaint is safe because we check file_pruned and
  1397. # wiki_file_regexp.
  1398. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  1399. $file=possibly_foolish_untaint($file);
  1400. if (! defined $file || ! length $file ||
  1401. file_pruned($file)) {
  1402. error(gettext("bad file name %s"), $file);
  1403. }
  1404. my $type=pagetype($file);
  1405. my $page=pagename($file) if defined $type;
  1406. if ($change->{action} eq 'add') {
  1407. $newfiles{$file}=1;
  1408. }
  1409. if ($change->{action} eq 'change' ||
  1410. $change->{action} eq 'add') {
  1411. if (defined $page) {
  1412. check_canedit($page, $cgi, $session);
  1413. next;
  1414. }
  1415. else {
  1416. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  1417. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  1418. check_canedit($file, $cgi, $session);
  1419. next;
  1420. }
  1421. }
  1422. }
  1423. elsif ($change->{action} eq 'remove') {
  1424. # check_canremove tests to see if the file is present
  1425. # on disk. This will fail when a single commit adds a
  1426. # file and then removes it again. Avoid the problem
  1427. # by not testing the removal in such pairs of changes.
  1428. # (The add is still tested, just to make sure that
  1429. # no data is added to the repo that a web edit
  1430. # could not add.)
  1431. next if $newfiles{$file};
  1432. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  1433. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  1434. check_canedit(defined $page ? $page : $file, $cgi, $session);
  1435. next;
  1436. }
  1437. }
  1438. else {
  1439. error "unknown action ".$change->{action};
  1440. }
  1441. error sprintf(gettext("you are not allowed to change %s"), $file);
  1442. }
  1443. }
  1444. my $wikilock;
  1445. sub lockwiki () {
  1446. # Take an exclusive lock on the wiki to prevent multiple concurrent
  1447. # run issues. The lock will be dropped on program exit.
  1448. if (! -d $config{wikistatedir}) {
  1449. mkdir($config{wikistatedir});
  1450. }
  1451. open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
  1452. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  1453. if (! flock($wikilock, 2)) { # LOCK_EX
  1454. error("failed to get lock");
  1455. }
  1456. return 1;
  1457. }
  1458. sub unlockwiki () {
  1459. POSIX::close($ENV{IKIWIKI_CGILOCK_FD}) if exists $ENV{IKIWIKI_CGILOCK_FD};
  1460. return close($wikilock) if $wikilock;
  1461. return;
  1462. }
  1463. my $commitlock;
  1464. sub commit_hook_enabled () {
  1465. open($commitlock, '+>', "$config{wikistatedir}/commitlock") ||
  1466. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1467. if (! flock($commitlock, 1 | 4)) { # LOCK_SH | LOCK_NB to test
  1468. close($commitlock) || error("failed closing commitlock: $!");
  1469. return 0;
  1470. }
  1471. close($commitlock) || error("failed closing commitlock: $!");
  1472. return 1;
  1473. }
  1474. sub disable_commit_hook () {
  1475. open($commitlock, '>', "$config{wikistatedir}/commitlock") ||
  1476. error("cannot write to $config{wikistatedir}/commitlock: $!");
  1477. if (! flock($commitlock, 2)) { # LOCK_EX
  1478. error("failed to get commit lock");
  1479. }
  1480. return 1;
  1481. }
  1482. sub enable_commit_hook () {
  1483. return close($commitlock) if $commitlock;
  1484. return;
  1485. }
  1486. sub loadindex () {
  1487. %oldrenderedfiles=%pagectime=();
  1488. if (! $config{rebuild}) {
  1489. %pagesources=%pagemtime=%oldlinks=%links=%depends=
  1490. %destsources=%renderedfiles=%pagecase=%pagestate=
  1491. %depends_simple=%typedlinks=%oldtypedlinks=();
  1492. }
  1493. my $in;
  1494. if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
  1495. if (-e "$config{wikistatedir}/index") {
  1496. system("ikiwiki-transition", "indexdb", $config{srcdir});
  1497. open ($in, "<", "$config{wikistatedir}/indexdb") || return;
  1498. }
  1499. else {
  1500. $config{gettime}=1; # first build
  1501. return;
  1502. }
  1503. }
  1504. my $index=Storable::fd_retrieve($in);
  1505. if (! defined $index) {
  1506. return 0;
  1507. }
  1508. my $pages;
  1509. if (exists $index->{version} && ! ref $index->{version}) {
  1510. $pages=$index->{page};
  1511. %wikistate=%{$index->{state}};
  1512. # Handle plugins that got disabled by loading a new setup.
  1513. if (exists $config{setupfile}) {
  1514. require IkiWiki::Setup;
  1515. IkiWiki::Setup::disabled_plugins(
  1516. grep { ! $loaded_plugins{$_} } keys %wikistate);
  1517. }
  1518. }
  1519. else {
  1520. $pages=$index;
  1521. %wikistate=();
  1522. }
  1523. foreach my $src (keys %$pages) {
  1524. my $d=$pages->{$src};
  1525. my $page=pagename($src);
  1526. $pagectime{$page}=$d->{ctime};
  1527. $pagesources{$page}=$src;
  1528. if (! $config{rebuild}) {
  1529. $pagemtime{$page}=$d->{mtime};
  1530. $renderedfiles{$page}=$d->{dest};
  1531. if (exists $d->{links} && ref $d->{links}) {
  1532. $links{$page}=$d->{links};
  1533. $oldlinks{$page}=[@{$d->{links}}];
  1534. }
  1535. if (ref $d->{depends_simple} eq 'ARRAY') {
  1536. # old format
  1537. $depends_simple{$page}={
  1538. map { $_ => 1 } @{$d->{depends_simple}}
  1539. };
  1540. }
  1541. elsif (exists $d->{depends_simple}) {
  1542. $depends_simple{$page}=$d->{depends_simple};
  1543. }
  1544. if (exists $d->{dependslist}) {
  1545. # old format
  1546. $depends{$page}={
  1547. map { $_ => $DEPEND_CONTENT }
  1548. @{$d->{dependslist}}
  1549. };
  1550. }
  1551. elsif (exists $d->{depends} && ! ref $d->{depends}) {
  1552. # old format
  1553. $depends{$page}={$d->{depends} => $DEPEND_CONTENT };
  1554. }
  1555. elsif (exists $d->{depends}) {
  1556. $depends{$page}=$d->{depends};
  1557. }
  1558. if (exists $d->{state}) {
  1559. $pagestate{$page}=$d->{state};
  1560. }
  1561. if (exists $d->{typedlinks}) {
  1562. $typedlinks{$page}=$d->{typedlinks};
  1563. while (my ($type, $links) = each %{$typedlinks{$page}}) {
  1564. next unless %$links;
  1565. $oldtypedlinks{$page}{$type} = {%$links};
  1566. }
  1567. }
  1568. }
  1569. $oldrenderedfiles{$page}=[@{$d->{dest}}];
  1570. }
  1571. foreach my $page (keys %pagesources) {
  1572. $pagecase{lc $page}=$page;
  1573. }
  1574. foreach my $page (keys %renderedfiles) {
  1575. $destsources{$_}=$page foreach @{$renderedfiles{$page}};
  1576. }
  1577. return close($in);
  1578. }
  1579. sub saveindex () {
  1580. run_hooks(savestate => sub { shift->() });
  1581. my @plugins=keys %loaded_plugins;
  1582. if (! -d $config{wikistatedir}) {
  1583. mkdir($config{wikistatedir});
  1584. }
  1585. my $newfile="$config{wikistatedir}/indexdb.new";
  1586. my $cleanup = sub { unlink($newfile) };
  1587. open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
  1588. my %index;
  1589. foreach my $page (keys %pagemtime) {
  1590. next unless $pagemtime{$page};
  1591. my $src=$pagesources{$page};
  1592. $index{page}{$src}={
  1593. ctime => $pagectime{$page},
  1594. mtime => $pagemtime{$page},
  1595. dest => $renderedfiles{$page},
  1596. links => $links{$page},
  1597. };
  1598. if (exists $depends{$page}) {
  1599. $index{page}{$src}{depends} = $depends{$page};
  1600. }
  1601. if (exists $depends_simple{$page}) {
  1602. $index{page}{$src}{depends_simple} = $depends_simple{$page};
  1603. }
  1604. if (exists $typedlinks{$page} && %{$typedlinks{$page}}) {
  1605. $index{page}{$src}{typedlinks} = $typedlinks{$page};
  1606. }
  1607. if (exists $pagestate{$page}) {
  1608. foreach my $id (@plugins) {
  1609. foreach my $key (keys %{$pagestate{$page}{$id}}) {
  1610. $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
  1611. }
  1612. }
  1613. }
  1614. }
  1615. $index{state}={};
  1616. foreach my $id (@plugins) {
  1617. $index{state}{$id}={}; # used to detect disabled plugins
  1618. foreach my $key (keys %{$wikistate{$id}}) {
  1619. $index{state}{$id}{$key}=$wikistate{$id}{$key};
  1620. }
  1621. }
  1622. $index{version}="3";
  1623. my $ret=Storable::nstore_fd(\%index, $out);
  1624. return if ! defined $ret || ! $ret;
  1625. close $out || error("failed saving to $newfile: $!", $cleanup);
  1626. rename($newfile, "$config{wikistatedir}/indexdb") ||
  1627. error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
  1628. return 1;
  1629. }
  1630. sub template_file ($) {
  1631. my $name=shift;
  1632. my $tpage=($name =~ s/^\///) ? $name : "templates/$name";
  1633. my $template;
  1634. if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
  1635. $template=srcfile($pagesources{$tpage}, 1);
  1636. $name.=".tmpl";
  1637. }
  1638. else {
  1639. $template=srcfile($tpage, 1);
  1640. }
  1641. if (defined $template) {
  1642. return $template, $tpage, 1 if wantarray;
  1643. return $template;
  1644. }
  1645. else {
  1646. $name=~s:/::; # avoid path traversal
  1647. foreach my $dir ($config{templatedir},
  1648. "$installdir/share/ikiwiki/templates") {
  1649. if (-e "$dir/$name") {
  1650. $template="$dir/$name";
  1651. last;
  1652. }
  1653. }
  1654. if (defined $template) {
  1655. return $template, $tpage if wantarray;
  1656. return $template;
  1657. }
  1658. }
  1659. return;
  1660. }
  1661. sub template_depends ($$;@) {
  1662. my $name=shift;
  1663. my $page=shift;
  1664. my ($filename, $tpage, $untrusted)=template_file($name);
  1665. if (! defined $filename) {
  1666. error(sprintf(gettext("template %s not found"), $name))
  1667. }
  1668. if (defined $page && defined $tpage) {
  1669. add_depends($page, $tpage);
  1670. }
  1671. my @opts=(
  1672. filter => sub {
  1673. my $text_ref = shift;
  1674. ${$text_ref} = decode_utf8(${$text_ref});
  1675. },
  1676. loop_context_vars => 1,
  1677. die_on_bad_params => 0,
  1678. filename => $filename,
  1679. @_,
  1680. ($untrusted ? (no_includes => 1) : ()),
  1681. );
  1682. return @opts if wantarray;
  1683. require HTML::Template;
  1684. return HTML::Template->new(@opts);
  1685. }
  1686. sub template ($;@) {
  1687. template_depends(shift, undef, @_);
  1688. }
  1689. sub misctemplate ($$;@) {
  1690. my $title=shift;
  1691. my $content=shift;
  1692. my %params=@_;
  1693. my $template=template("page.tmpl");
  1694. my $page="";
  1695. if (exists $params{page}) {
  1696. $page=delete $params{page};
  1697. }
  1698. run_hooks(pagetemplate => sub {
  1699. shift->(
  1700. page => $page,
  1701. destpage => $page,
  1702. template => $template,
  1703. );
  1704. });
  1705. templateactions($template, "");
  1706. $template->param(
  1707. dynamic => 1,
  1708. title => $title,
  1709. wikiname => $config{wikiname},
  1710. content => $content,
  1711. baseurl => baseurl(),
  1712. html5 => $config{html5},
  1713. %params,
  1714. );
  1715. return $template->output;
  1716. }
  1717. sub templateactions ($$) {
  1718. my $template=shift;
  1719. my $page=shift;
  1720. my $have_actions=0;
  1721. my @actions;
  1722. run_hooks(pageactions => sub {
  1723. push @actions, map { { action => $_ } }
  1724. grep { defined } shift->(page => $page);
  1725. });
  1726. $template->param(actions => \@actions);
  1727. if ($config{cgiurl} && exists $hooks{auth}) {
  1728. $template->param(prefsurl => cgiurl(do => "prefs"));
  1729. $have_actions=1;
  1730. }
  1731. if ($have_actions || @actions) {
  1732. $template->param(have_actions => 1);
  1733. }
  1734. }
  1735. sub hook (@) {
  1736. my %param=@_;
  1737. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  1738. error 'hook requires type, call, and id parameters';
  1739. }
  1740. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  1741. $hooks{$param{type}}{$param{id}}=\%param;
  1742. return 1;
  1743. }
  1744. sub run_hooks ($$) {
  1745. # Calls the given sub for each hook of the given type,
  1746. # passing it the hook function to call.
  1747. my $type=shift;
  1748. my $sub=shift;
  1749. if (exists $hooks{$type}) {
  1750. my (@first, @middle, @last);
  1751. foreach my $id (keys %{$hooks{$type}}) {
  1752. if ($hooks{$type}{$id}{first}) {
  1753. push @first, $id;
  1754. }
  1755. elsif ($hooks{$type}{$id}{last}) {
  1756. push @last, $id;
  1757. }
  1758. else {
  1759. push @middle, $id;
  1760. }
  1761. }
  1762. foreach my $id (@first, @middle, @last) {
  1763. $sub->($hooks{$type}{$id}{call});
  1764. }
  1765. }
  1766. return 1;
  1767. }
  1768. sub rcs_update () {
  1769. $hooks{rcs}{rcs_update}{call}->(@_);
  1770. }
  1771. sub rcs_prepedit ($) {
  1772. $hooks{rcs}{rcs_prepedit}{call}->(@_);
  1773. }
  1774. sub rcs_commit (@) {
  1775. $hooks{rcs}{rcs_commit}{call}->(@_);
  1776. }
  1777. sub rcs_commit_staged (@) {
  1778. $hooks{rcs}{rcs_commit_staged}{call}->(@_);
  1779. }
  1780. sub rcs_add ($) {
  1781. $hooks{rcs}{rcs_add}{call}->(@_);
  1782. }
  1783. sub rcs_remove ($) {
  1784. $hooks{rcs}{rcs_remove}{call}->(@_);
  1785. }
  1786. sub rcs_rename ($$) {
  1787. $hooks{rcs}{rcs_rename}{call}->(@_);
  1788. }
  1789. sub rcs_recentchanges ($) {
  1790. $hooks{rcs}{rcs_recentchanges}{call}->(@_);
  1791. }
  1792. sub rcs_diff ($) {
  1793. $hooks{rcs}{rcs_diff}{call}->(@_);
  1794. }
  1795. sub rcs_getctime ($) {
  1796. $hooks{rcs}{rcs_getctime}{call}->(@_);
  1797. }
  1798. sub rcs_getmtime ($) {
  1799. $hooks{rcs}{rcs_getmtime}{call}->(@_);
  1800. }
  1801. sub rcs_receive () {
  1802. $hooks{rcs}{rcs_receive}{call}->();
  1803. }
  1804. sub add_depends ($$;$) {
  1805. my $page=shift;
  1806. my $pagespec=shift;
  1807. my $deptype=shift || $DEPEND_CONTENT;
  1808. # Is the pagespec a simple page name?
  1809. if ($pagespec =~ /$config{wiki_file_regexp}/ &&
  1810. $pagespec !~ /[\s*?()!]/) {
  1811. $depends_simple{$page}{lc $pagespec} |= $deptype;
  1812. return 1;
  1813. }
  1814. # Add explicit dependencies for influences.
  1815. my $sub=pagespec_translate($pagespec);
  1816. return unless defined $sub;
  1817. foreach my $p (keys %pagesources) {
  1818. my $r=$sub->($p, location => $page);
  1819. my $i=$r->influences;
  1820. my $static=$r->influences_static;
  1821. foreach my $k (keys %$i) {
  1822. next unless $r || $static || $k eq $page;
  1823. $depends_simple{$page}{lc $k} |= $i->{$k};
  1824. }
  1825. last if $static;
  1826. }
  1827. $depends{$page}{$pagespec} |= $deptype;
  1828. return 1;
  1829. }
  1830. sub deptype (@) {
  1831. my $deptype=0;
  1832. foreach my $type (@_) {
  1833. if ($type eq 'presence') {
  1834. $deptype |= $DEPEND_PRESENCE;
  1835. }
  1836. elsif ($type eq 'links') {
  1837. $deptype |= $DEPEND_LINKS;
  1838. }
  1839. elsif ($type eq 'content') {
  1840. $deptype |= $DEPEND_CONTENT;
  1841. }
  1842. }
  1843. return $deptype;
  1844. }
  1845. my $file_prune_regexp;
  1846. sub file_pruned ($) {
  1847. my $file=shift;
  1848. if (defined $config{include} && length $config{include}) {
  1849. return 0 if $file =~ m/$config{include}/;
  1850. }
  1851. if (! defined $file_prune_regexp) {
  1852. $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  1853. $file_prune_regexp=qr/$file_prune_regexp/;
  1854. }
  1855. return $file =~ m/$file_prune_regexp/;
  1856. }
  1857. sub define_gettext () {
  1858. # If translation is needed, redefine the gettext function to do it.
  1859. # Otherwise, it becomes a quick no-op.
  1860. my $gettext_obj;
  1861. my $getobj;
  1862. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  1863. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  1864. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  1865. $getobj=sub {
  1866. $gettext_obj=eval q{
  1867. use Locale::gettext q{textdomain};
  1868. Locale::gettext->domain('ikiwiki')
  1869. };
  1870. };
  1871. }
  1872. no warnings 'redefine';
  1873. *gettext=sub {
  1874. $getobj->() if $getobj;
  1875. if ($gettext_obj) {
  1876. $gettext_obj->get(shift);
  1877. }
  1878. else {
  1879. return shift;
  1880. }
  1881. };
  1882. *ngettext=sub {
  1883. $getobj->() if $getobj;
  1884. if ($gettext_obj) {
  1885. $gettext_obj->nget(@_);
  1886. }
  1887. else {
  1888. return ($_[2] == 1 ? $_[0] : $_[1])
  1889. }
  1890. };
  1891. }
  1892. sub gettext {
  1893. define_gettext();
  1894. gettext(@_);
  1895. }
  1896. sub ngettext {
  1897. define_gettext();
  1898. ngettext(@_);
  1899. }
  1900. sub yesno ($) {
  1901. my $val=shift;
  1902. return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
  1903. }
  1904. sub inject {
  1905. # Injects a new function into the symbol table to replace an
  1906. # exported function.
  1907. my %params=@_;
  1908. # This is deep ugly perl foo, beware.
  1909. no strict;
  1910. no warnings;
  1911. if (! defined $params{parent}) {
  1912. $params{parent}='::';
  1913. $params{old}=\&{$params{name}};
  1914. $params{name}=~s/.*:://;
  1915. }
  1916. my $parent=$params{parent};
  1917. foreach my $ns (grep /^\w+::/, keys %{$parent}) {
  1918. $ns = $params{parent} . $ns;
  1919. inject(%params, parent => $ns) unless $ns eq '::main::';
  1920. *{$ns . $params{name}} = $params{call}
  1921. if exists ${$ns}{$params{name}} &&
  1922. \&{${$ns}{$params{name}}} == $params{old};
  1923. }
  1924. use strict;
  1925. use warnings;
  1926. }
  1927. sub add_link ($$;$) {
  1928. my $page=shift;
  1929. my $link=shift;
  1930. my $type=shift;
  1931. push @{$links{$page}}, $link
  1932. unless grep { $_ eq $link } @{$links{$page}};
  1933. if (defined $type) {
  1934. $typedlinks{$page}{$type}{$link} = 1;
  1935. }
  1936. }
  1937. sub add_autofile ($$$) {
  1938. my $file=shift;
  1939. my $plugin=shift;
  1940. my $generator=shift;
  1941. $autofiles{$file}{plugin}=$plugin;
  1942. $autofiles{$file}{generator}=$generator;
  1943. }
  1944. sub sortspec_translate ($$) {
  1945. my $spec = shift;
  1946. my $reverse = shift;
  1947. my $code = "";
  1948. my @data;
  1949. while ($spec =~ m{
  1950. \s*
  1951. (-?) # group 1: perhaps negated
  1952. \s*
  1953. ( # group 2: a word
  1954. \w+\([^\)]*\) # command(params)
  1955. |
  1956. [^\s]+ # or anything else
  1957. )
  1958. \s*
  1959. }gx) {
  1960. my $negated = $1;
  1961. my $word = $2;
  1962. my $params = undef;
  1963. if ($word =~ m/^(\w+)\((.*)\)$/) {
  1964. # command with parameters
  1965. $params = $2;
  1966. $word = $1;
  1967. }
  1968. elsif ($word !~ m/^\w+$/) {
  1969. error(sprintf(gettext("invalid sort type %s"), $word));
  1970. }
  1971. if (length $code) {
  1972. $code .= " || ";
  1973. }
  1974. if ($negated) {
  1975. $code .= "-";
  1976. }
  1977. if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
  1978. if (defined $params) {
  1979. push @data, $params;
  1980. $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
  1981. }
  1982. else {
  1983. $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
  1984. }
  1985. }
  1986. else {
  1987. error(sprintf(gettext("unknown sort type %s"), $word));
  1988. }
  1989. }
  1990. if (! length $code) {
  1991. # undefined sorting method... sort arbitrarily
  1992. return sub { 0 };
  1993. }
  1994. if ($reverse) {
  1995. $code="-($code)";
  1996. }
  1997. no warnings;
  1998. return eval 'sub { '.$code.' }';
  1999. }
  2000. sub pagespec_translate ($) {
  2001. my $spec=shift;
  2002. # Convert spec to perl code.
  2003. my $code="";
  2004. my @data;
  2005. while ($spec=~m{
  2006. \s* # ignore whitespace
  2007. ( # 1: match a single word
  2008. \! # !
  2009. |
  2010. \( # (
  2011. |
  2012. \) # )
  2013. |
  2014. \w+\([^\)]*\) # command(params)
  2015. |
  2016. [^\s()]+ # any other text
  2017. )
  2018. \s* # ignore whitespace
  2019. }gx) {
  2020. my $word=$1;
  2021. if (lc $word eq 'and') {
  2022. $code.=' &';
  2023. }
  2024. elsif (lc $word eq 'or') {
  2025. $code.=' |';
  2026. }
  2027. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  2028. $code.=' '.$word;
  2029. }
  2030. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  2031. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  2032. push @data, $2;
  2033. $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
  2034. }
  2035. else {
  2036. push @data, qq{unknown function in pagespec "$word"};
  2037. $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
  2038. }
  2039. }
  2040. else {
  2041. push @data, $word;
  2042. $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
  2043. }
  2044. }
  2045. if (! length $code) {
  2046. $code="IkiWiki::FailReason->new('empty pagespec')";
  2047. }
  2048. no warnings;
  2049. return eval 'sub { my $page=shift; '.$code.' }';
  2050. }
  2051. sub pagespec_match ($$;@) {
  2052. my $page=shift;
  2053. my $spec=shift;
  2054. my @params=@_;
  2055. # Backwards compatability with old calling convention.
  2056. if (@params == 1) {
  2057. unshift @params, 'location';
  2058. }
  2059. my $sub=pagespec_translate($spec);
  2060. return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
  2061. if ! defined $sub;
  2062. return $sub->($page, @params);
  2063. }
  2064. sub pagespec_match_list ($$;@) {
  2065. my $page=shift;
  2066. my $pagespec=shift;
  2067. my %params=@_;
  2068. # Backwards compatability with old calling convention.
  2069. if (ref $page) {
  2070. print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
  2071. $params{list}=$page;
  2072. $page=$params{location}; # ugh!
  2073. }
  2074. my $sub=pagespec_translate($pagespec);
  2075. error "syntax error in pagespec \"$pagespec\""
  2076. if ! defined $sub;
  2077. my $sort=sortspec_translate($params{sort}, $params{reverse})
  2078. if defined $params{sort};
  2079. my @candidates;
  2080. if (exists $params{list}) {
  2081. @candidates=exists $params{filter}
  2082. ? grep { ! $params{filter}->($_) } @{$params{list}}
  2083. : @{$params{list}};
  2084. }
  2085. else {
  2086. @candidates=exists $params{filter}
  2087. ? grep { ! $params{filter}->($_) } keys %pagesources
  2088. : keys %pagesources;
  2089. }
  2090. # clear params, remainder is passed to pagespec
  2091. $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
  2092. my $num=$params{num};
  2093. delete @params{qw{num deptype reverse sort filter list}};
  2094. # when only the top matches will be returned, it's efficient to
  2095. # sort before matching to pagespec,
  2096. if (defined $num && defined $sort) {
  2097. @candidates=IkiWiki::SortSpec::sort_pages(
  2098. $sort, @candidates);
  2099. }
  2100. my @matches;
  2101. my $firstfail;
  2102. my $count=0;
  2103. my $accum=IkiWiki::SuccessReason->new();
  2104. foreach my $p (@candidates) {
  2105. my $r=$sub->($p, %params, location => $page);
  2106. error(sprintf(gettext("cannot match pages: %s"), $r))
  2107. if $r->isa("IkiWiki::ErrorReason");
  2108. unless ($r || $r->influences_static) {
  2109. $r->remove_influence($p);
  2110. }
  2111. $accum |= $r;
  2112. if ($r) {
  2113. push @matches, $p;
  2114. last if defined $num && ++$count == $num;
  2115. }
  2116. }
  2117. # Add simple dependencies for accumulated influences.
  2118. my $i=$accum->influences;
  2119. foreach my $k (keys %$i) {
  2120. $depends_simple{$page}{lc $k} |= $i->{$k};
  2121. }
  2122. # when all matches will be returned, it's efficient to
  2123. # sort after matching
  2124. if (! defined $num && defined $sort) {
  2125. return IkiWiki::SortSpec::sort_pages(
  2126. $sort, @matches);
  2127. }
  2128. else {
  2129. return @matches;
  2130. }
  2131. }
  2132. sub pagespec_valid ($) {
  2133. my $spec=shift;
  2134. return defined pagespec_translate($spec);
  2135. }
  2136. sub glob2re ($) {
  2137. my $re=quotemeta(shift);
  2138. $re=~s/\\\*/.*/g;
  2139. $re=~s/\\\?/./g;
  2140. return qr/^$re$/i;
  2141. }
  2142. package IkiWiki::FailReason;
  2143. use overload (
  2144. '""' => sub { $_[0][0] },
  2145. '0+' => sub { 0 },
  2146. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  2147. '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
  2148. '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
  2149. fallback => 1,
  2150. );
  2151. our @ISA = 'IkiWiki::SuccessReason';
  2152. package IkiWiki::SuccessReason;
  2153. use overload (
  2154. '""' => sub { $_[0][0] },
  2155. '0+' => sub { 1 },
  2156. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  2157. '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
  2158. '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
  2159. fallback => 1,
  2160. );
  2161. sub new {
  2162. my $class = shift;
  2163. my $value = shift;
  2164. return bless [$value, {@_}], $class;
  2165. }
  2166. sub influences {
  2167. my $this=shift;
  2168. $this->[1]={@_} if @_;
  2169. my %i=%{$this->[1]};
  2170. delete $i{""};
  2171. return \%i;
  2172. }
  2173. sub influences_static {
  2174. return ! $_[0][1]->{""};
  2175. }
  2176. sub merge_influences {
  2177. my $this=shift;
  2178. my $other=shift;
  2179. my $anded=shift;
  2180. if (! $anded || (($this || %{$this->[1]}) &&
  2181. ($other || %{$other->[1]}))) {
  2182. foreach my $influence (keys %{$other->[1]}) {
  2183. $this->[1]{$influence} |= $other->[1]{$influence};
  2184. }
  2185. }
  2186. else {
  2187. # influence blocker
  2188. $this->[1]={};
  2189. }
  2190. }
  2191. sub remove_influence {
  2192. my $this=shift;
  2193. my $torm=shift;
  2194. delete $this->[1]{$torm};
  2195. }
  2196. package IkiWiki::ErrorReason;
  2197. our @ISA = 'IkiWiki::FailReason';
  2198. package IkiWiki::PageSpec;
  2199. sub derel ($$) {
  2200. my $path=shift;
  2201. my $from=shift;
  2202. if ($path =~ m!^\.(/|$)!) {
  2203. if ($1) {
  2204. $from=~s#/?[^/]+$## if defined $from;
  2205. $path=~s#^\./##;
  2206. $path="$from/$path" if defined $from && length $from;
  2207. }
  2208. else {
  2209. $path = $from;
  2210. $path = "" unless defined $path;
  2211. }
  2212. }
  2213. return $path;
  2214. }
  2215. my %glob_cache;
  2216. sub match_glob ($$;@) {
  2217. my $page=shift;
  2218. my $glob=shift;
  2219. my %params=@_;
  2220. $glob=derel($glob, $params{location});
  2221. # Instead of converting the glob to a regex every time,
  2222. # cache the compiled regex to save time.
  2223. my $re=$glob_cache{$glob};
  2224. unless (defined $re) {
  2225. $glob_cache{$glob} = $re = IkiWiki::glob2re($glob);
  2226. }
  2227. if ($page =~ $re) {
  2228. if (! IkiWiki::isinternal($page) || $params{internal}) {
  2229. return IkiWiki::SuccessReason->new("$glob matches $page");
  2230. }
  2231. else {
  2232. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  2233. }
  2234. }
  2235. else {
  2236. return IkiWiki::FailReason->new("$glob does not match $page");
  2237. }
  2238. }
  2239. sub match_internal ($$;@) {
  2240. return match_glob(shift, shift, @_, internal => 1)
  2241. }
  2242. sub match_page ($$;@) {
  2243. my $page=shift;
  2244. my $match=match_glob($page, shift, @_);
  2245. if ($match) {
  2246. my $source=exists $IkiWiki::pagesources{$page} ?
  2247. $IkiWiki::pagesources{$page} :
  2248. $IkiWiki::delpagesources{$page};
  2249. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  2250. if (! defined $type) {
  2251. return IkiWiki::FailReason->new("$page is not a page");
  2252. }
  2253. }
  2254. return $match;
  2255. }
  2256. sub match_link ($$;@) {
  2257. my $page=shift;
  2258. my $link=lc(shift);
  2259. my %params=@_;
  2260. $link=derel($link, $params{location});
  2261. my $from=exists $params{location} ? $params{location} : '';
  2262. my $linktype=$params{linktype};
  2263. my $qualifier='';
  2264. if (defined $linktype) {
  2265. $qualifier=" with type $linktype";
  2266. }
  2267. my $links = $IkiWiki::links{$page};
  2268. return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2269. unless $links && @{$links};
  2270. my $bestlink = IkiWiki::bestlink($from, $link);
  2271. foreach my $p (@{$links}) {
  2272. next unless (! defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p});
  2273. if (length $bestlink) {
  2274. if ($bestlink eq IkiWiki::bestlink($page, $p)) {
  2275. return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2276. }
  2277. }
  2278. else {
  2279. if (match_glob($p, $link, %params)) {
  2280. return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2281. }
  2282. my ($p_rel)=$p=~/^\/?(.*)/;
  2283. $link=~s/^\///;
  2284. if (match_glob($p_rel, $link, %params)) {
  2285. return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2286. }
  2287. }
  2288. }
  2289. return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
  2290. }
  2291. sub match_backlink ($$;@) {
  2292. my $ret=match_link($_[1], $_[0], @_);
  2293. $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
  2294. return $ret;
  2295. }
  2296. sub match_created_before ($$;@) {
  2297. my $page=shift;
  2298. my $testpage=shift;
  2299. my %params=@_;
  2300. $testpage=derel($testpage, $params{location});
  2301. if (exists $IkiWiki::pagectime{$testpage}) {
  2302. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  2303. return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2304. }
  2305. else {
  2306. return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2307. }
  2308. }
  2309. else {
  2310. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2311. }
  2312. }
  2313. sub match_created_after ($$;@) {
  2314. my $page=shift;
  2315. my $testpage=shift;
  2316. my %params=@_;
  2317. $testpage=derel($testpage, $params{location});
  2318. if (exists $IkiWiki::pagectime{$testpage}) {
  2319. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  2320. return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2321. }
  2322. else {
  2323. return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2324. }
  2325. }
  2326. else {
  2327. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2328. }
  2329. }
  2330. sub match_creation_day ($$;@) {
  2331. my $page=shift;
  2332. my $d=shift;
  2333. if ($d !~ /^\d+$/) {
  2334. return IkiWiki::ErrorReason->new("invalid day $d");
  2335. }
  2336. if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
  2337. return IkiWiki::SuccessReason->new('creation_day matched');
  2338. }
  2339. else {
  2340. return IkiWiki::FailReason->new('creation_day did not match');
  2341. }
  2342. }
  2343. sub match_creation_month ($$;@) {
  2344. my $page=shift;
  2345. my $m=shift;
  2346. if ($m !~ /^\d+$/) {
  2347. return IkiWiki::ErrorReason->new("invalid month $m");
  2348. }
  2349. if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
  2350. return IkiWiki::SuccessReason->new('creation_month matched');
  2351. }
  2352. else {
  2353. return IkiWiki::FailReason->new('creation_month did not match');
  2354. }
  2355. }
  2356. sub match_creation_year ($$;@) {
  2357. my $page=shift;
  2358. my $y=shift;
  2359. if ($y !~ /^\d+$/) {
  2360. return IkiWiki::ErrorReason->new("invalid year $y");
  2361. }
  2362. if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
  2363. return IkiWiki::SuccessReason->new('creation_year matched');
  2364. }
  2365. else {
  2366. return IkiWiki::FailReason->new('creation_year did not match');
  2367. }
  2368. }
  2369. sub match_user ($$;@) {
  2370. shift;
  2371. my $user=shift;
  2372. my %params=@_;
  2373. my $regexp=IkiWiki::glob2re($user);
  2374. if (! exists $params{user}) {
  2375. return IkiWiki::ErrorReason->new("no user specified");
  2376. }
  2377. if (defined $params{user} && $params{user}=~$regexp) {
  2378. return IkiWiki::SuccessReason->new("user is $user");
  2379. }
  2380. elsif (! defined $params{user}) {
  2381. return IkiWiki::FailReason->new("not logged in");
  2382. }
  2383. else {
  2384. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  2385. }
  2386. }
  2387. sub match_admin ($$;@) {
  2388. shift;
  2389. shift;
  2390. my %params=@_;
  2391. if (! exists $params{user}) {
  2392. return IkiWiki::ErrorReason->new("no user specified");
  2393. }
  2394. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  2395. return IkiWiki::SuccessReason->new("user is an admin");
  2396. }
  2397. elsif (! defined $params{user}) {
  2398. return IkiWiki::FailReason->new("not logged in");
  2399. }
  2400. else {
  2401. return IkiWiki::FailReason->new("user is not an admin");
  2402. }
  2403. }
  2404. sub match_ip ($$;@) {
  2405. shift;
  2406. my $ip=shift;
  2407. my %params=@_;
  2408. if (! exists $params{ip}) {
  2409. return IkiWiki::ErrorReason->new("no IP specified");
  2410. }
  2411. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  2412. return IkiWiki::SuccessReason->new("IP is $ip");
  2413. }
  2414. else {
  2415. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  2416. }
  2417. }
  2418. package IkiWiki::SortSpec;
  2419. # This is in the SortSpec namespace so that the $a and $b that sort() uses
  2420. # are easily available in this namespace, for cmp functions to use them.
  2421. sub sort_pages {
  2422. my $f=shift;
  2423. sort $f @_
  2424. }
  2425. sub cmp_title {
  2426. IkiWiki::pagetitle(IkiWiki::basename($a))
  2427. cmp
  2428. IkiWiki::pagetitle(IkiWiki::basename($b))
  2429. }
  2430. sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
  2431. sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
  2432. 1