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