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