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