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 if (exists $params{postformtext}) {
141 $formtemplate->param(postformtext =>
142 $params{postformtext});
145 $formtemplate->param(postformtext =>
146 gettext("Add a new post titled:"));
148 $ret.=$formtemplate->output;
152 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
153 $linktemplate->param(rssurl => $rssurl) if $rss;
154 $linktemplate->param(atomurl => $atomurl) if $atom;
155 $ret.=$linktemplate->output;
158 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
160 return sprintf(gettext("nonexistant template %s"), $params{template});
162 my $template=HTML::Template->new(@params) unless $raw;
164 foreach my $page (@list) {
165 my $file = $pagesources{$page};
166 my $type = pagetype($file);
167 if (! $raw || ($raw && ! defined $type)) {
168 unless ($archive && $quick) {
169 # Get the content before populating the
170 # template, since getting the content uses
171 # the same template if inlines are nested.
172 my $content=get_inline_content($page, $params{destpage});
173 $template->param(content => $content);
175 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
176 $template->param(title => pagetitle(basename($page)));
177 $template->param(ctime => displaytime($pagectime{$page}));
180 my $file = $pagesources{$page};
181 my $type = pagetype($file);
182 if ($config{discussion}) {
183 my $discussionlink=gettext("discussion");
184 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
185 (length $config{cgiurl} ||
186 exists $links{$page."/".$discussionlink})) {
187 $template->param(have_actions => 1);
188 $template->param(discussionlink =>
191 gettext("Discussion"),
196 if (length $config{cgiurl} && defined $type) {
197 $template->param(have_actions => 1);
198 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
202 run_hooks(pagetemplate => sub {
203 shift->(page => $page, destpage => $params{page},
204 template => $template,);
207 $ret.=$template->output;
208 $template->clear_params;
213 linkify($page, $params{page},
214 preprocess($page, $params{page},
216 readfile(srcfile($file)))));
222 if (exists $params{feedshow} && @list > $params{feedshow}) {
223 @list=@list[0..$params{feedshow} - 1];
227 my $rssp=rsspage($params{page});
228 will_render($params{page}, $rssp);
229 writefile($rssp, $config{destdir},
230 genfeed("rss", $rssurl, $desc, $params{page}, @list));
231 $toping{$params{page}}=1 unless $config{rebuild};
232 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
235 my $atomp=atompage($params{page});
236 will_render($params{page}, $atomp);
237 writefile($atomp, $config{destdir},
238 genfeed("atom", $atomurl, $desc, $params{page}, @list));
239 $toping{$params{page}}=1 unless $config{rebuild};
240 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
247 sub pagetemplate_inline (@) { #{{{
249 my $page=$params{page};
250 my $template=$params{template};
252 $template->param(feedlinks => $feedlinks{$page})
253 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
256 sub get_inline_content ($$) { #{{{
260 my $file=$pagesources{$page};
261 my $type=pagetype($file);
263 return htmlize($page, $type,
264 linkify($page, $destpage,
265 preprocess($page, $destpage,
267 readfile(srcfile($file))))));
274 sub date_822 ($) { #{{{
277 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
278 POSIX::setlocale(&POSIX::LC_TIME, "C");
279 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
280 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
284 sub date_3339 ($) { #{{{
287 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
288 POSIX::setlocale(&POSIX::LC_TIME, "C");
289 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
290 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
294 sub absolute_urls ($$) { #{{{
295 # sucky sub because rss sucks
302 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
303 $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
304 $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
308 sub rsspage ($) { #{{{
309 return targetpage(shift, "rss");
312 sub atompage ($) { #{{{
313 return targetpage(shift, "atom");
316 sub genfeed ($$$$@) { #{{{
323 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
325 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
328 foreach my $p (@pages) {
329 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
330 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
332 $itemtemplate->param(
333 title => pagetitle(basename($p), 1),
336 date_822 => date_822($pagectime{$p}),
337 date_3339 => date_3339($pagectime{$p}),
340 if ($itemtemplate->query(name => "enclosure")) {
341 my $file=$pagesources{$p};
342 my $type=pagetype($file);
344 $itemtemplate->param(content => $pcontent);
347 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
349 eval q{use File::MimeInfo};
351 $mime = mimetype($file);
353 $itemtemplate->param(
361 $itemtemplate->param(content => $pcontent);
364 run_hooks(pagetemplate => sub {
365 shift->(page => $p, destpage => $page,
366 template => $itemtemplate);
369 $content.=$itemtemplate->output;
370 $itemtemplate->clear_params;
372 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
375 my $template=template($feedtype."page.tmpl", blind_cache => 1);
377 title => $page ne "index" ? pagetitle($page, 1) : $config{wikiname},
378 wikiname => $config{wikiname},
381 feeddesc => $feeddesc,
382 feeddate => date_3339($lasttime),
384 version => $IkiWiki::version,
386 run_hooks(pagetemplate => sub {
387 shift->(page => $page, destpage => $page,
388 template => $template);
391 return $template->output;
394 sub pingurl (@) { #{{{
395 return unless @{$config{pingurl}} && %toping;
397 eval q{require RPC::XML::Client};
399 debug(gettext("RPC::XML::Client not found, not pinging"));
403 # daemonize here so slow pings don't slow down wiki updates
404 defined(my $pid = fork) or error("Can't fork: $!");
407 setsid() or error("Can't start a new session: $!");
408 open STDIN, '/dev/null';
409 open STDOUT, '>/dev/null';
410 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
412 # Don't need to keep a lock on the wiki as a daemon.
413 IkiWiki::unlockwiki();
415 foreach my $page (keys %toping) {
416 my $title=pagetitle(basename($page), 0);
417 my $url="$config{url}/".urlto($page, "");
418 foreach my $pingurl (@{$config{pingurl}}) {
419 debug("Pinging $pingurl for $page");
421 my $client = RPC::XML::Client->new($pingurl);
422 my $req = RPC::XML::request->new('weblogUpdates.ping',
424 my $res = $client->send_request($req);
426 debug("Did not receive response to ping");
429 if (! exists $r->{flerror} || $r->{flerror}) {
430 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
434 debug "Ping failed: $@";
439 exit 0; # daemon done