]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki.pm
(no commit message)
[ikiwiki.git] / IkiWiki.pm
index 59fefc69933e49768e6c36d2b389f61d0fc68b6a..1043ef4024ba5994d773b524103c46baad50b084 100644 (file)
@@ -14,15 +14,15 @@ use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
        %pagestate %wikistate %renderedfiles %oldrenderedfiles
        %pagesources %delpagesources %destsources %depends %depends_simple
        @mass_depends %hooks %forcerebuild %loaded_plugins %typedlinks
-       %oldtypedlinks %autofiles};
+       %oldtypedlinks %autofiles @underlayfiles $lastrev $phase};
 
 use Exporter q{import};
 our @EXPORT = qw(hook debug error htmlpage template template_depends
        deptype add_depends pagespec_match pagespec_match_list bestlink
        htmllink readfile writefile pagetype srcfile pagename
-       displaytime will_render gettext ngettext urlto targetpage
+       displaytime strftime_utf8 will_render gettext ngettext urlto targetpage
        add_underlay pagetitle titlepage linkpage newpagefile
-       inject add_link add_autofile
+       inject add_link add_autofile useragent
        %config %links %pagestate %wikistate %renderedfiles
        %pagesources %destsources %typedlinks);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
@@ -34,6 +34,11 @@ our $DEPEND_CONTENT=1;
 our $DEPEND_PRESENCE=2;
 our $DEPEND_LINKS=4;
 
+# Phases of processing.
+sub PHASE_SCAN () { 0 }
+sub PHASE_RENDER () { 1 }
+$phase = PHASE_SCAN;
+
 # Optimisation.
 use Memoize;
 memoize("abs2rel");
@@ -103,6 +108,14 @@ sub getsetup () {
                safe => 1,
                rebuild => 1,
        },
