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) {
117 if (! exists $params{feedshow} && exists $params{show}) {
118 $params{feedshow}=$params{show};
121 if (exists $params{description}) {
122 $desc = $params{description}
125 $desc = $config{wikiname};
127 my $actions=yesno($params{actions});
128 if (exists $params{template}) {
129 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
132 $params{template} = $archive ? "archivepage" : "inlinepage";
136 foreach my $page (keys %pagesources) {
137 next if $page eq $params{page};
138 if (pagespec_match($page, $params{pages}, location => $params{page})) {
143 if (exists $params{sort} && $params{sort} eq 'title') {
146 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
147 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
149 elsif (! exists $params{sort} || $params{sort} eq 'age') {
150 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
153 return sprintf(gettext("unknown sort type %s"), $params{sort});
156 if (yesno($params{reverse})) {
157 @list=reverse(@list);
160 if (exists $params{skip}) {
161 @list=@list[$params{skip} .. scalar @list - 1];
166 if (exists $params{feedshow} &&
167 $params{feedshow} && @list > $params{feedshow}) {
168 @feedlist=@list[0..$params{feedshow} - 1];
175 if ($params{show} && @list > $params{show}) {
176 @list=@list[0..$params{show} - 1];
179 add_depends($params{page}, $params{pages});
180 # Explicitly add all currently displayed pages as dependencies, so
181 # that if they are removed or otherwise changed, the inline will be
182 # sure to be updated.
183 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
187 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
188 if (exists $knownfeeds{$feedid}) {
189 $feednum=$knownfeeds{$feedid};
192 if (exists $page_numfeeds{$params{destpage}}) {
194 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
198 $feednum=$knownfeeds{$feedid}="";
200 $page_numfeeds{$params{destpage}}=1;
205 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
206 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
209 if ($config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
210 (exists $params{postform} && yesno($params{postform})))) {
211 # Add a blog post form, with feed buttons.
212 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
213 $formtemplate->param(cgiurl => $config{cgiurl});
214 $formtemplate->param(rootpage =>
215 exists $params{rootpage} ? $params{rootpage} : $params{page});
216 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
217 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
218 if (exists $params{postformtext}) {
219 $formtemplate->param(postformtext =>
220 $params{postformtext});
223 $formtemplate->param(postformtext =>
224 gettext("Add a new post titled:"));
226 $ret.=$formtemplate->output;
228 elsif ($feeds && !$params{preview}) {
230 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
231 $linktemplate->param(rssurl => $rssurl) if $rss;
232 $linktemplate->param(atomurl => $atomurl) if $atom;
233 $ret.=$linktemplate->output;
237 require HTML::Template;
238 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
240 return sprintf(gettext("nonexistant template %s"), $params{template});
242 my $template=HTML::Template->new(@params) unless $raw;
244 foreach my $page (@list) {
245 my $file = $pagesources{$page};
246 my $type = pagetype($file);
247 if (! $raw || ($raw && ! defined $type)) {
248 unless ($archive && $quick) {
249 # Get the content before populating the
250 # template, since getting the content uses
251 # the same template if inlines are nested.
252 my $content=get_inline_content($page, $params{destpage});
253 $template->param(content => $content);
255 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
256 $template->param(title => pagetitle(basename($page)));
257 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
258 $template->param(first => 1) if $page eq $list[0];
259 $template->param(last => 1) if $page eq $list[$#list];
262 my $file = $pagesources{$page};
263 my $type = pagetype($file);
264 if ($config{discussion}) {
265 my $discussionlink=gettext("discussion");
266 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
267 (length $config{cgiurl} ||
268 exists $links{$page."/".$discussionlink})) {
269 $template->param(have_actions => 1);
270 $template->param(discussionlink =>
273 gettext("Discussion"),
278 if (length $config{cgiurl} && defined $type) {
279 $template->param(have_actions => 1);
280 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
284 run_hooks(pagetemplate => sub {
285 shift->(page => $page, destpage => $params{destpage},
286 template => $template,);
289 $ret.=$template->output;
290 $template->clear_params;
295 linkify($page, $params{destpage},
296 preprocess($page, $params{destpage},
297 filter($page, $params{destpage},
298 readfile(srcfile($file)))));
305 if (exists $params{feedpages}) {
306 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
310 my $rssp=rsspage($params{destpage}).$feednum;
311 will_render($params{destpage}, $rssp);
312 if (! $params{preview}) {
313 writefile($rssp, $config{destdir},
315 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{destpage}, @feedlist));
316 $toping{$params{destpage}}=1 unless $config{rebuild};
317 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
321 my $atomp=atompage($params{destpage}).$feednum;
322 will_render($params{destpage}, $atomp);
323 if (! $params{preview}) {
324 writefile($atomp, $config{destdir},
325 genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{destpage}, @feedlist));
326 $toping{$params{destpage}}=1 unless $config{rebuild};
327 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
334 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
337 sub pagetemplate_inline (@) { #{{{
339 my $page=$params{page};
340 my $template=$params{template};
342 $template->param(feedlinks => $feedlinks{$page})
343 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
346 sub get_inline_content ($$) { #{{{
350 my $file=$pagesources{$page};
351 my $type=pagetype($file);
353 return htmlize($page, $type,
354 linkify($page, $destpage,
355 preprocess($page, $destpage,
356 filter($page, $destpage,
357 readfile(srcfile($file))))));
364 sub date_822 ($) { #{{{
367 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
368 POSIX::setlocale(&POSIX::LC_TIME, "C");
369 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
370 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
374 sub date_3339 ($) { #{{{
377 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
378 POSIX::setlocale(&POSIX::LC_TIME, "C");
379 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
380 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
384 sub absolute_urls ($$) { #{{{
385 # sucky sub because rss sucks
392 # what is the non path part of the url?
393 my $top_uri = URI->new($url);
394 $top_uri->path_query(""); # reset the path
395 my $urltop = $top_uri->as_string;
397 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
398 # relative to another wiki page
399 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
400 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
401 # relative to the top of the site
402 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
403 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
407 sub rsspage ($) { #{{{
408 return targetpage(shift, "rss");
411 sub atompage ($) { #{{{
412 return targetpage(shift, "atom");
415 sub genfeed ($$$$@) { #{{{
422 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
424 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
427 foreach my $p (@pages) {
428 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
429 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
431 $itemtemplate->param(
432 title => pagetitle(basename($p)),
435 cdate_822 => date_822($pagectime{$p}),
436 mdate_822 => date_822($pagemtime{$p}),
437 cdate_3339 => date_3339($pagectime{$p}),
438 mdate_3339 => date_3339($pagemtime{$p}),
441 if ($itemtemplate->query(name => "enclosure")) {
442 my $file=$pagesources{$p};
443 my $type=pagetype($file);
445 $itemtemplate->param(content => $pcontent);
448 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
450 eval q{use File::MimeInfo};
452 $mime = mimetype($file);
454 $itemtemplate->param(
462 $itemtemplate->param(content => $pcontent);
465 run_hooks(pagetemplate => sub {
466 shift->(page => $p, destpage => $page,
467 template => $itemtemplate);
470 $content.=$itemtemplate->output;
471 $itemtemplate->clear_params;
473 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
476 my $template=template($feedtype."page.tmpl", blind_cache => 1);
478 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
479 wikiname => $config{wikiname},
482 feeddesc => $feeddesc,
483 feeddate => date_3339($lasttime),
485 version => $IkiWiki::version,
487 run_hooks(pagetemplate => sub {
488 shift->(page => $page, destpage => $page,
489 template => $template);
492 return $template->output;
495 sub pingurl (@) { #{{{
496 return unless @{$config{pingurl}} && %toping;
498 eval q{require RPC::XML::Client};
500 debug(gettext("RPC::XML::Client not found, not pinging"));
504 # daemonize here so slow pings don't slow down wiki updates
505 defined(my $pid = fork) or error("Can't fork: $!");
508 setsid() or error("Can't start a new session: $!");
509 open STDIN, '/dev/null';
510 open STDOUT, '>/dev/null';
511 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
513 # Don't need to keep a lock on the wiki as a daemon.
514 IkiWiki::unlockwiki();
516 foreach my $page (keys %toping) {
517 my $title=pagetitle(basename($page), 0);
518 my $url="$config{url}/".urlto($page, "");
519 foreach my $pingurl (@{$config{pingurl}}) {
520 debug("Pinging $pingurl for $page");
522 my $client = RPC::XML::Client->new($pingurl);
523 my $req = RPC::XML::request->new('weblogUpdates.ping',
525 my $res = $client->send_request($req);
527 debug("Did not receive response to ping");
530 if (! exists $r->{flerror} || $r->{flerror}) {
531 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
535 debug "Ping failed: $@";
540 exit 0; # daemon done