]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/inline.pm
avoid setting default value in websetup_force_plugins
[git.ikiwiki.info.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
5 use warnings;
6 use strict;
7 use Encode;
8 use IkiWiki 2.00;
9 use URI;
11 my %knownfeeds;
12 my %page_numfeeds;
13 my @inline;
14 my $nested=0;
16 sub import { #{{{
17         hook(type => "getopt", id => "inline", call => \&getopt);
18         hook(type => "getsetup", id => "inline", call => \&getsetup);
19         hook(type => "checkconfig", id => "inline", call => \&checkconfig);
20         hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
21         hook(type => "preprocess", id => "inline", 
22                 call => \&IkiWiki::preprocess_inline);
23         hook(type => "pagetemplate", id => "inline",
24                 call => \&IkiWiki::pagetemplate_inline);
25         hook(type => "format", id => "inline", call => \&format);
26         # Hook to change to do pinging since it's called late.
27         # This ensures each page only pings once and prevents slow
28         # pings interrupting page builds.
29         hook(type => "change", id => "inline", 
30                 call => \&IkiWiki::pingurl);
31 } # }}}
33 sub getopt () { #{{{
34         eval q{use Getopt::Long};
35         error($@) if $@;
36         Getopt::Long::Configure('pass_through');
37         GetOptions(
38                 "rss!" => \$config{rss},
39                 "atom!" => \$config{atom},
40                 "allowrss!" => \$config{allowrss},
41                 "allowatom!" => \$config{allowatom},
42                 "pingurl=s" => sub {
43                         push @{$config{pingurl}}, $_[1];
44                 },      
45         );
46 } #}}}
48 sub getsetup () { #{{{
49         return
50                 rss => {
51                         type => "boolean",
52                         example => 0,
53                         description => "enable rss feeds by default?",
54                         safe => 1,
55                         rebuild => 1,
56                 },
57                 atom => {
58                         type => "boolean",
59                         example => 0,
60                         description => "enable atom feeds by default?",
61                         safe => 1,
62                         rebuild => 1,
63                 },
64                 allowrss => {
65                         type => "boolean",
66                         example => 0,
67                         description => "allow rss feeds to be used?",
68                         safe => 1,
69                         rebuild => 1,
70                 },
71                 allowatom => {
72                         type => "boolean",
73                         example => 0,
74                         description => "allow atom feeds to be used?",
75                         safe => 1,
76                         rebuild => 1,
77                 },
78                 pingurl => {
79                         type => "string",
80                         example => "http://rpc.technorati.com/rpc/ping",
81                         description => "urls to ping (using XML-RPC) on feed update",
82                         safe => 1,
83                         rebuild => 0,
84                 },
85 } #}}}
87 sub checkconfig () { #{{{
88         if (($config{rss} || $config{atom}) && ! length $config{url}) {
89                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
90         }
91         if ($config{rss}) {
92                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
93         }
94         if ($config{atom}) {
95                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
96         }
97         if (! exists $config{pingurl}) {
98                 $config{pingurl}=[];
99         }
100 } #}}}
102 sub format (@) { #{{{
103         my %params=@_;
105         # Fill in the inline content generated earlier. This is actually an
106         # optimisation.
107         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
108                 delete @inline[$1,]
109         }eg;
110         return $params{content};
111 } #}}}
113 sub sessioncgi () { #{{{
114         my $q=shift;
115         my $session=shift;
117         if ($q->param('do') eq 'blog') {
118                 my $page=IkiWiki::titlepage(decode_utf8($q->param('title')));
119                 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
120                 # if the page already exists, munge it to be unique
121                 my $from=$q->param('from');
122                 my $add="";
123                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
124                         $add=1 unless length $add;
125                         $add++;
126                 }
127                 $q->param('page', $page.$add);
128                 # now go create the page
129                 $q->param('do', 'create');
130                 IkiWiki::cgi_editpage($q, $session);
131                 exit;
132         }
135 # Back to ikiwiki namespace for the rest, this code is very much
136 # internal to ikiwiki even though it's separated into a plugin.
137 package IkiWiki;
139 my %toping;
140 my %feedlinks;
142 sub preprocess_inline (@) { #{{{
143         my %params=@_;
144         
145         if (! exists $params{pages}) {
146                 error gettext("missing pages parameter");
147         }
148         my $raw=yesno($params{raw});
149         my $archive=yesno($params{archive});
150         my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
151         my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
152         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
153         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
154         my $feedonly=yesno($params{feedonly});
155         if (! exists $params{show} && ! $archive) {
156                 $params{show}=10;
157         }
158         if (! exists $params{feedshow} && exists $params{show}) {
159                 $params{feedshow}=$params{show};
160         }
161         my $desc;
162         if (exists $params{description}) {
163                 $desc = $params{description} 
164         }
165         else {
166                 $desc = $config{wikiname};
167         }
168         my $actions=yesno($params{actions});
169         if (exists $params{template}) {
170                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
171         }
172         else {
173                 $params{template} = $archive ? "archivepage" : "inlinepage";
174         }
176         my @list;
177         foreach my $page (keys %pagesources) {
178                 next if $page eq $params{page};
179                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
180                         push @list, $page;
181                 }
182         }
184         if (exists $params{sort} && $params{sort} eq 'title') {
185                 @list=sort @list;
186         }
187         elsif (exists $params{sort} && $params{sort} eq 'mtime') {
188                 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
189         }
190         elsif (! exists $params{sort} || $params{sort} eq 'age') {
191                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
192         }
193         else {
194                 return sprintf(gettext("unknown sort type %s"), $params{sort});
195         }
197         if (yesno($params{reverse})) {
198                 @list=reverse(@list);
199         }
201         if (exists $params{skip}) {
202                 @list=@list[$params{skip} .. scalar @list - 1];
203         }
204         
205         my @feedlist;
206         if ($feeds) {
207                 if (exists $params{feedshow} &&
208                     $params{feedshow} && @list > $params{feedshow}) {
209                         @feedlist=@list[0..$params{feedshow} - 1];
210                 }
211                 else {
212                         @feedlist=@list;
213                 }
214         }
215         
216         if ($params{show} && @list > $params{show}) {
217                 @list=@list[0..$params{show} - 1];
218         }
220         add_depends($params{page}, $params{pages});
221         # Explicitly add all currently displayed pages as dependencies, so
222         # that if they are removed or otherwise changed, the inline will be
223         # sure to be updated.
224         add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
226         my $feednum="";
228         my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
229         if (exists $knownfeeds{$feedid}) {
230                 $feednum=$knownfeeds{$feedid};
231         }
232         else {
233                 if (exists $page_numfeeds{$params{destpage}}) {
234                         if ($feeds) {
235                                 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
236                         }
237                 }
238                 else {
239                         $feednum=$knownfeeds{$feedid}="";
240                         if ($feeds) {
241                                 $page_numfeeds{$params{destpage}}=1;
242                         }
243                 }
244         }
246         my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
247         my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
248         my $ret="";
250         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
251                         (exists $params{postform} && yesno($params{postform})))) {
252                 # Add a blog post form, with feed buttons.
253                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
254                 $formtemplate->param(cgiurl => $config{cgiurl});
255                 $formtemplate->param(rootpage => 
256                         exists $params{rootpage} ? $params{rootpage} : $params{page});
257                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
258                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
259                 if (exists $params{postformtext}) {
260                         $formtemplate->param(postformtext =>
261                                 $params{postformtext});
262                 }
263                 else {
264                         $formtemplate->param(postformtext =>
265                                 gettext("Add a new post titled:"));
266                 }
267                 $ret.=$formtemplate->output;
268         }
269         elsif ($feeds && !$params{preview}) {
270                 # Add feed buttons.
271                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
272                 $linktemplate->param(rssurl => $rssurl) if $rss;
273                 $linktemplate->param(atomurl => $atomurl) if $atom;
274                 $ret.=$linktemplate->output;
275         }
276         
277         if (! $feedonly) {
278                 require HTML::Template;
279                 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
280                 if (! @params) {
281                         return sprintf(gettext("nonexistant template %s"), $params{template});
282                 }
283                 my $template=HTML::Template->new(@params) unless $raw;
284         
285                 foreach my $page (@list) {
286                         my $file = $pagesources{$page};
287                         my $type = pagetype($file);
288                         if (! $raw || ($raw && ! defined $type)) {
289                                 unless ($archive && $quick) {
290                                         # Get the content before populating the
291                                         # template, since getting the content uses
292                                         # the same template if inlines are nested.
293                                         my $content=get_inline_content($page, $params{destpage});
294                                         $template->param(content => $content);
295                                 }
296                                 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
297                                 $template->param(title => pagetitle(basename($page)));
298                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
299                                 $template->param(first => 1) if $page eq $list[0];
300                                 $template->param(last => 1) if $page eq $list[$#list];
301         
302                                 if ($actions) {
303                                         my $file = $pagesources{$page};
304                                         my $type = pagetype($file);
305                                         if ($config{discussion}) {
306                                                 my $discussionlink=gettext("discussion");
307                                                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
308                                                     (length $config{cgiurl} ||
309                                                      exists $links{$page."/".$discussionlink})) {
310                                                         $template->param(have_actions => 1);
311                                                         $template->param(discussionlink =>
312                                                                 htmllink($page,
313                                                                         $params{destpage},
314                                                                         gettext("Discussion"),
315                                                                         noimageinline => 1,
316                                                                         forcesubpage => 1));
317                                                 }
318                                         }
319                                         if (length $config{cgiurl} && defined $type) {
320                                                 $template->param(have_actions => 1);
321                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
322                                         }
323                                 }
324         
325                                 run_hooks(pagetemplate => sub {
326                                         shift->(page => $page, destpage => $params{destpage},
327                                                 template => $template,);
328                                 });
329         
330                                 $ret.=$template->output;
331                                 $template->clear_params;
332                         }
333                         else {
334                                 if (defined $type) {
335                                         $ret.="\n".
336                                               linkify($page, $params{destpage},
337                                               preprocess($page, $params{destpage},
338                                               filter($page, $params{destpage},
339                                               readfile(srcfile($file)))));
340                                 }
341                         }
342                 }
343         }
344         
345         if ($feeds) {
346                 if (exists $params{feedpages}) {
347                         @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
348                 }
349         
350                 if ($rss) {
351                         my $rssp=rsspage($params{destpage}).$feednum;
352                         will_render($params{destpage}, $rssp);
353                         if (! $params{preview}) {
354                                 writefile($rssp, $config{destdir},
355                                         genfeed("rss",
356                                                 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
357                                 $toping{$params{destpage}}=1 unless $config{rebuild};
358                                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
359                         }
360                 }
361                 if ($atom) {
362                         my $atomp=atompage($params{destpage}).$feednum;
363                         will_render($params{destpage}, $atomp);
364                         if (! $params{preview}) {
365                                 writefile($atomp, $config{destdir},
366                                         genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
367                                 $toping{$params{destpage}}=1 unless $config{rebuild};
368                                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
369                         }
370                 }
371         }
372         
373         return $ret if $raw || $nested;
374         push @inline, $ret;
375         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
376 } #}}}
378 sub pagetemplate_inline (@) { #{{{
379         my %params=@_;
380         my $page=$params{page};
381         my $template=$params{template};
383         $template->param(feedlinks => $feedlinks{$page})
384                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
385 } #}}}
387 sub get_inline_content ($$) { #{{{
388         my $page=shift;
389         my $destpage=shift;
390         
391         my $file=$pagesources{$page};
392         my $type=pagetype($file);
393         if (defined $type) {
394                 $nested++;
395                 my $ret=htmlize($page, $destpage, $type,
396                        linkify($page, $destpage,
397                        preprocess($page, $destpage,
398                        filter($page, $destpage,
399                        readfile(srcfile($file))))));
400                 $nested--;
401                 return $ret;
402         }
403         else {
404                 return "";
405         }
406 } #}}}
408 sub date_822 ($) { #{{{
409         my $time=shift;
411         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
412         POSIX::setlocale(&POSIX::LC_TIME, "C");
413         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
414         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
415         return $ret;
416 } #}}}
418 sub date_3339 ($) { #{{{
419         my $time=shift;
421         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
422         POSIX::setlocale(&POSIX::LC_TIME, "C");
423         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
424         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
425         return $ret;
426 } #}}}
428 sub absolute_urls ($$) { #{{{
429         # sucky sub because rss sucks
430         my $content=shift;
431         my $baseurl=shift;
433         my $url=$baseurl;
434         $url=~s/[^\/]+$//;
436         # what is the non path part of the url?
437         my $top_uri = URI->new($url);
438         $top_uri->path_query(""); # reset the path
439         my $urltop = $top_uri->as_string;
441         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
442         # relative to another wiki page
443         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
444         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
445         # relative to the top of the site
446         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
447         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
448         return $content;
449 } #}}}
451 sub rsspage ($) { #{{{
452         return targetpage(shift, "rss");
453 } #}}}
455 sub atompage ($) { #{{{
456         return targetpage(shift, "atom");
457 } #}}}
459 sub genfeed ($$$$$@) { #{{{
460         my $feedtype=shift;
461         my $feedurl=shift;
462         my $feeddesc=shift;
463         my $guid=shift;
464         my $page=shift;
465         my @pages=@_;
466         
467         my $url=URI->new(encode_utf8(urlto($page,"",1)));
468         
469         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
470         my $content="";
471         my $lasttime = 0;
472         foreach my $p (@pages) {
473                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
474                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
476                 $itemtemplate->param(
477                         title => pagetitle(basename($p)),
478                         url => $u,
479                         permalink => $u,
480                         cdate_822 => date_822($pagectime{$p}),
481                         mdate_822 => date_822($pagemtime{$p}),
482                         cdate_3339 => date_3339($pagectime{$p}),
483                         mdate_3339 => date_3339($pagemtime{$p}),
484                 );
486                 if (exists $pagestate{$p} &&
487                     exists $pagestate{$p}{meta}{guid}) {
488                         $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
489                 }
491                 if ($itemtemplate->query(name => "enclosure")) {
492                         my $file=$pagesources{$p};
493                         my $type=pagetype($file);
494                         if (defined $type) {
495                                 $itemtemplate->param(content => $pcontent);
496                         }
497                         else {
498                                 my $size=(srcfile_stat($file))[8];
499                                 my $mime="unknown";
500                                 eval q{use File::MimeInfo};
501                                 if (! $@) {
502                                         $mime = mimetype($file);
503                                 }
504                                 $itemtemplate->param(
505                                         enclosure => $u,
506                                         type => $mime,
507                                         length => $size,
508                                 );
509                         }
510                 }
511                 else {
512                         $itemtemplate->param(content => $pcontent);
513                 }
515                 run_hooks(pagetemplate => sub {
516                         shift->(page => $p, destpage => $page,
517                                 template => $itemtemplate);
518                 });
520                 $content.=$itemtemplate->output;
521                 $itemtemplate->clear_params;
523                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
524         }
526         my $template=template($feedtype."page.tmpl", blind_cache => 1);
527         $template->param(
528                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
529                 wikiname => $config{wikiname},
530                 pageurl => $url,
531                 content => $content,
532                 feeddesc => $feeddesc,
533                 guid => $guid,
534                 feeddate => date_3339($lasttime),
535                 feedurl => $feedurl,
536                 version => $IkiWiki::version,
537         );
538         run_hooks(pagetemplate => sub {
539                 shift->(page => $page, destpage => $page,
540                         template => $template);
541         });
542         
543         return $template->output;
544 } #}}}
546 sub pingurl (@) { #{{{
547         return unless @{$config{pingurl}} && %toping;
549         eval q{require RPC::XML::Client};
550         if ($@) {
551                 debug(gettext("RPC::XML::Client not found, not pinging"));
552                 return;
553         }
555         # daemonize here so slow pings don't slow down wiki updates
556         defined(my $pid = fork) or error("Can't fork: $!");
557         return if $pid;
558         chdir '/';
559         POSIX::setsid() or error("Can't start a new session: $!");
560         open STDIN, '/dev/null';
561         open STDOUT, '>/dev/null';
562         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
564         # Don't need to keep a lock on the wiki as a daemon.
565         IkiWiki::unlockwiki();
567         foreach my $page (keys %toping) {
568                 my $title=pagetitle(basename($page), 0);
569                 my $url=urlto($page, "", 1);
570                 foreach my $pingurl (@{$config{pingurl}}) {
571                         debug("Pinging $pingurl for $page");
572                         eval {
573                                 my $client = RPC::XML::Client->new($pingurl);
574                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
575                                         $title, $url);
576                                 my $res = $client->send_request($req);
577                                 if (! ref $res) {
578                                         debug("Did not receive response to ping");
579                                 }
580                                 my $r=$res->value;
581                                 if (! exists $r->{flerror} || $r->{flerror}) {
582                                         debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
583                                 }
584                         };
585                         if ($@) {
586                                 debug "Ping failed: $@";
587                         }
588                 }
589         }
591         exit 0; # daemon done
592 } #}}}