summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 7f6480e0c7438b561c644872e685c29477aea1d7 (plain)
  1. #!/usr/bin/perl -T
  2. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  3. use lib '.'; # For use without installation, removed by Makefile.
  4. package IkiWiki;
  5. use warnings;
  6. use strict;
  7. use File::Spec;
  8. use HTML::Template;
  9. use vars qw{%config %links %oldlinks %oldpagemtime %renderedfiles %pagesources};
  10. # Holds global config settings, also used by some modules.
  11. our %config=( #{{{
  12. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
  13. wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
  14. wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
  15. verbose => 0,
  16. wikiname => "wiki",
  17. default_pageext => ".mdwn",
  18. cgi => 0,
  19. svn => 1,
  20. url => '',
  21. cgiurl => '',
  22. historyurl => '',
  23. diffurl => '',
  24. anonok => 0,
  25. rebuild => 0,
  26. wrapper => undef,
  27. wrappermode => undef,
  28. srcdir => undef,
  29. destdir => undef,
  30. templatedir => "/usr/share/ikiwiki/templates",
  31. setup => undef,
  32. adminuser => undef,
  33. ); #}}}
  34. # option parsing #{{{
  35. if (! exists $ENV{WRAPPED_OPTIONS}) {
  36. eval q{use Getopt::Long};
  37. GetOptions(
  38. "setup|s=s" => \$config{setup},
  39. "wikiname=s" => \$config{wikiname},
  40. "verbose|v!" => \$config{verbose},
  41. "rebuild!" => \$config{rebuild},
  42. "wrapper:s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
  43. "wrappermode=i" => \$config{wrappermode},
  44. "svn!" => \$config{svn},
  45. "anonok!" => \$config{anonok},
  46. "cgi!" => \$config{cgi},
  47. "url=s" => \$config{url},
  48. "cgiurl=s" => \$config{cgiurl},
  49. "historyurl=s" => \$config{historyurl},
  50. "diffurl=s" => \$config{diffurl},
  51. "exclude=s@" => sub {
  52. $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
  53. },
  54. "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
  55. "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
  56. ) || usage();
  57. if (! $config{setup}) {
  58. usage() unless @ARGV == 2;
  59. $config{srcdir} = possibly_foolish_untaint(shift);
  60. $config{destdir} = possibly_foolish_untaint(shift);
  61. checkoptions();
  62. }
  63. }
  64. else {
  65. # wrapper passes a full config structure in the environment
  66. # variable
  67. eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
  68. checkoptions();
  69. }
  70. #}}}
  71. sub checkoptions { #{{{
  72. if ($config{cgi} && ! length $config{url}) {
  73. error("Must specify url to wiki with --url when using --cgi");
  74. }
  75. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  76. unless exists $config{wikistatedir};
  77. if ($config{svn}) {
  78. require IkiWiki::RCS::SVN;
  79. $config{rcs}=1;
  80. }
  81. else {
  82. require IkiWiki::RCS::Stub;
  83. $config{rcs}=0;
  84. }
  85. } #}}}
  86. sub usage { #{{{
  87. die "usage: ikiwiki [options] source dest\n";
  88. } #}}}
  89. sub error { #{{{
  90. if ($config{cgi}) {
  91. print "Content-type: text/html\n\n";
  92. print misctemplate("Error", "<p>Error: @_</p>");
  93. }
  94. die @_;
  95. } #}}}
  96. sub debug ($) { #{{{
  97. return unless $config{verbose};
  98. if (! $config{cgi}) {
  99. print "@_\n";
  100. }
  101. else {
  102. print STDERR "@_\n";
  103. }
  104. } #}}}
  105. sub possibly_foolish_untaint { #{{{
  106. my $tainted=shift;
  107. my ($untainted)=$tainted=~/(.*)/;
  108. return $untainted;
  109. } #}}}
  110. sub basename ($) { #{{{
  111. my $file=shift;
  112. $file=~s!.*/!!;
  113. return $file;
  114. } #}}}
  115. sub dirname ($) { #{{{
  116. my $file=shift;
  117. $file=~s!/?[^/]+$!!;
  118. return $file;
  119. } #}}}
  120. sub pagetype ($) { #{{{
  121. my $page=shift;
  122. if ($page =~ /\.mdwn$/) {
  123. return ".mdwn";
  124. }
  125. else {
  126. return "unknown";
  127. }
  128. } #}}}
  129. sub pagename ($) { #{{{
  130. my $file=shift;
  131. my $type=pagetype($file);
  132. my $page=$file;
  133. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  134. return $page;
  135. } #}}}
  136. sub htmlpage ($) { #{{{
  137. my $page=shift;
  138. return $page.".html";
  139. } #}}}
  140. sub readfile ($) { #{{{
  141. my $file=shift;
  142. if (-l $file) {
  143. error("cannot read a symlink ($file)");
  144. }
  145. local $/=undef;
  146. open (IN, "$file") || error("failed to read $file: $!");
  147. my $ret=<IN>;
  148. close IN;
  149. return $ret;
  150. } #}}}
  151. sub writefile ($$) { #{{{
  152. my $file=shift;
  153. my $content=shift;
  154. if (-l $file) {
  155. error("cannot write to a symlink ($file)");
  156. }
  157. my $dir=dirname($file);
  158. if (! -d $dir) {
  159. my $d="";
  160. foreach my $s (split(m!/+!, $dir)) {
  161. $d.="$s/";
  162. if (! -d $d) {
  163. mkdir($d) || error("failed to create directory $d: $!");
  164. }
  165. }
  166. }
  167. open (OUT, ">$file") || error("failed to write $file: $!");
  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)$/;
  195. } #}}}
  196. sub htmllink { #{{{
  197. my $page=shift;
  198. my $link=shift;
  199. my $noimageinline=shift; # don't turn links into inline html images
  200. my $forcesubpage=shift; # force a link to a subpage
  201. my $bestlink;
  202. if (! $forcesubpage) {
  203. $bestlink=bestlink($page, $link);
  204. }
  205. else {
  206. $bestlink="$page/".lc($link);
  207. }
  208. return $link if length $bestlink && $page eq $bestlink;
  209. # TODO BUG: %renderedfiles may not have it, if the linked to page
  210. # was also added and isn't yet rendered! Note that this bug is
  211. # masked by the bug mentioned below that makes all new files
  212. # be rendered twice.
  213. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  214. $bestlink=htmlpage($bestlink);
  215. }
  216. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  217. return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
  218. }
  219. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  220. if (! $noimageinline && isinlinableimage($bestlink)) {
  221. return "<img src=\"$bestlink\">";
  222. }
  223. return "<a href=\"$bestlink\">$link</a>";
  224. } #}}}
  225. sub indexlink () { #{{{
  226. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  227. } #}}}
  228. sub lockwiki () { #{{{
  229. # Take an exclusive lock on the wiki to prevent multiple concurrent
  230. # run issues. The lock will be dropped on program exit.
  231. if (! -d $config{wikistatedir}) {
  232. mkdir($config{wikistatedir});
  233. }
  234. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  235. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  236. if (! flock(WIKILOCK, 2 | 4)) {
  237. debug("wiki seems to be locked, waiting for lock");
  238. my $wait=600; # arbitrary, but don't hang forever to
  239. # prevent process pileup
  240. for (1..600) {
  241. return if flock(WIKILOCK, 2 | 4);
  242. sleep 1;
  243. }
  244. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  245. }
  246. } #}}}
  247. sub unlockwiki () { #{{{
  248. close WIKILOCK;
  249. } #}}}
  250. sub loadindex () { #{{{
  251. open (IN, "$config{wikistatedir}/index") || return;
  252. while (<IN>) {
  253. $_=possibly_foolish_untaint($_);
  254. chomp;
  255. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  256. my $page=pagename($file);
  257. $pagesources{$page}=$file;
  258. $oldpagemtime{$page}=$mtime;
  259. $oldlinks{$page}=[@links];
  260. $links{$page}=[@links];
  261. $renderedfiles{$page}=$rendered;
  262. }
  263. close IN;
  264. } #}}}
  265. sub saveindex () { #{{{
  266. if (! -d $config{wikistatedir}) {
  267. mkdir($config{wikistatedir});
  268. }
  269. open (OUT, ">$config{wikistatedir}/index") ||
  270. error("cannot write to $config{wikistatedir}/index: $!");
  271. foreach my $page (keys %oldpagemtime) {
  272. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  273. join(" ", @{$links{$page}})."\n"
  274. if $oldpagemtime{$page};
  275. }
  276. close OUT;
  277. } #}}}
  278. sub misctemplate ($$) { #{{{
  279. my $title=shift;
  280. my $pagebody=shift;
  281. my $template=HTML::Template->new(
  282. filename => "$config{templatedir}/misc.tmpl"
  283. );
  284. $template->param(
  285. title => $title,
  286. indexlink => indexlink(),
  287. wikiname => $config{wikiname},
  288. pagebody => $pagebody,
  289. );
  290. return $template->output;
  291. }#}}}
  292. sub userinfo_get ($$) { #{{{
  293. my $user=shift;
  294. my $field=shift;
  295. eval q{use Storable};
  296. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  297. if (! defined $userdata || ! ref $userdata ||
  298. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  299. ! exists $userdata->{$user}->{$field}) {
  300. return "";
  301. }
  302. return $userdata->{$user}->{$field};
  303. } #}}}
  304. sub userinfo_set ($$$) { #{{{
  305. my $user=shift;
  306. my $field=shift;
  307. my $value=shift;
  308. eval q{use Storable};
  309. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  310. if (! defined $userdata || ! ref $userdata ||
  311. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  312. return "";
  313. }
  314. $userdata->{$user}->{$field}=$value;
  315. my $oldmask=umask(077);
  316. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  317. umask($oldmask);
  318. return $ret;
  319. } #}}}
  320. sub userinfo_setall ($$) { #{{{
  321. my $user=shift;
  322. my $info=shift;
  323. eval q{use Storable};
  324. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  325. if (! defined $userdata || ! ref $userdata) {
  326. $userdata={};
  327. }
  328. $userdata->{$user}=$info;
  329. my $oldmask=umask(077);
  330. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  331. umask($oldmask);
  332. return $ret;
  333. } #}}}
  334. sub is_admin ($) { #{{{
  335. my $user_name=shift;
  336. return grep { $_ eq $user_name } @{$config{adminuser}};
  337. } #}}}
  338. sub glob_match ($$) { #{{{
  339. my $page=shift;
  340. my $glob=shift;
  341. # turn glob into safe regexp
  342. $glob=quotemeta($glob);
  343. $glob=~s/\\\*/.*/g;
  344. $glob=~s/\\\?/./g;
  345. $glob=~s!\\/!/!g;
  346. $page=~/^$glob$/i;
  347. } #}}}
  348. sub globlist_match ($$) { #{{{
  349. my $page=shift;
  350. my @globlist=split(" ", shift);
  351. # check any negated globs first
  352. foreach my $glob (@globlist) {
  353. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  354. }
  355. foreach my $glob (@globlist) {
  356. return 1 if glob_match($page, $glob);
  357. }
  358. return 0;
  359. } #}}}
  360. # main {{{
  361. if ($config{setup}) {
  362. require IkiWiki::Setup;
  363. setup();
  364. }
  365. lockwiki();
  366. if ($config{wrapper}) {
  367. require IkiWiki::Wrapper;
  368. gen_wrapper();
  369. exit;
  370. }
  371. loadindex() unless $config{rebuild};
  372. if ($config{cgi}) {
  373. require IkiWiki::CGI;
  374. cgi();
  375. }
  376. else {
  377. require IkiWiki::Render;
  378. rcs_update();
  379. refresh();
  380. saveindex();
  381. }
  382. #}}}