2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
11 hook(type => "getopt", id => "inline", call => \&getopt);
12 hook(type => "checkconfig", id => "inline", call => \&checkconfig);
13 hook(type => "preprocess", id => "inline",
14 call => \&IkiWiki::preprocess_inline);
15 hook(type => "pagetemplate", id => "inline",
16 call => \&IkiWiki::pagetemplate_inline);
17 # Hook to change to do pinging since it's called late.
18 # This ensures each page only pings once and prevents slow
19 # pings interrupting page builds.
20 hook(type => "change", id => "inline",
21 call => \&IkiWiki::pingurl);
25 eval q{use Getopt::Long};
27 Getopt::Long::Configure('pass_through');
29 "rss!" => \$config{rss},
30 "atom!" => \$config{atom},
34 sub checkconfig () { #{{{
35 if (($config{rss} || $config{atom}) && ! length $config{url}) {
36 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
39 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
42 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
46 # Back to ikiwiki namespace for the rest, this code is very much
47 # internal to ikiwiki even though it's separated into a plugin.
55 return (defined $val && lc($val) eq "yes");
58 sub preprocess_inline (@) { #{{{
61 if (! exists $params{pages}) {
64 my $raw=yesno($params{raw});
65 my $archive=yesno($params{archive});
66 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
67 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
68 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
69 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
70 $feeds=0 if $params{preview};
71 if (! exists $params{show} && ! $archive) {
75 if (exists $params{description}) {
76 $desc = $params{description}
78 $desc = $config{wikiname};
80 my $actions=yesno($params{actions});
81 if (exists $params{template}) {
82 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
85 $params{template} = $archive ? "archivepage" : "inlinepage";
89 foreach my $page (keys %pagesources) {
90 next if $page eq $params{page};
91 if (pagespec_match($page, $params{pages}, $params{page})) {
96 if (exists $params{sort} && $params{sort} eq 'title') {
99 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
100 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
102 elsif (! exists $params{sort} || $params{sort} eq 'age') {
103 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
106 return sprintf(gettext("unknown sort type %s"), $params{sort});
109 if (yesno($params{reverse})) {
110 @list=reverse(@list);
113 if (exists $params{skip}) {
114 @list=@list[$params{skip} .. scalar @list - 1];
117 if ($params{show} && @list > $params{show}) {
118 @list=@list[0..$params{show} - 1];
121 add_depends($params{page}, $params{pages});
122 # Explicitly add all currently displayed pages as dependencies, so
123 # that if they are removed or otherwise changed, the inline will be
124 # sure to be updated.
125 add_depends($params{page}, join(" or ", @list));
127 my $rssurl=basename(rsspage($params{page}));
128 my $atomurl=basename(atompage($params{page}));
131 if ($config{cgiurl} && (exists $params{rootpage} ||
132 (exists $params{postform} && yesno($params{postform})))) {
133 # Add a blog post form, with feed buttons.
134 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
135 $formtemplate->param(cgiurl => $config{cgiurl});
136 $formtemplate->param(rootpage =>
137 exists $params{rootpage} ? $params{rootpage} : $params{page});
138 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
139 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
140 $ret.=$formtemplate->output;
144 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
145 $linktemplate->param(rssurl => $rssurl) if $rss;
146 $linktemplate->param(atomurl => $atomurl) if $atom;
147 $ret.=$linktemplate->output;
150 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
152 return sprintf(gettext("nonexistant template %s"), $params{template});
154 my $template=HTML::Template->new(@params) unless $raw;
156 foreach my $page (@list) {
157 my $file = $pagesources{$page};
158 my $type = pagetype($file);
159 if (! $raw || ($raw && ! defined $type)) {
160 unless ($archive && $quick) {
161 # Get the content before populating the
162 # template, since getting the content uses
163 # the same template if inlines are nested.
164 my $content=get_inline_content($page, $params{destpage});
165 $template->param(content => $content);
167 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
168 $template->param(title => pagetitle(basename($page)));
169 $template->param(ctime => displaytime($pagectime{$page}));
172 my $file = $pagesources{$page};
173 my $type = pagetype($file);
174 if ($config{discussion}) {
175 my $discussionlink=gettext("discussion");
176 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
177 (length $config{cgiurl} ||
178 exists $links{$page."/".$discussionlink})) {
179 $template->param(have_actions => 1);
180 $template->param(discussionlink =>
183 gettext("Discussion"),
188 if (length $config{cgiurl} && defined $type) {
189 $template->param(have_actions => 1);
190 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
194 run_hooks(pagetemplate => sub {
195 shift->(page => $page, destpage => $params{page},
196 template => $template,);
199 $ret.=$template->output;
200 $template->clear_params;
205 linkify($page, $params{page},
206 preprocess($page, $params{page},
208 readfile(srcfile($file)))));
214 if (exists $params{feedshow} && @list > $params{feedshow}) {
215 @list=@list[0..$params{feedshow} - 1];
219 my $rssp=rsspage($params{page});
220 will_render($params{page}, $rssp);
221 writefile($rssp, $config{destdir},
222 genfeed("rss", $rssurl, $desc, $params{page}, @list));
223 $toping{$params{page}}=1 unless $config{rebuild};
224 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
227 my $atomp=atompage($params{page});
228 will_render($params{page}, $atomp);
229 writefile($atomp, $config{destdir},
230 genfeed("atom", $atomurl, $desc, $params{page}, @list));
231 $toping{$params{page}}=1 unless $config{rebuild};
232 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
239 sub pagetemplate_inline (@) { #{{{
241 my $page=$params{page};
242 my $template=$params{template};
244 $template->param(feedlinks => $feedlinks{$page})
245 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
248 sub get_inline_content ($$) { #{{{
252 my $file=$pagesources{$page};
253 my $type=pagetype($file);
255 return htmlize($page, $type,
256 linkify($page, $destpage,
257 preprocess($page, $destpage,
259 readfile(srcfile($file))))));
266 sub date_822 ($) { #{{{
271 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
272 POSIX::setlocale(&POSIX::LC_TIME, "C");
273 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
274 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
278 sub date_3339 ($) { #{{{
283 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
284 POSIX::setlocale(&POSIX::LC_TIME, "C");
285 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
286 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
290 sub absolute_urls ($$) { #{{{
291 # sucky sub because rss sucks
298 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
299 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
300 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
304 sub rsspage ($) { #{{{
305 return targetpage(shift, "rss");
308 sub atompage ($) { #{{{
309 return targetpage(shift, "atom");
312 sub genfeed ($$$$@) { #{{{
319 my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
321 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
324 foreach my $p (@pages) {
325 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
327 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
329 $itemtemplate->param(
330 title => pagetitle(basename($p), 1),
333 date_822 => date_822($pagectime{$p}),
334 date_3339 => date_3339($pagectime{$p}),
337 if ($itemtemplate->query(name => "enclosure")) {
338 my $file=$pagesources{$p};
339 my $type=pagetype($file);
341 $itemtemplate->param(content => $pcontent);
344 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
346 eval q{use File::MimeInfo};
348 $mime = mimetype($file);
350 $itemtemplate->param(
358 $itemtemplate->param(content => $pcontent);
361 run_hooks(pagetemplate => sub {
362 shift->(page => $p, destpage => $page,
363 template => $itemtemplate);
366 $content.=$itemtemplate->output;
367 $itemtemplate->clear_params;
369 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
372 my $template=template($feedtype."page.tmpl", blind_cache => 1);
374 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
375 wikiname => $config{wikiname},
378 feeddesc => $feeddesc,
379 feeddate => date_3339($lasttime),
381 version => $IkiWiki::version,
383 run_hooks(pagetemplate => sub {
384 shift->(page => $page, destpage => $page,
385 template => $template);
388 return $template->output;
391 sub pingurl (@) { #{{{
392 return unless @{$config{pingurl}} && %toping;
394 eval q{require RPC::XML::Client};
396 debug(gettext("RPC::XML::Client not found, not pinging"));
400 # daemonize here so slow pings don't slow down wiki updates
401 defined(my $pid = fork) or error("Can't fork: $!");
404 eval q{use POSIX 'setsid'};
405 setsid() or error("Can't start a new session: $!");
406 open STDIN, '/dev/null';
407 open STDOUT, '>/dev/null';
408 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
410 # Don't need to keep a lock on the wiki as a daemon.
411 IkiWiki::unlockwiki();
413 foreach my $page (keys %toping) {
414 my $title=pagetitle(basename($page), 0);
415 my $url="$config{url}/".htmlpage($page);
416 foreach my $pingurl (@{$config{pingurl}}) {
417 debug("Pinging $pingurl for $page");
419 my $client = RPC::XML::Client->new($pingurl);
420 my $req = RPC::XML::request->new('weblogUpdates.ping',
422 my $res = $client->send_request($req);
424 debug("Did not receive response to ping");
427 if (! exists $r->{flerror} || $r->{flerror}) {
428 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
432 debug "Ping failed: $@";
437 exit 0; # daemon done