summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 357c1cd2d0ed0567538b41d585f7483d86238847 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use HTML::Template;
  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. 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. binmode(IN) if $binary;
  141. my $ret=<IN>;
  142. close IN;
  143. return $ret;
  144. } #}}}
  145. sub writefile ($$$;$) { #{{{
  146. my $file=shift; # can include subdirs
  147. my $destdir=shift; # directory to put file in
  148. my $content=shift;
  149. my $binary=shift;
  150. my $test=$file;
  151. while (length $test) {
  152. if (-l "$destdir/$test") {
  153. error("cannot write to a symlink ($test)");
  154. }
  155. $test=dirname($test);
  156. }
  157. my $dir=dirname("$destdir/$file");
  158. if (! -d $dir) {
  159. my $d="";
  160. foreach my $s (split(m!/+!, $dir)) {
  161. $d.="$s/";
  162. if (! -d $d) {
  163. mkdir($d) || error("failed to create directory $d: $!");
  164. }
  165. }
  166. }
  167. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  168. binmode(OUT) if $binary;
  169. print OUT $content;
  170. close OUT;
  171. } #}}}
  172. sub bestlink ($$) { #{{{
  173. # Given a page and the text of a link on the page, determine which
  174. # existing page that link best points to. Prefers pages under a
  175. # subdirectory with the same name as the source page, failing that
  176. # goes down the directory tree to the base looking for matching
  177. # pages.
  178. my $page=shift;
  179. my $link=lc(shift);
  180. my $cwd=$page;
  181. do {
  182. my $l=$cwd;
  183. $l.="/" if length $l;
  184. $l.=$link;
  185. if (exists $links{$l}) {
  186. #debug("for $page, \"$link\", use $l");
  187. return $l;
  188. }
  189. } while $cwd=~s!/?[^/]+$!!;
  190. #print STDERR "warning: page $page, broken link: $link\n";
  191. return "";
  192. } #}}}
  193. sub isinlinableimage ($) { #{{{
  194. my $file=shift;
  195. $file=~/\.(png|gif|jpg|jpeg)$/i;
  196. } #}}}
  197. sub pagetitle ($) { #{{{
  198. my $page=shift;
  199. $page=~s/__(\d+)__/&#$1;/g;
  200. $page=~y/_/ /;
  201. return $page;
  202. } #}}}
  203. sub titlepage ($) { #{{{
  204. my $title=shift;
  205. $title=~y/ /_/;
  206. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  207. return $title;
  208. } #}}}
  209. sub cgiurl (@) { #{{{
  210. my %params=@_;
  211. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  212. } #}}}
  213. sub styleurl (;$) { #{{{
  214. my $page=shift;
  215. return "$config{url}/style.css" if ! defined $page;
  216. $page=~s/[^\/]+$//;
  217. $page=~s/[^\/]+\//..\//g;
  218. return $page."style.css";
  219. } #}}}
  220. sub htmllink ($$$;$$$) { #{{{
  221. my $lpage=shift; # the page doing the linking
  222. my $page=shift; # the page that will contain the link (different for inline)
  223. my $link=shift;
  224. my $noimageinline=shift; # don't turn links into inline html images
  225. my $forcesubpage=shift; # force a link to a subpage
  226. my $linktext=shift; # set to force the link text to something
  227. my $bestlink;
  228. if (! $forcesubpage) {
  229. $bestlink=bestlink($lpage, $link);
  230. }
  231. else {
  232. $bestlink="$lpage/".lc($link);
  233. }
  234. $linktext=pagetitle(basename($link)) unless defined $linktext;
  235. return $linktext if length $bestlink && $page eq $bestlink;
  236. # TODO BUG: %renderedfiles may not have it, if the linked to page
  237. # was also added and isn't yet rendered! Note that this bug is
  238. # masked by the bug that makes all new files be rendered twice.
  239. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  240. $bestlink=htmlpage($bestlink);
  241. }
  242. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  243. return "<span><a href=\"".
  244. cgiurl(do => "create", page => $link, from => $page).
  245. "\">?</a>$linktext</span>"
  246. }
  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 misctemplate ($$) { #{{{
  325. my $title=shift;
  326. my $pagebody=shift;
  327. my $template=HTML::Template->new(
  328. filename => "$config{templatedir}/misc.tmpl"
  329. );
  330. $template->param(
  331. title => $title,
  332. indexlink => indexlink(),
  333. wikiname => $config{wikiname},
  334. pagebody => $pagebody,
  335. styleurl => styleurl(),
  336. baseurl => "$config{url}/",
  337. );
  338. return $template->output;
  339. }#}}}
  340. sub glob_match ($$) { #{{{
  341. my $page=shift;
  342. my $glob=shift;
  343. if ($glob =~ /^link\((.+)\)$/) {
  344. my $rev = $links{$page} or return undef;
  345. foreach my $p (@$rev) {
  346. return 1 if lc $p eq $1;
  347. }
  348. return 0;
  349. } elsif ($glob =~ /^backlink\((.+)\)$/) {
  350. my $rev = $links{$1} or return undef;
  351. foreach my $p (@$rev) {
  352. return 1 if lc $p eq $page;
  353. }
  354. return 0;
  355. } else {
  356. # turn glob into safe regexp
  357. $glob=quotemeta($glob);
  358. $glob=~s/\\\*/.*/g;
  359. $glob=~s/\\\?/./g;
  360. $glob=~s!\\/!/!g;
  361. return $page=~/^$glob$/i;
  362. }
  363. } #}}}
  364. sub globlist_match ($$) { #{{{
  365. my $page=shift;
  366. my @globlist=split(" ", shift);
  367. # check any negated globs first
  368. foreach my $glob (@globlist) {
  369. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  370. }
  371. foreach my $glob (@globlist) {
  372. return 1 if glob_match($page, $glob);
  373. }
  374. return 0;
  375. } #}}}
  376. sub hook (@) { # {{{
  377. my %param=@_;
  378. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  379. error "hook requires type, call, and id parameters";
  380. }
  381. $hooks{$param{type}}{$param{id}}=\%param;
  382. } # }}}
  383. 1