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, scan => 1);
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 => "rendered", 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];
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",
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}) {
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', "/$from/$page$add");
132 # now go create the page
133 $q->param('do', 'create');
134 # make sure the editpage plugin is 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} && ! exists $params{pagenames}) {
156 error gettext("missing pages parameter");
159 if (! defined wantarray) {
160 # Running in scan mode: only do the essentials
162 if (yesno($params{trail}) && IkiWiki::Plugin::trail->can("preprocess_trailitems")) {
163 # default to sorting age, the same as inline itself,
164 # but let the params override that
165 IkiWiki::Plugin::trail::preprocess_trailitems(sort => 'age', %params);
171 if (yesno($params{trail}) && IkiWiki::Plugin::trail->can("preprocess_trailitems")) {
172 scalar IkiWiki::Plugin::trail::preprocess_trailitems(sort => 'age', %params);
175 my $raw=yesno($params{raw});
176 my $archive=yesno($params{archive});
177 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
178 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
179 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
180 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick && ! $raw;
181 my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
182 my $feedonly=yesno($params{feedonly});
183 if (! exists $params{show} && ! $archive) {
186 if (! exists $params{feedshow} && exists $params{show}) {
187 $params{feedshow}=$params{show};
190 if (exists $params{description}) {
191 $desc = $params{description}
194 $desc = $config{wikiname};
196 my $actions=yesno($params{actions});
197 if (exists $params{template}) {
198 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
201 $params{template} = $archive ? "archivepage" : "inlinepage";
206 if (exists $params{pagenames}) {
207 foreach my $p (qw(sort pages)) {
208 if (exists $params{$p}) {
209 error sprintf(gettext("the %s and %s parameters cannot be used together"),
214 @list = split ' ', $params{pagenames};
216 if (yesno($params{reverse})) {
217 @list=reverse(@list);
220 foreach my $p (@list) {
221 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
224 @list = grep { exists $pagesources{$_} } @list;
231 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
232 $num=$params{feedshow};
234 if ($params{skip} && $num) {
238 @list = pagespec_match_list($params{page}, $params{pages},
239 deptype => deptype($quick ? "presence" : "content"),
240 filter => sub { $_[0] eq $params{page} },
241 sort => exists $params{sort} ? $params{sort} : "age",
242 reverse => yesno($params{reverse}),
243 ($num ? (num => $num) : ()),
247 if (exists $params{skip}) {
248 @list=@list[$params{skip} .. $#list];
253 if (exists $params{feedshow} &&
254 $params{feedshow} && @list > $params{feedshow}) {
255 @feedlist=@list[0..$params{feedshow} - 1];
262 if ($params{show} && @list > $params{show}) {
263 @list=@list[0..$params{show} - 1];
266 if ($feeds && exists $params{feedpages}) {
267 @feedlist = pagespec_match_list(
268 $params{page}, "($params{pages}) and ($params{feedpages})",
269 deptype => deptype($quick ? "presence" : "content"),
274 my ($feedbase, $feednum);
276 # Ensure that multiple feeds on a page go to unique files.
278 # Feedfile can lead to conflicts if usedirs is not enabled,
279 # so avoid supporting it in that case.
280 delete $params{feedfile} if ! $config{usedirs};
281 # Tight limits on legal feedfiles, to avoid security issues
283 if (defined $params{feedfile}) {
284 if ($params{feedfile} =~ /\// ||
285 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
286 error("illegal feedfile");
288 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
290 $feedbase=targetpage($params{page}, "", $params{feedfile});
292 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
293 if (exists $knownfeeds{$feedid}) {
294 $feednum=$knownfeeds{$feedid};
297 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
299 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
303 $feednum=$knownfeeds{$feedid}="";
305 $page_numfeeds{$params{destpage}}{$feedbase}=1;
311 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
314 $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
315 $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
318 $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
319 $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
325 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
326 (exists $params{postform} && yesno($params{postform}))) &&
327 IkiWiki->can("cgi_editpage")) {
328 # Add a blog post form, with feed buttons.
329 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
330 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
331 $formtemplate->param(rootpage => rootpage(%params));
334 $formtemplate->param(rssurl => $rssurl);
335 $formtemplate->param(rssdesc => $rssdesc);
338 $formtemplate->param(atomurl => $atomurl);
339 $formtemplate->param(atomdesc => $atomdesc);
342 if (exists $params{postformtext}) {
343 $formtemplate->param(postformtext =>
344 $params{postformtext});
347 $formtemplate->param(postformtext =>
348 gettext("Add a new post titled:"));
350 if (exists $params{id}) {
351 $formtemplate->param(postformid =>
354 $ret.=$formtemplate->output;
356 # The post form includes the feed buttons, so
357 # emptyfeeds cannot be hidden.
360 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
362 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
364 $linktemplate->param(rssurl => $rssurl);
365 $linktemplate->param(rssdesc => $rssdesc);
368 $linktemplate->param(atomurl => $atomurl);
369 $linktemplate->param(atomdesc => $atomdesc);
371 if (exists $params{id}) {
372 $linktemplate->param(id => $params{id});
374 $ret.=$linktemplate->output;
380 # cannot use wiki pages as templates; template not sanitized due to
383 $template=template_depends($params{template}.".tmpl", $params{page},
387 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
390 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
392 foreach my $page (@list) {
393 my $file = $pagesources{$page};
394 my $type = pagetype($file);
397 # Get the content before populating the
398 # template, since getting the content uses
399 # the same template if inlines are nested.
400 my $content=get_inline_content($page, $params{destpage});
401 $template->param(content => $content);
403 $template->param(pageurl => urlto($page, $params{destpage}));
404 $template->param(inlinepage => $page);
405 $template->param(title => pagetitle(basename($page)));
406 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
407 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
408 $template->param(first => 1) if $page eq $list[0];
409 $template->param(last => 1) if $page eq $list[$#list];
410 $template->param(html5 => $config{html5});
413 my $file = $pagesources{$page};
414 my $type = pagetype($file);
415 if ($config{discussion}) {
416 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
417 (length $config{cgiurl} ||
418 exists $pagesources{$page."/".lc($config{discussionpage})})) {
419 $template->param(have_actions => 1);
420 $template->param(discussionlink =>
423 $config{discussionpage},
428 if (length $config{cgiurl} &&
430 IkiWiki->can("cgi_editpage")) {
431 $template->param(have_actions => 1);
432 $template->param(editurl => cgiurl(do => "edit", page => $page));
437 run_hooks(pagetemplate => sub {
438 shift->(page => $page, destpage => $params{destpage},
439 template => $template,);
442 $ret.=$template->output;
443 $template->clear_params;
448 linkify($page, $params{destpage},
449 preprocess($page, $params{destpage},
450 filter($page, $params{destpage},
451 readfile(srcfile($file)))));
455 readfile(srcfile($file));
461 if ($feeds && ($emptyfeeds || @feedlist)) {
463 my $rssp=$feedbase."rss".$feednum;
464 will_render($params{destpage}, $rssp);
465 if (! $params{preview}) {
466 writefile($rssp, $config{destdir},
468 $config{url}."/".$rssp, $desc, $params{guid}, $params{page}, @feedlist));
469 $toping{$params{destpage}}=1 unless $config{rebuild};
470 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
474 my $atomp=$feedbase."atom".$feednum;
475 will_render($params{destpage}, $atomp);
476 if (! $params{preview}) {
477 writefile($atomp, $config{destdir},
478 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{page}, @feedlist));
479 $toping{$params{destpage}}=1 unless $config{rebuild};
480 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
485 clear_inline_content_cache();
487 return $ret if $raw || $nested;
489 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
492 sub pagetemplate_inline (@) {
494 my $page=$params{page};
495 my $template=$params{template};
497 $template->param(feedlinks => $feedlinks{$page})
498 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
503 my $cached_destpage="";
505 sub get_inline_content ($$) {
509 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
510 return $inline_content{$page};
513 my $file=$pagesources{$page};
514 my $type=pagetype($file);
518 $ret=htmlize($page, $destpage, $type,
519 linkify($page, $destpage,
520 preprocess($page, $destpage,
521 filter($page, $destpage,
522 readfile(srcfile($file))))));
524 if (isinternal($page)) {
525 # make inlined text of internal pages searchable
526 run_hooks(indexhtml => sub {
527 shift->(page => $page, destpage => $destpage,
533 if ($cached_destpage ne $destpage) {
534 clear_inline_content_cache();
535 $cached_destpage=$destpage;
537 return $inline_content{$page}=$ret;
540 sub clear_inline_content_cache () {
549 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
550 POSIX::setlocale(&POSIX::LC_TIME, "C");
551 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
552 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
556 sub absolute_urls ($$) {
557 # needed because rss sucks
563 my $urltop; # calculated if needed
567 eval q{use HTML::Parser; use HTML::Tagset};
569 my $p = HTML::Parser->new(api_version => 3);
570 $p->handler(default => sub { $ret.=join("", @_) }, "text");
571 $p->handler(start => sub {
572 my ($tagname, $pos, $text) = @_;
573 if (ref $HTML::Tagset::linkElements{$tagname}) {
575 # use attribute sets from right to left
576 # to avoid invalidating the offsets
577 # when replacing the values
578 my ($k_offset, $k_len, $v_offset, $v_len) =
580 my $attrname = lc(substr($text, $k_offset, $k_len));
581 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
582 next unless $v_offset; # 0 v_offset means no value
583 my $v = substr($text, $v_offset, $v_len);
584 $v =~ s/^([\'\"])(.*)\1$/$2/;
585 eval q{use HTML::Entities};
586 my $dv = decode_entities($v);
588 $v=$baseurl.$v; # anchor
590 elsif ($dv=~/^(?!\w+:)[^\/]/) {
591 $v=$url.$v; # relative url
594 if (! defined $urltop) {
595 # what is the non path part of the url?
596 my $top_uri = URI->new($url);
597 $top_uri->path_query(""); # reset the path
598 $urltop = $top_uri->as_string;
600 $v=$urltop.$v; # url relative to top of site
602 $v =~ s/\"/"/g; # since we quote with ""
603 substr($text, $v_offset, $v_len) = qq("$v");
607 }, "tagname, tokenpos, text");
614 sub genfeed ($$$$$@) {
622 my $url=URI->new(encode_utf8(urlto($page,"",1)));
624 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
627 foreach my $p (@pages) {
628 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
629 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
631 $itemtemplate->param(
632 title => pagetitle(basename($p)),
635 cdate_822 => date_822($pagectime{$p}),
636 mdate_822 => date_822($pagemtime{$p}),
637 cdate_3339 => date_3339($pagectime{$p}),
638 mdate_3339 => date_3339($pagemtime{$p}),
641 if (exists $pagestate{$p}) {
642 if (exists $pagestate{$p}{meta}{guid}) {
643 eval q{use HTML::Entities};
644 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
647 if (exists $pagestate{$p}{meta}{updated}) {
648 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
649 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
653 if ($itemtemplate->query(name => "enclosure")) {
654 my $file=$pagesources{$p};
655 my $type=pagetype($file);
657 $itemtemplate->param(content => $pcontent);
660 my $size=(srcfile_stat($file))[8];
662 eval q{use File::MimeInfo};
664 $mime = mimetype($file);
666 $itemtemplate->param(
674 $itemtemplate->param(content => $pcontent);
677 run_hooks(pagetemplate => sub {
678 shift->(page => $p, destpage => $page,
679 template => $itemtemplate);
682 $content.=$itemtemplate->output;
683 $itemtemplate->clear_params;
685 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
688 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
690 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
691 wikiname => $config{wikiname},
694 feeddesc => $feeddesc,
696 feeddate => date_3339($lasttime),
699 run_hooks(pagetemplate => sub {
700 shift->(page => $page, destpage => $page,
701 template => $template);
704 return $template->output;
708 return unless @{$config{pingurl}} && %toping;
710 eval q{require RPC::XML::Client};
712 debug(gettext("RPC::XML::Client not found, not pinging"));
716 # daemonize here so slow pings don't slow down wiki updates
717 defined(my $pid = fork) or error("Can't fork: $!");
720 POSIX::setsid() or error("Can't start a new session: $!");
721 open STDIN, '/dev/null';
722 open STDOUT, '>/dev/null';
723 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
725 # Don't need to keep a lock on the wiki as a daemon.
726 IkiWiki::unlockwiki();
728 foreach my $page (keys %toping) {
729 my $title=pagetitle(basename($page), 0);
730 my $url=urlto($page, "", 1);
731 foreach my $pingurl (@{$config{pingurl}}) {
732 debug("Pinging $pingurl for $page");
734 my $client = RPC::XML::Client->new($pingurl);
735 my $req = RPC::XML::request->new('weblogUpdates.ping',
737 my $res = $client->send_request($req);
739 error("Did not receive response to ping");
742 if (! exists $r->{flerror} || $r->{flerror}) {
743 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
747 error "Ping failed: $@";
752 exit 0; # daemon done
760 if (exists $params{rootpage}) {
761 $rootpage=bestlink($params{page}, $params{rootpage});
762 if (!length $rootpage) {
763 $rootpage=$params{rootpage};
767 $rootpage=$params{page};