summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 4c815dfa3ca0013a2d59866f48d8b86d72e49295 (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 %items;
  273. $items{link}=[];
  274. foreach my $i (split(/ /, $_)) {
  275. my ($item, $val)=split(/=/, $i, 2);
  276. push @{$items{$item}}, $val;
  277. }
  278. my $page=pagename($items{src}[0]);
  279. $pagesources{$page}=$items{src}[0];
  280. $oldpagemtime{$page}=$items{mtime}[0];
  281. $oldlinks{$page}=[@{$items{link}}];
  282. $links{$page}=[@{$items{link}}];
  283. $renderedfiles{$page}=$items{dest}[0];
  284. }
  285. close IN;
  286. } #}}}
  287. sub saveindex () { #{{{
  288. if (! -d $config{wikistatedir}) {
  289. mkdir($config{wikistatedir});
  290. }
  291. open (OUT, ">$config{wikistatedir}/index") ||
  292. error("cannot write to $config{wikistatedir}/index: $!");
  293. foreach my $page (keys %oldpagemtime) {
  294. my $line="mtime=$oldpagemtime{$page} ".
  295. "src=$pagesources{$page} ".
  296. "dest=$renderedfiles{$page}";
  297. if ($oldpagemtime{$page}) {
  298. $line.=" link=$_" foreach @{$links{$page}};
  299. }
  300. print OUT $line."\n";
  301. }
  302. close OUT;
  303. } #}}}
  304. sub misctemplate ($$) { #{{{
  305. my $title=shift;
  306. my $pagebody=shift;
  307. my $template=HTML::Template->new(
  308. filename => "$config{templatedir}/misc.tmpl"
  309. );
  310. $template->param(
  311. title => $title,
  312. indexlink => indexlink(),
  313. wikiname => $config{wikiname},
  314. pagebody => $pagebody,
  315. );
  316. return $template->output;
  317. }#}}}
  318. sub userinfo_get ($$) { #{{{
  319. my $user=shift;
  320. my $field=shift;
  321. eval q{use Storable};
  322. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  323. if (! defined $userdata || ! ref $userdata ||
  324. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  325. ! exists $userdata->{$user}->{$field}) {
  326. return "";
  327. }
  328. return $userdata->{$user}->{$field};
  329. } #}}}
  330. sub userinfo_set ($$$) { #{{{
  331. my $user=shift;
  332. my $field=shift;
  333. my $value=shift;
  334. eval q{use Storable};
  335. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  336. if (! defined $userdata || ! ref $userdata ||
  337. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  338. return "";
  339. }
  340. $userdata->{$user}->{$field}=$value;
  341. my $oldmask=umask(077);
  342. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  343. umask($oldmask);
  344. return $ret;
  345. } #}}}
  346. sub userinfo_setall ($$) { #{{{
  347. my $user=shift;
  348. my $info=shift;
  349. eval q{use Storable};
  350. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  351. if (! defined $userdata || ! ref $userdata) {
  352. $userdata={};
  353. }
  354. $userdata->{$user}=$info;
  355. my $oldmask=umask(077);
  356. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  357. umask($oldmask);
  358. return $ret;
  359. } #}}}
  360. sub is_admin ($) { #{{{
  361. my $user_name=shift;
  362. return grep { $_ eq $user_name } @{$config{adminuser}};
  363. } #}}}
  364. sub glob_match ($$) { #{{{
  365. my $page=shift;
  366. my $glob=shift;
  367. # turn glob into safe regexp
  368. $glob=quotemeta($glob);
  369. $glob=~s/\\\*/.*/g;
  370. $glob=~s/\\\?/./g;
  371. $glob=~s!\\/!/!g;
  372. $page=~/^$glob$/i;
  373. } #}}}
  374. sub globlist_match ($$) { #{{{
  375. my $page=shift;
  376. my @globlist=split(" ", shift);
  377. # check any negated globs first
  378. foreach my $glob (@globlist) {
  379. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  380. }
  381. foreach my $glob (@globlist) {
  382. return 1 if glob_match($page, $glob);
  383. }
  384. return 0;
  385. } #}}}
  386. sub main () { #{{{
  387. getconfig();
  388. if ($config{setup}) {
  389. require IkiWiki::Setup;
  390. setup();
  391. }
  392. elsif ($config{wrapper}) {
  393. lockwiki();
  394. require IkiWiki::Wrapper;
  395. gen_wrapper();
  396. }
  397. elsif ($config{cgi}) {
  398. lockwiki();
  399. require IkiWiki::CGI;
  400. cgi();
  401. }
  402. else {
  403. lockwiki();
  404. loadindex() unless $config{rebuild};
  405. require IkiWiki::Render;
  406. rcs_update();
  407. refresh();
  408. saveindex();
  409. }
  410. } #}}}
  411. main;