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 () { #{{{
53 description => "enable rss feeds by default?",
60 description => "enable atom feeds by default?",
67 description => "allow rss feeds to be used?",
74 description => "allow atom feeds to be used?",
80 example => "http://rpc.technorati.com/rpc/ping",
81 description => "urls to ping (using XML-RPC) on feed update",
87 sub checkconfig () { #{{{
88 if (($config{rss} || $config{atom}) && ! length $config{url}) {
89 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
92 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
95 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
97 if (! exists $config{pingurl}) {
102 sub format (@) { #{{{
105 # Fill in the inline content generated earlier. This is actually an
107 $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
110 return $params{content};
113 sub sessioncgi () { #{{{
117 if ($q->param('do') eq 'blog') {
118 my $page=IkiWiki::titlepage(decode_utf8($q->param('title')));
119 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
120 # if the page already exists, munge it to be unique
121 my $from=$q->param('from');
123 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
124 $add=1 unless length $add;
127 $q->param('page', $page.$add);
128 # now go create the page
129 $q->param('do', 'create');
130 IkiWiki::cgi_editpage($q, $session);
135 # Back to ikiwiki namespace for the rest, this code is very much
136 # internal to ikiwiki even though it's separated into a plugin.
142 sub preprocess_inline (@) { #{{{
145 if (! exists $params{pages}) {
146 error gettext("missing pages parameter");
148 my $raw=yesno($params{raw});
149 my $archive=yesno($params{archive});
150 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
151 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
152 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
153 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
154 my $feedonly=yesno($params{feedonly});
155 if (! exists $params{show} && ! $archive) {
158 if (! exists $params{feedshow} && exists $params{show}) {
159 $params{feedshow}=$params{show};
162 if (exists $params{description}) {
163 $desc = $params{description}
166 $desc = $config{wikiname};
168 my $actions=yesno($params{actions});
169 if (exists $params{template}) {
170 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
173 $params{template} = $archive ? "archivepage" : "inlinepage";
177 foreach my $page (keys %pagesources) {
178 next if $page eq $params{page};
179 if (pagespec_match($page, $params{pages}, location => $params{page})) {
184 if (exists $params{sort} && $params{sort} eq 'title') {
187 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
188 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
190 elsif (! exists $params{sort} || $params{sort} eq 'age') {
191 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
194 return sprintf(gettext("unknown sort type %s"), $params{sort});
197 if (yesno($params{reverse})) {
198 @list=reverse(@list);
201 if (exists $params{skip}) {
202 @list=@list[$params{skip} .. scalar @list - 1];
207 if (exists $params{feedshow} &&
208 $params{feedshow} && @list > $params{feedshow}) {
209 @feedlist=@list[0..$params{feedshow} - 1];
216 if ($params{show} && @list > $params{show}) {
217 @list=@list[0..$params{show} - 1];
220 add_depends($params{page}, $params{pages});
221 # Explicitly add all currently displayed pages as dependencies, so
222 # that if they are removed or otherwise changed, the inline will be
223 # sure to be updated.
224 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
228 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
229 if (exists $knownfeeds{$feedid}) {
230 $feednum=$knownfeeds{$feedid};
233 if (exists $page_numfeeds{$params{destpage}}) {
235 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
239 $feednum=$knownfeeds{$feedid}="";
241 $page_numfeeds{$params{destpage}}=1;
246 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
247 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
250 if ($config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
251 (exists $params{postform} && yesno($params{postform})))) {
252 # Add a blog post form, with feed buttons.
253 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
254 $formtemplate->param(cgiurl => $config{cgiurl});
255 $formtemplate->param(rootpage =>
256 exists $params{rootpage} ? $params{rootpage} : $params{page});
257 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
258 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
259 if (exists $params{postformtext}) {
260 $formtemplate->param(postformtext =>
261 $params{postformtext});
264 $formtemplate->param(postformtext =>
265 gettext("Add a new post titled:"));
267 $ret.=$formtemplate->output;
269 elsif ($feeds && !$params{preview}) {
271 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
272 $linktemplate->param(rssurl => $rssurl) if $rss;
273 $linktemplate->param(atomurl => $atomurl) if $atom;
274 $ret.=$linktemplate->output;
278 require HTML::Template;
279 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
281 return sprintf(gettext("nonexistant template %s"), $params{template});
283 my $template=HTML::Template->new(@params) unless $raw;
285 foreach my $page (@list) {
286 my $file = $pagesources{$page};
287 my $type = pagetype($file);
288 if (! $raw || ($raw && ! defined $type)) {
289 unless ($archive && $quick) {
290 # Get the content before populating the
291 # template, since getting the content uses
292 # the same template if inlines are nested.
293 my $content=get_inline_content($page, $params{destpage});
294 $template->param(content => $content);
296 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
297 $template->param(title => pagetitle(basename($page)));
298 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
299 $template->param(first => 1) if $page eq $list[0];
300 $template->param(last => 1) if $page eq $list[$#list];
303 my $file = $pagesources{$page};
304 my $type = pagetype($file);
305 if ($config{discussion}) {
306 my $discussionlink=gettext("discussion");
307 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
308 (length $config{cgiurl} ||
309 exists $links{$page."/".$discussionlink})) {
310 $template->param(have_actions => 1);
311 $template->param(discussionlink =>
314 gettext("Discussion"),
319 if (length $config{cgiurl} && defined $type) {
320 $template->param(have_actions => 1);
321 $template->param(editurl => cgiurl(do => "edit", page => $page));
325 run_hooks(pagetemplate => sub {
326 shift->(page => $page, destpage => $params{destpage},
327 template => $template,);
330 $ret.=$template->output;
331 $template->clear_params;
336 linkify($page, $params{destpage},
337 preprocess($page, $params{destpage},
338 filter($page, $params{destpage},
339 readfile(srcfile($file)))));
346 if (exists $params{feedpages}) {
347 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
351 my $rssp=rsspage($params{destpage}).$feednum;
352 will_render($params{destpage}, $rssp);
353 if (! $params{preview}) {
354 writefile($rssp, $config{destdir},
356 $config{url}."/".rsspage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
357 $toping{$params{destpage}}=1 unless $config{rebuild};
358 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
362 my $atomp=atompage($params{destpage}).$feednum;
363 will_render($params{destpage}, $atomp);
364 if (! $params{preview}) {
365 writefile($atomp, $config{destdir},
366 genfeed("atom", $config{url}."/".atompage($params{destpage}).$feednum, $desc, $params{guid}, $params{destpage}, @feedlist));
367 $toping{$params{destpage}}=1 unless $config{rebuild};
368 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
373 return $ret if $raw || $nested;
375 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
378 sub pagetemplate_inline (@) { #{{{
380 my $page=$params{page};
381 my $template=$params{template};
383 $template->param(feedlinks => $feedlinks{$page})
384 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
387 sub get_inline_content ($$) { #{{{
391 my $file=$pagesources{$page};
392 my $type=pagetype($file);
395 my $ret=htmlize($page, $destpage, $type,
396 linkify($page, $destpage,
397 preprocess($page, $destpage,
398 filter($page, $destpage,
399 readfile(srcfile($file))))));
408 sub date_822 ($) { #{{{
411 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
412 POSIX::setlocale(&POSIX::LC_TIME, "C");
413 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
414 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
418 sub date_3339 ($) { #{{{
421 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
422 POSIX::setlocale(&POSIX::LC_TIME, "C");
423 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
424 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
428 sub absolute_urls ($$) { #{{{
429 # sucky sub because rss sucks
436 # what is the non path part of the url?
437 my $top_uri = URI->new($url);
438 $top_uri->path_query(""); # reset the path
439 my $urltop = $top_uri->as_string;
441 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
442 # relative to another wiki page
443 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
444 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
445 # relative to the top of the site
446 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
447 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
451 sub rsspage ($) { #{{{
452 return targetpage(shift, "rss");
455 sub atompage ($) { #{{{
456 return targetpage(shift, "atom");
459 sub genfeed ($$$$$@) { #{{{
467 my $url=URI->new(encode_utf8(urlto($page,"",1)));
469 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
472 foreach my $p (@pages) {
473 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
474 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
476 $itemtemplate->param(
477 title => pagetitle(basename($p)),
480 cdate_822 => date_822($pagectime{$p}),
481 mdate_822 => date_822($pagemtime{$p}),
482 cdate_3339 => date_3339($pagectime{$p}),
483 mdate_3339 => date_3339($pagemtime{$p}),
486 if (exists $pagestate{$p} &&
487 exists $pagestate{$p}{meta}{guid}) {
488 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
491 if ($itemtemplate->query(name => "enclosure")) {
492 my $file=$pagesources{$p};
493 my $type=pagetype($file);
495 $itemtemplate->param(content => $pcontent);
498 my $size=(srcfile_stat($file))[8];
500 eval q{use File::MimeInfo};
502 $mime = mimetype($file);
504 $itemtemplate->param(
512 $itemtemplate->param(content => $pcontent);
515 run_hooks(pagetemplate => sub {
516 shift->(page => $p, destpage => $page,
517 template => $itemtemplate);
520 $content.=$itemtemplate->output;
521 $itemtemplate->clear_params;
523 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
526 my $template=template($feedtype."page.tmpl", blind_cache => 1);
528 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
529 wikiname => $config{wikiname},
532 feeddesc => $feeddesc,
534 feeddate => date_3339($lasttime),
536 version => $IkiWiki::version,
538 run_hooks(pagetemplate => sub {
539 shift->(page => $page, destpage => $page,
540 template => $template);
543 return $template->output;
546 sub pingurl (@) { #{{{
547 return unless @{$config{pingurl}} && %toping;
549 eval q{require RPC::XML::Client};
551 debug(gettext("RPC::XML::Client not found, not pinging"));
555 # daemonize here so slow pings don't slow down wiki updates
556 defined(my $pid = fork) or error("Can't fork: $!");
559 POSIX::setsid() or error("Can't start a new session: $!");
560 open STDIN, '/dev/null';
561 open STDOUT, '>/dev/null';
562 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
564 # Don't need to keep a lock on the wiki as a daemon.
565 IkiWiki::unlockwiki();
567 foreach my $page (keys %toping) {
568 my $title=pagetitle(basename($page), 0);
569 my $url=urlto($page, "", 1);
570 foreach my $pingurl (@{$config{pingurl}}) {
571 debug("Pinging $pingurl for $page");
573 my $client = RPC::XML::Client->new($pingurl);
574 my $req = RPC::XML::request->new('weblogUpdates.ping',
576 my $res = $client->send_request($req);
578 debug("Did not receive response to ping");
581 if (! exists $r->{flerror} || $r->{flerror}) {
582 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
586 debug "Ping failed: $@";
591 exit 0; # daemon done