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