summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 4e30119182c81d3e376027cfac05031c384d9729 (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 styleurl (;$) { #{{{
  262. my $page=shift;
  263. return "$config{url}/style.css" if ! defined $page;
  264. $page=~s/[^\/]+$//;
  265. $page=~s/[^\/]+\//..\//g;
  266. return $page."style.css";
  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. styleurl => styleurl(),
  410. baseurl => "$config{url}/",
  411. );
  412. return $template->output;
  413. }#}}}
  414. sub hook (@) { # {{{
  415. my %param=@_;
  416. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  417. error "hook requires type, call, and id parameters";
  418. }
  419. $hooks{$param{type}}{$param{id}}=\%param;
  420. } # }}}
  421. sub run_hooks ($$) { # {{{
  422. # Calls the given sub for each hook of the given type,
  423. # passing it the hook function to call.
  424. my $type=shift;
  425. my $sub=shift;
  426. if (exists $hooks{$type}) {
  427. foreach my $id (keys %{$hooks{$type}}) {
  428. $sub->($hooks{$type}{$id}{call});
  429. }
  430. }
  431. } #}}}
  432. sub globlist_to_pagespec ($) { #{{{
  433. my @globlist=split(' ', shift);
  434. my (@spec, @skip);
  435. foreach my $glob (@globlist) {
  436. if ($glob=~/^!(.*)/) {
  437. push @skip, $glob;
  438. }
  439. else {
  440. push @spec, $glob;
  441. }
  442. }
  443. my $spec=join(" or ", @spec);
  444. if (@skip) {
  445. my $skip=join(" and ", @skip);
  446. if (length $spec) {
  447. $spec="$skip and ($spec)";
  448. }
  449. else {
  450. $spec=$skip;
  451. }
  452. }
  453. return $spec;
  454. } #}}}
  455. sub is_globlist ($) { #{{{
  456. my $s=shift;
  457. $s=~/[^\s]+\s+([^\s]+)/ && $1 ne "and" && $1 ne "or";
  458. } #}}}
  459. sub safequote ($) { #{{{
  460. my $s=shift;
  461. $s=~s/[{}]//g;
  462. return "q{$s}";
  463. } #}}}
  464. sub pagespec_merge ($$) { #{{{
  465. my $a=shift;
  466. my $b=shift;
  467. # Support for old-style GlobLists.
  468. if (is_globlist($a)) {
  469. $a=globlist_to_pagespec($a);
  470. }
  471. if (is_globlist($b)) {
  472. $b=globlist_to_pagespec($b);
  473. }
  474. return "($a) or ($b)";
  475. } #}}}
  476. sub pagespec_translate ($) { #{{{
  477. # This assumes that $page is in scope in the function
  478. # that evalulates the translated pagespec code.
  479. my $spec=shift;
  480. # Support for old-style GlobLists.
  481. if (is_globlist($spec)) {
  482. $spec=globlist_to_pagespec($spec);
  483. }
  484. # Convert spec to perl code.
  485. my $code="";
  486. while ($spec=~m/\s*(\!|\(|\)|\w+\([^\)]+\)|[^\s()]+)\s*/ig) {
  487. my $word=$1;
  488. if (lc $word eq "and") {
  489. $code.=" &&";
  490. }
  491. elsif (lc $word eq "or") {
  492. $code.=" ||";
  493. }
  494. elsif ($word eq "(" || $word eq ")" || $word eq "!") {
  495. $code.=" ".$word;
  496. }
  497. elsif ($word =~ /^(link|backlink|created_before|created_after|creation_month|creation_year|creation_day)\((.+)\)$/) {
  498. $code.=" match_$1(\$page, ".safequote($2).")";
  499. }
  500. else {
  501. $code.=" match_glob(\$page, ".safequote($word).")";
  502. }
  503. }
  504. return $code;
  505. } #}}}
  506. sub pagespec_match ($$) { #{{{
  507. my $page=shift;
  508. my $spec=shift;
  509. return eval pagespec_translate($spec);
  510. } #}}}
  511. sub match_glob ($$) { #{{{
  512. my $page=shift;
  513. my $glob=shift;
  514. # turn glob into safe regexp
  515. $glob=quotemeta($glob);
  516. $glob=~s/\\\*/.*/g;
  517. $glob=~s/\\\?/./g;
  518. return $page=~/^$glob$/i;
  519. } #}}}
  520. sub match_link ($$) { #{{{
  521. my $page=shift;
  522. my $link=lc(shift);
  523. my $links = $links{$page} or return undef;
  524. foreach my $p (@$links) {
  525. return 1 if lc $p eq $link;
  526. }
  527. return 0;
  528. } #}}}
  529. sub match_backlink ($$) { #{{{
  530. match_link(pop, pop);
  531. } #}}}
  532. sub match_created_before ($$) { #{{{
  533. my $page=shift;
  534. my $testpage=shift;
  535. if (exists $pagectime{$testpage}) {
  536. return $pagectime{$page} < $pagectime{$testpage};
  537. }
  538. else {
  539. return 0;
  540. }
  541. } #}}}
  542. sub match_created_after ($$) { #{{{
  543. my $page=shift;
  544. my $testpage=shift;
  545. if (exists $pagectime{$testpage}) {
  546. return $pagectime{$page} > $pagectime{$testpage};
  547. }
  548. else {
  549. return 0;
  550. }
  551. } #}}}
  552. sub match_creation_day ($$) { #{{{
  553. return ((gmtime($pagectime{shift()}))[3] == shift);
  554. } #}}}
  555. sub match_creation_month ($$) { #{{{
  556. return ((gmtime($pagectime{shift()}))[4] + 1 == shift);
  557. } #}}}
  558. sub match_creation_year ($$) { #{{{
  559. return ((gmtime($pagectime{shift()}))[5] + 1900 == shift);
  560. } #}}}
  561. 1