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