+       reverse_proxy => {
+               type => "boolean",
+               default => 0,
+               description => "do not adjust cgiurl if CGI is accessed via different URL",
+               advanced => 0,
+               safe => 1,
+               rebuild => 0, # only affects CGI requests
+       },
        cgi_wrapper => {
                type => "string",
                default => '',
@@ -118,6 +131,29 @@ sub getsetup () {
                safe => 0,
                rebuild => 0,
        },
+       cgi_overload_delay => {
+               type => "string",
+               default => '',
+               example => "10",
+               description => "number of seconds to delay CGI requests when overloaded",
+               safe => 1,
+               rebuild => 0,
+       },
+       cgi_overload_message => {
+               type => "string",
+               default => '',
+               example => "Please wait",
+               description => "message to display when overloaded (may contain html)",
+               safe => 1,
+               rebuild => 0,
+       },
+       only_committed_changes => {
+               type => "boolean",
+               default => 0,
+               description => "enable optimization of only refreshing committed changes?",
+               safe => 1,
+               rebuild => 0,
+       },
        rcs => {
                type => "string",
                default => '',
@@ -129,7 +165,8 @@ sub getsetup () {
                type => "internal",
                default => [qw{mdwn link inline meta htmlscrubber passwordauth
                                openid signinedit lockedit conditional
-                               recentchanges parentlinks editpage}],
+                               recentchanges parentlinks editpage
+                               templatebody}],
                description => "plugins to enable by default",
                safe => 0,
                rebuild => 1,
@@ -497,6 +534,21 @@ sub getsetup () {
                safe => 0,
                rebuild => 0,
        },
+       cookiejar => {
+               type => "string",
+               default => { file => "$ENV{HOME}/.ikiwiki/cookies" },
+               description => "cookie control",
+               safe => 0, # hooks into perl module internals
+               rebuild => 0,
+       },
+       useragent => {
+               type => "string",
+               default => "ikiwiki/$version",
+               example => "Wget/1.13.4 (linux-gnu)",
+               description => "set custom user agent string for outbound HTTP requests e.g. when fetching aggregated RSS feeds",
+               safe => 0,
+               rebuild => 0,
+       },
 }
 
 sub defaultconfig () {
@@ -569,12 +621,39 @@ sub checkconfig () {
 
                        $local_cgiurl = $cgiurl->path;
 
-                       if ($cgiurl->scheme ne $baseurl->scheme or
-                               $cgiurl->authority ne $baseurl->authority) {
+                       if ($cgiurl->scheme eq 'https' &&
+                               $baseurl->scheme eq 'http') {
+                               # We assume that the same content is available
+                               # over both http and https, because if it
+                               # wasn't, accessing the static content
+                               # from the CGI would be mixed-content,
+                               # which would be a security flaw.
+
+                               if ($cgiurl->authority ne $baseurl->authority) {
+                                       # use protocol-relative URL for
+                                       # static content
+                                       $local_url = "$config{url}/";
+                                       $local_url =~ s{^http://}{//};
+                               }
+                               # else use host-relative URL for static content
+
+                               # either way, CGI needs to be absolute
+                               $local_cgiurl = $config{cgiurl};
+                       }
+                       elsif ($cgiurl->scheme ne $baseurl->scheme) {
                                # too far apart, fall back to absolute URLs
                                $local_url = "$config{url}/";
                                $local_cgiurl = $config{cgiurl};
                        }
+                       elsif ($cgiurl->authority ne $baseurl->authority) {
+                               # slightly too far apart, fall back to
+                               # protocol-relative URLs
+                               $local_url = "$config{url}/";
+                               $local_url =~ s{^https?://}{//};
+                               $local_cgiurl = $config{cgiurl};
+                               $local_cgiurl =~ s{^https?://}{//};
+                       }
+                       # else keep host-relative URLs
                }
 
                $local_url =~ s{//$}{/};
@@ -705,6 +784,7 @@ sub debug ($) {
 }
 
 my $log_open=0;
+my $log_failed=0;
 sub log_message ($$) {
        my $type=shift;
 
@@ -715,9 +795,18 @@ sub log_message ($$) {
                        Sys::Syslog::openlog('ikiwiki', '', 'user');
                        $log_open=1;
                }
-               return eval {
-                       Sys::Syslog::syslog($type, "[$config{wikiname}] %s", join(" ", @_));
+               eval {
+                       # keep a copy to avoid editing the original config repeatedly
+                       my $wikiname = $config{wikiname};
+                       utf8::encode($wikiname);
+                       Sys::Syslog::syslog($type, "[$wikiname] %s", join(" ", @_));
                };
+                if ($@) {
+                    print STDERR "failed to syslog: $@" unless $log_failed;
+                    $log_failed=1;
+                    print STDERR "@_\n";
+                }
+                return $@;
        }
        elsif (! $config{cgi}) {
                return print "@_\n";
@@ -1092,6 +1181,11 @@ sub cgiurl (@) {
                join("&", map $_."=".uri_escape_utf8($params{$_}), keys %params);
 }
 
+sub cgiurl_abs (@) {
+       eval q{use URI};
+       URI->new_abs(cgiurl(@_), $config{cgiurl});
+}
+
 sub baseurl (;$) {
        my $page=shift;
 
@@ -1148,9 +1242,19 @@ sub formattime ($;$) {
                $format=$config{timeformat};
        }
 
+       return strftime_utf8($format, localtime($time));
+}
+
+my $strftime_encoding;
+sub strftime_utf8 {
        # strftime doesn't know about encodings, so make sure
-       # its output is properly treated as utf8
-       return decode_utf8(POSIX::strftime($format, localtime($time)));
+       # its output is properly treated as utf8.
+       # Note that this does not handle utf-8 in the format string.
+       ($strftime_encoding) = POSIX::setlocale(&POSIX::LC_TIME) =~ m#\.([^@]+)#
+               unless defined $strftime_encoding;
+       $strftime_encoding
+               ? Encode::decode($strftime_encoding, POSIX::strftime(@_))
+               : POSIX::strftime(@_);
 }
 
 sub date_3339 ($) {
@@ -1402,7 +1506,7 @@ sub preprocess ($$$;$$) {
                        # consider it significant.
                        my @params;
                        while ($params =~ m{
-                               (?:([-\w]+)=)?          # 1: named parameter key?
+                               (?:([-.\w]+)=)?         # 1: named parameter key?
                                (?:
                                        """(.*?)"""     # 2: triple-quoted value
                                |
@@ -1445,7 +1549,7 @@ sub preprocess ($$$;$$) {
                                        push @params, $val, '';
                                }
                        }
-                       if ($preprocessing{$page}++ > 3) {
+                       if ($preprocessing{$page}++ > 8) {
                                # Avoid loops of preprocessed pages preprocessing
                                # other pages that preprocess them, etc.
                                return "[[!$command <span class=\"error\">".
@@ -1499,7 +1603,7 @@ sub preprocess ($$$;$$) {
                        (               # 4: the parameters..
                                \s+     # Must have space if parameters present
                                (?:
-                                       (?:[-\w]+=)?            # named parameter key?
+                                       (?:[-.\w]+=)?           # named parameter key?
                                        (?:
                                                """.*?"""       # triple-quoted value
                                                |
@@ -1527,7 +1631,7 @@ sub preprocess ($$$;$$) {
                        \s+
                        (               # 4: the parameters..
                                (?:
-                                       (?:[-\w]+=)?            # named parameter key?
+                                       (?:[-.\w]+=)?           # named parameter key?
                                        (?:
                                                """.*?"""       # triple-quoted value
                                                |
@@ -1743,7 +1847,8 @@ sub enable_commit_hook () {
 
 sub loadindex () {
        %oldrenderedfiles=%pagectime=();
-       if (! $config{rebuild}) {
+       my $rebuild=$config{rebuild};
+       if (! $rebuild) {
                %pagesources=%pagemtime=%oldlinks=%links=%depends=
                %destsources=%renderedfiles=%pagecase=%pagestate=
                %depends_simple=%typedlinks=%oldtypedlinks=();
@@ -1755,7 +1860,8 @@ sub loadindex () {
                        open ($in, "<", "$config{wikistatedir}/indexdb") || return;
                }
                else {
-                       $config{gettime}=1; # first build
+                       # gettime on first build
+                       $config{gettime}=1 unless defined $config{gettime};
                        return;
                }
        }
@@ -1783,10 +1889,16 @@ sub loadindex () {
 
        foreach my $src (keys %$pages) {
                my $d=$pages->{$src};
-               my $page=pagename($src);
+               my $page;
+               if (exists $d->{page} && ! $rebuild) {
+                       $page=$d->{page};
+               }
+               else {
+                       $page=pagename($src);
+               }
                $pagectime{$page}=$d->{ctime};
                $pagesources{$page}=$src;
-               if (! $config{rebuild}) {
+               if (! $rebuild) {
                        $pagemtime{$page}=$d->{mtime};
                        $renderedfiles{$page}=$d->{dest};
                        if (exists $d->{links} && ref $d->{links}) {
@@ -1836,6 +1948,8 @@ sub loadindex () {
        foreach my $page (keys %renderedfiles) {
                $destsources{$_}=$page foreach @{$renderedfiles{$page}};
        }
+       $lastrev=$index->{lastrev};
+       @underlayfiles=@{$index->{underlayfiles}} if ref $index->{underlayfiles};
        return close($in);
 }
 
@@ -1857,6 +1971,7 @@ sub saveindex () {
                my $src=$pagesources{$page};
 
                $index{page}{$src}={
+                       page => $page,
                        ctime => $pagectime{$page},
                        mtime => $pagemtime{$page},
                        dest => $renderedfiles{$page},
@@ -1876,11 +1991,7 @@ sub saveindex () {
                }
 
                if (exists $pagestate{$page}) {
-                       foreach my $id (@plugins) {
-                               foreach my $key (keys %{$pagestate{$page}{$id}}) {
-                                       $index{page}{$src}{state}{$id}{$key}=$pagestate{$page}{$id}{$key};
-                               }
-                       }
+                       $index{page}{$src}{state}=$pagestate{$page};
                }
        }
 
@@ -1892,6 +2003,9 @@ sub saveindex () {
                }
        }
        
+       $index{lastrev}=$lastrev;
+       $index{underlayfiles}=\@underlayfiles;
+
        $index{version}="3";
        my $ret=Storable::nstore_fd(\%index, $out);
        return if ! defined $ret || ! $ret;
@@ -1949,11 +2063,19 @@ sub template_depends ($$;@) {
        if (defined $page && defined $tpage) {
                add_depends($page, $tpage);
        }
-       
+
        my @opts=(
                filter => sub {
                        my $text_ref = shift;
                        ${$text_ref} = decode_utf8(${$text_ref});
+                       run_hooks(readtemplate => sub {
+                               ${$text_ref} = shift->(
+                                       id => $name,
+                                       page => $tpage,
+                                       content => ${$text_ref},
+                                       untrusted => $untrusted,
+                               );
+                       });
                },
                loop_context_vars => 1,
                die_on_bad_params => 0,
@@ -2243,6 +2365,14 @@ sub add_autofile ($$$) {
        $autofiles{$file}{generator}=$generator;
 }
 
+sub useragent () {
+       return LWP::UserAgent->new(
+               cookie_jar => $config{cookiejar},
+               env_proxy => 1,         # respect proxy env vars
+               agent => $config{useragent},
+       );
+}
+
 sub sortspec_translate ($$) {
        my $spec = shift;
        my $reverse = shift;
@@ -2379,6 +2509,19 @@ sub pagespec_match ($$;@) {
        return $sub->($page, @params);
 }
 
+# e.g. @pages = sort_pages("title", \@pages, reverse => "yes")
+#
+# Not exported yet, but could be in future if it is generally useful.
+# Note that this signature is not the same as IkiWiki::SortSpec::sort_pages,
+# which is "more internal".
+sub sort_pages ($$;@) {
+       my $sort = shift;
+       my $list = shift;
+       my %params = @_;
+       $sort = sortspec_translate($sort, $params{reverse});
+       return IkiWiki::SortSpec::sort_pages($sort, @$list);
+}
+
 sub pagespec_match_list ($$;@) {
        my $page=shift;
        my $pagespec=shift;
@@ -2484,21 +2627,48 @@ our @ISA = 'IkiWiki::SuccessReason';
 
 package IkiWiki::SuccessReason;
 
+# A blessed array-ref:
+#
+# [0]: human-readable reason for success (or, in FailReason subclass, failure)
+# [1]{""}:
+#      - if absent or false, the influences of this evaluation are "static",
+#        see the influences_static method
+#      - if true, they are dynamic (not static)
+# [1]{any other key}:
+#      the dependency types of influences, as returned by the influences method
+
 use overload (
+       # in string context, it's the human-readable reason
        '""'    => sub { $_[0][0] },
+       # in boolean context, SuccessReason is 1 and FailReason is 0
        '0+'    => sub { 1 },
+       # negating a result gives the opposite result with the same influences
        '!'     => sub { bless $_[0], 'IkiWiki::FailReason'},
+       # A & B = (A ? B : A) with the influences of both
        '&'     => sub { $_[1]->merge_influences($_[0], 1); $_[1] },
+       # A | B = (A ? A : B) with the influences of both
        '|'     => sub { $_[0]->merge_influences($_[1]); $_[0] },
        fallback => 1,
 );
 
+# SuccessReason->new("human-readable reason", page => deptype, ...)
+
 sub new {
        my $class = shift;
        my $value = shift;
        return bless [$value, {@_}], $class;
 }
 
+# influences(): return a reference to a copy of the hash
+# { page => dependency type } describing the pages that indirectly influenced
+# this result, but would not cause a dependency through ikiwiki's core
+# dependency logic.
+#
+# See [[todo/dependency_types]] for extensive discussion of what this means.
+#
+# influences(page => deptype, ...): remove all influences, replace them
+# with the arguments, and return a reference to a copy of the new influences.
+
 sub influences {
        my $this=shift;
        $this->[1]={@_} if @_;
@@ -2507,15 +2677,46 @@ sub influences {
        return \%i;
 }
 
+# True if this result has the same influences whichever page it matches,
+# For instance, whether bar matches backlink(foo) is influenced only by
+# the set of links in foo, so its only influence is { foo => DEPEND_LINKS },
+# which does not mention bar anywhere.
+#
+# False if this result would have different influences when matching
+# different pages. For instance, when testing whether link(foo) matches bar,
+# { bar => DEPEND_LINKS } is an influence on that result, because changing
+# bar's links could change the outcome; so its influences are not the same
+# as when testing whether link(foo) matches baz.
+#
+# Static influences are one of the things that make pagespec_match_list
+# more efficient than repeated calls to pagespec_match.
+
 sub influences_static {
        return ! $_[0][1]->{""};
 }
 
+# Change the influences of $this to be the influences of "$this & $other"
+# or "$this | $other".
+#
+# If both $this and $other are either successful or have influences,
+# or this is an "or" operation, the result has all the influences from
+# either of the arguments. It has dynamic influences if either argument
+# has dynamic influences.
+#
+# If this is an "and" operation, and at least one argument is a
+# FailReason with no influences, the result has no influences, and they
+# are not dynamic. For instance, link(foo) matching bar is influenced
+# by bar, but enabled(ddate) has no influences. Suppose ddate is disabled;
+# then (link(foo) and enabled(ddate)) not matching bar is not influenced by
+# bar, because it would be false however often you edit bar.
+
 sub merge_influences {
        my $this=shift;
        my $other=shift;
        my $anded=shift;
 
+       # This "if" is odd because it needs to avoid negating $this
+       # or $other, which would alter the objects in-place. Be careful.
        if (! $anded || (($this || %{$this->[1]}) &&
                         ($other || %{$other->[1]}))) {
                foreach my $influence (keys %{$other->[1]}) {
@@ -2528,6 +2729,8 @@ sub merge_influences {
        }
 }
 
+# Change $this so it is not considered to be influenced by $torm.
+
 sub remove_influence {
        my $this=shift;
        my $torm=shift;
@@ -2647,8 +2850,14 @@ sub match_link ($$;@) {
 }
 
 sub match_backlink ($$;@) {
-       my $ret=match_link($_[1], $_[0], @_);
-       $ret->influences($_[1] => $IkiWiki::DEPEND_LINKS);
+       my $page=shift;
+       my $testpage=shift;
+       my %params=@_;
+       if ($testpage eq '.') {
+               $testpage = $params{'location'}
+       }
+       my $ret=match_link($testpage, $page, @_);
+       $ret->influences($testpage => $IkiWiki::DEPEND_LINKS);
        return $ret;
 }
 
@@ -2739,12 +2948,12 @@ sub match_user ($$;@) {
        my $user=shift;
        my %params=@_;
        
-       my $regexp=IkiWiki::glob2re($user);
-       
        if (! exists $params{user}) {
                return IkiWiki::ErrorReason->new("no user specified");
        }
 
+       my $regexp=IkiWiki::glob2re($user);
+       
        if (defined $params{user} && $params{user}=~$regexp) {
                return IkiWiki::SuccessReason->new("user is $user");
        }
@@ -2784,8 +2993,10 @@ sub match_ip ($$;@) {
        if (! exists $params{ip}) {
                return IkiWiki::ErrorReason->new("no IP specified");
        }
+       
+       my $regexp=IkiWiki::glob2re(lc $ip);
 
-       if (defined $params{ip} && lc $params{ip} eq lc $ip) {
+       if (defined $params{ip} && lc $params{ip}=~$regexp) {
                return IkiWiki::SuccessReason->new("IP is $ip");
        }
        else {
@@ -2808,6 +3019,7 @@ sub cmp_title {
        IkiWiki::pagetitle(IkiWiki::basename($b))
 }
 
+sub cmp_path { IkiWiki::pagetitle($a) cmp IkiWiki::pagetitle($b) }
 sub cmp_mtime { $IkiWiki::pagemtime{$b} <=> $IkiWiki::pagemtime{$a} }
 sub cmp_age { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} }