9 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
12 $blosxom::version="is a proper perl module too much to ask?";
13 do "/usr/bin/markdown";
16 my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles,
18 my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
19 my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
20 my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.)!;
23 my $default_pagetype=".mdwn";
29 die "usage: ikiwiki [options] source dest\n";
34 print "Content-type: text/html\n\n";
44 print "@_\n" if $verbose;
50 return (stat($page))[9];
53 sub possibly_foolish_untaint ($) {
55 my ($untainted)=$tainted=~/(.*)/;
76 if ($page =~ /\.mdwn$/) {
87 my $type=pagetype($file);
89 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
103 open (IN, "$file") || error("failed to read $file: $!");
113 my $dir=dirname($file);
116 foreach my $s (split(m!/+!, $dir)) {
119 mkdir($d) || error("failed to create directory $d: $!");
124 open (OUT, ">$file") || error("failed to write $file: $!");
133 while ($content =~ /$wiki_link_regexp/g) {
139 # Given a page and the text of a link on the page, determine which existing
140 # page that link best points to. Prefers pages under a subdirectory with
141 # the same name as the source page, failing that goes down the directory tree
142 # to the base looking for matching pages.
150 $l.="/" if length $l;
153 if (exists $links{$l}) {
154 #debug("for $page, \"$link\", use $l");
157 } while $cwd=~s!/?[^/]+$!!;
159 #print STDERR "warning: page $page, broken link: $link\n";
163 sub isinlinableimage ($) {
166 $file=~/\.(png|gif|jpg|jpeg)$/;
173 my $bestlink=bestlink($page, $link);
175 return $link if $page eq $bestlink;
177 # TODO BUG: %renderedfiles may not have it, if the linked to page
178 # was also added and isn't yet rendered! Note that this bug is
179 # masked by the bug mentioned below that makes all new files
181 if (! grep { $_ eq $bestlink } values %renderedfiles) {
182 $bestlink=htmlpage($bestlink);
184 if (! grep { $_ eq $bestlink } values %renderedfiles) {
185 return "<a href=\"?\">?</a>$link"
188 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
190 if (isinlinableimage($bestlink)) {
191 return "<img src=\"$bestlink\">";
193 return "<a href=\"$bestlink\">$link</a>";
200 $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
209 if ($type eq '.mdwn') {
210 return Markdown::Markdown($content);
213 error("htmlization of $type not supported");
222 foreach my $p (keys %links) {
223 next if bestlink($page, $p) eq $page;
224 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
225 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
227 # Trim common dir prefixes from both pages.
229 my $page_trimmed=$page;
231 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
233 $p_trimmed=~s/^\Q$dir\E// &&
234 $page_trimmed=~s/^\Q$dir\E//;
236 push @links, "<a href=\"$href\">$p_trimmed</a>";
240 $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
248 my $title=basename($page);
253 foreach my $dir (reverse split("/", $page)) {
254 if (length($pagelink)) {
255 $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
262 $path=~s/\.\.\/$/index.html/;
263 $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
265 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
266 "<h1>$pagelink</h1>\n".
268 "</body>\n</html>\n";
276 my $type=pagetype($file);
277 my $content=readfile("$srcdir/$file");
278 if ($type ne 'unknown') {
279 my $page=pagename($file);
281 $links{$page}=[findlinks($content)];
283 $content=linkify($content, $file);
284 $content=htmlize($type, $content);
285 $content=linkbacks($content, $page);
286 $content=finalize($content, $page);
288 writefile("$destdir/".htmlpage($page), $content);
289 $oldpagemtime{$page}=time;
290 $renderedfiles{$page}=htmlpage($page);
294 writefile("$destdir/$file", $content);
295 $oldpagemtime{$file}=time;
296 $renderedfiles{$file}=$file;
301 open (IN, "$srcdir/.index") || return;
303 $_=possibly_foolish_untaint($_);
305 my ($mtime, $file, $rendered, @links)=split(' ', $_);
306 my $page=pagename($file);
307 $pagesources{$page}=$file;
308 $oldpagemtime{$page}=$mtime;
309 $oldlinks{$page}=[@links];
310 $links{$page}=[@links];
311 $renderedfiles{$page}=$rendered;
317 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
318 foreach my $page (keys %oldpagemtime) {
319 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
320 join(" ", @{$links{$page}})."\n"
321 if $oldpagemtime{$page};
327 if (-d "$srcdir/.svn") {
328 if (system("svn", "update", "--quiet", $srcdir) != 0) {
329 warn("svn update failed\n");
337 if (-d "$srcdir/.svn") {
338 if (system("svn", "commit", "--quiet", "-m",
339 possibly_foolish_untaint($message), $srcdir) != 0) {
340 warn("svn commit failed\n");
348 if (-d "$srcdir/.svn") {
349 if (system("svn", "add", "--quiet", $file) != 0) {
350 warn("svn add failed\n");
359 my $dir=dirname($file);
360 while (rmdir($dir)) {
366 # Find existing pages.
372 if (/$wiki_file_prune_regexp/) {
373 $File::Find::prune=1;
375 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
376 my ($f)=/$wiki_file_regexp/; # untaint
378 warn("skipping bad filename $_\n");
381 $f=~s/^\Q$srcdir\E\/?//;
383 $exists{pagename($f)}=1;
391 # check for added or removed pages
393 foreach my $file (@files) {
394 my $page=pagename($file);
395 if (! $oldpagemtime{$page}) {
396 debug("new page $page");
399 $pagesources{$page}=$file;
403 foreach my $page (keys %oldpagemtime) {
404 if (! $exists{$page}) {
405 debug("removing old page $page");
406 push @del, $renderedfiles{$page};
407 prune($destdir."/".$renderedfiles{$page});
408 delete $renderedfiles{$page};
409 $oldpagemtime{$page}=0;
410 delete $pagesources{$page};
414 # render any updated files
415 foreach my $file (@files) {
416 my $page=pagename($file);
418 if (! exists $oldpagemtime{$page} ||
419 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
420 debug("rendering changed file $file");
426 # if any files were added or removed, check to see if each page
427 # needs an update due to linking to them
428 # TODO: inefficient; pages may get rendered above and again here;
429 # problem is the bestlink may have changed and we won't know until
432 FILE: foreach my $file (@files) {
433 my $page=pagename($file);
434 foreach my $f (@add, @del) {
436 foreach my $link (@{$links{$page}}) {
437 if (bestlink($page, $link) eq $p) {
438 debug("rendering $file, which links to $p");
448 # handle linkbacks; if a page has added/removed links, update the
450 # TODO: inefficient; pages may get rendered above and again here;
451 # problem is the linkbacks could be wrong in the first pass render
455 foreach my $file (keys %rendered, @del) {
456 my $page=pagename($file);
457 if (exists $links{$page}) {
458 foreach my $link (@{$links{$page}}) {
459 $link=bestlink($page, $link);
461 ! exists $oldlinks{$page} ||
462 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
463 $linkchanged{$link}=1;
467 if (exists $oldlinks{$page}) {
468 foreach my $link (@{$oldlinks{$page}}) {
469 $link=bestlink($page, $link);
471 ! exists $links{$page} ||
472 ! grep { $_ eq $link } @{$links{$page}}) {
473 $linkchanged{$link}=1;
478 foreach my $link (keys %linkchanged) {
479 my $linkfile=$pagesources{$link};
480 if (defined $linkfile) {
481 debug("rendering $linkfile, to update its linkbacks");
488 # Generates a C wrapper program for running ikiwiki in a specific way.
489 # The wrapper may be safely made suid.
490 sub gen_wrapper ($$) {
491 my ($svn, $rebuild)=@_;
493 eval {use Cwd 'abs_path'};
494 $srcdir=abs_path($srcdir);
495 $destdir=abs_path($destdir);
496 my $this=abs_path($0);
498 error("$this doesn't seem to be executable");
501 my @params=($srcdir, $destdir, "--wikiname=$wikiname");
502 push @params, "--verbose" if $verbose;
503 push @params, "--rebuild" if $rebuild;
504 push @params, "--nosvn" if !$svn;
505 push @params, "--cgi" if $cgi;
506 push @params, "--url=$url" if $url;
507 my $params=join(" ", @params);
509 foreach my $p ($this, $this, @params) {
515 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
516 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
518 foreach my $var (@envsave) {
520 if ((s=getenv("$var")))
521 asprintf(&newenviron[i++], "%s=%s", "$var", s);
525 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
527 /* A wrapper for ikiwiki, can be safely made suid. */
534 extern char **environ;
536 int main (int argc, char **argv) {
537 /* Sanitize environment. */
539 char *newenviron[$#envsave+3];
542 newenviron[i++]="HOME=$ENV{HOME}";
546 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
547 printf("$params\\n");
552 perror("failed to run $this");
557 if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
558 error("failed to compile ikiwiki-wrap.c");
560 unlink("ikiwiki-wrap.c");
561 print "successfully generated ikiwiki-wrap\n";
569 my $do=$q->param('do');
570 if (! defined $do || ! length $do) {
571 error("\"do\" parameter missing");
574 my ($page)=$q->param('page')=~/$wiki_file_regexp/; # untaint
575 if (! defined $page || ! length $page || $page ne $q->param('page') ||
576 $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
577 error("bad page name");
580 my $action=$q->request_uri;
585 if (exists $pagesources{lc($page)}) {
586 $content=readfile("$srcdir/$pagesources{lc($page)}");
587 $content=~s/\n/\r\n/g;
589 $q->param("do", "save");
591 $q->start_html("$wikiname: Editing $page"),
592 $q->h1("$wikiname: Editing $page"),
593 $q->start_form(-action => $action),
596 $q->textarea(-name => 'content',
597 -default => $content,
601 "Optional comment about this change",
603 $q->textfield(-name => "comments", -size => 80),
605 $q->submit("Save Changes"),
609 elsif ($do eq 'save') {
610 my $file=$page.$default_pagetype;
612 if (exists $pagesources{lc($page)}) {
613 $file=$pagesources{lc($page)};
617 my $content=$q->param('content');
618 $content=~s/\r\n/\n/g;
620 writefile("$srcdir/$file", $content);
622 my $message="web commit from $ENV{REMOTE_ADDR}";
623 if (defined $q->param('comments')) {
624 $message.=": ".$q->param('comments');
631 # presumably the commit will trigger an update
633 rcs_commit($message);
639 print $q->redirect("$url/".htmlpage($page));
642 error("unknown do parameter");
648 if (grep /^-/, @ARGV) {
649 eval {use Getopt::Long};
651 "wikiname=s" => \$wikiname,
652 "verbose|v" => \$verbose,
653 "rebuild" => \$rebuild,
654 "wrapper" => \$wrapper,
660 usage() unless @ARGV == 2;
661 ($srcdir) = possibly_foolish_untaint(shift);
662 ($destdir) = possibly_foolish_untaint(shift);
664 if ($cgi && ! length $url) {
665 error("Must specify url to wiki with --url when using --cgi");
668 gen_wrapper($svn, $rebuild) if $wrapper;
671 loadindex() unless $rebuild;
676 rcs_update() if $svn;