summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 3540f86675c9d449f41a09b3075b7c17012fd43d (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 pagetitle ($) { #{{{
  202. my $page=shift;
  203. $page=~s/__(\d+)__/&#$1;/g;
  204. $page=~y/_/ /;
  205. return $page;
  206. } #}}}
  207. sub htmllink ($$;$$) { #{{{
  208. my $page=shift;
  209. my $link=shift;
  210. my $noimageinline=shift; # don't turn links into inline html images
  211. my $forcesubpage=shift; # force a link to a subpage
  212. my $bestlink;
  213. if (! $forcesubpage) {
  214. $bestlink=bestlink($page, $link);
  215. }
  216. else {
  217. $bestlink="$page/".lc($link);
  218. }
  219. my $linktext=pagetitle($link);
  220. return $linktext if length $bestlink && $page eq $bestlink;
  221. # TODO BUG: %renderedfiles may not have it, if the linked to page
  222. # was also added and isn't yet rendered! Note that this bug is
  223. # masked by the bug mentioned below that makes all new files
  224. # be rendered twice.
  225. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  226. $bestlink=htmlpage($bestlink);
  227. }
  228. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  229. return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$linktext"
  230. }
  231. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  232. if (! $noimageinline && isinlinableimage($bestlink)) {
  233. return "<img src=\"$bestlink\">";
  234. }
  235. return "<a href=\"$bestlink\">$linktext</a>";
  236. } #}}}
  237. sub indexlink () { #{{{
  238. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  239. } #}}}
  240. sub lockwiki () { #{{{
  241. # Take an exclusive lock on the wiki to prevent multiple concurrent
  242. # run issues. The lock will be dropped on program exit.
  243. if (! -d $config{wikistatedir}) {
  244. mkdir($config{wikistatedir});
  245. }
  246. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  247. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  248. if (! flock(WIKILOCK, 2 | 4)) {
  249. debug("wiki seems to be locked, waiting for lock");
  250. my $wait=600; # arbitrary, but don't hang forever to
  251. # prevent process pileup
  252. for (1..600) {
  253. return if flock(WIKILOCK, 2 | 4);
  254. sleep 1;
  255. }
  256. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  257. }
  258. } #}}}
  259. sub unlockwiki () { #{{{
  260. close WIKILOCK;
  261. } #}}}
  262. sub loadindex () { #{{{
  263. open (IN, "$config{wikistatedir}/index") || return;
  264. while (<IN>) {
  265. $_=possibly_foolish_untaint($_);
  266. chomp;
  267. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  268. my $page=pagename($file);
  269. $pagesources{$page}=$file;
  270. $oldpagemtime{$page}=$mtime;
  271. $oldlinks{$page}=[@links];
  272. $links{$page}=[@links];
  273. $renderedfiles{$page}=$rendered;
  274. }
  275. close IN;
  276. } #}}}
  277. sub saveindex () { #{{{
  278. if (! -d $config{wikistatedir}) {
  279. mkdir($config{wikistatedir});
  280. }
  281. open (OUT, ">$config{wikistatedir}/index") ||
  282. error("cannot write to $config{wikistatedir}/index: $!");
  283. foreach my $page (keys %oldpagemtime) {
  284. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  285. join(" ", @{$links{$page}})."\n"
  286. if $oldpagemtime{$page};
  287. }
  288. close OUT;
  289. } #}}}
  290. sub misctemplate ($$) { #{{{
  291. my $title=shift;
  292. my $pagebody=shift;
  293. my $template=HTML::Template->new(
  294. filename => "$config{templatedir}/misc.tmpl"
  295. );
  296. $template->param(
  297. title => $title,
  298. indexlink => indexlink(),
  299. wikiname => $config{wikiname},
  300. pagebody => $pagebody,
  301. );
  302. return $template->output;
  303. }#}}}
  304. sub userinfo_get ($$) { #{{{
  305. my $user=shift;
  306. my $field=shift;
  307. eval q{use Storable};
  308. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  309. if (! defined $userdata || ! ref $userdata ||
  310. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  311. ! exists $userdata->{$user}->{$field}) {
  312. return "";
  313. }
  314. return $userdata->{$user}->{$field};
  315. } #}}}
  316. sub userinfo_set ($$$) { #{{{
  317. my $user=shift;
  318. my $field=shift;
  319. my $value=shift;
  320. eval q{use Storable};
  321. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  322. if (! defined $userdata || ! ref $userdata ||
  323. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  324. return "";
  325. }
  326. $userdata->{$user}->{$field}=$value;
  327. my $oldmask=umask(077);
  328. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  329. umask($oldmask);
  330. return $ret;
  331. } #}}}
  332. sub userinfo_setall ($$) { #{{{
  333. my $user=shift;
  334. my $info=shift;
  335. eval q{use Storable};
  336. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  337. if (! defined $userdata || ! ref $userdata) {
  338. $userdata={};
  339. }
  340. $userdata->{$user}=$info;
  341. my $oldmask=umask(077);
  342. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  343. umask($oldmask);
  344. return $ret;
  345. } #}}}
  346. sub is_admin ($) { #{{{
  347. my $user_name=shift;
  348. return grep { $_ eq $user_name } @{$config{adminuser}};
  349. } #}}}
  350. sub glob_match ($$) { #{{{
  351. my $page=shift;
  352. my $glob=shift;
  353. # turn glob into safe regexp
  354. $glob=quotemeta($glob);
  355. $glob=~s/\\\*/.*/g;
  356. $glob=~s/\\\?/./g;
  357. $glob=~s!\\/!/!g;
  358. $page=~/^$glob$/i;
  359. } #}}}
  360. sub globlist_match ($$) { #{{{
  361. my $page=shift;
  362. my @globlist=split(" ", shift);
  363. # check any negated globs first
  364. foreach my $glob (@globlist) {
  365. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  366. }
  367. foreach my $glob (@globlist) {
  368. return 1 if glob_match($page, $glob);
  369. }
  370. return 0;
  371. } #}}}
  372. sub main () { #{{{
  373. getconfig();
  374. if ($config{setup}) {
  375. require IkiWiki::Setup;
  376. setup();
  377. }
  378. elsif ($config{wrapper}) {
  379. lockwiki();
  380. require IkiWiki::Wrapper;
  381. gen_wrapper();
  382. }
  383. elsif ($config{cgi}) {
  384. lockwiki();
  385. require IkiWiki::CGI;
  386. cgi();
  387. }
  388. else {
  389. lockwiki();
  390. loadindex() unless $config{rebuild};
  391. require IkiWiki::Render;
  392. rcs_update();
  393. refresh();
  394. saveindex();
  395. }
  396. } #}}}
  397. main;