summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: d7c082b1decf3e691048bc8eaea1e863404f58d4 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Encode;
  6. use open qw{:utf8 :std};
  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{mdwn 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 =~ /\.([^.]+)$/) {
  110. return $1 if exists $hooks{htmlize}{$1};
  111. }
  112. return undef;
  113. } #}}}
  114. sub pagename ($) { #{{{
  115. my $file=shift;
  116. my $type=pagetype($file);
  117. my $page=$file;
  118. $page=~s/\Q.$type\E*$// if defined $type;
  119. return $page;
  120. } #}}}
  121. sub htmlpage ($) { #{{{
  122. my $page=shift;
  123. return $page.".html";
  124. } #}}}
  125. sub srcfile ($) { #{{{
  126. my $file=shift;
  127. return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
  128. return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
  129. error("internal error: $file cannot be found");
  130. } #}}}
  131. sub readfile ($;$) { #{{{
  132. my $file=shift;
  133. my $binary=shift;
  134. if (-l $file) {
  135. error("cannot read a symlink ($file)");
  136. }
  137. local $/=undef;
  138. open (IN, $file) || error("failed to read $file: $!");
  139. binmode(IN) if ($binary);
  140. my $ret=<IN>;
  141. close IN;
  142. return $ret;
  143. } #}}}
  144. sub writefile ($$$;$) { #{{{
  145. my $file=shift; # can include subdirs
  146. my $destdir=shift; # directory to put file in
  147. my $content=shift;
  148. my $binary=shift;
  149. my $test=$file;
  150. while (length $test) {
  151. if (-l "$destdir/$test") {
  152. error("cannot write to a symlink ($test)");
  153. }
  154. $test=dirname($test);
  155. }
  156. my $dir=dirname("$destdir/$file");
  157. if (! -d $dir) {
  158. my $d="";
  159. foreach my $s (split(m!/+!, $dir)) {
  160. $d.="$s/";
  161. if (! -d $d) {
  162. mkdir($d) || error("failed to create directory $d: $!");
  163. }
  164. }
  165. }
  166. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  167. binmode(OUT) if ($binary);
  168. print OUT $content;
  169. close OUT;
  170. } #}}}
  171. sub bestlink ($$) { #{{{
  172. # Given a page and the text of a link on the page, determine which
  173. # existing page that link best points to. Prefers pages under a
  174. # subdirectory with the same name as the source page, failing that
  175. # goes down the directory tree to the base looking for matching
  176. # pages.
  177. my $page=shift;
  178. my $link=lc(shift);
  179. my $cwd=$page;
  180. do {
  181. my $l=$cwd;
  182. $l.="/" if length $l;
  183. $l.=$link;
  184. if (exists $links{$l}) {
  185. #debug("for $page, \"$link\", use $l");
  186. return $l;
  187. }
  188. } while $cwd=~s!/?[^/]+$!!;
  189. #print STDERR "warning: page $page, broken link: $link\n";
  190. return "";
  191. } #}}}
  192. sub isinlinableimage ($) { #{{{
  193. my $file=shift;
  194. $file=~/\.(png|gif|jpg|jpeg)$/i;
  195. } #}}}
  196. sub pagetitle ($) { #{{{
  197. my $page=shift;
  198. $page=~s/__(\d+)__/&#$1;/g;
  199. $page=~y/_/ /;
  200. return $page;
  201. } #}}}
  202. sub titlepage ($) { #{{{
  203. my $title=shift;
  204. $title=~y/ /_/;
  205. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  206. return $title;
  207. } #}}}
  208. sub cgiurl (@) { #{{{
  209. my %params=@_;
  210. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  211. } #}}}
  212. sub styleurl (;$) { #{{{
  213. my $page=shift;
  214. return "$config{url}/style.css" if ! defined $page;
  215. $page=~s/[^\/]+$//;
  216. $page=~s/[^\/]+\//..\//g;
  217. return $page."style.css";
  218. } #}}}
  219. sub htmllink ($$$;$$$) { #{{{
  220. my $lpage=shift; # the page doing the linking
  221. my $page=shift; # the page that will contain the link (different for inline)
  222. my $link=shift;
  223. my $noimageinline=shift; # don't turn links into inline html images
  224. my $forcesubpage=shift; # force a link to a subpage
  225. my $linktext=shift; # set to force the link text to something
  226. my $bestlink;
  227. if (! $forcesubpage) {
  228. $bestlink=bestlink($lpage, $link);
  229. }
  230. else {
  231. $bestlink="$lpage/".lc($link);
  232. }
  233. $linktext=pagetitle(basename($link)) unless defined $linktext;
  234. return $linktext if length $bestlink && $page eq $bestlink;
  235. # TODO BUG: %renderedfiles may not have it, if the linked to page
  236. # was also added and isn't yet rendered! Note that this bug is
  237. # masked by the bug that makes all new files be rendered twice.
  238. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  239. $bestlink=htmlpage($bestlink);
  240. }
  241. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  242. return "<span><a href=\"".
  243. cgiurl(do => "create", page => $link, from => $page).
  244. "\">?</a>$linktext</span>"
  245. }
  246. require File::Spec;
  247. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  248. if (! $noimageinline && isinlinableimage($bestlink)) {
  249. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  250. }
  251. return "<a href=\"$bestlink\">$linktext</a>";
  252. } #}}}
  253. sub indexlink () { #{{{
  254. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  255. } #}}}
  256. sub lockwiki () { #{{{
  257. # Take an exclusive lock on the wiki to prevent multiple concurrent
  258. # run issues. The lock will be dropped on program exit.
  259. if (! -d $config{wikistatedir}) {
  260. mkdir($config{wikistatedir});
  261. }
  262. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  263. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  264. if (! flock(WIKILOCK, 2 | 4)) {
  265. debug("wiki seems to be locked, waiting for lock");
  266. my $wait=600; # arbitrary, but don't hang forever to
  267. # prevent process pileup
  268. for (1..600) {
  269. return if flock(WIKILOCK, 2 | 4);
  270. sleep 1;
  271. }
  272. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  273. }
  274. } #}}}
  275. sub unlockwiki () { #{{{
  276. close WIKILOCK;
  277. } #}}}
  278. sub loadindex () { #{{{
  279. open (IN, "$config{wikistatedir}/index") || return;
  280. while (<IN>) {
  281. $_=possibly_foolish_untaint($_);
  282. chomp;
  283. my %items;
  284. $items{link}=[];
  285. foreach my $i (split(/ /, $_)) {
  286. my ($item, $val)=split(/=/, $i, 2);
  287. push @{$items{$item}}, $val;
  288. }
  289. next unless exists $items{src}; # skip bad lines for now
  290. my $page=pagename($items{src}[0]);
  291. if (! $config{rebuild}) {
  292. $pagesources{$page}=$items{src}[0];
  293. $oldpagemtime{$page}=$items{mtime}[0];
  294. $oldlinks{$page}=[@{$items{link}}];
  295. $links{$page}=[@{$items{link}}];
  296. $depends{$page}=join(" ", @{$items{depends}})
  297. if exists $items{depends};
  298. $renderedfiles{$page}=$items{dest}[0];
  299. }
  300. $pagectime{$page}=$items{ctime}[0];
  301. }
  302. close IN;
  303. } #}}}
  304. sub saveindex () { #{{{
  305. if (! -d $config{wikistatedir}) {
  306. mkdir($config{wikistatedir});
  307. }
  308. open (OUT, ">$config{wikistatedir}/index") ||
  309. error("cannot write to $config{wikistatedir}/index: $!");
  310. foreach my $page (keys %oldpagemtime) {
  311. next unless $oldpagemtime{$page};
  312. my $line="mtime=$oldpagemtime{$page} ".
  313. "ctime=$pagectime{$page} ".
  314. "src=$pagesources{$page} ".
  315. "dest=$renderedfiles{$page}";
  316. $line.=" link=$_" foreach @{$links{$page}};
  317. if (exists $depends{$page}) {
  318. $line.=" depends=$_" foreach split " ", $depends{$page};
  319. }
  320. print OUT $line."\n";
  321. }
  322. close OUT;
  323. } #}}}
  324. sub template_params (@) { #{{{
  325. my $filename=shift;
  326. require HTML::Template;
  327. return filter => sub {
  328. my $text_ref = shift;
  329. $$text_ref=&Encode::decode_utf8($$text_ref);
  330. },
  331. filename => "$config{templatedir}/$filename", @_;
  332. } #}}}
  333. sub template ($;@) { #{{{
  334. HTML::Template->new(template_params(@_));
  335. } #}}}
  336. sub misctemplate ($$) { #{{{
  337. my $title=shift;
  338. my $pagebody=shift;
  339. my $template=template("misc.tmpl");
  340. $template->param(
  341. title => $title,
  342. indexlink => indexlink(),
  343. wikiname => $config{wikiname},
  344. pagebody => $pagebody,
  345. styleurl => styleurl(),
  346. baseurl => "$config{url}/",
  347. );
  348. return $template->output;
  349. }#}}}
  350. sub glob_match ($$) { #{{{
  351. my $page=shift;
  352. my $glob=shift;
  353. if ($glob =~ /^link\((.+)\)$/) {
  354. my $rev = $links{$page} or return undef;
  355. foreach my $p (@$rev) {
  356. return 1 if lc $p eq $1;
  357. }
  358. return 0;
  359. } elsif ($glob =~ /^backlink\((.+)\)$/) {
  360. my $rev = $links{$1} or return undef;
  361. foreach my $p (@$rev) {
  362. return 1 if lc $p eq $page;
  363. }
  364. return 0;
  365. } else {
  366. # turn glob into safe regexp
  367. $glob=quotemeta($glob);
  368. $glob=~s/\\\*/.*/g;
  369. $glob=~s/\\\?/./g;
  370. $glob=~s!\\/!/!g;
  371. return $page=~/^$glob$/i;
  372. }
  373. } #}}}
  374. sub globlist_match ($$) { #{{{
  375. my $page=shift;
  376. my @globlist=split(" ", shift);
  377. # check any negated globs first
  378. foreach my $glob (@globlist) {
  379. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  380. }
  381. foreach my $glob (@globlist) {
  382. return 1 if glob_match($page, $glob);
  383. }
  384. return 0;
  385. } #}}}
  386. sub hook (@) { # {{{
  387. my %param=@_;
  388. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  389. error "hook requires type, call, and id parameters";
  390. }
  391. $hooks{$param{type}}{$param{id}}=\%param;
  392. } # }}}
  393. 1