summaryrefslogtreecommitdiff
path: root/IkiWiki.pm
blob: 76472302e12a9bce6c3989e90969399184a9cafd (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. if (! $binary) {
  141. binmode(IN, ":utf8");
  142. }
  143. else {
  144. binmode(IN);
  145. }
  146. my $ret=<IN>;
  147. close IN;
  148. return $ret;
  149. } #}}}
  150. sub writefile ($$$;$) { #{{{
  151. my $file=shift; # can include subdirs
  152. my $destdir=shift; # directory to put file in
  153. my $content=shift;
  154. my $binary=shift;
  155. my $test=$file;
  156. while (length $test) {
  157. if (-l "$destdir/$test") {
  158. error("cannot write to a symlink ($test)");
  159. }
  160. $test=dirname($test);
  161. }
  162. my $dir=dirname("$destdir/$file");
  163. if (! -d $dir) {
  164. my $d="";
  165. foreach my $s (split(m!/+!, $dir)) {
  166. $d.="$s/";
  167. if (! -d $d) {
  168. mkdir($d) || error("failed to create directory $d: $!");
  169. }
  170. }
  171. }
  172. open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
  173. if (! $binary) {
  174. binmode(OUT, ":utf8");
  175. }
  176. else {
  177. binmode(OUT);
  178. }
  179. print OUT $content;
  180. close OUT;
  181. } #}}}
  182. sub bestlink ($$) { #{{{
  183. # Given a page and the text of a link on the page, determine which
  184. # existing page that link best points to. Prefers pages under a
  185. # subdirectory with the same name as the source page, failing that
  186. # goes down the directory tree to the base looking for matching
  187. # pages.
  188. my $page=shift;
  189. my $link=lc(shift);
  190. my $cwd=$page;
  191. do {
  192. my $l=$cwd;
  193. $l.="/" if length $l;
  194. $l.=$link;
  195. if (exists $links{$l}) {
  196. #debug("for $page, \"$link\", use $l");
  197. return $l;
  198. }
  199. } while $cwd=~s!/?[^/]+$!!;
  200. #print STDERR "warning: page $page, broken link: $link\n";
  201. return "";
  202. } #}}}
  203. sub isinlinableimage ($) { #{{{
  204. my $file=shift;
  205. $file=~/\.(png|gif|jpg|jpeg)$/i;
  206. } #}}}
  207. sub pagetitle ($) { #{{{
  208. my $page=shift;
  209. $page=~s/__(\d+)__/&#$1;/g;
  210. $page=~y/_/ /;
  211. return $page;
  212. } #}}}
  213. sub titlepage ($) { #{{{
  214. my $title=shift;
  215. $title=~y/ /_/;
  216. $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
  217. return $title;
  218. } #}}}
  219. sub cgiurl (@) { #{{{
  220. my %params=@_;
  221. return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
  222. } #}}}
  223. sub styleurl (;$) { #{{{
  224. my $page=shift;
  225. return "$config{url}/style.css" if ! defined $page;
  226. $page=~s/[^\/]+$//;
  227. $page=~s/[^\/]+\//..\//g;
  228. return $page."style.css";
  229. } #}}}
  230. sub htmllink ($$$;$$$) { #{{{
  231. my $lpage=shift; # the page doing the linking
  232. my $page=shift; # the page that will contain the link (different for inline)
  233. my $link=shift;
  234. my $noimageinline=shift; # don't turn links into inline html images
  235. my $forcesubpage=shift; # force a link to a subpage
  236. my $linktext=shift; # set to force the link text to something
  237. my $bestlink;
  238. if (! $forcesubpage) {
  239. $bestlink=bestlink($lpage, $link);
  240. }
  241. else {
  242. $bestlink="$lpage/".lc($link);
  243. }
  244. $linktext=pagetitle(basename($link)) unless defined $linktext;
  245. return $linktext if length $bestlink && $page eq $bestlink;
  246. # TODO BUG: %renderedfiles may not have it, if the linked to page
  247. # was also added and isn't yet rendered! Note that this bug is
  248. # masked by the bug that makes all new files be rendered twice.
  249. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  250. $bestlink=htmlpage($bestlink);
  251. }
  252. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  253. return "<span><a href=\"".
  254. cgiurl(do => "create", page => $link, from => $page).
  255. "\">?</a>$linktext</span>"
  256. }
  257. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  258. if (! $noimageinline && isinlinableimage($bestlink)) {
  259. return "<img src=\"$bestlink\" alt=\"$linktext\" />";
  260. }
  261. return "<a href=\"$bestlink\">$linktext</a>";
  262. } #}}}
  263. sub indexlink () { #{{{
  264. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  265. } #}}}
  266. sub lockwiki () { #{{{
  267. # Take an exclusive lock on the wiki to prevent multiple concurrent
  268. # run issues. The lock will be dropped on program exit.
  269. if (! -d $config{wikistatedir}) {
  270. mkdir($config{wikistatedir});
  271. }
  272. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  273. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  274. if (! flock(WIKILOCK, 2 | 4)) {
  275. debug("wiki seems to be locked, waiting for lock");
  276. my $wait=600; # arbitrary, but don't hang forever to
  277. # prevent process pileup
  278. for (1..600) {
  279. return if flock(WIKILOCK, 2 | 4);
  280. sleep 1;
  281. }
  282. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  283. }
  284. } #}}}
  285. sub unlockwiki () { #{{{
  286. close WIKILOCK;
  287. } #}}}
  288. sub loadindex () { #{{{
  289. open (IN, "$config{wikistatedir}/index") || return;
  290. while (<IN>) {
  291. $_=possibly_foolish_untaint($_);
  292. chomp;
  293. my %items;
  294. $items{link}=[];
  295. foreach my $i (split(/ /, $_)) {
  296. my ($item, $val)=split(/=/, $i, 2);
  297. push @{$items{$item}}, $val;
  298. }
  299. next unless exists $items{src}; # skip bad lines for now
  300. my $page=pagename($items{src}[0]);
  301. if (! $config{rebuild}) {
  302. $pagesources{$page}=$items{src}[0];
  303. $oldpagemtime{$page}=$items{mtime}[0];
  304. $oldlinks{$page}=[@{$items{link}}];
  305. $links{$page}=[@{$items{link}}];
  306. $depends{$page}=join(" ", @{$items{depends}})
  307. if exists $items{depends};
  308. $renderedfiles{$page}=$items{dest}[0];
  309. }
  310. $pagectime{$page}=$items{ctime}[0];
  311. }
  312. close IN;
  313. } #}}}
  314. sub saveindex () { #{{{
  315. if (! -d $config{wikistatedir}) {
  316. mkdir($config{wikistatedir});
  317. }
  318. open (OUT, ">$config{wikistatedir}/index") ||
  319. error("cannot write to $config{wikistatedir}/index: $!");
  320. foreach my $page (keys %oldpagemtime) {
  321. next unless $oldpagemtime{$page};
  322. my $line="mtime=$oldpagemtime{$page} ".
  323. "ctime=$pagectime{$page} ".
  324. "src=$pagesources{$page} ".
  325. "dest=$renderedfiles{$page}";
  326. $line.=" link=$_" foreach @{$links{$page}};
  327. if (exists $depends{$page}) {
  328. $line.=" depends=$_" foreach split " ", $depends{$page};
  329. }
  330. print OUT $line."\n";
  331. }
  332. close OUT;
  333. } #}}}
  334. sub misctemplate ($$) { #{{{
  335. my $title=shift;
  336. my $pagebody=shift;
  337. my $template=HTML::Template->new(
  338. filename => "$config{templatedir}/misc.tmpl"
  339. );
  340. $template->param(
  341. title => $title,
  342. indexlink => indexlink(),
  343. wikiname => $config{wikiname},
  344. pagebody => $pagebody,
  345. styleurl => styleurl(),
  346. baseurl => "$config{url}/",
  347. );
  348. return $template->output;
  349. }#}}}
  350. sub glob_match ($$) { #{{{
  351. my $page=shift;
  352. my $glob=shift;
  353. if ($glob =~ /^link\((.+)\)$/) {
  354. my $rev = $links{$page} or return undef;
  355. foreach my $p (@$rev) {
  356. return 1 if lc $p eq $1;
  357. }
  358. return 0;
  359. } elsif ($glob =~ /^backlink\((.+)\)$/) {
  360. my $rev = $links{$1} or return undef;
  361. foreach my $p (@$rev) {
  362. return 1 if lc $p eq $page;
  363. }
  364. return 0;
  365. } else {
  366. # turn glob into safe regexp
  367. $glob=quotemeta($glob);
  368. $glob=~s/\\\*/.*/g;
  369. $glob=~s/\\\?/./g;
  370. $glob=~s!\\/!/!g;
  371. return $page=~/^$glob$/i;
  372. }
  373. } #}}}
  374. sub globlist_match ($$) { #{{{
  375. my $page=shift;
  376. my @globlist=split(" ", shift);
  377. # check any negated globs first
  378. foreach my $glob (@globlist) {
  379. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  380. }
  381. foreach my $glob (@globlist) {
  382. return 1 if glob_match($page, $glob);
  383. }
  384. return 0;
  385. } #}}}
  386. sub hook (@) { # {{{
  387. my %param=@_;
  388. if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
  389. error "hook requires type, call, and id parameters";
  390. }
  391. $hooks{$param{type}}{$param{id}}=\%param;
  392. } # }}}
  393. 1