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