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