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