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