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, first => 1);
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", 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},
42 push @{$config{pingurl}}, $_[1];
56 description => "enable rss feeds by default?",
63 description => "enable atom feeds by default?",
70 description => "allow rss feeds to be used?",
77 description => "allow atom feeds to be used?",
83 example => "http://rpc.technorati.com/rpc/ping",
84 description => "urls to ping (using XML-RPC) on feed update",
91 if (($config{rss} || $config{atom}) && ! length $config{url}) {
92 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
95 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
98 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
100 if (! exists $config{pingurl}) {
108 # Fill in the inline content generated earlier. This is actually an
110 $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
113 return $params{content};
116 sub sessioncgi ($$) {
120 if ($q->param('do') eq 'blog') {
121 my $page=titlepage(decode_utf8($q->param('title')));
122 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
123 # if the page already exists, munge it to be unique
124 my $from=$q->param('from');
126 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
127 $add=1 unless length $add;
130 $q->param('page', $page.$add);
131 # now go create the page
132 $q->param('do', 'create');
133 # make sure the editpage plugin in loaded
134 if (IkiWiki->can("cgi_editpage")) {
135 IkiWiki::cgi_editpage($q, $session);
138 error(gettext("page editing not allowed"));
144 # Back to ikiwiki namespace for the rest, this code is very much
145 # internal to ikiwiki even though it's separated into a plugin.
151 sub preprocess_inline (@) {
154 if (! exists $params{pages} && ! exists $params{pagenames}) {
155 error gettext("missing pages parameter");
157 my $raw=yesno($params{raw});
158 my $archive=yesno($params{archive});
159 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
160 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
161 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
162 my $feeds=! $nested && (exists $params{feeds} ? yesno($params{feeds}) : !$quick);
163 my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
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";
188 if (exists $params{pagenames}) {
189 foreach my $p (qw(sort pages)) {
190 if (exists $params{$p}) {
191 error sprintf(gettext("the %s and %s parameters cannot be used together"),
196 @list = map { bestlink($params{page}, $_) }
197 split ' ', $params{pagenames};
199 if (yesno($params{reverse})) {
200 @list=reverse(@list);
203 foreach my $p (@list) {
204 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
212 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
213 $num=$params{feedshow};
215 if ($params{skip} && $num) {
219 @list = pagespec_match_list($params{page}, $params{pages},
220 deptype => deptype($quick ? "presence" : "content"),
221 filter => sub { $_[0] eq $params{page} },
222 sort => exists $params{sort} ? $params{sort} : "age",
223 reverse => yesno($params{reverse}),
224 ($num ? (num => $num) : ()),
228 if (exists $params{skip}) {
229 @list=@list[$params{skip} .. $#list];
234 if (exists $params{feedshow} &&
235 $params{feedshow} && @list > $params{feedshow}) {
236 @feedlist=@list[0..$params{feedshow} - 1];
243 if ($params{show} && @list > $params{show}) {
244 @list=@list[0..$params{show} - 1];
247 if ($feeds && exists $params{feedpages}) {
248 @feedlist = pagespec_match_list(
249 $params{page}, "($params{pages}) and ($params{feedpages})",
250 deptype => deptype($quick ? "presence" : "content"),
255 my ($feedbase, $feednum);
257 # Ensure that multiple feeds on a page go to unique files.
259 # Feedfile can lead to conflicts if usedirs is not enabled,
260 # so avoid supporting it in that case.
261 delete $params{feedfile} if ! $config{usedirs};
262 # Tight limits on legal feedfiles, to avoid security issues
264 if (defined $params{feedfile}) {
265 if ($params{feedfile} =~ /\// ||
266 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
267 error("illegal feedfile");
269 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
271 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
273 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
274 if (exists $knownfeeds{$feedid}) {
275 $feednum=$knownfeeds{$feedid};
278 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
280 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
284 $feednum=$knownfeeds{$feedid}="";
286 $page_numfeeds{$params{destpage}}{$feedbase}=1;
292 my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
293 my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
297 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
298 (exists $params{postform} && yesno($params{postform}))) &&
299 IkiWiki->can("cgi_editpage")) {
300 # Add a blog post form, with feed buttons.
301 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
302 $formtemplate->param(cgiurl => $config{cgiurl});
303 $formtemplate->param(rootpage => rootpage(%params));
304 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
305 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
306 if (exists $params{postformtext}) {
307 $formtemplate->param(postformtext =>
308 $params{postformtext});
311 $formtemplate->param(postformtext =>
312 gettext("Add a new post titled:"));
314 $ret.=$formtemplate->output;
316 # The post form includes the feed buttons, so
317 # emptyfeeds cannot be hidden.
320 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
322 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
323 $linktemplate->param(rssurl => $rssurl) if $rss;
324 $linktemplate->param(atomurl => $atomurl) if $atom;
325 $ret.=$linktemplate->output;
329 require HTML::Template;
330 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
332 error sprintf(gettext("nonexistant template %s"), $params{template});
334 my $template=HTML::Template->new(@params) unless $raw;
335 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
337 foreach my $page (@list) {
338 my $file = $pagesources{$page};
339 my $type = pagetype($file);
342 # Get the content before populating the
343 # template, since getting the content uses
344 # the same template if inlines are nested.
345 my $content=get_inline_content($page, $params{destpage});
346 $template->param(content => $content);
348 $template->param(pageurl => urlto($page, $params{destpage}));
349 $template->param(inlinepage => $page);
350 $template->param(title => pagetitle(basename($page)));
351 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
352 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
353 $template->param(first => 1) if $page eq $list[0];
354 $template->param(last => 1) if $page eq $list[$#list];
357 my $file = $pagesources{$page};
358 my $type = pagetype($file);
359 if ($config{discussion}) {
360 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
361 (length $config{cgiurl} ||
362 exists $pagesources{$page."/".$config{discussionpage}})) {
363 $template->param(have_actions => 1);
364 $template->param(discussionlink =>
367 $config{discussionpage},
372 if (length $config{cgiurl} && defined $type) {
373 $template->param(have_actions => 1);
374 $template->param(editurl => cgiurl(do => "edit", page => $page));
378 run_hooks(pagetemplate => sub {
379 shift->(page => $page, destpage => $params{destpage},
380 template => $template,);
383 $ret.=$template->output;
384 $template->clear_params;
389 linkify($page, $params{destpage},
390 preprocess($page, $params{destpage},
391 filter($page, $params{destpage},
392 readfile(srcfile($file)))));
396 readfile(srcfile($file));
402 if ($feeds && ($emptyfeeds || @feedlist)) {
404 my $rssp=$feedbase."rss".$feednum;
405 will_render($params{destpage}, $rssp);
406 if (! $params{preview}) {
407 writefile($rssp, $config{destdir},
409 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
410 $toping{$params{destpage}}=1 unless $config{rebuild};
411 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
415 my $atomp=$feedbase."atom".$feednum;
416 will_render($params{destpage}, $atomp);
417 if (! $params{preview}) {
418 writefile($atomp, $config{destdir},
419 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
420 $toping{$params{destpage}}=1 unless $config{rebuild};
421 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
426 clear_inline_content_cache();
428 return $ret if $raw || $nested;
430 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
433 sub pagetemplate_inline (@) {
435 my $page=$params{page};
436 my $template=$params{template};
438 $template->param(feedlinks => $feedlinks{$page})
439 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
444 my $cached_destpage="";
446 sub get_inline_content ($$) {
450 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
451 return $inline_content{$page};
454 my $file=$pagesources{$page};
455 my $type=pagetype($file);
459 $ret=htmlize($page, $destpage, $type,
460 linkify($page, $destpage,
461 preprocess($page, $destpage,
462 filter($page, $destpage,
463 readfile(srcfile($file))))));
467 if ($cached_destpage ne $destpage) {
468 clear_inline_content_cache();
469 $cached_destpage=$destpage;
471 return $inline_content{$page}=$ret;
474 sub clear_inline_content_cache () {
483 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
484 POSIX::setlocale(&POSIX::LC_TIME, "C");
485 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
486 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
493 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
494 POSIX::setlocale(&POSIX::LC_TIME, "C");
495 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
496 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
500 sub absolute_urls ($$) {
501 # sucky sub because rss sucks
508 # what is the non path part of the url?
509 my $top_uri = URI->new($url);
510 $top_uri->path_query(""); # reset the path
511 my $urltop = $top_uri->as_string;
513 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
514 # relative to another wiki page
515 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
516 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
517 # relative to the top of the site
518 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
519 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
523 sub genfeed ($$$$$@) {
531 my $url=URI->new(encode_utf8(urlto($page,"",1)));
533 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
536 foreach my $p (@pages) {
537 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
538 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
540 $itemtemplate->param(
541 title => pagetitle(basename($p)),
544 cdate_822 => date_822($pagectime{$p}),
545 mdate_822 => date_822($pagemtime{$p}),
546 cdate_3339 => date_3339($pagectime{$p}),
547 mdate_3339 => date_3339($pagemtime{$p}),
550 if (exists $pagestate{$p}) {
551 if (exists $pagestate{$p}{meta}{guid}) {
552 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
555 if (exists $pagestate{$p}{meta}{updated}) {
556 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
557 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
561 if ($itemtemplate->query(name => "enclosure")) {
562 my $file=$pagesources{$p};
563 my $type=pagetype($file);
565 $itemtemplate->param(content => $pcontent);
568 my $size=(srcfile_stat($file))[8];
570 eval q{use File::MimeInfo};
572 $mime = mimetype($file);
574 $itemtemplate->param(
582 $itemtemplate->param(content => $pcontent);
585 run_hooks(pagetemplate => sub {
586 shift->(page => $p, destpage => $page,
587 template => $itemtemplate);
590 $content.=$itemtemplate->output;
591 $itemtemplate->clear_params;
593 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
596 my $template=template($feedtype."page.tmpl", blind_cache => 1);
598 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
599 wikiname => $config{wikiname},
602 feeddesc => $feeddesc,
604 feeddate => date_3339($lasttime),
606 version => $IkiWiki::version,
608 run_hooks(pagetemplate => sub {
609 shift->(page => $page, destpage => $page,
610 template => $template);
613 return $template->output;
617 return unless @{$config{pingurl}} && %toping;
619 eval q{require RPC::XML::Client};
621 debug(gettext("RPC::XML::Client not found, not pinging"));
625 # daemonize here so slow pings don't slow down wiki updates
626 defined(my $pid = fork) or error("Can't fork: $!");
629 POSIX::setsid() or error("Can't start a new session: $!");
630 open STDIN, '/dev/null';
631 open STDOUT, '>/dev/null';
632 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
634 # Don't need to keep a lock on the wiki as a daemon.
635 IkiWiki::unlockwiki();
637 foreach my $page (keys %toping) {
638 my $title=pagetitle(basename($page), 0);
639 my $url=urlto($page, "", 1);
640 foreach my $pingurl (@{$config{pingurl}}) {
641 debug("Pinging $pingurl for $page");
643 my $client = RPC::XML::Client->new($pingurl);
644 my $req = RPC::XML::request->new('weblogUpdates.ping',
646 my $res = $client->send_request($req);
648 error("Did not receive response to ping");
651 if (! exists $r->{flerror} || $r->{flerror}) {
652 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
656 error "Ping failed: $@";
661 exit 0; # daemon done
669 if (exists $params{rootpage}) {
670 $rootpage=bestlink($params{page}, $params{rootpage});
671 if (!length $rootpage) {
672 $rootpage=$params{rootpage};
676 $rootpage=$params{page};