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