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/|\.\.|^\.|\/\.|\.html?$)!;
23 my $default_pagetype=".mdwn";
30 die "usage: ikiwiki [options] source dest\n";
35 print "Content-type: text/html\n\n";
45 print "@_\n" if $verbose;
51 return (stat($page))[9];
54 sub possibly_foolish_untaint ($) {
56 my ($untainted)=$tainted=~/(.*)/;
77 if ($page =~ /\.mdwn$/) {
88 my $type=pagetype($file);
90 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
104 open (IN, "$file") || error("failed to read $file: $!");
114 my $dir=dirname($file);
117 foreach my $s (split(m!/+!, $dir)) {
120 mkdir($d) || error("failed to create directory $d: $!");
125 open (OUT, ">$file") || error("failed to write $file: $!");
134 while ($content =~ /$wiki_link_regexp/g) {
140 # Given a page and the text of a link on the page, determine which existing
141 # page that link best points to. Prefers pages under a subdirectory with
142 # the same name as the source page, failing that goes down the directory tree
143 # to the base looking for matching pages.
151 $l.="/" if length $l;
154 if (exists $links{$l}) {
155 #debug("for $page, \"$link\", use $l");
158 } while $cwd=~s!/?[^/]+$!!;
160 #print STDERR "warning: page $page, broken link: $link\n";
164 sub isinlinableimage ($) {
167 $file=~/\.(png|gif|jpg|jpeg)$/;
174 my $bestlink=bestlink($page, $link);
176 return $link if $page eq $bestlink;
178 # TODO BUG: %renderedfiles may not have it, if the linked to page
179 # was also added and isn't yet rendered! Note that this bug is
180 # masked by the bug mentioned below that makes all new files
182 if (! grep { $_ eq $bestlink } values %renderedfiles) {
183 $bestlink=htmlpage($bestlink);
185 if (! grep { $_ eq $bestlink } values %renderedfiles) {
186 return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
189 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
191 if (isinlinableimage($bestlink)) {
192 return "<img src=\"$bestlink\">";
194 return "<a href=\"$bestlink\">$link</a>";
201 $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
210 if ($type eq '.mdwn') {
211 return Markdown::Markdown($content);
214 error("htmlization of $type not supported");
223 foreach my $p (keys %links) {
224 next if bestlink($page, $p) eq $page;
225 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
226 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
228 # Trim common dir prefixes from both pages.
230 my $page_trimmed=$page;
232 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
234 $p_trimmed=~s/^\Q$dir\E// &&
235 $page_trimmed=~s/^\Q$dir\E//;
237 push @links, "<a href=\"$href\">$p_trimmed</a>";
241 $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
249 my $title=basename($page);
254 foreach my $dir (reverse split("/", $page)) {
255 if (length($pagelink)) {
256 $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
263 $path=~s/\.\.\/$/index.html/;
264 $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
267 if (length $cgiurl) {
268 push @actions, "<a href=\"$cgiurl?do=edit&page=$page\">Edit</a>";
269 push @actions, "<a href=\"$cgiurl?do=recentchanges\">RecentChanges</a>";
272 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
273 "<h1>$pagelink</h1>\n".
276 "</body>\n</html>\n";
284 my $type=pagetype($file);
285 my $content=readfile("$srcdir/$file");
286 if ($type ne 'unknown') {
287 my $page=pagename($file);
289 $links{$page}=[findlinks($content)];
291 $content=linkify($content, $file);
292 $content=htmlize($type, $content);
293 $content=linkbacks($content, $page);
294 $content=finalize($content, $page);
296 writefile("$destdir/".htmlpage($page), $content);
297 $oldpagemtime{$page}=time;
298 $renderedfiles{$page}=htmlpage($page);
302 writefile("$destdir/$file", $content);
303 $oldpagemtime{$file}=time;
304 $renderedfiles{$file}=$file;
309 open (IN, "$srcdir/.index") || return;
311 $_=possibly_foolish_untaint($_);
313 my ($mtime, $file, $rendered, @links)=split(' ', $_);
314 my $page=pagename($file);
315 $pagesources{$page}=$file;
316 $oldpagemtime{$page}=$mtime;
317 $oldlinks{$page}=[@links];
318 $links{$page}=[@links];
319 $renderedfiles{$page}=$rendered;
325 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
326 foreach my $page (keys %oldpagemtime) {
327 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
328 join(" ", @{$links{$page}})."\n"
329 if $oldpagemtime{$page};
335 if (-d "$srcdir/.svn") {
336 if (system("svn", "update", "--quiet", $srcdir) != 0) {
337 warn("svn update failed\n");
345 if (-d "$srcdir/.svn") {
346 if (system("svn", "commit", "--quiet", "-m",
347 possibly_foolish_untaint($message), $srcdir) != 0) {
348 warn("svn commit failed\n");
356 if (-d "$srcdir/.svn") {
357 my $parent=dirname($file);
358 while (! -d "$srcdir/$parent/.svn") {
360 $parent=dirname($file);
363 if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
364 warn("svn add failed\n");
373 my $dir=dirname($file);
374 while (rmdir($dir)) {
380 # Find existing pages.
386 if (/$wiki_file_prune_regexp/) {
387 $File::Find::prune=1;
390 my ($f)=/$wiki_file_regexp/; # untaint
392 warn("skipping bad filename $_\n");
395 $f=~s/^\Q$srcdir\E\/?//;
397 $exists{pagename($f)}=1;
405 # check for added or removed pages
407 foreach my $file (@files) {
408 my $page=pagename($file);
409 if (! $oldpagemtime{$page}) {
410 debug("new page $page");
413 $pagesources{$page}=$file;
417 foreach my $page (keys %oldpagemtime) {
418 if (! $exists{$page}) {
419 debug("removing old page $page");
420 push @del, $renderedfiles{$page};
421 prune($destdir."/".$renderedfiles{$page});
422 delete $renderedfiles{$page};
423 $oldpagemtime{$page}=0;
424 delete $pagesources{$page};
428 # render any updated files
429 foreach my $file (@files) {
430 my $page=pagename($file);
432 if (! exists $oldpagemtime{$page} ||
433 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
434 debug("rendering changed file $file");
440 # if any files were added or removed, check to see if each page
441 # needs an update due to linking to them
442 # TODO: inefficient; pages may get rendered above and again here;
443 # problem is the bestlink may have changed and we won't know until
446 FILE: foreach my $file (@files) {
447 my $page=pagename($file);
448 foreach my $f (@add, @del) {
450 foreach my $link (@{$links{$page}}) {
451 if (bestlink($page, $link) eq $p) {
452 debug("rendering $file, which links to $p");
462 # handle linkbacks; if a page has added/removed links, update the
464 # TODO: inefficient; pages may get rendered above and again here;
465 # problem is the linkbacks could be wrong in the first pass render
469 foreach my $file (keys %rendered, @del) {
470 my $page=pagename($file);
471 if (exists $links{$page}) {
472 foreach my $link (@{$links{$page}}) {
473 $link=bestlink($page, $link);
475 ! exists $oldlinks{$page} ||
476 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
477 $linkchanged{$link}=1;
481 if (exists $oldlinks{$page}) {
482 foreach my $link (@{$oldlinks{$page}}) {
483 $link=bestlink($page, $link);
485 ! exists $links{$page} ||
486 ! grep { $_ eq $link } @{$links{$page}}) {
487 $linkchanged{$link}=1;
492 foreach my $link (keys %linkchanged) {
493 my $linkfile=$pagesources{$link};
494 if (defined $linkfile) {
495 debug("rendering $linkfile, to update its linkbacks");
502 # Generates a C wrapper program for running ikiwiki in a specific way.
503 # The wrapper may be safely made suid.
504 sub gen_wrapper ($$) {
505 my ($svn, $rebuild)=@_;
507 eval {use Cwd 'abs_path'};
508 $srcdir=abs_path($srcdir);
509 $destdir=abs_path($destdir);
510 my $this=abs_path($0);
512 error("$this doesn't seem to be executable");
515 my @params=($srcdir, $destdir, "--wikiname=$wikiname");
516 push @params, "--verbose" if $verbose;
517 push @params, "--rebuild" if $rebuild;
518 push @params, "--nosvn" if !$svn;
519 push @params, "--cgi" if $cgi;
520 push @params, "--url=$url" if $url;
521 push @params, "--cgiurl=$cgiurl" if $cgiurl;
522 my $params=join(" ", @params);
524 foreach my $p ($this, $this, @params) {
530 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
531 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
533 foreach my $var (@envsave) {
535 if ((s=getenv("$var")))
536 asprintf(&newenviron[i++], "%s=%s", "$var", s);
540 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
542 /* A wrapper for ikiwiki, can be safely made suid. */
549 extern char **environ;
551 int main (int argc, char **argv) {
552 /* Sanitize environment. */
554 char *newenviron[$#envsave+3];
557 newenviron[i++]="HOME=$ENV{HOME}";
561 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
562 printf("$params\\n");
567 perror("failed to run $this");
572 if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
573 error("failed to compile ikiwiki-wrap.c");
575 unlink("ikiwiki-wrap.c");
576 print "successfully generated ikiwiki-wrap\n";
584 my $do=$q->param('do');
585 if (! defined $do || ! length $do) {
586 error("\"do\" parameter missing");
589 my ($page)=$q->param('page')=~/$wiki_file_regexp/;
590 if (! defined $page || ! length $page || $page ne $q->param('page') ||
591 $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
592 error("bad page name");
596 my $action=$q->request_uri;
599 if ($do eq 'create') {
600 if (exists $pagesources{lc($page)}) {
601 # hmm, someone else made the page in the meantime?
602 print $q->redirect("$url/".htmlpage($page));
606 my ($from)=$q->param('from')=~/$wiki_file_regexp/;
607 if (! defined $from || ! length $from ||
608 $from ne $q->param('from') ||
609 $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
615 push @page_locs, $dir.$page;
616 push @page_locs, "$from/$page";
617 while (length $dir) {
619 push @page_locs, $dir.$page;
623 $q->param("do", "save");
625 $q->start_html("$wikiname: Creating $page"),
626 $q->h1("$wikiname: Creating $page"),
627 $q->start_form(-action => $action),
629 "Select page location:",
630 $q->popup_menu('page', \@page_locs),
631 $q->textarea(-name => 'content',
636 "Optional comment about this change:",
638 $q->textfield(-name => "comments", -size => 80),
640 $q->submit("Save Page"),
644 elsif ($do eq 'edit') {
646 if (exists $pagesources{lc($page)}) {
647 $content=readfile("$srcdir/$pagesources{lc($page)}");
648 $content=~s/\n/\r\n/g;
650 $q->param("do", "save");
652 $q->start_html("$wikiname: Editing $page"),
653 $q->h1("$wikiname: Editing $page"),
654 $q->start_form(-action => $action),
657 $q->textarea(-name => 'content',
658 -default => $content,
662 "Optional comment about this change:",
664 $q->textfield(-name => "comments", -size => 80),
666 $q->submit("Save Page"),
670 elsif ($do eq 'save') {
671 my $file=$page.$default_pagetype;
673 if (exists $pagesources{lc($page)}) {
674 $file=$pagesources{lc($page)};
678 my $content=$q->param('content');
679 $content=~s/\r\n/\n/g;
681 writefile("$srcdir/$file", $content);
683 my $message="web commit from $ENV{REMOTE_ADDR}";
684 if (defined $q->param('comments')) {
685 $message.=": ".$q->param('comments');
692 # presumably the commit will trigger an update
694 rcs_commit($message);
700 print $q->redirect("$url/".htmlpage($page));
703 error("unknown do parameter");
709 if (grep /^-/, @ARGV) {
710 eval {use Getopt::Long};
712 "wikiname=s" => \$wikiname,
713 "verbose|v" => \$verbose,
714 "rebuild" => \$rebuild,
715 "wrapper" => \$wrapper,
719 "cgiurl=s" => \$cgiurl,
722 usage() unless @ARGV == 2;
723 ($srcdir) = possibly_foolish_untaint(shift);
724 ($destdir) = possibly_foolish_untaint(shift);
726 if ($cgi && ! length $url) {
727 error("Must specify url to wiki with --url when using --cgi");
730 gen_wrapper($svn, $rebuild) if $wrapper;
733 loadindex() unless $rebuild;
738 rcs_update() if $svn;