2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
17 hook(type => "getopt", id => "inline", call => \&getopt);
18 hook(type => "getsetup", id => "inline", call => \&getsetup);
19 hook(type => "checkconfig", id => "inline", call => \&checkconfig);
20 hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
21 hook(type => "preprocess", id => "inline",
22 call => \&IkiWiki::preprocess_inline);
23 hook(type => "pagetemplate", id => "inline",
24 call => \&IkiWiki::pagetemplate_inline);
25 hook(type => "format", id => "inline", call => \&format);
26 # Hook to change to do pinging since it's called late.
27 # This ensures each page only pings once and prevents slow
28 # pings interrupting page builds.
29 hook(type => "change", id => "inline",
30 call => \&IkiWiki::pingurl);
34 eval q{use Getopt::Long};
36 Getopt::Long::Configure('pass_through');
38 "rss!" => \$config{rss},
39 "atom!" => \$config{atom},
40 "allowrss!" => \$config{allowrss},
41 "allowatom!" => \$config{allowatom},
43 push @{$config{pingurl}}, $_[1];
48 sub getsetup () { #{{{
57 description => "enable rss feeds by default?",
64 description => "enable atom feeds by default?",
71 description => "allow rss feeds to be used?",
78 description => "allow atom feeds to be used?",
84 example => "http://rpc.technorati.com/rpc/ping",
85 description => "urls to ping (using XML-RPC) on feed update",
91 sub checkconfig () { #{{{
92 if (($config{rss} || $config{atom}) && ! length $config{url}) {
93 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
96 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
99 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
101 if (! exists $config{pingurl}) {
106 sub format (@) { #{{{
109 # Fill in the inline content generated earlier. This is actually an
111 $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
114 return $params{content};
117 sub sessioncgi () { #{{{
121 if ($q->param('do') eq 'blog') {
122 my $page=IkiWiki::titlepage(decode_utf8($q->param('title')));
123 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
124 # if the page already exists, munge it to be unique
125 my $from=$q->param('from');
127 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
128 $add=1 unless length $add;
131 $q->param('page', $page.$add);
132 # now go create the page
133 $q->param('do', 'create');
134 IkiWiki::cgi_editpage($q, $session);
139 # Back to ikiwiki namespace for the rest, this code is very much
140 # internal to ikiwiki even though it's separated into a plugin.
146 sub preprocess_inline (@) { #{{{
149 if (! exists $params{pages}) {
150 error gettext("missing pages parameter");
152 my $raw=yesno($params{raw});
153 my $archive=yesno($params{archive});
154 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
155 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
156 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
157 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
158 my $feedonly=yesno($params{feedonly});
159 if (! exists $params{show} && ! $archive) {
162 if (! exists $params{feedshow} && exists $params{show}) {
163 $params{feedshow}=$params{show};
166 if (exists $params{description}) {
167 $desc = $params{description}
170 $desc = $config{wikiname};
172 my $actions=yesno($params{actions});
173 if (exists $params{template}) {
174 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
177 $params{template} = $archive ? "archivepage" : "inlinepage";
181 foreach my $page (keys %pagesources) {
182 next if $page eq $params{page};
183 if (pagespec_match($page, $params{pages}, location => $params{page})) {
188 if (exists $params{sort} && $params{sort} eq 'title') {
191 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
192 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
194 elsif (! exists $params{sort} || $params{sort} eq 'age') {
195 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
198 return sprintf(gettext("unknown sort type %s"), $params{sort});
201 if (yesno($params{reverse})) {
202 @list=reverse(@list);
205 if (exists $params{skip}) {
206 @list=@list[$params{skip} .. scalar @list - 1];
211 if (exists $params{feedshow} &&
212 $params{feedshow} && @list > $params{feedshow}) {
213 @feedlist=@list[0..$params{feedshow} - 1];
220 if ($params{show} && @list > $params{show}) {
221 @list=@list[0..$params{show} - 1];
224 add_depends($params{page}, $params{pages});
225 # Explicitly add all currently displayed pages as dependencies, so
226 # that if they are removed or otherwise changed, the inline will be
227 # sure to be updated.
228 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
232 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
233 if (exists $knownfeeds{$feedid}) {
234 $feednum=$knownfeeds{$feedid};
237 if (exists $page_numfeeds{$params{destpage}}) {
239 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
243 $feednum=$knownfeeds{$feedid}="";
245 $page_numfeeds{$params{destpage}}=1;
250 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
251 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
254 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
255 (exists $params{postform} && yesno($params{postform})))) {
256 # Add a blog post form, with feed buttons.
257 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
258 $formtemplate->param(cgiurl => $config{cgiurl});
259 $formtemplate->param(rootpage =>
260 exists $params{rootpage} ? $params{rootpage} : $params{page});
261 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
262 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
263 if (exists $params{postformtext}) {
264 $formtemplate->param(postformtext =>
265 $params{postformtext});
268 $formtemplate->param(postformtext =>
269 gettext("Add a new post titled:"));
271 $ret.=$formtemplate->output;
273 elsif ($feeds && !$params{preview}) {
275 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
276 $linktemplate->param(rssurl => $rssurl) if $rss;
277 $linktemplate->param(atomurl => $atomurl) if $atom;
278 $ret.=$linktemplate->output;
282 require HTML::Template;
283 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
285 return sprintf(gettext("nonexistant template %s"), $params{template});
287 my $template=HTML::Template->new(@params) unless $raw;
289 foreach my $page (@list) {
290 my $file = $pagesources{$page};
291 my $type = pagetype($file);
292 if (! $raw || ($raw && ! defined $type)) {
293 unless ($archive && $quick) {
294 # Get the content before populating the
295 # template, since getting the content uses
296 # the same template if inlines are nested.
297 my $content=get_inline_content($page, $params{destpage});
298 $template->param(content => $content);
300 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
301 $template->param(title => pagetitle(basename($page)));
302 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
303 $template->param(first => 1) if $page eq $list[0];
304 $template->param(last => 1) if $page eq $list[$#list];
307 my $file = $pagesources{$page};
308 my $type = pagetype($file);
309 if ($config{discussion}) {
310 my $discussionlink=gettext("discussion");
311 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
312 (length $config{cgiurl} ||
313 exists $links{$page."/".$discussionlink})) {
314 $template->param(have_actions => 1);
315 $template->param(discussionlink =>
318 gettext("Discussion"),
323 if (length $config{cgiurl} && defined $type) {
324 $template->param(have_actions => 1);
325 $template->param(editurl => cgiurl(do => "edit", page => $page));
329 run_hooks(pagetemplate => sub {
330 shift->(page => $page, destpage => $params{destpage},
331 template => $template,);
334 $ret.=$template->output;
335 $template->clear_params;
340 linkify($page, $params{destpage},
341 preprocess($page, $params{destpage},
342 filter($page, $params{destpage},
343 readfile(srcfile($file)))));
350 if (exists $params{feedpages}) {
351 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
355 my $rssp=rsspage($params{destpage}).$feednum;
356 will_render($params{destpage}, $rssp);
357 if (! $params{preview}) {
358 writefile($rssp, $config{destdir},
360 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
361 $toping{$params{destpage}}=1 unless $config{rebuild};
362 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
366 my $atomp=atompage($params{destpage}).$feednum;
367 will_render($params{destpage}, $atomp);
368 if (! $params{preview}) {
369 writefile($atomp, $config{destdir},
370 genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
371 $toping{$params{destpage}}=1 unless $config{rebuild};
372 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
377 return $ret if $raw || $nested;
379 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
382 sub pagetemplate_inline (@) { #{{{
384 my $page=$params{page};
385 my $template=$params{template};
387 $template->param(feedlinks => $feedlinks{$page})
388 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
391 sub get_inline_content ($$) { #{{{
395 my $file=$pagesources{$page};
396 my $type=pagetype($file);
399 my $ret=htmlize($page, $destpage, $type,
400 linkify($page, $destpage,
401 preprocess($page, $destpage,
402 filter($page, $destpage,
403 readfile(srcfile($file))))));
412 sub date_822 ($) { #{{{
415 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
416 POSIX::setlocale(&POSIX::LC_TIME, "C");
417 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
418 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
422 sub date_3339 ($) { #{{{
425 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
426 POSIX::setlocale(&POSIX::LC_TIME, "C");
427 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
428 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
432 sub absolute_urls ($$) { #{{{
433 # sucky sub because rss sucks
440 # what is the non path part of the url?
441 my $top_uri = URI->new($url);
442 $top_uri->path_query(""); # reset the path
443 my $urltop = $top_uri->as_string;
445 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
446 # relative to another wiki page
447 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
448 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
449 # relative to the top of the site
450 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
451 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
455 sub rsspage ($) { #{{{
456 return targetpage(shift, "rss");
459 sub atompage ($) { #{{{
460 return targetpage(shift, "atom");
463 sub genfeed ($$$$$@) { #{{{
471 my $url=URI->new(encode_utf8(urlto($page,"",1)));
473 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
476 foreach my $p (@pages) {
477 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
478 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
480 $itemtemplate->param(
481 title => pagetitle(basename($p)),
484 cdate_822 => date_822($pagectime{$p}),
485 mdate_822 => date_822($pagemtime{$p}),
486 cdate_3339 => date_3339($pagectime{$p}),
487 mdate_3339 => date_3339($pagemtime{$p}),
490 if (exists $pagestate{$p} &&
491 exists $pagestate{$p}{meta}{guid}) {
492 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
495 if ($itemtemplate->query(name => "enclosure")) {
496 my $file=$pagesources{$p};
497 my $type=pagetype($file);
499 $itemtemplate->param(content => $pcontent);
502 my $size=(srcfile_stat($file))[8];
504 eval q{use File::MimeInfo};
506 $mime = mimetype($file);
508 $itemtemplate->param(
516 $itemtemplate->param(content => $pcontent);
519 run_hooks(pagetemplate => sub {
520 shift->(page => $p, destpage => $page,
521 template => $itemtemplate);
524 $content.=$itemtemplate->output;
525 $itemtemplate->clear_params;
527 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
530 my $template=template($feedtype."page.tmpl", blind_cache => 1);
532 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
533 wikiname => $config{wikiname},
536 feeddesc => $feeddesc,
538 feeddate => date_3339($lasttime),
540 version => $IkiWiki::version,
542 run_hooks(pagetemplate => sub {
543 shift->(page => $page, destpage => $page,
544 template => $template);
547 return $template->output;
550 sub pingurl (@) { #{{{
551 return unless @{$config{pingurl}} && %toping;
553 eval q{require RPC::XML::Client};
555 debug(gettext("RPC::XML::Client not found, not pinging"));
559 # daemonize here so slow pings don't slow down wiki updates
560 defined(my $pid = fork) or error("Can't fork: $!");
563 POSIX::setsid() or error("Can't start a new session: $!");
564 open STDIN, '/dev/null';
565 open STDOUT, '>/dev/null';
566 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
568 # Don't need to keep a lock on the wiki as a daemon.
569 IkiWiki::unlockwiki();
571 foreach my $page (keys %toping) {
572 my $title=pagetitle(basename($page), 0);
573 my $url=urlto($page, "", 1);
574 foreach my $pingurl (@{$config{pingurl}}) {
575 debug("Pinging $pingurl for $page");
577 my $client = RPC::XML::Client->new($pingurl);
578 my $req = RPC::XML::request->new('weblogUpdates.ping',
580 my $res = $client->send_request($req);
582 debug("Did not receive response to ping");
585 if (! exists $r->{flerror} || $r->{flerror}) {
586 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
590 debug "Ping failed: $@";
595 exit 0; # daemon done