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