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>}{
64 return $params{content};
67 sub sessioncgi () { #{{{
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');
77 while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
78 $add=1 unless length $add;
81 $q->param('page', $page.$add);
82 # now go create the page
83 $q->param('do', 'create');
84 IkiWiki::cgi_editpage($q, $session);
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.
98 return (defined $val && lc($val) eq "yes");
101 sub preprocess_inline (@) { #{{{
104 if (! exists $params{pages}) {
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) {
118 if (exists $params{description}) {
119 $desc = $params{description}
121 $desc = $config{wikiname};
123 my $actions=yesno($params{actions});
124 if (exists $params{template}) {
125 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
128 $params{template} = $archive ? "archivepage" : "inlinepage";
132 foreach my $page (keys %pagesources) {
133 next if $page eq $params{page};
134 if (pagespec_match($page, $params{pages}, location => $params{page})) {
139 if (exists $params{sort} && $params{sort} eq 'title') {
142 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
143 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
145 elsif (! exists $params{sort} || $params{sort} eq 'age') {
146 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
149 return sprintf(gettext("unknown sort type %s"), $params{sort});
152 if (yesno($params{reverse})) {
153 @list=reverse(@list);
156 if (exists $params{skip}) {
157 @list=@list[$params{skip} .. scalar @list - 1];
160 if ($params{show} && @list > $params{show}) {
161 @list=@list[0..$params{show} - 1];
164 add_depends($params{page}, $params{pages});
165 # Explicitly add all currently displayed pages as dependencies, so
166 # that if they are removed or otherwise changed, the inline will be
167 # sure to be updated.
168 add_depends($params{page}, join(" or ", @list));
172 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
173 if (exists $knownfeeds{$feedid}) {
174 $feednum=$knownfeeds{$feedid};
177 if (exists $page_numfeeds{$params{destpage}}) {
179 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
183 $feednum=$knownfeeds{$feedid}="";
185 $page_numfeeds{$params{destpage}}=1;
190 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
191 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
194 if ($config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
195 (exists $params{postform} && yesno($params{postform})))) {
196 # Add a blog post form, with feed buttons.
197 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
198 $formtemplate->param(cgiurl => $config{cgiurl});
199 $formtemplate->param(rootpage =>
200 exists $params{rootpage} ? $params{rootpage} : $params{page});
201 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
202 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
203 if (exists $params{postformtext}) {
204 $formtemplate->param(postformtext =>
205 $params{postformtext});
208 $formtemplate->param(postformtext =>
209 gettext("Add a new post titled:"));
211 $ret.=$formtemplate->output;
213 elsif ($feeds && !$params{preview}) {
215 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
216 $linktemplate->param(rssurl => $rssurl) if $rss;
217 $linktemplate->param(atomurl => $atomurl) if $atom;
218 $ret.=$linktemplate->output;
222 require HTML::Template;
223 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
225 return sprintf(gettext("nonexistant template %s"), $params{template});
227 my $template=HTML::Template->new(@params) unless $raw;
229 foreach my $page (@list) {
230 my $file = $pagesources{$page};
231 my $type = pagetype($file);
232 if (! $raw || ($raw && ! defined $type)) {
233 unless ($archive && $quick) {
234 # Get the content before populating the
235 # template, since getting the content uses
236 # the same template if inlines are nested.
237 my $content=get_inline_content($page, $params{destpage});
238 $template->param(content => $content);
240 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
241 $template->param(title => pagetitle(basename($page)));
242 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
243 $template->param(first => 1) if $page eq $list[0];
244 $template->param(last => 1) if $page eq $list[$#list];
247 my $file = $pagesources{$page};
248 my $type = pagetype($file);
249 if ($config{discussion}) {
250 my $discussionlink=gettext("discussion");
251 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
252 (length $config{cgiurl} ||
253 exists $links{$page."/".$discussionlink})) {
254 $template->param(have_actions => 1);
255 $template->param(discussionlink =>
258 gettext("Discussion"),
263 if (length $config{cgiurl} && defined $type) {
264 $template->param(have_actions => 1);
265 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
269 run_hooks(pagetemplate => sub {
270 shift->(page => $page, destpage => $params{destpage},
271 template => $template,);
274 $ret.=$template->output;
275 $template->clear_params;
280 linkify($page, $params{destpage},
281 preprocess($page, $params{destpage},
282 filter($page, $params{destpage},
283 readfile(srcfile($file)))));
290 if (exists $params{feedshow} && @list > $params{feedshow}) {
291 @list=@list[0..$params{feedshow} - 1];
293 if (exists $params{feedpages}) {
294 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
298 my $rssp=rsspage($params{destpage}).$feednum;
299 will_render($params{destpage}, $rssp);
300 if (! $params{preview}) {
301 writefile($rssp, $config{destdir},
303 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{destpage}, @list));
304 $toping{$params{destpage}}=1 unless $config{rebuild};
305 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
309 my $atomp=atompage($params{destpage}).$feednum;
310 will_render($params{destpage}, $atomp);
311 if (! $params{preview}) {
312 writefile($atomp, $config{destdir},
313 genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{destpage}, @list));
314 $toping{$params{destpage}}=1 unless $config{rebuild};
315 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
322 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
325 sub pagetemplate_inline (@) { #{{{
327 my $page=$params{page};
328 my $template=$params{template};
330 $template->param(feedlinks => $feedlinks{$page})
331 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
334 sub get_inline_content ($$) { #{{{
338 my $file=$pagesources{$page};
339 my $type=pagetype($file);
341 return htmlize($page, $type,
342 linkify($page, $destpage,
343 preprocess($page, $destpage,
344 filter($page, $destpage,
345 readfile(srcfile($file))))));
352 sub date_822 ($) { #{{{
355 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
356 POSIX::setlocale(&POSIX::LC_TIME, "C");
357 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
358 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
362 sub date_3339 ($) { #{{{
365 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
366 POSIX::setlocale(&POSIX::LC_TIME, "C");
367 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
368 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
372 sub absolute_urls ($$) { #{{{
373 # sucky sub because rss sucks
380 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
381 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^"]+)"/$1 href="$url$2"/mig;
382 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^"]+)"/$1 src="$url$2"/mig;
386 sub rsspage ($) { #{{{
387 return targetpage(shift, "rss");
390 sub atompage ($) { #{{{
391 return targetpage(shift, "atom");
394 sub genfeed ($$$$@) { #{{{
401 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
403 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
406 foreach my $p (@pages) {
407 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
408 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
410 $itemtemplate->param(
411 title => pagetitle(basename($p)),
414 cdate_822 => date_822($pagectime{$p}),
415 mdate_822 => date_822($pagemtime{$p}),
416 cdate_3339 => date_3339($pagectime{$p}),
417 mdate_3339 => date_3339($pagemtime{$p}),
420 if ($itemtemplate->query(name => "enclosure")) {
421 my $file=$pagesources{$p};
422 my $type=pagetype($file);
424 $itemtemplate->param(content => $pcontent);
427 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
429 eval q{use File::MimeInfo};
431 $mime = mimetype($file);
433 $itemtemplate->param(
441 $itemtemplate->param(content => $pcontent);
444 run_hooks(pagetemplate => sub {
445 shift->(page => $p, destpage => $page,
446 template => $itemtemplate);
449 $content.=$itemtemplate->output;
450 $itemtemplate->clear_params;
452 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
455 my $template=template($feedtype."page.tmpl", blind_cache => 1);
457 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
458 wikiname => $config{wikiname},
461 feeddesc => $feeddesc,
462 feeddate => date_3339($lasttime),
464 version => $IkiWiki::version,
466 run_hooks(pagetemplate => sub {
467 shift->(page => $page, destpage => $page,
468 template => $template);
471 return $template->output;
474 sub pingurl (@) { #{{{
475 return unless @{$config{pingurl}} && %toping;
477 eval q{require RPC::XML::Client};
479 debug(gettext("RPC::XML::Client not found, not pinging"));
483 # daemonize here so slow pings don't slow down wiki updates
484 defined(my $pid = fork) or error("Can't fork: $!");
487 setsid() or error("Can't start a new session: $!");
488 open STDIN, '/dev/null';
489 open STDOUT, '>/dev/null';
490 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
492 # Don't need to keep a lock on the wiki as a daemon.
493 IkiWiki::unlockwiki();
495 foreach my $page (keys %toping) {
496 my $title=pagetitle(basename($page), 0);
497 my $url="$config{url}/".urlto($page, "");
498 foreach my $pingurl (@{$config{pingurl}}) {
499 debug("Pinging $pingurl for $page");
501 my $client = RPC::XML::Client->new($pingurl);
502 my $req = RPC::XML::request->new('weblogUpdates.ping',
504 my $res = $client->send_request($req);
506 debug("Did not receive response to ping");
509 if (! exists $r->{flerror} || $r->{flerror}) {
510 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
514 debug "Ping failed: $@";
519 exit 0; # daemon done