2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
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);
33 eval q{use Getopt::Long};
35 Getopt::Long::Configure('pass_through');
37 "rss!" => \$config{rss},
38 "atom!" => \$config{atom},
39 "allowrss!" => \$config{allowrss},
40 "allowatom!" => \$config{allowatom},
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"));
49 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
52 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
59 # Fill in the inline content generated earlier. This is actually an
61 $params{content}=~s!<div class="inline" id="([^"]+)"></div>!$inline[$1]!g;
62 return $params{content};
65 sub sessioncgi () { #{{{
69 if ($q->param('do') eq 'blog') {
70 my $page=decode_utf8($q->param('title'));
71 $page=~s/\///g; # no slashes in blog posts
72 # if the page already exists, munge it to be unique
73 my $from=$q->param('from');
75 while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
76 $add=1 unless length $add;
79 $q->param('page', $page.$add);
80 # now go create the page
81 $q->param('do', 'create');
82 IkiWiki::cgi_editpage($q, $session);
87 # Back to ikiwiki namespace for the rest, this code is very much
88 # internal to ikiwiki even though it's separated into a plugin.
96 return (defined $val && lc($val) eq "yes");
99 sub preprocess_inline (@) { #{{{
102 if (! exists $params{pages}) {
105 my $raw=yesno($params{raw});
106 my $archive=yesno($params{archive});
107 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
108 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
109 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
110 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
111 my $feedonly=yesno($params{feedonly});
112 if (! exists $params{show} && ! $archive) {
116 if (exists $params{description}) {
117 $desc = $params{description}
119 $desc = $config{wikiname};
121 my $actions=yesno($params{actions});
122 if (exists $params{template}) {
123 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
126 $params{template} = $archive ? "archivepage" : "inlinepage";
130 foreach my $page (keys %pagesources) {
131 next if $page eq $params{page};
132 if (pagespec_match($page, $params{pages}, location => $params{page})) {
137 if (exists $params{sort} && $params{sort} eq 'title') {
140 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
141 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
143 elsif (! exists $params{sort} || $params{sort} eq 'age') {
144 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
147 return sprintf(gettext("unknown sort type %s"), $params{sort});
150 if (yesno($params{reverse})) {
151 @list=reverse(@list);
154 if (exists $params{skip}) {
155 @list=@list[$params{skip} .. scalar @list - 1];
158 if ($params{show} && @list > $params{show}) {
159 @list=@list[0..$params{show} - 1];
162 add_depends($params{page}, $params{pages});
163 # Explicitly add all currently displayed pages as dependencies, so
164 # that if they are removed or otherwise changed, the inline will be
165 # sure to be updated.
166 add_depends($params{page}, join(" or ", @list));
170 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
171 if (exists $knownfeeds{$feedid}) {
172 $feednum=$knownfeeds{$feedid};
175 if (exists $page_numfeeds{$params{destpage}}) {
177 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
181 $feednum=$knownfeeds{$feedid}="";
183 $page_numfeeds{$params{destpage}}=1;
188 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
189 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
192 if ($config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
193 (exists $params{postform} && yesno($params{postform})))) {
194 # Add a blog post form, with feed buttons.
195 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
196 $formtemplate->param(cgiurl => $config{cgiurl});
197 $formtemplate->param(rootpage =>
198 exists $params{rootpage} ? $params{rootpage} : $params{page});
199 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
200 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
201 if (exists $params{postformtext}) {
202 $formtemplate->param(postformtext =>
203 $params{postformtext});
206 $formtemplate->param(postformtext =>
207 gettext("Add a new post titled:"));
209 $ret.=$formtemplate->output;
211 elsif ($feeds && !$params{preview}) {
213 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
214 $linktemplate->param(rssurl => $rssurl) if $rss;
215 $linktemplate->param(atomurl => $atomurl) if $atom;
216 $ret.=$linktemplate->output;
220 require HTML::Template;
221 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
223 return sprintf(gettext("nonexistant template %s"), $params{template});
225 my $template=HTML::Template->new(@params) unless $raw;
227 foreach my $page (@list) {
228 my $file = $pagesources{$page};
229 my $type = pagetype($file);
230 if (! $raw || ($raw && ! defined $type)) {
231 unless ($archive && $quick) {
232 # Get the content before populating the
233 # template, since getting the content uses
234 # the same template if inlines are nested.
235 my $content=get_inline_content($page, $params{destpage});
236 $template->param(content => $content);
238 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
239 $template->param(title => pagetitle(basename($page)));
240 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
241 $template->param(first => 1) if $page eq $list[0];
242 $template->param(last => 1) if $page eq $list[$#list];
245 my $file = $pagesources{$page};
246 my $type = pagetype($file);
247 if ($config{discussion}) {
248 my $discussionlink=gettext("discussion");
249 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
250 (length $config{cgiurl} ||
251 exists $links{$page."/".$discussionlink})) {
252 $template->param(have_actions => 1);
253 $template->param(discussionlink =>
256 gettext("Discussion"),
261 if (length $config{cgiurl} && defined $type) {
262 $template->param(have_actions => 1);
263 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
267 run_hooks(pagetemplate => sub {
268 shift->(page => $page, destpage => $params{destpage},
269 template => $template,);
272 $ret.=$template->output;
273 $template->clear_params;
278 linkify($page, $params{destpage},
279 preprocess($page, $params{destpage},
280 filter($page, $params{destpage},
281 readfile(srcfile($file)))));
288 if (exists $params{feedshow} && @list > $params{feedshow}) {
289 @list=@list[0..$params{feedshow} - 1];
291 if (exists $params{feedpages}) {
292 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
296 my $rssp=rsspage($params{destpage}).$feednum;
297 will_render($params{destpage}, $rssp);
298 if (! $params{preview}) {
299 writefile($rssp, $config{destdir},
301 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{destpage}, @list));
302 $toping{$params{destpage}}=1 unless $config{rebuild};
303 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
307 my $atomp=atompage($params{destpage}).$feednum;
308 will_render($params{destpage}, $atomp);
309 if (! $params{preview}) {
310 writefile($atomp, $config{destdir},
311 genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{destpage}, @list));
312 $toping{$params{destpage}}=1 unless $config{rebuild};
313 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
320 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
323 sub pagetemplate_inline (@) { #{{{
325 my $page=$params{page};
326 my $template=$params{template};
328 $template->param(feedlinks => $feedlinks{$page})
329 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
332 sub get_inline_content ($$) { #{{{
336 my $file=$pagesources{$page};
337 my $type=pagetype($file);
339 return htmlize($page, $type,
340 linkify($page, $destpage,
341 preprocess($page, $destpage,
342 filter($page, $destpage,
343 readfile(srcfile($file))))));
350 sub date_822 ($) { #{{{
353 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
354 POSIX::setlocale(&POSIX::LC_TIME, "C");
355 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
356 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
360 sub date_3339 ($) { #{{{
363 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
364 POSIX::setlocale(&POSIX::LC_TIME, "C");
365 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
366 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
370 sub absolute_urls ($$) { #{{{
371 # sucky sub because rss sucks
378 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
379 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^"]+)"/$1 href="$url$2"/mig;
380 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^"]+)"/$1 src="$url$2"/mig;
384 sub rsspage ($) { #{{{
385 return targetpage(shift, "rss");
388 sub atompage ($) { #{{{
389 return targetpage(shift, "atom");
392 sub genfeed ($$$$@) { #{{{
399 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
401 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
404 foreach my $p (@pages) {
405 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
406 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
408 $itemtemplate->param(
409 title => pagetitle(basename($p)),
412 cdate_822 => date_822($pagectime{$p}),
413 mdate_822 => date_822($pagemtime{$p}),
414 cdate_3339 => date_3339($pagectime{$p}),
415 mdate_3339 => date_3339($pagemtime{$p}),
418 if ($itemtemplate->query(name => "enclosure")) {
419 my $file=$pagesources{$p};
420 my $type=pagetype($file);
422 $itemtemplate->param(content => $pcontent);
425 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
427 eval q{use File::MimeInfo};
429 $mime = mimetype($file);
431 $itemtemplate->param(
439 $itemtemplate->param(content => $pcontent);
442 run_hooks(pagetemplate => sub {
443 shift->(page => $p, destpage => $page,
444 template => $itemtemplate);
447 $content.=$itemtemplate->output;
448 $itemtemplate->clear_params;
450 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
453 my $template=template($feedtype."page.tmpl", blind_cache => 1);
455 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
456 wikiname => $config{wikiname},
459 feeddesc => $feeddesc,
460 feeddate => date_3339($lasttime),
462 version => $IkiWiki::version,
464 run_hooks(pagetemplate => sub {
465 shift->(page => $page, destpage => $page,
466 template => $template);
469 return $template->output;
472 sub pingurl (@) { #{{{
473 return unless @{$config{pingurl}} && %toping;
475 eval q{require RPC::XML::Client};
477 debug(gettext("RPC::XML::Client not found, not pinging"));
481 # daemonize here so slow pings don't slow down wiki updates
482 defined(my $pid = fork) or error("Can't fork: $!");
485 setsid() or error("Can't start a new session: $!");
486 open STDIN, '/dev/null';
487 open STDOUT, '>/dev/null';
488 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
490 # Don't need to keep a lock on the wiki as a daemon.
491 IkiWiki::unlockwiki();
493 foreach my $page (keys %toping) {
494 my $title=pagetitle(basename($page), 0);
495 my $url="$config{url}/".urlto($page, "");
496 foreach my $pingurl (@{$config{pingurl}}) {
497 debug("Pinging $pingurl for $page");
499 my $client = RPC::XML::Client->new($pingurl);
500 my $req = RPC::XML::request->new('weblogUpdates.ping',
502 my $res = $client->send_request($req);
504 debug("Did not receive response to ping");
507 if (! exists $r->{flerror} || $r->{flerror}) {
508 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
512 debug "Ping failed: $@";
517 exit 0; # daemon done