]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
web commit by https://id.mayfirst.org/jamie/
[ikiwiki.git] / IkiWiki.pm
index e7422cba571337f2db7f70d519fab896b0c9c3e0..5a05a0f73ddf68e8bb624ac7ca74d2965dab9277 100644 (file)
@@ -87,6 +87,8 @@ sub defaultconfig () { #{{{
        numbacklinks => 10,
        account_creation_password => "",
        prefix_directives => 0,
+       hardlink => 0,
+       cgi_disable_uploads => 1,
 } #}}}
 
 sub checkconfig () { #{{{
@@ -102,6 +104,12 @@ sub checkconfig () { #{{{
                }
        }
 
+       if (ref $config{ENV} eq 'HASH') {
+               foreach my $val (keys %{$config{ENV}}) {
+                       $ENV{$val}=$config{ENV}{$val};
+               }
+       }
+
        if ($config{w3mmode}) {
                eval q{use Cwd q{abs_path}};
                error($@) if $@;
@@ -281,17 +289,22 @@ sub htmlpage ($) { #{{{
        return targetpage($page, $config{htmlext});
 } #}}}
 
-sub srcfile ($) { #{{{
+sub srcfile_stat { #{{{
        my $file=shift;
+       my $nothrow=shift;
 
-       return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
+       return "$config{srcdir}/$file", stat(_) if -e "$config{srcdir}/$file";
        foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
-               return "$dir/$file" if -e "$dir/$file";
+               return "$dir/$file", stat(_) if -e "$dir/$file";
        }
-       error("internal error: $file cannot be found in $config{srcdir} or underlay");
+       error("internal error: $file cannot be found in $config{srcdir} or underlay") unless $nothrow;
        return;
 } #}}}
 
+sub srcfile ($;$) { #{{{
+       return (srcfile_stat(@_))[0];
+} #}}}
+
 sub add_underlay ($) { #{{{
        my $dir=shift;
 
@@ -323,12 +336,9 @@ sub readfile ($;$$) { #{{{
        return $ret;
 } #}}}
 
-sub writefile ($$$;$$) { #{{{
-       my $file=shift; # can include subdirs
-       my $destdir=shift; # directory to put file in
-       my $content=shift;
-       my $binary=shift;
-       my $writer=shift;
+sub prep_writefile ($$) {
+       my $file=shift;
+       my $destdir=shift;
        
        my $test=$file;
        while (length $test) {
@@ -337,12 +347,8 @@ sub writefile ($$$;$$) { #{{{
                }
                $test=dirname($test);
        }
-       my $newfile="$destdir/$file.ikiwiki-new";
-       if (-l $newfile) {
-               error("cannot write to a symlink ($newfile)");
-       }
 
-       my $dir=dirname($newfile);
+       my $dir=dirname("$destdir/$file");
        if (! -d $dir) {
                my $d="";
                foreach my $s (split(m!/+!, $dir)) {
@@ -353,6 +359,23 @@ sub writefile ($$$;$$) { #{{{
                }
        }
 
+       return 1;
+}
+
+sub writefile ($$$;$$) { #{{{
+       my $file=shift; # can include subdirs
+       my $destdir=shift; # directory to put file in
+       my $content=shift;
+       my $binary=shift;
+       my $writer=shift;
+       
+       prep_writefile($file, $destdir);
+       
+       my $newfile="$destdir/$file.ikiwiki-new";
+       if (-l $newfile) {
+               error("cannot write to a symlink ($newfile)");
+       }
+       
        my $cleanup = sub { unlink($newfile) };
        open (my $out, '>', $newfile) || error("failed to write $newfile: $!", $cleanup);
        binmode($out) if ($binary);
@@ -515,7 +538,12 @@ sub beautify_url ($) { #{{{
        if ($config{usedirs}) {
                $url =~ s!/index.$config{htmlext}$!/!;
        }
-       $url =~ s!^$!./!; # Browsers don't like empty links...
+
+       # Ensure url is not an empty link, and
+       # if it's relative, make that explicit to avoid colon confusion.
+       if ($url !~ /\//) {
+               $url="./$url";
+       }
 
        return $url;
 } #}}}
@@ -525,7 +553,7 @@ sub urlto ($$) { #{{{
        my $from=shift;
 
        if (! length $to) {
-               return beautify_url(baseurl($from));
+               return beautify_url(baseurl($from)."index.$config{htmlext}");
        }
 
        if (! $destsources{$to}) {
@@ -573,10 +601,10 @@ sub htmllink ($$$;@) { #{{{
                        return "<span class=\"createlink\"><a href=\"".
                                cgiurl(
                                        do => "create",
-                                       page => pagetitle(lc($link), 1),
+                                       page => lc($link),
                                        from => $lpage
                                ).
-                               "\">?</a>$linktext</span>"
+                               "\" rel=\"nofollow\">?</a>$linktext</span>"
                }
        }
        
@@ -610,14 +638,18 @@ sub userlink ($) { #{{{
                return "<a href=\"$user\">$oiduser</a>";
        }
        else {
+               eval q{use CGI 'escapeHTML'};
+               error($@) if $@;
+
                return htmllink("", "", escapeHTML(
                        length $config{userdir} ? $config{userdir}."/".$user : $user
                ), noimageinline => 1);
        }
 } #}}}
 
-sub htmlize ($$$) { #{{{
+sub htmlize ($$$$) { #{{{
        my $page=shift;
+       my $destpage=shift;
        my $type=shift;
        my $content=shift;
        
@@ -636,6 +668,7 @@ sub htmlize ($$$) { #{{{
        run_hooks(sanitize => sub {
                $content=shift->(
                        page => $page,
+                       destpage => $destpage,
                        content => $content,
                );
        });
@@ -667,7 +700,7 @@ sub linkify ($$$) { #{{{
        return $content;
 } #}}}
 
-my %preprocessing;
+our %preprocessing;
 our $preprocess_preview=0;
 sub preprocess ($$$;$$) { #{{{
        my $page=shift; # the page the data comes from
@@ -899,18 +932,28 @@ sub loadindex () { #{{{
                %pagesources=%pagemtime=%oldlinks=%links=%depends=
                %destsources=%renderedfiles=%pagecase=%pagestate=();
        }
-       open (my $in, "<", "$config{wikistatedir}/index") || return;
+       my $in;
+       if (! open ($in, "<", "$config{wikistatedir}/indexdb")) {
+               if (-e "$config{wikistatedir}/index") {
+                       system("ikiwiki-transition", "indexdb", $config{srcdir});
+                       open ($in, "<", "$config{wikistatedir}/indexdb") || return;
+               }
+               else {
+                       return;
+               }
+       }
        my $ret=Storable::fd_retrieve($in);
        if (! defined $ret) {
                return 0;
        }
        my %index=%$ret;
-       foreach my $page (keys %index) {
-               my %d=%{$index{$page}};
+       foreach my $src (keys %index) {
+               my %d=%{$index{$src}};
+               my $page=pagename($src);
                $pagectime{$page}=$d{ctime};
                if (! $config{rebuild}) {
+                       $pagesources{$page}=$src;
                        $pagemtime{$page}=$d{mtime};
-                       $pagesources{$page}=$d{src};
                        $renderedfiles{$page}=$d{dest};
                        if (exists $d{links} && ref $d{links}) {
                                $links{$page}=$d{links};
@@ -946,29 +989,29 @@ sub saveindex () { #{{{
        if (! -d $config{wikistatedir}) {
                mkdir($config{wikistatedir});
        }
-       my $newfile="$config{wikistatedir}/index.new";
+       my $newfile="$config{wikistatedir}/indexdb.new";
        my $cleanup = sub { unlink($newfile) };
        open (my $out, '>', $newfile) || error("cannot write to $newfile: $!", $cleanup);
        my %index;
        foreach my $page (keys %pagemtime) {
                next unless $pagemtime{$page};
+               my $src=$pagesources{$page};
 
-               $index{$page}={
+               $index{$src}={
                        ctime => $pagectime{$page},
                        mtime => $pagemtime{$page},
-                       src => $pagesources{$page},
                        dest => $renderedfiles{$page},
                        links => $links{$page},
                };
 
                if (exists $depends{$page}) {
-                       $index{$page}{depends} = $depends{$page};
+                       $index{$src}{depends} = $depends{$page};
                }
 
                if (exists $pagestate{$page}) {
                        foreach my $id (@hookids) {
                                foreach my $key (keys %{$pagestate{$page}{$id}}) {
-                                       $index{$page}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
+                                       $index{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
                                }
                        }
                }
@@ -976,8 +1019,8 @@ sub saveindex () { #{{{
        my $ret=Storable::nstore_fd(\%index, $out);
        return if ! defined $ret || ! $ret;
        close $out || error("failed saving to $newfile: $!", $cleanup);
-       rename($newfile, "$config{wikistatedir}/index") ||
-               error("failed renaming $newfile to $config{wikistatedir}/index", $cleanup);
+       rename($newfile, "$config{wikistatedir}/indexdb") ||
+               error("failed renaming $newfile to $config{wikistatedir}/indexdb", $cleanup);
        
        return 1;
 } #}}}
@@ -1223,6 +1266,11 @@ sub pagespec_translate ($) { #{{{
                }
        }
 
+       if (! length $code) {
+               $code=0;
+       }
+
+       no warnings;
        return eval 'sub { my $page=shift; '.$code.' }';
 } #}}}
 
@@ -1237,7 +1285,7 @@ sub pagespec_match ($$;@) { #{{{
        }
 
        my $sub=pagespec_translate($spec);
-       return IkiWiki::FailReason->new('syntax error') if $@;
+       return IkiWiki::FailReason->new("syntax error in pagespec \"$spec\"") if $@;
        return $sub->($page, @params);
 } #}}}
 
@@ -1247,6 +1295,13 @@ sub pagespec_valid ($) { #{{{
        my $sub=pagespec_translate($spec);
        return ! $@;
 } #}}}
+       
+sub glob2re ($) { #{{{
+       my $re=quotemeta(shift);
+       $re=~s/\\\*/.*/g;
+       $re=~s/\\\?/./g;
+       return $re;
+} #}}}
 
 package IkiWiki::FailReason;
 
@@ -1258,7 +1313,9 @@ use overload ( #{{{
 ); #}}}
 
 sub new { #{{{
-       return bless \$_[1], $_[0];
+       my $class = shift;
+       my $value = shift;
+       return bless \$value, $class;
 } #}}}
 
 package IkiWiki::SuccessReason;
@@ -1271,7 +1328,9 @@ use overload ( #{{{
 ); #}}}
 
 sub new { #{{{
-       return bless \$_[1], $_[0];
+       my $class = shift;
+       my $value = shift;
+       return bless \$value, $class;
 }; #}}}
 
 package IkiWiki::PageSpec;
@@ -1290,12 +1349,8 @@ sub match_glob ($$;@) { #{{{
                $glob="$from/$glob" if length $from;
        }
 
-       # turn glob into safe regexp
-       $glob=quotemeta($glob);
-       $glob=~s/\\\*/.*/g;
-       $glob=~s/\\\?/./g;
-
-       if ($page=~/^$glob$/i) {
+       my $regexp=IkiWiki::glob2re($glob);
+       if ($page=~/^$regexp$/i) {
                if (! IkiWiki::isinternal($page) || $params{internal}) {
                        return IkiWiki::SuccessReason->new("$glob matches $page");
                }