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