summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: a2af406a93a692bdc5743b31e95248804521907f (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. 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. $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, "$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, ">$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 misctemplate ($$) { #{{{
  336. my $title=shift;
  337. my $pagebody=shift;
  338. my $template=HTML::Template->new(
  339. filename => "$config{templatedir}/misc.tmpl"
  340. );
  341. $template->param(
  342. title => $title,
  343. indexlink => indexlink(),
  344. wikiname => $config{wikiname},
  345. pagebody => $pagebody,
  346. styleurl => styleurl(),
  347. baseurl => "$config{url}/",
  348. );
  349. return $template->output;
  350. }#}}}
  351. sub glob_match ($$) { #{{{
  352. my $page=shift;
  353. my $glob=shift;
  354. if ($glob =~ /^link\((.+)\)$/) {
  355. my $rev = $links{$page} or return undef;
  356. foreach my $p (@$rev) {
  357. return 1 if lc $p eq $1;
  358. }
  359. return 0;
  360. } elsif ($glob =~ /^backlink\((.+)\)$/) {
  361. my $rev = $links{$1} or return undef;
  362. foreach my $p (@$rev) {
  363. return 1 if lc $p eq $page;
  364. }
  365. return 0;
  366. } else {
  367. # turn glob into safe regexp
  368. $glob=quotemeta($glob);
  369. $glob=~s/\\\*/.*/g;
  370. $glob=~s/\\\?/./g;
  371. $glob=~s!\\/!/!g;
  372. return $page=~/^$glob$/i;
  373. }
  374. } #}}}
  375. sub globlist_match ($$) { #{{{
  376. my $page=shift;
  377. my @globlist=split(" ", shift);
  378. # check any negated globs first
  379. foreach my $glob (@globlist) {
  380. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  381. }
  382. foreach my $glob (@globlist) {
  383. return 1 if glob_match($page, $glob);
  384. }
  385. return 0;
  386. } #}}}
  387. sub hook (@) { # {{{
  388. my %param=@_;
  389. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  390. error "hook requires type, call, and id parameters";
  391. }
  392. $hooks{$param{type}}{$param{id}}=\%param;
  393. } # }}}
  394. 1