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=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 # make sure the editpage plugin in loaded
135 if (IkiWiki->can("cgi_editpage")) {
136 IkiWiki::cgi_editpage($q, $session);
139 error(gettext("page editing not allowed"));
145 # Back to ikiwiki namespace for the rest, this code is very much
146 # internal to ikiwiki even though it's separated into a plugin.
152 sub preprocess_inline (@) { #{{{
155 if (! exists $params{pages}) {
156 error gettext("missing pages parameter");
158 my $raw=yesno($params{raw});
159 my $archive=yesno($params{archive});
160 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
161 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
162 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
163 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
164 my $feedonly=yesno($params{feedonly});
165 if (! exists $params{show} && ! $archive) {
168 if (! exists $params{feedshow} && exists $params{show}) {
169 $params{feedshow}=$params{show};
172 if (exists $params{description}) {
173 $desc = $params{description}
176 $desc = $config{wikiname};
178 my $actions=yesno($params{actions});
179 if (exists $params{template}) {
180 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
183 $params{template} = $archive ? "archivepage" : "inlinepage";
187 foreach my $page (keys %pagesources) {
188 next if $page eq $params{page};
189 if (pagespec_match($page, $params{pages}, location => $params{page})) {
194 if (exists $params{sort} && $params{sort} eq 'title') {
195 @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
197 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
198 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
200 elsif (! exists $params{sort} || $params{sort} eq 'age') {
201 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
204 return sprintf(gettext("unknown sort type %s"), $params{sort});
207 if (yesno($params{reverse})) {
208 @list=reverse(@list);
211 if (exists $params{skip}) {
212 @list=@list[$params{skip} .. scalar @list - 1];
217 if (exists $params{feedshow} &&
218 $params{feedshow} && @list > $params{feedshow}) {
219 @feedlist=@list[0..$params{feedshow} - 1];
226 if ($params{show} && @list > $params{show}) {
227 @list=@list[0..$params{show} - 1];
230 add_depends($params{page}, $params{pages});
231 # Explicitly add all currently displayed pages as dependencies, so
232 # that if they are removed or otherwise changed, the inline will be
233 # sure to be updated.
234 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
238 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
239 if (exists $knownfeeds{$feedid}) {
240 $feednum=$knownfeeds{$feedid};
243 if (exists $page_numfeeds{$params{destpage}}) {
245 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
249 $feednum=$knownfeeds{$feedid}="";
251 $page_numfeeds{$params{destpage}}=1;
256 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
257 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
260 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
261 (exists $params{postform} && yesno($params{postform}))) &&
262 IkiWiki->can("cgi_editpage")) {
263 # Add a blog post form, with feed buttons.
264 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
265 $formtemplate->param(cgiurl => $config{cgiurl});
267 if (exists $params{rootpage}) {
268 $rootpage=bestlink($params{page}, $params{rootpage});
269 if (!length $rootpage) {
270 $rootpage=$params{rootpage};
274 $rootpage=$params{page};
276 $formtemplate->param(rootpage => $rootpage);
277 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
278 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
279 if (exists $params{postformtext}) {
280 $formtemplate->param(postformtext =>
281 $params{postformtext});
284 $formtemplate->param(postformtext =>
285 gettext("Add a new post titled:"));
287 $ret.=$formtemplate->output;
289 elsif ($feeds && !$params{preview}) {
291 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
292 $linktemplate->param(rssurl => $rssurl) if $rss;
293 $linktemplate->param(atomurl => $atomurl) if $atom;
294 $ret.=$linktemplate->output;
298 require HTML::Template;
299 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
301 return sprintf(gettext("nonexistant template %s"), $params{template});
303 my $template=HTML::Template->new(@params) unless $raw;
305 foreach my $page (@list) {
306 my $file = $pagesources{$page};
307 my $type = pagetype($file);
308 if (! $raw || ($raw && ! defined $type)) {
309 unless ($archive && $quick) {
310 # Get the content before populating the
311 # template, since getting the content uses
312 # the same template if inlines are nested.
313 my $content=get_inline_content($page, $params{destpage});
314 $template->param(content => $content);
316 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
317 $template->param(title => pagetitle(basename($page)));
318 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
319 $template->param(ctime_raw => scalar localtime($pagectime{$page}));
320 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
321 $template->param(mtime_raw => scalar localtime($pagemtime{$page}));
322 $template->param(first => 1) if $page eq $list[0];
323 $template->param(last => 1) if $page eq $list[$#list];
326 my $file = $pagesources{$page};
327 my $type = pagetype($file);
328 if ($config{discussion}) {
329 my $discussionlink=gettext("discussion");
330 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
331 (length $config{cgiurl} ||
332 exists $links{$page."/".$discussionlink})) {
333 $template->param(have_actions => 1);
334 $template->param(discussionlink =>
337 gettext("Discussion"),
342 if (length $config{cgiurl} && defined $type) {
343 $template->param(have_actions => 1);
344 $template->param(editurl => cgiurl(do => "edit", page => $page));
348 run_hooks(pagetemplate => sub {
349 shift->(page => $page, destpage => $params{destpage},
350 template => $template,);
353 $ret.=$template->output;
354 $template->clear_params;
359 linkify($page, $params{destpage},
360 preprocess($page, $params{destpage},
361 filter($page, $params{destpage},
362 readfile(srcfile($file)))));
369 if (exists $params{feedpages}) {
370 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
374 my $rssp=rsspage($params{destpage}).$feednum;
375 will_render($params{destpage}, $rssp);
376 if (! $params{preview}) {
377 writefile($rssp, $config{destdir},
379 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
380 $toping{$params{destpage}}=1 unless $config{rebuild};
381 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
385 my $atomp=atompage($params{destpage}).$feednum;
386 will_render($params{destpage}, $atomp);
387 if (! $params{preview}) {
388 writefile($atomp, $config{destdir},
389 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
390 $toping{$params{destpage}}=1 unless $config{rebuild};
391 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
396 return $ret if $raw || $nested;
398 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
401 sub pagetemplate_inline (@) { #{{{
403 my $page=$params{page};
404 my $template=$params{template};
406 $template->param(feedlinks => $feedlinks{$page})
407 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
410 sub get_inline_content ($$) { #{{{
414 my $file=$pagesources{$page};
415 my $type=pagetype($file);
418 my $ret=htmlize($page, $destpage, $type,
419 linkify($page, $destpage,
420 preprocess($page, $destpage,
421 filter($page, $destpage,
422 readfile(srcfile($file))))));
431 sub date_822 ($) { #{{{
434 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
435 POSIX::setlocale(&POSIX::LC_TIME, "C");
436 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
437 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
441 sub date_3339 ($) { #{{{
444 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
445 POSIX::setlocale(&POSIX::LC_TIME, "C");
446 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
447 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
451 sub absolute_urls ($$) { #{{{
452 # sucky sub because rss sucks
459 # what is the non path part of the url?
460 my $top_uri = URI->new($url);
461 $top_uri->path_query(""); # reset the path
462 my $urltop = $top_uri->as_string;
464 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
465 # relative to another wiki page
466 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
467 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
468 # relative to the top of the site
469 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
470 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
474 sub rsspage ($) { #{{{
475 return targetpage(shift, "rss");
478 sub atompage ($) { #{{{
479 return targetpage(shift, "atom");
482 sub genfeed ($$$$$@) { #{{{
490 my $url=URI->new(encode_utf8(urlto($page,"",1)));
492 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
495 foreach my $p (@pages) {
496 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
497 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
499 $itemtemplate->param(
500 title => pagetitle(basename($p)),
503 cdate_822 => date_822($pagectime{$p}),
504 mdate_822 => date_822($pagemtime{$p}),
505 cdate_3339 => date_3339($pagectime{$p}),
506 mdate_3339 => date_3339($pagemtime{$p}),
509 if (exists $pagestate{$p} &&
510 exists $pagestate{$p}{meta}{guid}) {
511 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
514 if ($itemtemplate->query(name => "enclosure")) {
515 my $file=$pagesources{$p};
516 my $type=pagetype($file);
518 $itemtemplate->param(content => $pcontent);
521 my $size=(srcfile_stat($file))[8];
523 eval q{use File::MimeInfo};
525 $mime = mimetype($file);
527 $itemtemplate->param(
535 $itemtemplate->param(content => $pcontent);
538 run_hooks(pagetemplate => sub {
539 shift->(page => $p, destpage => $page,
540 template => $itemtemplate);
543 $content.=$itemtemplate->output;
544 $itemtemplate->clear_params;
546 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
549 my $template=template($feedtype."page.tmpl", blind_cache => 1);
551 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
552 wikiname => $config{wikiname},
555 feeddesc => $feeddesc,
557 feeddate => date_3339($lasttime),
559 version => $IkiWiki::version,
561 run_hooks(pagetemplate => sub {
562 shift->(page => $page, destpage => $page,
563 template => $template);
566 return $template->output;
569 sub pingurl (@) { #{{{
570 return unless @{$config{pingurl}} && %toping;
572 eval q{require RPC::XML::Client};
574 debug(gettext("RPC::XML::Client not found, not pinging"));
578 # daemonize here so slow pings don't slow down wiki updates
579 defined(my $pid = fork) or error("Can't fork: $!");
582 POSIX::setsid() or error("Can't start a new session: $!");
583 open STDIN, '/dev/null';
584 open STDOUT, '>/dev/null';
585 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
587 # Don't need to keep a lock on the wiki as a daemon.
588 IkiWiki::unlockwiki();
590 foreach my $page (keys %toping) {
591 my $title=pagetitle(basename($page), 0);
592 my $url=urlto($page, "", 1);
593 foreach my $pingurl (@{$config{pingurl}}) {
594 debug("Pinging $pingurl for $page");
596 my $client = RPC::XML::Client->new($pingurl);
597 my $req = RPC::XML::request->new('weblogUpdates.ping',
599 my $res = $client->send_request($req);
601 error("Did not receive response to ping");
604 if (! exists $r->{flerror} || $r->{flerror}) {
605 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
609 error "Ping failed: $@";
614 exit 0; # daemon done