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