summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 1ac0ce9c0a0cf5880b9713236610cccef829d10e (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 getconfig () { #{{{
  11. if (! exists $ENV{WRAPPED_OPTIONS}) {
  12. %config=(
  13. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
  14. wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
  15. wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
  16. verbose => 0,
  17. wikiname => "wiki",
  18. default_pageext => ".mdwn",
  19. cgi => 0,
  20. svn => 1,
  21. url => '',
  22. cgiurl => '',
  23. historyurl => '',
  24. diffurl => '',
  25. anonok => 0,
  26. rebuild => 0,
  27. wrapper => undef,
  28. wrappermode => undef,
  29. srcdir => undef,
  30. destdir => undef,
  31. templatedir => "/usr/share/ikiwiki/templates",
  32. setup => undef,
  33. adminuser => undef,
  34. );
  35. eval q{use Getopt::Long};
  36. GetOptions(
  37. "setup|s=s" => \$config{setup},
  38. "wikiname=s" => \$config{wikiname},
  39. "verbose|v!" => \$config{verbose},
  40. "rebuild!" => \$config{rebuild},
  41. "wrappermode=i" => \$config{wrappermode},
  42. "svn!" => \$config{svn},
  43. "anonok!" => \$config{anonok},
  44. "cgi!" => \$config{cgi},
  45. "url=s" => \$config{url},
  46. "cgiurl=s" => \$config{cgiurl},
  47. "historyurl=s" => \$config{historyurl},
  48. "diffurl=s" => \$config{diffurl},
  49. "exclude=s@" => sub {
  50. $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
  51. },
  52. "adminuser=s@" => sub {
  53. push @{$config{adminuser}}, $_[1]
  54. },
  55. "templatedir=s" => sub {
  56. $config{templatedir}=possibly_foolish_untaint($_[1])
  57. },
  58. "wrapper:s" => sub {
  59. $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
  60. },
  61. ) || usage();
  62. if (! $config{setup}) {
  63. usage() unless @ARGV == 2;
  64. $config{srcdir} = possibly_foolish_untaint(shift);
  65. $config{destdir} = possibly_foolish_untaint(shift);
  66. checkconfig();
  67. }
  68. }
  69. else {
  70. # wrapper passes a full config structure in the environment
  71. # variable
  72. eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
  73. checkconfig();
  74. }
  75. } #}}}
  76. sub checkconfig () { #{{{
  77. if ($config{cgi} && ! length $config{url}) {
  78. error("Must specify url to wiki with --url when using --cgi");
  79. }
  80. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  81. unless exists $config{wikistatedir};
  82. if ($config{svn}) {
  83. require IkiWiki::RCS::SVN;
  84. $config{rcs}=1;
  85. }
  86. else {
  87. require IkiWiki::RCS::Stub;
  88. $config{rcs}=0;
  89. }
  90. } #}}}
  91. sub error ($) { #{{{
  92. if ($config{cgi}) {
  93. print "Content-type: text/html\n\n";
  94. print misctemplate("Error", "<p>Error: @_</p>");
  95. }
  96. die @_;
  97. } #}}}
  98. sub usage () { #{{{
  99. die "usage: ikiwiki [options] source dest\n";
  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;