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