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