summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 4be5612f1bf8df3d3090dda0ad9963f866a201f4 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use encoding "utf8";
  6. use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
  7. %renderedfiles %pagesources %depends %hooks};
  8. sub defaultconfig () { #{{{
  9. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
  10. wiki_link_regexp => qr/\[\[(?:([^\]\|]+)\|)?([^\s\]]+)\]\]/,
  11. wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]*)\]\]/,
  12. wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
  13. verbose => 0,
  14. wikiname => "wiki",
  15. default_pageext => ".mdwn",
  16. cgi => 0,
  17. rcs => 'svn',
  18. notify => 0,
  19. url => '',
  20. cgiurl => '',
  21. historyurl => '',
  22. diffurl => '',
  23. anonok => 0,
  24. rss => 0,
  25. discussion => 1,
  26. rebuild => 0,
  27. refresh => 0,
  28. getctime => 0,
  29. wrapper => undef,
  30. wrappermode => undef,
  31. svnrepo => undef,
  32. svnpath => "trunk",
  33. srcdir => undef,
  34. destdir => undef,
  35. pingurl => [],
  36. templatedir => "/usr/share/ikiwiki/templates",
  37. underlaydir => "/usr/share/ikiwiki/basewiki",
  38. setup => undef,
  39. adminuser => undef,
  40. adminemail => undef,
  41. plugin => [qw{inline htmlscrubber}],
  42. timeformat => '%c',
  43. } #}}}
  44. sub checkconfig () { #{{{
  45. if ($config{cgi} && ! length $config{url}) {
  46. error("Must specify url to wiki with --url when using --cgi\n");
  47. }
  48. if ($config{rss} && ! length $config{url}) {
  49. error("Must specify url to wiki with --url when using --rss\n");
  50. }
  51. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  52. unless exists $config{wikistatedir};
  53. if ($config{rcs}) {
  54. eval qq{require IkiWiki::Rcs::$config{rcs}};
  55. if ($@) {
  56. error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
  57. }
  58. }
  59. else {
  60. require IkiWiki::Rcs::Stub;
  61. }
  62. foreach my $plugin (@{$config{plugin}}) {
  63. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  64. eval qq{use $mod};
  65. if ($@) {
  66. error("Failed to load plugin $mod: $@");
  67. }
  68. }
  69. if (exists $hooks{checkconfig}) {
  70. foreach my $id (keys %{$hooks{checkconfig}}) {
  71. $hooks{checkconfig}{$id}{call}->();
  72. }
  73. }
  74. } #}}}
  75. sub error ($) { #{{{
  76. if ($config{cgi}) {
  77. print "Content-type: text/html\n\n";
  78. print misctemplate("Error", "<p>Error: @_</p>");
  79. }
  80. die @_;
  81. } #}}}
  82. sub debug ($) { #{{{
  83. return unless $config{verbose};
  84. if (! $config{cgi}) {
  85. print "@_\n";
  86. }
  87. else {
  88. print STDERR "@_\n";
  89. }
  90. } #}}}
  91. sub possibly_foolish_untaint ($) { #{{{
  92. my $tainted=shift;
  93. my ($untainted)=$tainted=~/(.*)/;
  94. return $untainted;
  95. } #}}}
  96. sub basename ($) { #{{{
  97. my $file=shift;
  98. $file=~s!.*/+!!;
  99. return $file;
  100. } #}}}
  101. sub dirname ($) { #{{{
  102. my $file=shift;
  103. $file=~s!/*[^/]+$!!;
  104. return $file;
  105. } #}}}
  106. sub pagetype ($) { #{{{
  107. my $page=shift;
  108. if ($page =~ /\.mdwn$/) {
  109. return ".mdwn";
  110. }
  111. else {
  112. return "unknown";
  113. }
  114. } #}}}
  115. sub pagename ($) { #{{{
  116. my $file=shift;
  117. my $type=pagetype($file);
  118. my $page=$file;
  119. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  120. return $page;
  121. } #}}}
  122. sub htmlpage ($) { #{{{
  123. my $page=shift;
  124. return $page.".html";
  125. } #}}}
  126. sub srcfile ($) { #{{{
  127. my $file=shift;
  128. return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
  129. return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
  130. error("internal error: $file cannot be found");
  131. } #}}}
  132. sub readfile ($;$) { #{{{
  133. my $file=shift;
  134. my $binary=shift;
  135. if (-l $file) {
  136. error("cannot read a symlink ($file)");
  137. }
  138. local $/=undef;
  139. open (IN, $file) || error("failed to read $file: $!");
  140. if (! $binary) {
  141. binmode(IN, ":utf8");
  142. }
  143. else {
  144. binmode(IN);
  145. }
  146. my $ret=<IN>;
  147. close IN;
  148. return $ret;
  149. } #}}}
  150. sub writefile ($$$;$) { #{{{
  151. my $file=shift; # can include subdirs
  152. my $destdir=shift; # directory to put file in
  153. my $content=shift;
  154. my $binary=shift;
  155. my $test=$file;
  156. while (length $test) {
  157. if (-l "$destdir/$test") {
  158. error("cannot write to a symlink ($test)");
  159. }
  160. $test=dirname($test);
  161. }
  162. my $dir=dirname("$destdir/$file");
  163. if (! -d $dir) {
  164. my $d="";
  165. foreach my $s (split(m!/+!, $dir)) {
  166. $d.="$s/";
  167. if (! -d $d) {
  168. mkdir($d) || error("failed to create directory $d: $!");
  169. }
  170. }
  171. }
  172. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  173. if (! $binary) {
  174. binmode(OUT, ":utf8");
  175. }
  176. else {
  177. binmode(OUT);
  178. }
  179. print OUT $content;
  180. close OUT;
  181. } #}}}
  182. sub bestlink ($$) { #{{{
  183. # Given a page and the text of a link on the page, determine which
  184. # existing page that link best points to. Prefers pages under a
  185. # subdirectory with the same name as the source page, failing that
  186. # goes down the directory tree to the base looking for matching
  187. # pages.
  188. my $page=shift;
  189. my $link=lc(shift);
  190. my $cwd=$page;
  191. do {
  192. my $l=$cwd;
  193. $l.="/" if length $l;
  194. $l.=$link;
  195. if (exists $links{$l}) {
  196. #debug("for $page, \"$link\", use $l");
  197. return $l;
  198. }
  199. } while $cwd=~s!/?[^/]+$!!;
  200. #print STDERR "warning: page $page, broken link: $link\n";
  201. return "";
  202. } #}}}
  203. sub isinlinableimage ($) { #{{{
  204. my $file=shift;
  205. $file=~/\.(png|gif|jpg|jpeg)$/i;
  206. } #}}}
  207. sub pagetitle ($) { #{{{
  208. my $page=shift;
  209. $page=~s/__(\d+)__/&#$1;/g;
  210. $page=~y/_/ /;
  211. return $page;
  212. } #}}}
  213. sub titlepage ($) { #{{{
  214. my $title=shift;
  215. $title=~y/ /_/;
  216. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  217. return $title;
  218. } #}}}
  219. sub cgiurl (@) { #{{{
  220. my %params=@_;
  221. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  222. } #}}}
  223. sub styleurl (;$) { #{{{
  224. my $page=shift;
  225. return "$config{url}/style.css" if ! defined $page;
  226. $page=~s/[^\/]+$//;
  227. $page=~s/[^\/]+\//..\//g;
  228. return $page."style.css";
  229. } #}}}
  230. sub htmllink ($$$;$$$) { #{{{
  231. my $lpage=shift; # the page doing the linking
  232. my $page=shift; # the page that will contain the link (different for inline)
  233. my $link=shift;
  234. my $noimageinline=shift; # don't turn links into inline html images
  235. my $forcesubpage=shift; # force a link to a subpage
  236. my $linktext=shift; # set to force the link text to something
  237. my $bestlink;
  238. if (! $forcesubpage) {
  239. $bestlink=bestlink($lpage, $link);
  240. }
  241. else {
  242. $bestlink="$lpage/".lc($link);
  243. }
  244. $linktext=pagetitle(basename($link)) unless defined $linktext;
  245. return $linktext if length $bestlink && $page eq $bestlink;
  246. # TODO BUG: %renderedfiles may not have it, if the linked to page
  247. # was also added and isn't yet rendered! Note that this bug is
  248. # masked by the bug that makes all new files be rendered twice.
  249. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  250. $bestlink=htmlpage($bestlink);
  251. }
  252. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  253. return "<span><a href=\"".
  254. cgiurl(do => "create", page => $link, from => $page).
  255. "\">?</a>$linktext</span>"
  256. }
  257. require File::Spec;
  258. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  259. if (! $noimageinline && isinlinableimage($bestlink)) {
  260. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  261. }
  262. return "<a href=\"$bestlink\">$linktext</a>";
  263. } #}}}
  264. sub indexlink () { #{{{
  265. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  266. } #}}}
  267. sub lockwiki () { #{{{
  268. # Take an exclusive lock on the wiki to prevent multiple concurrent
  269. # run issues. The lock will be dropped on program exit.
  270. if (! -d $config{wikistatedir}) {
  271. mkdir($config{wikistatedir});
  272. }
  273. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  274. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  275. if (! flock(WIKILOCK, 2 | 4)) {
  276. debug("wiki seems to be locked, waiting for lock");
  277. my $wait=600; # arbitrary, but don't hang forever to
  278. # prevent process pileup
  279. for (1..600) {
  280. return if flock(WIKILOCK, 2 | 4);
  281. sleep 1;
  282. }
  283. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  284. }
  285. } #}}}
  286. sub unlockwiki () { #{{{
  287. close WIKILOCK;
  288. } #}}}
  289. sub loadindex () { #{{{
  290. open (IN, "<:utf8", "$config{wikistatedir}/index") || return;
  291. while (<IN>) {
  292. $_=possibly_foolish_untaint($_);
  293. chomp;
  294. my %items;
  295. $items{link}=[];
  296. foreach my $i (split(/ /, $_)) {
  297. my ($item, $val)=split(/=/, $i, 2);
  298. push @{$items{$item}}, $val;
  299. }
  300. next unless exists $items{src}; # skip bad lines for now
  301. my $page=pagename($items{src}[0]);
  302. if (! $config{rebuild}) {
  303. $pagesources{$page}=$items{src}[0];
  304. $oldpagemtime{$page}=$items{mtime}[0];
  305. $oldlinks{$page}=[@{$items{link}}];
  306. $links{$page}=[@{$items{link}}];
  307. $depends{$page}=join(" ", @{$items{depends}})
  308. if exists $items{depends};
  309. $renderedfiles{$page}=$items{dest}[0];
  310. }
  311. $pagectime{$page}=$items{ctime}[0];
  312. }
  313. close IN;
  314. } #}}}
  315. sub saveindex () { #{{{
  316. if (! -d $config{wikistatedir}) {
  317. mkdir($config{wikistatedir});
  318. }
  319. open (OUT, ">:utf8", "$config{wikistatedir}/index") ||
  320. error("cannot write to $config{wikistatedir}/index: $!");
  321. foreach my $page (keys %oldpagemtime) {
  322. next unless $oldpagemtime{$page};
  323. my $line="mtime=$oldpagemtime{$page} ".
  324. "ctime=$pagectime{$page} ".
  325. "src=$pagesources{$page} ".
  326. "dest=$renderedfiles{$page}";
  327. $line.=" link=$_" foreach @{$links{$page}};
  328. if (exists $depends{$page}) {
  329. $line.=" depends=$_" foreach split " ", $depends{$page};
  330. }
  331. print OUT $line."\n";
  332. }
  333. close OUT;
  334. } #}}}
  335. sub template_params (@) { #{{{
  336. my $filename=shift;
  337. require Encode;
  338. require HTML::Template;
  339. return filter => sub {
  340. my $text_ref = shift;
  341. $$text_ref=&Encode::decode_utf8($$text_ref);
  342. },
  343. filename => "$config{templatedir}/$filename", @_;
  344. } #}}}
  345. sub template ($;@) { #{{{
  346. HTML::Template->new(template_params(@_));
  347. } #}}}
  348. sub misctemplate ($$) { #{{{
  349. my $title=shift;
  350. my $pagebody=shift;
  351. my $template=template("misc.tmpl");
  352. $template->param(
  353. title => $title,
  354. indexlink => indexlink(),
  355. wikiname => $config{wikiname},
  356. pagebody => $pagebody,
  357. styleurl => styleurl(),
  358. baseurl => "$config{url}/",
  359. );
  360. return $template->output;
  361. }#}}}
  362. sub glob_match ($$) { #{{{
  363. my $page=shift;
  364. my $glob=shift;
  365. if ($glob =~ /^link\((.+)\)$/) {
  366. my $rev = $links{$page} or return undef;
  367. foreach my $p (@$rev) {
  368. return 1 if lc $p eq $1;
  369. }
  370. return 0;
  371. } elsif ($glob =~ /^backlink\((.+)\)$/) {
  372. my $rev = $links{$1} or return undef;
  373. foreach my $p (@$rev) {
  374. return 1 if lc $p eq $page;
  375. }
  376. return 0;
  377. } else {
  378. # turn glob into safe regexp
  379. $glob=quotemeta($glob);
  380. $glob=~s/\\\*/.*/g;
  381. $glob=~s/\\\?/./g;
  382. $glob=~s!\\/!/!g;
  383. return $page=~/^$glob$/i;
  384. }
  385. } #}}}
  386. sub globlist_match ($$) { #{{{
  387. my $page=shift;
  388. my @globlist=split(" ", shift);
  389. # check any negated globs first
  390. foreach my $glob (@globlist) {
  391. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  392. }
  393. foreach my $glob (@globlist) {
  394. return 1 if glob_match($page, $glob);
  395. }
  396. return 0;
  397. } #}}}
  398. sub hook (@) { # {{{
  399. my %param=@_;
  400. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  401. error "hook requires type, call, and id parameters";
  402. }
  403. $hooks{$param{type}}{$param{id}}=\%param;
  404. } # }}}
  405. 1