summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 35e64d81568f6c98aedf18af682c77836310fc8b (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 misctemplate ($$;@) {
  1706. my $title=shift;
  1707. my $content=shift;
  1708. my %params=@_;
  1709. my $template=template("page.tmpl");
  1710. my $page="";
  1711. if (exists $params{page}) {
  1712. $page=delete $params{page};
  1713. }
  1714. run_hooks(pagetemplate => sub {
  1715. shift->(
  1716. page => $page,
  1717. destpage => $page,
  1718. template => $template,
  1719. );
  1720. });
  1721. templateactions($template, "");
  1722. $template->param(
  1723. dynamic => 1,
  1724. title => $title,
  1725. wikiname => $config{wikiname},
  1726. content => $content,
  1727. baseurl => $config{url}.'/',
  1728. html5 => $config{html5},
  1729. %params,
  1730. );
  1731. return $template->output;
  1732. }
  1733. sub templateactions ($$) {
  1734. my $template=shift;
  1735. my $page=shift;
  1736. my $have_actions=0;
  1737. my @actions;
  1738. run_hooks(pageactions => sub {
  1739. push @actions, map { { action => $_ } }
  1740. grep { defined } shift->(page => $page);
  1741. });
  1742. $template->param(actions => \@actions);
  1743. if ($config{cgiurl} && exists $hooks{auth}) {
  1744. $template->param(prefsurl => cgiurl(do => "prefs"));
  1745. $have_actions=1;
  1746. }
  1747. if ($have_actions || @actions) {
  1748. $template->param(have_actions => 1);
  1749. }
  1750. }
  1751. sub hook (@) {
  1752. my %param=@_;
  1753. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  1754. error 'hook requires type, call, and id parameters';
  1755. }
  1756. return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
  1757. $hooks{$param{type}}{$param{id}}=\%param;
  1758. return 1;
  1759. }
  1760. sub run_hooks ($$) {
  1761. # Calls the given sub for each hook of the given type,
  1762. # passing it the hook function to call.
  1763. my $type=shift;
  1764. my $sub=shift;
  1765. if (exists $hooks{$type}) {
  1766. my (@first, @middle, @last);
  1767. foreach my $id (keys %{$hooks{$type}}) {
  1768. if ($hooks{$type}{$id}{first}) {
  1769. push @first, $id;
  1770. }
  1771. elsif ($hooks{$type}{$id}{last}) {
  1772. push @last, $id;
  1773. }
  1774. else {
  1775. push @middle, $id;
  1776. }
  1777. }
  1778. foreach my $id (@first, @middle, @last) {
  1779. $sub->($hooks{$type}{$id}{call});
  1780. }
  1781. }
  1782. return 1;
  1783. }
  1784. sub rcs_update () {
  1785. $hooks{rcs}{rcs_update}{call}->(@_);
  1786. }
  1787. sub rcs_prepedit ($) {
  1788. $hooks{rcs}{rcs_prepedit}{call}->(@_);
  1789. }
  1790. sub rcs_commit (@) {
  1791. $hooks{rcs}{rcs_commit}{call}->(@_);
  1792. }
  1793. sub rcs_commit_staged (@) {
  1794. $hooks{rcs}{rcs_commit_staged}{call}->(@_);
  1795. }
  1796. sub rcs_add ($) {
  1797. $hooks{rcs}{rcs_add}{call}->(@_);
  1798. }
  1799. sub rcs_remove ($) {
  1800. $hooks{rcs}{rcs_remove}{call}->(@_);
  1801. }
  1802. sub rcs_rename ($$) {
  1803. $hooks{rcs}{rcs_rename}{call}->(@_);
  1804. }
  1805. sub rcs_recentchanges ($) {
  1806. $hooks{rcs}{rcs_recentchanges}{call}->(@_);
  1807. }
  1808. sub rcs_diff ($;$) {
  1809. $hooks{rcs}{rcs_diff}{call}->(@_);
  1810. }
  1811. sub rcs_getctime ($) {
  1812. $hooks{rcs}{rcs_getctime}{call}->(@_);
  1813. }
  1814. sub rcs_getmtime ($) {
  1815. $hooks{rcs}{rcs_getmtime}{call}->(@_);
  1816. }
  1817. sub rcs_receive () {
  1818. $hooks{rcs}{rcs_receive}{call}->();
  1819. }
  1820. sub add_depends ($$;$) {
  1821. my $page=shift;
  1822. my $pagespec=shift;
  1823. my $deptype=shift || $DEPEND_CONTENT;
  1824. # Is the pagespec a simple page name?
  1825. if ($pagespec =~ /$config{wiki_file_regexp}/ &&
  1826. $pagespec !~ /[\s*?()!]/) {
  1827. $depends_simple{$page}{lc $pagespec} |= $deptype;
  1828. return 1;
  1829. }
  1830. # Add explicit dependencies for influences.
  1831. my $sub=pagespec_translate($pagespec);
  1832. return unless defined $sub;
  1833. foreach my $p (keys %pagesources) {
  1834. my $r=$sub->($p, location => $page);
  1835. my $i=$r->influences;
  1836. my $static=$r->influences_static;
  1837. foreach my $k (keys %$i) {
  1838. next unless $r || $static || $k eq $page;
  1839. $depends_simple{$page}{lc $k} |= $i->{$k};
  1840. }
  1841. last if $static;
  1842. }
  1843. $depends{$page}{$pagespec} |= $deptype;
  1844. return 1;
  1845. }
  1846. sub deptype (@) {
  1847. my $deptype=0;
  1848. foreach my $type (@_) {
  1849. if ($type eq 'presence') {
  1850. $deptype |= $DEPEND_PRESENCE;
  1851. }
  1852. elsif ($type eq 'links') {
  1853. $deptype |= $DEPEND_LINKS;
  1854. }
  1855. elsif ($type eq 'content') {
  1856. $deptype |= $DEPEND_CONTENT;
  1857. }
  1858. }
  1859. return $deptype;
  1860. }
  1861. my $file_prune_regexp;
  1862. sub file_pruned ($) {
  1863. my $file=shift;
  1864. if (defined $config{include} && length $config{include}) {
  1865. return 0 if $file =~ m/$config{include}/;
  1866. }
  1867. if (! defined $file_prune_regexp) {
  1868. $file_prune_regexp='('.join('|', @{$config{wiki_file_prune_regexps}}).')';
  1869. $file_prune_regexp=qr/$file_prune_regexp/;
  1870. }
  1871. return $file =~ m/$file_prune_regexp/;
  1872. }
  1873. sub define_gettext () {
  1874. # If translation is needed, redefine the gettext function to do it.
  1875. # Otherwise, it becomes a quick no-op.
  1876. my $gettext_obj;
  1877. my $getobj;
  1878. if ((exists $ENV{LANG} && length $ENV{LANG}) ||
  1879. (exists $ENV{LC_ALL} && length $ENV{LC_ALL}) ||
  1880. (exists $ENV{LC_MESSAGES} && length $ENV{LC_MESSAGES})) {
  1881. $getobj=sub {
  1882. $gettext_obj=eval q{
  1883. use Locale::gettext q{textdomain};
  1884. Locale::gettext->domain('ikiwiki')
  1885. };
  1886. };
  1887. }
  1888. no warnings 'redefine';
  1889. *gettext=sub {
  1890. $getobj->() if $getobj;
  1891. if ($gettext_obj) {
  1892. $gettext_obj->get(shift);
  1893. }
  1894. else {
  1895. return shift;
  1896. }
  1897. };
  1898. *ngettext=sub {
  1899. $getobj->() if $getobj;
  1900. if ($gettext_obj) {
  1901. $gettext_obj->nget(@_);
  1902. }
  1903. else {
  1904. return ($_[2] == 1 ? $_[0] : $_[1])
  1905. }
  1906. };
  1907. }
  1908. sub gettext {
  1909. define_gettext();
  1910. gettext(@_);
  1911. }
  1912. sub ngettext {
  1913. define_gettext();
  1914. ngettext(@_);
  1915. }
  1916. sub yesno ($) {
  1917. my $val=shift;
  1918. return (defined $val && (lc($val) eq gettext("yes") || lc($val) eq "yes" || $val eq "1"));
  1919. }
  1920. sub inject {
  1921. # Injects a new function into the symbol table to replace an
  1922. # exported function.
  1923. my %params=@_;
  1924. # This is deep ugly perl foo, beware.
  1925. no strict;
  1926. no warnings;
  1927. if (! defined $params{parent}) {
  1928. $params{parent}='::';
  1929. $params{old}=\&{$params{name}};
  1930. $params{name}=~s/.*:://;
  1931. }
  1932. my $parent=$params{parent};
  1933. foreach my $ns (grep /^\w+::/, keys %{$parent}) {
  1934. $ns = $params{parent} . $ns;
  1935. inject(%params, parent => $ns) unless $ns eq '::main::';
  1936. *{$ns . $params{name}} = $params{call}
  1937. if exists ${$ns}{$params{name}} &&
  1938. \&{${$ns}{$params{name}}} == $params{old};
  1939. }
  1940. use strict;
  1941. use warnings;
  1942. }
  1943. sub add_link ($$;$) {
  1944. my $page=shift;
  1945. my $link=shift;
  1946. my $type=shift;
  1947. push @{$links{$page}}, $link
  1948. unless grep { $_ eq $link } @{$links{$page}};
  1949. if (defined $type) {
  1950. $typedlinks{$page}{$type}{$link} = 1;
  1951. }
  1952. }
  1953. sub add_autofile ($$$) {
  1954. my $file=shift;
  1955. my $plugin=shift;
  1956. my $generator=shift;
  1957. $autofiles{$file}{plugin}=$plugin;
  1958. $autofiles{$file}{generator}=$generator;
  1959. }
  1960. sub sortspec_translate ($$) {
  1961. my $spec = shift;
  1962. my $reverse = shift;
  1963. my $code = "";
  1964. my @data;
  1965. while ($spec =~ m{
  1966. \s*
  1967. (-?) # group 1: perhaps negated
  1968. \s*
  1969. ( # group 2: a word
  1970. \w+\([^\)]*\) # command(params)
  1971. |
  1972. [^\s]+ # or anything else
  1973. )
  1974. \s*
  1975. }gx) {
  1976. my $negated = $1;
  1977. my $word = $2;
  1978. my $params = undef;
  1979. if ($word =~ m/^(\w+)\((.*)\)$/) {
  1980. # command with parameters
  1981. $params = $2;
  1982. $word = $1;
  1983. }
  1984. elsif ($word !~ m/^\w+$/) {
  1985. error(sprintf(gettext("invalid sort type %s"), $word));
  1986. }
  1987. if (length $code) {
  1988. $code .= " || ";
  1989. }
  1990. if ($negated) {
  1991. $code .= "-";
  1992. }
  1993. if (exists $IkiWiki::SortSpec::{"cmp_$word"}) {
  1994. if (defined $params) {
  1995. push @data, $params;
  1996. $code .= "IkiWiki::SortSpec::cmp_$word(\$data[$#data])";
  1997. }
  1998. else {
  1999. $code .= "IkiWiki::SortSpec::cmp_$word(undef)";
  2000. }
  2001. }
  2002. else {
  2003. error(sprintf(gettext("unknown sort type %s"), $word));
  2004. }
  2005. }
  2006. if (! length $code) {
  2007. # undefined sorting method... sort arbitrarily
  2008. return sub { 0 };
  2009. }
  2010. if ($reverse) {
  2011. $code="-($code)";
  2012. }
  2013. no warnings;
  2014. return eval 'sub { '.$code.' }';
  2015. }
  2016. sub pagespec_translate ($) {
  2017. my $spec=shift;
  2018. # Convert spec to perl code.
  2019. my $code="";
  2020. my @data;
  2021. while ($spec=~m{
  2022. \s* # ignore whitespace
  2023. ( # 1: match a single word
  2024. \! # !
  2025. |
  2026. \( # (
  2027. |
  2028. \) # )
  2029. |
  2030. \w+\([^\)]*\) # command(params)
  2031. |
  2032. [^\s()]+ # any other text
  2033. )
  2034. \s* # ignore whitespace
  2035. }gx) {
  2036. my $word=$1;
  2037. if (lc $word eq 'and') {
  2038. $code.=' &';
  2039. }
  2040. elsif (lc $word eq 'or') {
  2041. $code.=' |';
  2042. }
  2043. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  2044. $code.=' '.$word;
  2045. }
  2046. elsif ($word =~ /^(\w+)\((.*)\)$/) {
  2047. if (exists $IkiWiki::PageSpec::{"match_$1"}) {
  2048. push @data, $2;
  2049. $code.="IkiWiki::PageSpec::match_$1(\$page, \$data[$#data], \@_)";
  2050. }
  2051. else {
  2052. push @data, qq{unknown function in pagespec "$word"};
  2053. $code.="IkiWiki::ErrorReason->new(\$data[$#data])";
  2054. }
  2055. }
  2056. else {
  2057. push @data, $word;
  2058. $code.=" IkiWiki::PageSpec::match_glob(\$page, \$data[$#data], \@_)";
  2059. }
  2060. }
  2061. if (! length $code) {
  2062. $code="IkiWiki::FailReason->new('empty pagespec')";
  2063. }
  2064. no warnings;
  2065. return eval 'sub { my $page=shift; '.$code.' }';
  2066. }
  2067. sub pagespec_match ($$;@) {
  2068. my $page=shift;
  2069. my $spec=shift;
  2070. my @params=@_;
  2071. # Backwards compatability with old calling convention.
  2072. if (@params == 1) {
  2073. unshift @params, 'location';
  2074. }
  2075. my $sub=pagespec_translate($spec);
  2076. return IkiWiki::ErrorReason->new("syntax error in pagespec \"$spec\"")
  2077. if ! defined $sub;
  2078. return $sub->($page, @params);
  2079. }
  2080. sub pagespec_match_list ($$;@) {
  2081. my $page=shift;
  2082. my $pagespec=shift;
  2083. my %params=@_;
  2084. # Backwards compatability with old calling convention.
  2085. if (ref $page) {
  2086. print STDERR "warning: a plugin (".caller().") is using pagespec_match_list in an obsolete way, and needs to be updated\n";
  2087. $params{list}=$page;
  2088. $page=$params{location}; # ugh!
  2089. }
  2090. my $sub=pagespec_translate($pagespec);
  2091. error "syntax error in pagespec \"$pagespec\""
  2092. if ! defined $sub;
  2093. my $sort=sortspec_translate($params{sort}, $params{reverse})
  2094. if defined $params{sort};
  2095. my @candidates;
  2096. if (exists $params{list}) {
  2097. @candidates=exists $params{filter}
  2098. ? grep { ! $params{filter}->($_) } @{$params{list}}
  2099. : @{$params{list}};
  2100. }
  2101. else {
  2102. @candidates=exists $params{filter}
  2103. ? grep { ! $params{filter}->($_) } keys %pagesources
  2104. : keys %pagesources;
  2105. }
  2106. # clear params, remainder is passed to pagespec
  2107. $depends{$page}{$pagespec} |= ($params{deptype} || $DEPEND_CONTENT);
  2108. my $num=$params{num};
  2109. delete @params{qw{num deptype reverse sort filter list}};
  2110. # when only the top matches will be returned, it's efficient to
  2111. # sort before matching to pagespec,
  2112. if (defined $num && defined $sort) {
  2113. @candidates=IkiWiki::SortSpec::sort_pages(
  2114. $sort, @candidates);
  2115. }
  2116. my @matches;
  2117. my $firstfail;
  2118. my $count=0;
  2119. my $accum=IkiWiki::SuccessReason->new();
  2120. foreach my $p (@candidates) {
  2121. my $r=$sub->($p, %params, location => $page);
  2122. error(sprintf(gettext("cannot match pages: %s"), $r))
  2123. if $r->isa("IkiWiki::ErrorReason");
  2124. unless ($r || $r->influences_static) {
  2125. $r->remove_influence($p);
  2126. }
  2127. $accum |= $r;
  2128. if ($r) {
  2129. push @matches, $p;
  2130. last if defined $num && ++$count == $num;
  2131. }
  2132. }
  2133. # Add simple dependencies for accumulated influences.
  2134. my $i=$accum->influences;
  2135. foreach my $k (keys %$i) {
  2136. $depends_simple{$page}{lc $k} |= $i->{$k};
  2137. }
  2138. # when all matches will be returned, it's efficient to
  2139. # sort after matching
  2140. if (! defined $num && defined $sort) {
  2141. return IkiWiki::SortSpec::sort_pages(
  2142. $sort, @matches);
  2143. }
  2144. else {
  2145. return @matches;
  2146. }
  2147. }
  2148. sub pagespec_valid ($) {
  2149. my $spec=shift;
  2150. return defined pagespec_translate($spec);
  2151. }
  2152. sub glob2re ($) {
  2153. my $re=quotemeta(shift);
  2154. $re=~s/\\\*/.*/g;
  2155. $re=~s/\\\?/./g;
  2156. return qr/^$re$/i;
  2157. }
  2158. package IkiWiki::FailReason;
  2159. use overload (
  2160. '""' => sub { $_[0][0] },
  2161. '0+' => sub { 0 },
  2162. '!' => sub { bless $_[0], 'IkiWiki::SuccessReason'},
  2163. '&' => sub { $_[0]->merge_influences($_[1], 1); $_[0] },
  2164. '|' => sub { $_[1]->merge_influences($_[0]); $_[1] },
  2165. fallback => 1,
  2166. );
  2167. our @ISA = 'IkiWiki::SuccessReason';
  2168. package IkiWiki::SuccessReason;
  2169. use overload (
  2170. '""' => sub { $_[0][0] },
  2171. '0+' => sub { 1 },
  2172. '!' => sub { bless $_[0], 'IkiWiki::FailReason'},
  2173. '&' => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
  2174. '|' => sub { $_[0]->merge_influences($_[1]); $_[0] },
  2175. fallback => 1,
  2176. );
  2177. sub new {
  2178. my $class = shift;
  2179. my $value = shift;
  2180. return bless [$value, {@_}], $class;
  2181. }
  2182. sub influences {
  2183. my $this=shift;
  2184. $this->[1]={@_} if @_;
  2185. my %i=%{$this->[1]};
  2186. delete $i{""};
  2187. return \%i;
  2188. }
  2189. sub influences_static {
  2190. return ! $_[0][1]->{""};
  2191. }
  2192. sub merge_influences {
  2193. my $this=shift;
  2194. my $other=shift;
  2195. my $anded=shift;
  2196. if (! $anded || (($this || %{$this->[1]}) &&
  2197. ($other || %{$other->[1]}))) {
  2198. foreach my $influence (keys %{$other->[1]}) {
  2199. $this->[1]{$influence} |= $other->[1]{$influence};
  2200. }
  2201. }
  2202. else {
  2203. # influence blocker
  2204. $this->[1]={};
  2205. }
  2206. }
  2207. sub remove_influence {
  2208. my $this=shift;
  2209. my $torm=shift;
  2210. delete $this->[1]{$torm};
  2211. }
  2212. package IkiWiki::ErrorReason;
  2213. our @ISA = 'IkiWiki::FailReason';
  2214. package IkiWiki::PageSpec;
  2215. sub derel ($$) {
  2216. my $path=shift;
  2217. my $from=shift;
  2218. if ($path =~ m!^\.(/|$)!) {
  2219. if ($1) {
  2220. $from=~s#/?[^/]+$## if defined $from;
  2221. $path=~s#^\./##;
  2222. $path="$from/$path" if defined $from && length $from;
  2223. }
  2224. else {
  2225. $path = $from;
  2226. $path = "" unless defined $path;
  2227. }
  2228. }
  2229. return $path;
  2230. }
  2231. my %glob_cache;
  2232. sub match_glob ($$;@) {
  2233. my $page=shift;
  2234. my $glob=shift;
  2235. my %params=@_;
  2236. $glob=derel($glob, $params{location});
  2237. # Instead of converting the glob to a regex every time,
  2238. # cache the compiled regex to save time.
  2239. my $re=$glob_cache{$glob};
  2240. unless (defined $re) {
  2241. $glob_cache{$glob} = $re = IkiWiki::glob2re($glob);
  2242. }
  2243. if ($page =~ $re) {
  2244. if (! IkiWiki::isinternal($page) || $params{internal}) {
  2245. return IkiWiki::SuccessReason->new("$glob matches $page");
  2246. }
  2247. else {
  2248. return IkiWiki::FailReason->new("$glob matches $page, but the page is an internal page");
  2249. }
  2250. }
  2251. else {
  2252. return IkiWiki::FailReason->new("$glob does not match $page");
  2253. }
  2254. }
  2255. sub match_internal ($$;@) {
  2256. return match_glob(shift, shift, @_, internal => 1)
  2257. }
  2258. sub match_page ($$;@) {
  2259. my $page=shift;
  2260. my $match=match_glob($page, shift, @_);
  2261. if ($match) {
  2262. my $source=exists $IkiWiki::pagesources{$page} ?
  2263. $IkiWiki::pagesources{$page} :
  2264. $IkiWiki::delpagesources{$page};
  2265. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  2266. if (! defined $type) {
  2267. return IkiWiki::FailReason->new("$page is not a page");
  2268. }
  2269. }
  2270. return $match;
  2271. }
  2272. sub match_link ($$;@) {
  2273. my $page=shift;
  2274. my $link=lc(shift);
  2275. my %params=@_;
  2276. $link=derel($link, $params{location});
  2277. my $from=exists $params{location} ? $params{location} : '';
  2278. my $linktype=$params{linktype};
  2279. my $qualifier='';
  2280. if (defined $linktype) {
  2281. $qualifier=" with type $linktype";
  2282. }
  2283. my $links = $IkiWiki::links{$page};
  2284. return IkiWiki::FailReason->new("$page has no links", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2285. unless $links && @{$links};
  2286. my $bestlink = IkiWiki::bestlink($from, $link);
  2287. foreach my $p (@{$links}) {
  2288. next unless (! defined $linktype || exists $IkiWiki::typedlinks{$page}{$linktype}{$p});
  2289. if (length $bestlink) {
  2290. if ($bestlink eq IkiWiki::bestlink($page, $p)) {
  2291. return IkiWiki::SuccessReason->new("$page links to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2292. }
  2293. }
  2294. else {
  2295. if (match_glob($p, $link, %params)) {
  2296. return IkiWiki::SuccessReason->new("$page links to page $p$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2297. }
  2298. my ($p_rel)=$p=~/^\/?(.*)/;
  2299. $link=~s/^\///;
  2300. if (match_glob($p_rel, $link, %params)) {
  2301. return IkiWiki::SuccessReason->new("$page links to page $p_rel$qualifier, matching $link", $page => $IkiWiki::DEPEND_LINKS, "" => 1)
  2302. }
  2303. }
  2304. }
  2305. return IkiWiki::FailReason->new("$page does not link to $link$qualifier", $page => $IkiWiki::DEPEND_LINKS, "" => 1);
  2306. }
  2307. sub match_backlink ($$;@) {
  2308. my $ret=match_link($_[1], $_[0], @_);
  2309. $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
  2310. return $ret;
  2311. }
  2312. sub match_created_before ($$;@) {
  2313. my $page=shift;
  2314. my $testpage=shift;
  2315. my %params=@_;
  2316. $testpage=derel($testpage, $params{location});
  2317. if (exists $IkiWiki::pagectime{$testpage}) {
  2318. if ($IkiWiki::pagectime{$page} < $IkiWiki::pagectime{$testpage}) {
  2319. return IkiWiki::SuccessReason->new("$page created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2320. }
  2321. else {
  2322. return IkiWiki::FailReason->new("$page not created before $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2323. }
  2324. }
  2325. else {
  2326. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2327. }
  2328. }
  2329. sub match_created_after ($$;@) {
  2330. my $page=shift;
  2331. my $testpage=shift;
  2332. my %params=@_;
  2333. $testpage=derel($testpage, $params{location});
  2334. if (exists $IkiWiki::pagectime{$testpage}) {
  2335. if ($IkiWiki::pagectime{$page} > $IkiWiki::pagectime{$testpage}) {
  2336. return IkiWiki::SuccessReason->new("$page created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2337. }
  2338. else {
  2339. return IkiWiki::FailReason->new("$page not created after $testpage", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2340. }
  2341. }
  2342. else {
  2343. return IkiWiki::ErrorReason->new("$testpage does not exist", $testpage => $IkiWiki::DEPEND_PRESENCE);
  2344. }
  2345. }
  2346. sub match_creation_day ($$;@) {
  2347. my $page=shift;
  2348. my $d=shift;
  2349. if ($d !~ /^\d+$/) {
  2350. return IkiWiki::ErrorReason->new("invalid day $d");
  2351. }
  2352. if ((localtime($IkiWiki::pagectime{$page}))[3] == $d) {
  2353. return IkiWiki::SuccessReason->new('creation_day matched');
  2354. }
  2355. else {
  2356. return IkiWiki::FailReason->new('creation_day did not match');
  2357. }
  2358. }
  2359. sub match_creation_month ($$;@) {
  2360. my $page=shift;
  2361. my $m=shift;
  2362. if ($m !~ /^\d+$/) {
  2363. return IkiWiki::ErrorReason->new("invalid month $m");
  2364. }
  2365. if ((localtime($IkiWiki::pagectime{$page}))[4] + 1 == $m) {
  2366. return IkiWiki::SuccessReason->new('creation_month matched');
  2367. }
  2368. else {
  2369. return IkiWiki::FailReason->new('creation_month did not match');
  2370. }
  2371. }
  2372. sub match_creation_year ($$;@) {
  2373. my $page=shift;
  2374. my $y=shift;
  2375. if ($y !~ /^\d+$/) {
  2376. return IkiWiki::ErrorReason->new("invalid year $y");
  2377. }
  2378. if ((localtime($IkiWiki::pagectime{$page}))[5] + 1900 == $y) {
  2379. return IkiWiki::SuccessReason->new('creation_year matched');
  2380. }
  2381. else {
  2382. return IkiWiki::FailReason->new('creation_year did not match');
  2383. }
  2384. }
  2385. sub match_user ($$;@) {
  2386. shift;
  2387. my $user=shift;
  2388. my %params=@_;
  2389. my $regexp=IkiWiki::glob2re($user);
  2390. if (! exists $params{user}) {
  2391. return IkiWiki::ErrorReason->new("no user specified");
  2392. }
  2393. if (defined $params{user} && $params{user}=~$regexp) {
  2394. return IkiWiki::SuccessReason->new("user is $user");
  2395. }
  2396. elsif (! defined $params{user}) {
  2397. return IkiWiki::FailReason->new("not logged in");
  2398. }
  2399. else {
  2400. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  2401. }
  2402. }
  2403. sub match_admin ($$;@) {
  2404. shift;
  2405. shift;
  2406. my %params=@_;
  2407. if (! exists $params{user}) {
  2408. return IkiWiki::ErrorReason->new("no user specified");
  2409. }
  2410. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  2411. return IkiWiki::SuccessReason->new("user is an admin");
  2412. }
  2413. elsif (! defined $params{user}) {
  2414. return IkiWiki::FailReason->new("not logged in");
  2415. }
  2416. else {
  2417. return IkiWiki::FailReason->new("user is not an admin");
  2418. }
  2419. }
  2420. sub match_ip ($$;@) {
  2421. shift;
  2422. my $ip=shift;
  2423. my %params=@_;
  2424. if (! exists $params{ip}) {
  2425. return IkiWiki::ErrorReason->new("no IP specified");
  2426. }
  2427. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  2428. return IkiWiki::SuccessReason->new("IP is $ip");
  2429. }
  2430. else {
  2431. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  2432. }
  2433. }
  2434. package IkiWiki::SortSpec;
  2435. # This is in the SortSpec namespace so that the $a and $b that sort() uses
  2436. # are easily available in this namespace, for cmp functions to use them.
  2437. sub sort_pages {
  2438. my $f=shift;
  2439. sort $f @_
  2440. }
  2441. sub cmp_title {
  2442. IkiWiki::pagetitle(IkiWiki::basename($a))
  2443. cmp
  2444. IkiWiki::pagetitle(IkiWiki::basename($b))
  2445. }
  2446. sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
  2447. sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }
  2448. 1