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