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");
369 sub rcs_recentchanges ($) {
373 eval q{use Date::Parse};
374 eval q{use Time::Duration};
376 my %sourcepages=reverse %pagesources;
378 if (-d "$srcdir/.svn") {
379 my $info=`LANG=C svn info $srcdir`;
380 my ($svn_url)=$info=~/^URL: (.*)$/m;
382 # FIXME: currently assumes that the wiki is somewhere
383 # under trunk in svn, doesn't support other layouts.
384 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
386 my $div=qr/^--------------------+$/;
387 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
389 my ($rev, $user, $when, @pages, $message);
390 foreach (`LANG=C svn log -v '$svn_url'`) {
392 if ($state eq 'start' && /$div/) {
395 elsif ($state eq 'header' && /$infoline/) {
398 $when=concise(ago(time - str2time($3)));
400 elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
401 push @pages, exists $sourcepages{$1} ? $sourcepages{$1} : $1;
403 elsif ($state eq 'header' && /^$/) {
406 elsif ($state eq 'body' && /$div/) {
407 push @ret, { rev => $rev, user => $user,
408 when => $when, message => $message,
410 return @ret if @ret >= $num;
413 $message=$rev=$user=$when=undef;
416 elsif ($state eq 'body') {
417 $message.="$_<br>\n";
429 my $dir=dirname($file);
430 while (rmdir($dir)) {
436 # Find existing pages.
442 if (/$wiki_file_prune_regexp/) {
443 $File::Find::prune=1;
446 my ($f)=/$wiki_file_regexp/; # untaint
448 warn("skipping bad filename $_\n");
451 $f=~s/^\Q$srcdir\E\/?//;
453 $exists{pagename($f)}=1;
461 # check for added or removed pages
463 foreach my $file (@files) {
464 my $page=pagename($file);
465 if (! $oldpagemtime{$page}) {
466 debug("new page $page");
469 $pagesources{$page}=$file;
473 foreach my $page (keys %oldpagemtime) {
474 if (! $exists{$page}) {
475 debug("removing old page $page");
476 push @del, $renderedfiles{$page};
477 prune($destdir."/".$renderedfiles{$page});
478 delete $renderedfiles{$page};
479 $oldpagemtime{$page}=0;
480 delete $pagesources{$page};
484 # render any updated files
485 foreach my $file (@files) {
486 my $page=pagename($file);
488 if (! exists $oldpagemtime{$page} ||
489 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
490 debug("rendering changed file $file");
496 # if any files were added or removed, check to see if each page
497 # needs an update due to linking to them
498 # TODO: inefficient; pages may get rendered above and again here;
499 # problem is the bestlink may have changed and we won't know until
502 FILE: foreach my $file (@files) {
503 my $page=pagename($file);
504 foreach my $f (@add, @del) {
506 foreach my $link (@{$links{$page}}) {
507 if (bestlink($page, $link) eq $p) {
508 debug("rendering $file, which links to $p");
518 # handle linkbacks; if a page has added/removed links, update the
520 # TODO: inefficient; pages may get rendered above and again here;
521 # problem is the linkbacks could be wrong in the first pass render
525 foreach my $file (keys %rendered, @del) {
526 my $page=pagename($file);
527 if (exists $links{$page}) {
528 foreach my $link (@{$links{$page}}) {
529 $link=bestlink($page, $link);
531 ! exists $oldlinks{$page} ||
532 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
533 $linkchanged{$link}=1;
537 if (exists $oldlinks{$page}) {
538 foreach my $link (@{$oldlinks{$page}}) {
539 $link=bestlink($page, $link);
541 ! exists $links{$page} ||
542 ! grep { $_ eq $link } @{$links{$page}}) {
543 $linkchanged{$link}=1;
548 foreach my $link (keys %linkchanged) {
549 my $linkfile=$pagesources{$link};
550 if (defined $linkfile) {
551 debug("rendering $linkfile, to update its linkbacks");
558 # Generates a C wrapper program for running ikiwiki in a specific way.
559 # The wrapper may be safely made suid.
560 sub gen_wrapper ($$) {
561 my ($svn, $rebuild)=@_;
563 eval q{use Cwd 'abs_path'};
564 $srcdir=abs_path($srcdir);
565 $destdir=abs_path($destdir);
566 my $this=abs_path($0);
568 error("$this doesn't seem to be executable");
571 my @params=($srcdir, $destdir, "--wikiname=$wikiname");
572 push @params, "--verbose" if $verbose;
573 push @params, "--rebuild" if $rebuild;
574 push @params, "--nosvn" if !$svn;
575 push @params, "--cgi" if $cgi;
576 push @params, "--url=$url" if $url;
577 push @params, "--cgiurl=$cgiurl" if $cgiurl;
578 my $params=join(" ", @params);
580 foreach my $p ($this, $this, @params) {
586 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
587 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE} if $cgi;
589 foreach my $var (@envsave) {
591 if ((s=getenv("$var")))
592 asprintf(&newenviron[i++], "%s=%s", "$var", s);
596 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
598 /* A wrapper for ikiwiki, can be safely made suid. */
605 extern char **environ;
607 int main (int argc, char **argv) {
608 /* Sanitize environment. */
610 char *newenviron[$#envsave+3];
613 newenviron[i++]="HOME=$ENV{HOME}";
617 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
618 printf("$params\\n");
623 perror("failed to run $this");
628 if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
629 error("failed to compile ikiwiki-wrap.c");
631 unlink("ikiwiki-wrap.c");
632 print "successfully generated ikiwiki-wrap\n";
640 my $do=$q->param('do');
641 if (! defined $do || ! length $do) {
642 error("\"do\" parameter missing");
645 if ($do eq 'recentchanges') {
647 foreach my $change (rcs_recentchanges(100)) {
649 foreach my $page (@{$change->{pages}}) {
653 $list.="changed ".$change->{when}." by ".
654 htmllink("index", $change->{user}).
655 ": <i>".$change->{message}."</i>\n";
661 $q->start_html("RecentChanges"),
662 $q->h1("<a href=\"$url\">$wikiname</a>/ RecentChanges"),
669 my ($page)=$q->param('page')=~/$wiki_file_regexp/;
670 if (! defined $page || ! length $page || $page ne $q->param('page') ||
671 $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
672 error("bad page name");
676 my $action=$q->request_uri;
679 if ($do eq 'create') {
680 if (exists $pagesources{lc($page)}) {
681 # hmm, someone else made the page in the meantime?
682 print $q->redirect("$url/".htmlpage($page));
686 my ($from)=$q->param('from')=~/$wiki_file_regexp/;
687 if (! defined $from || ! length $from ||
688 $from ne $q->param('from') ||
689 $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
695 push @page_locs, $dir.$page;
696 push @page_locs, "$from/$page";
697 while (length $dir) {
699 push @page_locs, $dir.$page;
703 $q->param("do", "save");
705 $q->start_html("Creating $page"),
706 $q->start_h1("<a href=\"$url\">$wikiname</a>/ Creating $page"),
707 $q->start_form(-action => $action),
709 "Select page location:",
710 $q->popup_menu('page', \@page_locs),
711 $q->textarea(-name => 'content',
716 "Optional comment about this change:",
718 $q->textfield(-name => "comments", -size => 80),
720 $q->submit("Save Page"),
724 elsif ($do eq 'edit') {
726 if (exists $pagesources{lc($page)}) {
727 $content=readfile("$srcdir/$pagesources{lc($page)}");
728 $content=~s/\n/\r\n/g;
730 $q->param("do", "save");
732 $q->start_html("Editing $page"),
733 $q->h1("<a href=\"$url\">$wikiname</a>/ Editing $page"),
734 $q->start_form(-action => $action),
737 $q->textarea(-name => 'content',
738 -default => $content,
742 "Optional comment about this change:",
744 $q->textfield(-name => "comments", -size => 80),
746 $q->submit("Save Page"),
750 elsif ($do eq 'save') {
751 my $file=$page.$default_pagetype;
753 if (exists $pagesources{lc($page)}) {
754 $file=$pagesources{lc($page)};
758 my $content=$q->param('content');
759 $content=~s/\r\n/\n/g;
761 writefile("$srcdir/$file", $content);
763 my $message="web commit from $ENV{REMOTE_ADDR}";
764 if (defined $q->param('comments')) {
765 $message.=": ".$q->param('comments');
772 # presumably the commit will trigger an update
774 rcs_commit($message);
780 print $q->redirect("$url/".htmlpage($page));
783 error("unknown do parameter");
789 if (grep /^-/, @ARGV) {
790 eval {use Getopt::Long};
792 "wikiname=s" => \$wikiname,
793 "verbose|v" => \$verbose,
794 "rebuild" => \$rebuild,
795 "wrapper" => \$wrapper,
799 "cgiurl=s" => \$cgiurl,
802 usage() unless @ARGV == 2;
803 ($srcdir) = possibly_foolish_untaint(shift);
804 ($destdir) = possibly_foolish_untaint(shift);
806 if ($cgi && ! length $url) {
807 error("Must specify url to wiki with --url when using --cgi");
810 gen_wrapper($svn, $rebuild) if $wrapper;
813 loadindex() unless $rebuild;
818 rcs_update() if $svn;