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