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