summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 9556d6ef7732bba49e180f14323ddc14141bbe48 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use HTML::Entities;
  7. use open qw{:utf8 :std};
  8. # Optimisation.
  9. use Memoize;
  10. memoize("abs2rel");
  11. memoize("pagespec_translate");
  12. use vars qw{%config %links %oldlinks %oldpagemtime %pagectime %pagecase
  13. %renderedfiles %pagesources %depends %hooks %forcerebuild};
  14. sub defaultconfig () { #{{{
  15. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.x?html?$|\.rss$)},
  16. wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
  17. wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
  18. wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
  19. verbose => 0,
  20. syslog => 0,
  21. wikiname => "wiki",
  22. default_pageext => "mdwn",
  23. cgi => 0,
  24. rcs => 'svn',
  25. notify => 0,
  26. url => '',
  27. cgiurl => '',
  28. historyurl => '',
  29. diffurl => '',
  30. anonok => 0,
  31. rss => 0,
  32. discussion => 1,
  33. rebuild => 0,
  34. refresh => 0,
  35. getctime => 0,
  36. w3mmode => 0,
  37. wrapper => undef,
  38. wrappermode => undef,
  39. svnrepo => undef,
  40. svnpath => "trunk",
  41. srcdir => undef,
  42. destdir => undef,
  43. pingurl => [],
  44. templatedir => "/usr/share/ikiwiki/templates",
  45. underlaydir => "/usr/share/ikiwiki/basewiki",
  46. setup => undef,
  47. adminuser => undef,
  48. adminemail => undef,
  49. plugin => [qw{mdwn inline htmlscrubber}],
  50. timeformat => '%c',
  51. locale => undef,
  52. } #}}}
  53. sub checkconfig () { #{{{
  54. # locale stuff; avoid LC_ALL since it overrides everything
  55. if (defined $ENV{LC_ALL}) {
  56. $ENV{LANG} = $ENV{LC_ALL};
  57. delete $ENV{LC_ALL};
  58. }
  59. if (defined $config{locale}) {
  60. eval q{use POSIX};
  61. $ENV{LANG} = $config{locale}
  62. if POSIX::setlocale(&POSIX::LC_TIME, $config{locale});
  63. }
  64. if ($config{w3mmode}) {
  65. eval q{use Cwd q{abs_path}};
  66. $config{srcdir}=possibly_foolish_untaint(abs_path($config{srcdir}));
  67. $config{destdir}=possibly_foolish_untaint(abs_path($config{destdir}));
  68. $config{cgiurl}="file:///\$LIB/ikiwiki-w3m.cgi/".$config{cgiurl}
  69. unless $config{cgiurl} =~ m!file:///!;
  70. $config{url}="file://".$config{destdir};
  71. }
  72. if ($config{cgi} && ! length $config{url}) {
  73. error("Must specify url to wiki with --url when using --cgi\n");
  74. }
  75. if ($config{rss} && ! length $config{url}) {
  76. error("Must specify url to wiki with --url when using --rss\n");
  77. }
  78. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  79. unless exists $config{wikistatedir};
  80. if ($config{rcs}) {
  81. eval qq{require IkiWiki::Rcs::$config{rcs}};
  82. if ($@) {
  83. error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
  84. }
  85. }
  86. else {
  87. require IkiWiki::Rcs::Stub;
  88. }
  89. run_hooks(checkconfig => sub { shift->() });
  90. } #}}}
  91. sub loadplugins () { #{{{
  92. foreach my $plugin (@{$config{plugin}}) {
  93. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  94. eval qq{use $mod};
  95. if ($@) {
  96. error("Failed to load plugin $mod: $@");
  97. }
  98. }
  99. run_hooks(getopt => sub { shift->() });
  100. if (grep /^-/, @ARGV) {
  101. print STDERR "Unknown option: $_\n"
  102. foreach grep /^-/, @ARGV;
  103. usage();
  104. }
  105. } #}}}
  106. sub error ($) { #{{{
  107. if ($config{cgi}) {
  108. print "Content-type: text/html\n\n";
  109. print misctemplate("Error", "<p>Error: @_</p>");
  110. }
  111. log_message(error => @_);
  112. exit(1);
  113. } #}}}
  114. sub debug ($) { #{{{
  115. return unless $config{verbose};
  116. log_message(debug => @_);
  117. } #}}}
  118. my $log_open=0;
  119. sub log_message ($$) { #{{{
  120. my $type=shift;
  121. if ($config{syslog}) {
  122. require Sys::Syslog;
  123. unless ($log_open) {
  124. Sys::Syslog::setlogsock('unix');
  125. Sys::Syslog::openlog('ikiwiki', '', 'user');
  126. $log_open=1;
  127. }
  128. eval {
  129. Sys::Syslog::syslog($type, join(" ", @_));
  130. }
  131. }
  132. elsif (! $config{cgi}) {
  133. print "@_\n";
  134. }
  135. else {
  136. print STDERR "@_\n";
  137. }
  138. } #}}}
  139. sub possibly_foolish_untaint ($) { #{{{
  140. my $tainted=shift;
  141. my ($untainted)=$tainted=~/(.*)/;
  142. return $untainted;
  143. } #}}}
  144. sub basename ($) { #{{{
  145. my $file=shift;
  146. $file=~s!.*/+!!;
  147. return $file;
  148. } #}}}
  149. sub dirname ($) { #{{{
  150. my $file=shift;
  151. $file=~s!/*[^/]+$!!;
  152. return $file;
  153. } #}}}
  154. sub pagetype ($) { #{{{
  155. my $page=shift;
  156. if ($page =~ /\.([^.]+)$/) {
  157. return $1 if exists $hooks{htmlize}{$1};
  158. }
  159. return undef;
  160. } #}}}
  161. sub pagename ($) { #{{{
  162. my $file=shift;
  163. my $type=pagetype($file);
  164. my $page=$file;
  165. $page=~s/\Q.$type\E*$// if defined $type;
  166. return $page;
  167. } #}}}
  168. sub htmlpage ($) { #{{{
  169. my $page=shift;
  170. return $page.".html";
  171. } #}}}
  172. sub srcfile ($) { #{{{
  173. my $file=shift;
  174. return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
  175. return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
  176. error("internal error: $file cannot be found");
  177. } #}}}
  178. sub readfile ($;$) { #{{{
  179. my $file=shift;
  180. my $binary=shift;
  181. if (-l $file) {
  182. error("cannot read a symlink ($file)");
  183. }
  184. local $/=undef;
  185. open (IN, $file) || error("failed to read $file: $!");
  186. binmode(IN) if ($binary);
  187. my $ret=<IN>;
  188. close IN;
  189. return $ret;
  190. } #}}}
  191. sub writefile ($$$;$) { #{{{
  192. my $file=shift; # can include subdirs
  193. my $destdir=shift; # directory to put file in
  194. my $content=shift;
  195. my $binary=shift;
  196. my $test=$file;
  197. while (length $test) {
  198. if (-l "$destdir/$test") {
  199. error("cannot write to a symlink ($test)");
  200. }
  201. $test=dirname($test);
  202. }
  203. my $dir=dirname("$destdir/$file");
  204. if (! -d $dir) {
  205. my $d="";
  206. foreach my $s (split(m!/+!, $dir)) {
  207. $d.="$s/";
  208. if (! -d $d) {
  209. mkdir($d) || error("failed to create directory $d: $!");
  210. }
  211. }
  212. }
  213. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  214. binmode(OUT) if ($binary);
  215. print OUT $content;
  216. close OUT;
  217. } #}}}
  218. sub bestlink ($$) { #{{{
  219. # Given a page and the text of a link on the page, determine which
  220. # existing page that link best points to. Prefers pages under a
  221. # subdirectory with the same name as the source page, failing that
  222. # goes down the directory tree to the base looking for matching
  223. # pages.
  224. my $page=shift;
  225. my $link=shift;
  226. my $cwd=$page;
  227. do {
  228. my $l=$cwd;
  229. $l.="/" if length $l;
  230. $l.=$link;
  231. if (exists $links{$l}) {
  232. return $l;
  233. }
  234. elsif (exists $pagecase{lc $l}) {
  235. return $pagecase{lc $l};
  236. }
  237. } while $cwd=~s!/?[^/]+$!!;
  238. #print STDERR "warning: page $page, broken link: $link\n";
  239. return "";
  240. } #}}}
  241. sub isinlinableimage ($) { #{{{
  242. my $file=shift;
  243. $file=~/\.(png|gif|jpg|jpeg)$/i;
  244. } #}}}
  245. sub pagetitle ($) { #{{{
  246. my $page=shift;
  247. $page=~s/__(\d+)__/&#$1;/g;
  248. $page=~y/_/ /;
  249. return $page;
  250. } #}}}
  251. sub titlepage ($) { #{{{
  252. my $title=shift;
  253. $title=~y/ /_/;
  254. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  255. return $title;
  256. } #}}}
  257. sub cgiurl (@) { #{{{
  258. my %params=@_;
  259. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  260. } #}}}
  261. sub baseurl (;$) { #{{{
  262. my $page=shift;
  263. return "$config{url}/" if ! defined $page;
  264. $page=~s/[^\/]+$//;
  265. $page=~s/[^\/]+\//..\//g;
  266. return $page;
  267. } #}}}
  268. sub abs2rel ($$) { #{{{
  269. # Work around very innefficient behavior in File::Spec if abs2rel
  270. # is passed two relative paths. It's much faster if paths are
  271. # absolute!
  272. my $path="/".shift;
  273. my $base="/".shift;
  274. require File::Spec;
  275. my $ret=File::Spec->abs2rel($path, $base);
  276. $ret=~s/^// if defined $ret;
  277. return $ret;
  278. } #}}}
  279. sub htmllink ($$$;$$$) { #{{{
  280. my $lpage=shift; # the page doing the linking
  281. my $page=shift; # the page that will contain the link (different for inline)
  282. my $link=shift;
  283. my $noimageinline=shift; # don't turn links into inline html images
  284. my $forcesubpage=shift; # force a link to a subpage
  285. my $linktext=shift; # set to force the link text to something
  286. my $bestlink;
  287. if (! $forcesubpage) {
  288. $bestlink=bestlink($lpage, $link);
  289. }
  290. else {
  291. $bestlink="$lpage/".lc($link);
  292. }
  293. $linktext=pagetitle(basename($link)) unless defined $linktext;
  294. return "<span class=\"selflink\">$linktext</span>"
  295. if length $bestlink && $page eq $bestlink;
  296. # TODO BUG: %renderedfiles may not have it, if the linked to page
  297. # was also added and isn't yet rendered! Note that this bug is
  298. # masked by the bug that makes all new files be rendered twice.
  299. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  300. $bestlink=htmlpage($bestlink);
  301. }
  302. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  303. return "<span><a href=\"".
  304. cgiurl(do => "create", page => lc($link), from => $page).
  305. "\">?</a>$linktext</span>"
  306. }
  307. $bestlink=abs2rel($bestlink, dirname($page));
  308. if (! $noimageinline && isinlinableimage($bestlink)) {
  309. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  310. }
  311. return "<a href=\"$bestlink\">$linktext</a>";
  312. } #}}}
  313. sub indexlink () { #{{{
  314. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  315. } #}}}
  316. sub lockwiki () { #{{{
  317. # Take an exclusive lock on the wiki to prevent multiple concurrent
  318. # run issues. The lock will be dropped on program exit.
  319. if (! -d $config{wikistatedir}) {
  320. mkdir($config{wikistatedir});
  321. }
  322. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  323. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  324. if (! flock(WIKILOCK, 2 | 4)) {
  325. debug("wiki seems to be locked, waiting for lock");
  326. my $wait=600; # arbitrary, but don't hang forever to
  327. # prevent process pileup
  328. for (1..600) {
  329. return if flock(WIKILOCK, 2 | 4);
  330. sleep 1;
  331. }
  332. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  333. }
  334. } #}}}
  335. sub unlockwiki () { #{{{
  336. close WIKILOCK;
  337. } #}}}
  338. sub loadindex () { #{{{
  339. open (IN, "$config{wikistatedir}/index") || return;
  340. while (<IN>) {
  341. $_=possibly_foolish_untaint($_);
  342. chomp;
  343. my %items;
  344. $items{link}=[];
  345. foreach my $i (split(/ /, $_)) {
  346. my ($item, $val)=split(/=/, $i, 2);
  347. push @{$items{$item}}, decode_entities($val);
  348. }
  349. next unless exists $items{src}; # skip bad lines for now
  350. my $page=pagename($items{src}[0]);
  351. if (! $config{rebuild}) {
  352. $pagesources{$page}=$items{src}[0];
  353. $oldpagemtime{$page}=$items{mtime}[0];
  354. $oldlinks{$page}=[@{$items{link}}];
  355. $links{$page}=[@{$items{link}}];
  356. $depends{$page}=$items{depends}[0] if exists $items{depends};
  357. $renderedfiles{$page}=$items{dest}[0];
  358. $pagecase{lc $page}=$page;
  359. }
  360. $pagectime{$page}=$items{ctime}[0];
  361. }
  362. close IN;
  363. } #}}}
  364. sub saveindex () { #{{{
  365. run_hooks(savestate => sub { shift->() });
  366. if (! -d $config{wikistatedir}) {
  367. mkdir($config{wikistatedir});
  368. }
  369. open (OUT, ">$config{wikistatedir}/index") ||
  370. error("cannot write to $config{wikistatedir}/index: $!");
  371. foreach my $page (keys %oldpagemtime) {
  372. next unless $oldpagemtime{$page};
  373. my $line="mtime=$oldpagemtime{$page} ".
  374. "ctime=$pagectime{$page} ".
  375. "src=$pagesources{$page} ".
  376. "dest=$renderedfiles{$page}";
  377. $line.=" link=$_" foreach @{$links{$page}};
  378. if (exists $depends{$page}) {
  379. $line.=" depends=".encode_entities($depends{$page}, " \t\n");
  380. }
  381. print OUT $line."\n";
  382. }
  383. close OUT;
  384. } #}}}
  385. sub template_params (@) { #{{{
  386. my $filename=shift;
  387. require HTML::Template;
  388. return filter => sub {
  389. my $text_ref = shift;
  390. $$text_ref=&Encode::decode_utf8($$text_ref);
  391. },
  392. filename => "$config{templatedir}/$filename",
  393. loop_context_vars => 1,
  394. die_on_bad_params => 0,
  395. @_;
  396. } #}}}
  397. sub template ($;@) { #{{{
  398. HTML::Template->new(template_params(@_));
  399. } #}}}
  400. sub misctemplate ($$) { #{{{
  401. my $title=shift;
  402. my $pagebody=shift;
  403. my $template=template("misc.tmpl");
  404. $template->param(
  405. title => $title,
  406. indexlink => indexlink(),
  407. wikiname => $config{wikiname},
  408. pagebody => $pagebody,
  409. baseurl => baseurl(),
  410. );
  411. return $template->output;
  412. }#}}}
  413. sub hook (@) { # {{{
  414. my %param=@_;
  415. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  416. error "hook requires type, call, and id parameters";
  417. }
  418. $hooks{$param{type}}{$param{id}}=\%param;
  419. } # }}}
  420. sub run_hooks ($$) { # {{{
  421. # Calls the given sub for each hook of the given type,
  422. # passing it the hook function to call.
  423. my $type=shift;
  424. my $sub=shift;
  425. if (exists $hooks{$type}) {
  426. foreach my $id (keys %{$hooks{$type}}) {
  427. $sub->($hooks{$type}{$id}{call});
  428. }
  429. }
  430. } #}}}
  431. sub globlist_to_pagespec ($) { #{{{
  432. my @globlist=split(' ', shift);
  433. my (@spec, @skip);
  434. foreach my $glob (@globlist) {
  435. if ($glob=~/^!(.*)/) {
  436. push @skip, $glob;
  437. }
  438. else {
  439. push @spec, $glob;
  440. }
  441. }
  442. my $spec=join(" or ", @spec);
  443. if (@skip) {
  444. my $skip=join(" and ", @skip);
  445. if (length $spec) {
  446. $spec="$skip and ($spec)";
  447. }
  448. else {
  449. $spec=$skip;
  450. }
  451. }
  452. return $spec;
  453. } #}}}
  454. sub is_globlist ($) { #{{{
  455. my $s=shift;
  456. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  457. } #}}}
  458. sub safequote ($) { #{{{
  459. my $s=shift;
  460. $s=~s/[{}]//g;
  461. return "q{$s}";
  462. } #}}}
  463. sub pagespec_merge ($$) { #{{{
  464. my $a=shift;
  465. my $b=shift;
  466. # Support for old-style GlobLists.
  467. if (is_globlist($a)) {
  468. $a=globlist_to_pagespec($a);
  469. }
  470. if (is_globlist($b)) {
  471. $b=globlist_to_pagespec($b);
  472. }
  473. return "($a) or ($b)";
  474. } #}}}
  475. sub pagespec_translate ($) { #{{{
  476. # This assumes that $page is in scope in the function
  477. # that evalulates the translated pagespec code.
  478. my $spec=shift;
  479. # Support for old-style GlobLists.
  480. if (is_globlist($spec)) {
  481. $spec=globlist_to_pagespec($spec);
  482. }
  483. # Convert spec to perl code.
  484. my $code="";
  485. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  486. my $word=$1;
  487. if (lc $word eq "and") {
  488. $code.=" &&";
  489. }
  490. elsif (lc $word eq "or") {
  491. $code.=" ||";
  492. }
  493. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  494. $code.=" ".$word;
  495. }
  496. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  497. $code.=" match_$1(\$page, ".safequote($2).")";
  498. }
  499. else {
  500. $code.=" match_glob(\$page, ".safequote($word).")";
  501. }
  502. }
  503. return $code;
  504. } #}}}
  505. sub pagespec_match ($$) { #{{{
  506. my $page=shift;
  507. my $spec=shift;
  508. return eval pagespec_translate($spec);
  509. } #}}}
  510. sub match_glob ($$) { #{{{
  511. my $page=shift;
  512. my $glob=shift;
  513. # turn glob into safe regexp
  514. $glob=quotemeta($glob);
  515. $glob=~s/\\\*/.*/g;
  516. $glob=~s/\\\?/./g;
  517. return $page=~/^$glob$/i;
  518. } #}}}
  519. sub match_link ($$) { #{{{
  520. my $page=shift;
  521. my $link=lc(shift);
  522. my $links = $links{$page} or return undef;
  523. foreach my $p (@$links) {
  524. return 1 if lc $p eq $link;
  525. }
  526. return 0;
  527. } #}}}
  528. sub match_backlink ($$) { #{{{
  529. match_link(pop, pop);
  530. } #}}}
  531. sub match_created_before ($$) { #{{{
  532. my $page=shift;
  533. my $testpage=shift;
  534. if (exists $pagectime{$testpage}) {
  535. return $pagectime{$page} < $pagectime{$testpage};
  536. }
  537. else {
  538. return 0;
  539. }
  540. } #}}}
  541. sub match_created_after ($$) { #{{{
  542. my $page=shift;
  543. my $testpage=shift;
  544. if (exists $pagectime{$testpage}) {
  545. return $pagectime{$page} > $pagectime{$testpage};
  546. }
  547. else {
  548. return 0;
  549. }
  550. } #}}}
  551. sub match_creation_day ($$) { #{{{
  552. return ((gmtime($pagectime{shift()}))[3] == shift);
  553. } #}}}
  554. sub match_creation_month ($$) { #{{{
  555. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  556. } #}}}
  557. sub match_creation_year ($$) { #{{{
  558. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  559. } #}}}
  560. 1