summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 81a72d43d2e25a55984c39362eb87671db75b0e4 (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. } #}}}
  43. sub checkconfig () { #{{{
  44. if ($config{cgi} && ! length $config{url}) {
  45. error("Must specify url to wiki with --url when using --cgi\n");
  46. }
  47. if ($config{rss} && ! length $config{url}) {
  48. error("Must specify url to wiki with --url when using --rss\n");
  49. }
  50. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  51. unless exists $config{wikistatedir};
  52. if ($config{rcs}) {
  53. eval qq{require IkiWiki::Rcs::$config{rcs}};
  54. if ($@) {
  55. error("Failed to load RCS module IkiWiki::Rcs::$config{rcs}: $@");
  56. }
  57. }
  58. else {
  59. require IkiWiki::Rcs::Stub;
  60. }
  61. foreach my $plugin (@{$config{plugin}}) {
  62. my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
  63. eval qq{use $mod};
  64. if ($@) {
  65. error("Failed to load plugin $mod: $@");
  66. }
  67. }
  68. if (exists $hooks{checkconfig}) {
  69. foreach my $id (keys %{$hooks{checkconfig}}) {
  70. $hooks{checkconfig}{$id}{call}->();
  71. }
  72. }
  73. } #}}}
  74. sub error ($) { #{{{
  75. if ($config{cgi}) {
  76. print "Content-type: text/html\n\n";
  77. print misctemplate("Error", "<p>Error: @_</p>");
  78. }
  79. die @_;
  80. } #}}}
  81. sub debug ($) { #{{{
  82. return unless $config{verbose};
  83. if (! $config{cgi}) {
  84. print "@_\n";
  85. }
  86. else {
  87. print STDERR "@_\n";
  88. }
  89. } #}}}
  90. sub possibly_foolish_untaint ($) { #{{{
  91. my $tainted=shift;
  92. my ($untainted)=$tainted=~/(.*)/;
  93. return $untainted;
  94. } #}}}
  95. sub basename ($) { #{{{
  96. my $file=shift;
  97. $file=~s!.*/+!!;
  98. return $file;
  99. } #}}}
  100. sub dirname ($) { #{{{
  101. my $file=shift;
  102. $file=~s!/*[^/]+$!!;
  103. return $file;
  104. } #}}}
  105. sub pagetype ($) { #{{{
  106. my $page=shift;
  107. if ($page =~ /\.mdwn$/) {
  108. return ".mdwn";
  109. }
  110. else {
  111. return "unknown";
  112. }
  113. } #}}}
  114. sub pagename ($) { #{{{
  115. my $file=shift;
  116. my $type=pagetype($file);
  117. my $page=$file;
  118. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  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 $page=shift;
  221. my $link=shift;
  222. my $noimageinline=shift; # don't turn links into inline html images
  223. my $forcesubpage=shift; # force a link to a subpage
  224. my $linktext=shift; # set to force the link text to something
  225. my $bestlink;
  226. if (! $forcesubpage) {
  227. $bestlink=bestlink($page, $link);
  228. }
  229. else {
  230. $bestlink="$page/".lc($link);
  231. }
  232. $linktext=pagetitle(basename($link)) unless defined $linktext;
  233. return $linktext if length $bestlink && $page eq $bestlink;
  234. # TODO BUG: %renderedfiles may not have it, if the linked to page
  235. # was also added and isn't yet rendered! Note that this bug is
  236. # masked by the bug mentioned below that makes all new files
  237. # 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. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  247. if (! $noimageinline && isinlinableimage($bestlink)) {
  248. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  249. }
  250. return "<a href=\"$bestlink\">$linktext</a>";
  251. } #}}}
  252. sub indexlink () { #{{{
  253. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  254. } #}}}
  255. sub lockwiki () { #{{{
  256. # Take an exclusive lock on the wiki to prevent multiple concurrent
  257. # run issues. The lock will be dropped on program exit.
  258. if (! -d $config{wikistatedir}) {
  259. mkdir($config{wikistatedir});
  260. }
  261. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  262. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  263. if (! flock(WIKILOCK, 2 | 4)) {
  264. debug("wiki seems to be locked, waiting for lock");
  265. my $wait=600; # arbitrary, but don't hang forever to
  266. # prevent process pileup
  267. for (1..600) {
  268. return if flock(WIKILOCK, 2 | 4);
  269. sleep 1;
  270. }
  271. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  272. }
  273. } #}}}
  274. sub unlockwiki () { #{{{
  275. close WIKILOCK;
  276. } #}}}
  277. sub loadindex () { #{{{
  278. open (IN, "$config{wikistatedir}/index") || return;
  279. while (<IN>) {
  280. $_=possibly_foolish_untaint($_);
  281. chomp;
  282. my %items;
  283. $items{link}=[];
  284. foreach my $i (split(/ /, $_)) {
  285. my ($item, $val)=split(/=/, $i, 2);
  286. push @{$items{$item}}, $val;
  287. }
  288. next unless exists $items{src}; # skip bad lines for now
  289. my $page=pagename($items{src}[0]);
  290. if (! $config{rebuild}) {
  291. $pagesources{$page}=$items{src}[0];
  292. $oldpagemtime{$page}=$items{mtime}[0];
  293. $oldlinks{$page}=[@{$items{link}}];
  294. $links{$page}=[@{$items{link}}];
  295. $depends{$page}=join(" ", @{$items{depends}})
  296. if exists $items{depends};
  297. $renderedfiles{$page}=$items{dest}[0];
  298. }
  299. $pagectime{$page}=$items{ctime}[0];
  300. }
  301. close IN;
  302. } #}}}
  303. sub saveindex () { #{{{
  304. if (! -d $config{wikistatedir}) {
  305. mkdir($config{wikistatedir});
  306. }
  307. open (OUT, ">$config{wikistatedir}/index") ||
  308. error("cannot write to $config{wikistatedir}/index: $!");
  309. foreach my $page (keys %oldpagemtime) {
  310. next unless $oldpagemtime{$page};
  311. my $line="mtime=$oldpagemtime{$page} ".
  312. "ctime=$pagectime{$page} ".
  313. "src=$pagesources{$page} ".
  314. "dest=$renderedfiles{$page}";
  315. $line.=" link=$_" foreach @{$links{$page}};
  316. if (exists $depends{$page}) {
  317. $line.=" depends=$_" foreach split " ", $depends{$page};
  318. }
  319. print OUT $line."\n";
  320. }
  321. close OUT;
  322. } #}}}
  323. sub misctemplate ($$) { #{{{
  324. my $title=shift;
  325. my $pagebody=shift;
  326. my $template=HTML::Template->new(
  327. filename => "$config{templatedir}/misc.tmpl"
  328. );
  329. $template->param(
  330. title => $title,
  331. indexlink => indexlink(),
  332. wikiname => $config{wikiname},
  333. pagebody => $pagebody,
  334. styleurl => styleurl(),
  335. baseurl => "$config{url}/",
  336. );
  337. return $template->output;
  338. }#}}}
  339. sub glob_match ($$) { #{{{
  340. my $page=shift;
  341. my $glob=shift;
  342. # turn glob into safe regexp
  343. $glob=quotemeta($glob);
  344. $glob=~s/\\\*/.*/g;
  345. $glob=~s/\\\?/./g;
  346. $glob=~s!\\/!/!g;
  347. $page=~/^$glob$/i;
  348. } #}}}
  349. sub globlist_match ($$) { #{{{
  350. my $page=shift;
  351. my @globlist=split(" ", shift);
  352. # check any negated globs first
  353. foreach my $glob (@globlist) {
  354. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  355. }
  356. foreach my $glob (@globlist) {
  357. return 1 if glob_match($page, $glob);
  358. }
  359. return 0;
  360. } #}}}
  361. sub hook (@) { # {{{
  362. my %param=@_;
  363. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  364. error "hook requires type, call, and id parameters";
  365. }
  366. $hooks{$param{type}}{$param{id}}=\%param;
  367. } # }}}
  368. 1