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 $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
165 my $feedonly=yesno($params{feedonly});
166 if (! exists $params{show} && ! $archive) {
169 if (! exists $params{feedshow} && exists $params{show}) {
170 $params{feedshow}=$params{show};
173 if (exists $params{description}) {
174 $desc = $params{description}
177 $desc = $config{wikiname};
179 my $actions=yesno($params{actions});
180 if (exists $params{template}) {
181 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
184 $params{template} = $archive ? "archivepage" : "inlinepage";
188 foreach my $page (keys %pagesources) {
189 next if $page eq $params{page};
190 if (pagespec_match($page, $params{pages}, location => $params{page})) {
195 if (exists $params{sort} && $params{sort} eq 'title') {
196 @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
198 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
199 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
201 elsif (! exists $params{sort} || $params{sort} eq 'age') {
202 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
205 error sprintf(gettext("unknown sort type %s"), $params{sort});
208 if (yesno($params{reverse})) {
209 @list=reverse(@list);
212 if (exists $params{skip}) {
213 @list=@list[$params{skip} .. scalar @list - 1];
218 if (exists $params{feedshow} &&
219 $params{feedshow} && @list > $params{feedshow}) {
220 @feedlist=@list[0..$params{feedshow} - 1];
227 if ($params{show} && @list > $params{show}) {
228 @list=@list[0..$params{show} - 1];
231 add_depends($params{page}, $params{pages});
232 # Explicitly add all currently displayed pages as dependencies, so
233 # that if they are removed or otherwise changed, the inline will be
234 # sure to be updated.
235 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
237 if ($feeds && exists $params{feedpages}) {
238 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
243 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
244 if (exists $knownfeeds{$feedid}) {
245 $feednum=$knownfeeds{$feedid};
248 if (exists $page_numfeeds{$params{destpage}}) {
250 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
254 $feednum=$knownfeeds{$feedid}="";
256 $page_numfeeds{$params{destpage}}=1;
261 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
262 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
265 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
266 (exists $params{postform} && yesno($params{postform}))) &&
267 IkiWiki->can("cgi_editpage")) {
268 # Add a blog post form, with feed buttons.
269 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
270 $formtemplate->param(cgiurl => $config{cgiurl});
272 if (exists $params{rootpage}) {
273 $rootpage=bestlink($params{page}, $params{rootpage});
274 if (!length $rootpage) {
275 $rootpage=$params{rootpage};
279 $rootpage=$params{page};
281 $formtemplate->param(rootpage => $rootpage);
282 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
283 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
284 if (exists $params{postformtext}) {
285 $formtemplate->param(postformtext =>
286 $params{postformtext});
289 $formtemplate->param(postformtext =>
290 gettext("Add a new post titled:"));
292 $ret.=$formtemplate->output;
294 # The post form includes the feed buttons, so
295 # emptyfeeds cannot be hidden.
298 elsif ($feeds && !$params{preview} &&
299 ! (! $emptyfeeds && ! @feedlist)) {
301 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
302 $linktemplate->param(rssurl => $rssurl) if $rss;
303 $linktemplate->param(atomurl => $atomurl) if $atom;
304 $ret.=$linktemplate->output;
308 require HTML::Template;
309 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
311 error sprintf(gettext("nonexistant template %s"), $params{template});
313 my $template=HTML::Template->new(@params) unless $raw;
315 foreach my $page (@list) {
316 my $file = $pagesources{$page};
317 my $type = pagetype($file);
318 if (! $raw || ($raw && ! defined $type)) {
319 unless ($archive && $quick) {
320 # Get the content before populating the
321 # template, since getting the content uses
322 # the same template if inlines are nested.
323 my $content=get_inline_content($page, $params{destpage});
324 $template->param(content => $content);
326 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
327 $template->param(title => pagetitle(basename($page)));
328 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
329 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
330 $template->param(first => 1) if $page eq $list[0];
331 $template->param(last => 1) if $page eq $list[$#list];
334 my $file = $pagesources{$page};
335 my $type = pagetype($file);
336 if ($config{discussion}) {
337 my $discussionlink=gettext("discussion");
338 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
339 (length $config{cgiurl} ||
340 exists $links{$page."/".$discussionlink})) {
341 $template->param(have_actions => 1);
342 $template->param(discussionlink =>
345 gettext("Discussion"),
350 if (length $config{cgiurl} && defined $type) {
351 $template->param(have_actions => 1);
352 $template->param(editurl => cgiurl(do => "edit", page => $page));
356 run_hooks(pagetemplate => sub {
357 shift->(page => $page, destpage => $params{destpage},
358 template => $template,);
361 $ret.=$template->output;
362 $template->clear_params;
367 linkify($page, $params{destpage},
368 preprocess($page, $params{destpage},
369 filter($page, $params{destpage},
370 readfile(srcfile($file)))));
376 if ($feeds && ! (! $emptyfeeds && ! @feedlist)) {
378 my $rssp=rsspage($params{destpage}).$feednum;
379 will_render($params{destpage}, $rssp);
380 if (! $params{preview}) {
381 writefile($rssp, $config{destdir},
383 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
384 $toping{$params{destpage}}=1 unless $config{rebuild};
385 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
389 my $atomp=atompage($params{destpage}).$feednum;
390 will_render($params{destpage}, $atomp);
391 if (! $params{preview}) {
392 writefile($atomp, $config{destdir},
393 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
394 $toping{$params{destpage}}=1 unless $config{rebuild};
395 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
400 return $ret if $raw || $nested;
402 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
405 sub pagetemplate_inline (@) { #{{{
407 my $page=$params{page};
408 my $template=$params{template};
410 $template->param(feedlinks => $feedlinks{$page})
411 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
414 sub get_inline_content ($$) { #{{{
418 my $file=$pagesources{$page};
419 my $type=pagetype($file);
422 my $ret=htmlize($page, $destpage, $type,
423 linkify($page, $destpage,
424 preprocess($page, $destpage,
425 filter($page, $destpage,
426 readfile(srcfile($file))))));
435 sub date_822 ($) { #{{{
438 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
439 POSIX::setlocale(&POSIX::LC_TIME, "C");
440 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
441 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
445 sub date_3339 ($) { #{{{
448 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
449 POSIX::setlocale(&POSIX::LC_TIME, "C");
450 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
451 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
455 sub absolute_urls ($$) { #{{{
456 # sucky sub because rss sucks
463 # what is the non path part of the url?
464 my $top_uri = URI->new($url);
465 $top_uri->path_query(""); # reset the path
466 my $urltop = $top_uri->as_string;
468 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
469 # relative to another wiki page
470 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
471 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
472 # relative to the top of the site
473 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
474 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
478 sub rsspage ($) { #{{{
479 return targetpage(shift, "rss");
482 sub atompage ($) { #{{{
483 return targetpage(shift, "atom");
486 sub genfeed ($$$$$@) { #{{{
494 my $url=URI->new(encode_utf8(urlto($page,"",1)));
496 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
499 foreach my $p (@pages) {
500 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
501 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
503 $itemtemplate->param(
504 title => pagetitle(basename($p)),
507 cdate_822 => date_822($pagectime{$p}),
508 mdate_822 => date_822($pagemtime{$p}),
509 cdate_3339 => date_3339($pagectime{$p}),
510 mdate_3339 => date_3339($pagemtime{$p}),
513 if (exists $pagestate{$p} &&
514 exists $pagestate{$p}{meta}{guid}) {
515 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
518 if ($itemtemplate->query(name => "enclosure")) {
519 my $file=$pagesources{$p};
520 my $type=pagetype($file);
522 $itemtemplate->param(content => $pcontent);
525 my $size=(srcfile_stat($file))[8];
527 eval q{use File::MimeInfo};
529 $mime = mimetype($file);
531 $itemtemplate->param(
539 $itemtemplate->param(content => $pcontent);
542 run_hooks(pagetemplate => sub {
543 shift->(page => $p, destpage => $page,
544 template => $itemtemplate);
547 $content.=$itemtemplate->output;
548 $itemtemplate->clear_params;
550 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
553 my $template=template($feedtype."page.tmpl", blind_cache => 1);
555 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
556 wikiname => $config{wikiname},
559 feeddesc => $feeddesc,
561 feeddate => date_3339($lasttime),
563 version => $IkiWiki::version,
565 run_hooks(pagetemplate => sub {
566 shift->(page => $page, destpage => $page,
567 template => $template);
570 return $template->output;
573 sub pingurl (@) { #{{{
574 return unless @{$config{pingurl}} && %toping;
576 eval q{require RPC::XML::Client};
578 debug(gettext("RPC::XML::Client not found, not pinging"));
582 # daemonize here so slow pings don't slow down wiki updates
583 defined(my $pid = fork) or error("Can't fork: $!");
586 POSIX::setsid() or error("Can't start a new session: $!");
587 open STDIN, '/dev/null';
588 open STDOUT, '>/dev/null';
589 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
591 # Don't need to keep a lock on the wiki as a daemon.
592 IkiWiki::unlockwiki();
594 foreach my $page (keys %toping) {
595 my $title=pagetitle(basename($page), 0);
596 my $url=urlto($page, "", 1);
597 foreach my $pingurl (@{$config{pingurl}}) {
598 debug("Pinging $pingurl for $page");
600 my $client = RPC::XML::Client->new($pingurl);
601 my $req = RPC::XML::request->new('weblogUpdates.ping',
603 my $res = $client->send_request($req);
605 error("Did not receive response to ping");
608 if (! exists $r->{flerror} || $r->{flerror}) {
609 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
613 error "Ping failed: $@";
618 exit 0; # daemon done