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});
184 # Backwards compatibility
185 if (defined $params{show} && $params{show} =~ m/^\d+$/) {
186 $params{limit} = $params{show};
187 delete $params{show};
189 if (defined $params{feedshow} && $params{feedshow} =~ m/^\d+$/) {
190 $params{feedlimit} = $params{feedshow};
191 delete $params{feedshow};
194 if (! exists $params{limit} && ! $archive) {
197 if (! exists $params{feedlimit} && exists $params{limit}) {
198 $params{feedlimit}=$params{limit};
201 if (exists $params{title}) {
202 $title = $params{title};
205 $title = $params{page} ne "index" ? pagetitle($params{page}) : $config{wikiname};
208 if (exists $params{description}) {
209 $desc = $params{description}
212 $desc = $config{wikiname};
214 my $actions=yesno($params{actions});
215 if (exists $params{template}) {
216 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
219 $params{template} = $archive ? "archivepage" : "inlinepage";
224 if (exists $params{pagenames}) {
225 foreach my $p (qw(sort pages)) {
226 if (exists $params{$p}) {
227 error sprintf(gettext("the %s and %s parameters cannot be used together"),
232 @list = split ' ', $params{pagenames};
234 if (yesno($params{reverse})) {
235 @list=reverse(@list);
238 foreach my $p (@list) {
239 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
242 @list = grep { exists $pagesources{$_} } @list;
246 if ($params{limit}) {
249 if ($params{feedlimit} && $num < $params{feedlimit} && $num > 0) {
250 $num=$params{feedlimit};
252 if ($params{skip} && $num) {
256 @list = pagespec_match_list($params{page}, $params{pages},
257 deptype => deptype($quick ? "presence" : "content"),
258 filter => sub { $_[0] eq $params{page} },
259 sort => exists $params{sort} ? $params{sort} : "age",
260 reverse => yesno($params{reverse}),
261 ($num ? (num => $num) : ()),
265 if (exists $params{skip}) {
266 @list=@list[$params{skip} .. $#list];
271 if (exists $params{feedlimit} &&
272 $params{feedlimit} && @list > $params{feedlimit}) {
273 @feedlist=@list[0..$params{feedlimit} - 1];
280 if ($params{limit} && @list > $params{limit}) {
281 @list=@list[0..$params{limit} - 1];
284 if ($feeds && exists $params{feedpages}) {
285 @feedlist = pagespec_match_list(
286 $params{page}, "($params{pages}) and ($params{feedpages})",
287 deptype => deptype($quick ? "presence" : "content"),
292 my ($feedbase, $feednum);
294 # Ensure that multiple feeds on a page go to unique files.
296 # Feedfile can lead to conflicts if usedirs is not enabled,
297 # so avoid supporting it in that case.
298 delete $params{feedfile} if ! $config{usedirs};
299 # Tight limits on legal feedfiles, to avoid security issues
301 if (defined $params{feedfile}) {
302 if ($params{feedfile} =~ /\// ||
303 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
304 error("illegal feedfile");
306 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
308 $feedbase=targetpage($params{page}, "", $params{feedfile});
310 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
311 if (exists $knownfeeds{$feedid}) {
312 $feednum=$knownfeeds{$feedid};
315 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
317 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
321 $feednum=$knownfeeds{$feedid}="";
323 $page_numfeeds{$params{destpage}}{$feedbase}=1;
329 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
332 $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
333 $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
336 $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
337 $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
343 my $postform = (exists $params{rootpage});
344 if (exists $params{postform}) {
345 $postform = yesno($params{postform});
348 if (length $config{cgiurl} && ! $params{preview} && $postform &&
349 IkiWiki->can("cgi_editpage")) {
350 # Add a blog post form, with feed buttons.
351 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
352 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
353 $formtemplate->param(rootpage => rootpage(%params));
356 $formtemplate->param(rssurl => $rssurl);
357 $formtemplate->param(rssdesc => $rssdesc);
360 $formtemplate->param(atomurl => $atomurl);
361 $formtemplate->param(atomdesc => $atomdesc);
364 if (exists $params{postformtext}) {
365 $formtemplate->param(postformtext =>
366 $params{postformtext});
369 $formtemplate->param(postformtext =>
370 gettext("Add a new post titled:"));
372 if (exists $params{id}) {
373 $formtemplate->param(postformid =>
376 $ret.=$formtemplate->output;
378 # The post form includes the feed buttons, so
379 # emptyfeeds cannot be hidden.
382 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
384 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
386 $linktemplate->param(rssurl => $rssurl);
387 $linktemplate->param(rssdesc => $rssdesc);
390 $linktemplate->param(atomurl => $atomurl);
391 $linktemplate->param(atomdesc => $atomdesc);
393 if (exists $params{id}) {
394 $linktemplate->param(id => $params{id});
396 $ret.=$linktemplate->output;
402 # cannot use wiki pages as templates; template not sanitized due to
405 $template=template_depends($params{template}.".tmpl", $params{page},
409 # gettext can clobber $@
411 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $error";
414 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
416 foreach my $page (@list) {
417 my $file = $pagesources{$page};
418 my $type = pagetype($file);
421 # Get the content before populating the
422 # template, since getting the content uses
423 # the same template if inlines are nested.
424 my $content=get_inline_content($page, $params{destpage});
425 $template->param(content => $content);
427 $template->param(pageurl => urlto($page, $params{destpage}));
428 $template->param(inlinepage => $page);
429 $template->param(title => pagetitle(basename($page)));
430 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
431 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
432 $template->param(first => 1) if $page eq $list[0];
433 $template->param(last => 1) if $page eq $list[$#list];
434 $template->param(html5 => $config{html5});
437 my $file = $pagesources{$page};
438 my $type = pagetype($file);
439 if ($config{discussion}) {
440 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
441 (length $config{cgiurl} ||
442 exists $pagesources{$page."/".lc($config{discussionpage})})) {
443 $template->param(have_actions => 1);
444 $template->param(discussionlink =>
447 $config{discussionpage},
452 if (length $config{cgiurl} &&
454 IkiWiki->can("cgi_editpage")) {
455 $template->param(have_actions => 1);
456 $template->param(editurl => cgiurl(do => "edit", page => $page));
461 run_hooks(pagetemplate => sub {
462 shift->(page => $page, destpage => $params{destpage},
463 template => $template,);
466 $ret.=$template->output;
467 $template->clear_params;
472 linkify($page, $params{destpage},
473 preprocess($page, $params{destpage},
474 filter($page, $params{destpage},
475 readfile(srcfile($file)))));
479 readfile(srcfile($file));
485 if ($feeds && ($emptyfeeds || @feedlist)) {
487 my $rssp=$feedbase."rss".$feednum;
488 will_render($params{destpage}, $rssp);
489 if (! $params{preview}) {
490 writefile($rssp, $config{destdir},
492 $config{url}."/".$rssp, $title, $desc, $params{guid}, $params{page}, @feedlist));
493 $toping{$params{destpage}}=1 unless $config{rebuild};
494 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
498 my $atomp=$feedbase."atom".$feednum;
499 will_render($params{destpage}, $atomp);
500 if (! $params{preview}) {
501 writefile($atomp, $config{destdir},
502 genfeed("atom", $config{url}."/".$atomp, $title, $desc, $params{guid}, $params{page}, @feedlist));
503 $toping{$params{destpage}}=1 unless $config{rebuild};
504 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
509 clear_inline_content_cache();
511 return $ret if $raw || $nested;
513 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
516 sub pagetemplate_inline (@) {
518 my $page=$params{page};
519 my $template=$params{template};
521 $template->param(feedlinks => $feedlinks{$page})
522 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
527 my $cached_destpage="";
529 sub get_inline_content ($$) {
533 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
534 return $inline_content{$page};
537 my $file=$pagesources{$page};
538 my $type=pagetype($file);
542 $ret=htmlize($page, $destpage, $type,
543 linkify($page, $destpage,
544 preprocess($page, $destpage,
545 filter($page, $destpage,
546 readfile(srcfile($file))))));
548 if (isinternal($page)) {
549 # make inlined text of internal pages searchable
550 run_hooks(indexhtml => sub {
551 shift->(page => $page, destpage => $destpage,
557 if ($cached_destpage ne $destpage) {
558 clear_inline_content_cache();
559 $cached_destpage=$destpage;
561 return $inline_content{$page}=$ret;
564 sub clear_inline_content_cache () {
573 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
574 POSIX::setlocale(&POSIX::LC_TIME, "C");
575 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
576 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
580 sub absolute_urls ($$) {
581 # needed because rss sucks
587 my $urltop; # calculated if needed
591 eval q{use HTML::Parser; use HTML::Tagset};
593 my $p = HTML::Parser->new(api_version => 3);
594 $p->handler(default => sub { $ret.=join("", @_) }, "text");
595 $p->handler(start => sub {
596 my ($tagname, $pos, $text) = @_;
597 if (ref $HTML::Tagset::linkElements{$tagname}) {
599 # use attribute sets from right to left
600 # to avoid invalidating the offsets
601 # when replacing the values
602 my ($k_offset, $k_len, $v_offset, $v_len) =
604 my $attrname = lc(substr($text, $k_offset, $k_len));
605 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
606 next unless $v_offset; # 0 v_offset means no value
607 my $v = substr($text, $v_offset, $v_len);
608 $v =~ s/^([\'\"])(.*)\1$/$2/;
609 eval q{use HTML::Entities};
610 my $dv = decode_entities($v);
612 $v=$baseurl.$v; # anchor
614 elsif ($dv=~/^(?!\w+:)[^\/]/) {
615 $v=$url.$v; # relative url
618 if (! defined $urltop) {
619 # what is the non path part of the url?
620 my $top_uri = URI->new($url);
621 $top_uri->path_query(""); # reset the path
622 $urltop = $top_uri->as_string;
624 $v=$urltop.$v; # url relative to top of site
626 $v =~ s/\"/"/g; # since we quote with ""
627 substr($text, $v_offset, $v_len) = qq("$v");
631 }, "tagname, tokenpos, text");
639 my $itemtemplate=shift;
643 return unless $itemtemplate->query(name => "enclosure");
645 my $size=(srcfile_stat($file))[8];
647 eval q{use File::MimeInfo};
649 $mime = mimetype($file);
651 $itemtemplate->param(
658 sub genfeed ($$$$$@) {
667 my $url=URI->new(encode_utf8(urlto($page,"",1)));
669 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
672 foreach my $p (@pages) {
673 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
674 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
675 my $fancy_enclosure_seen = 0;
677 $itemtemplate->param(
678 title => pagetitle(basename($p)),
681 cdate_822 => date_822($pagectime{$p}),
682 mdate_822 => date_822($pagemtime{$p}),
683 cdate_3339 => date_3339($pagectime{$p}),
684 mdate_3339 => date_3339($pagemtime{$p}),
687 if (exists $pagestate{$p}) {
688 if (exists $pagestate{$p}{meta}{guid}) {
689 eval q{use HTML::Entities};
690 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
693 if (exists $pagestate{$p}{meta}{updated}) {
694 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
695 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
698 if (exists $pagestate{$p}{meta}{enclosure}) {
699 my $absurl = $pagestate{$p}{meta}{enclosure};
700 my $file = $pagestate{$p}{meta}{enclosurefile};
701 genenclosure($itemtemplate, $absurl, $file);
702 $fancy_enclosure_seen = 1;
706 my $file=$pagesources{$p};
707 unless ($fancy_enclosure_seen || defined(pagetype($file))) {
708 genenclosure($itemtemplate, $u, $file);
709 $itemtemplate->param(simplepodcast => 1);
712 $itemtemplate->param(content => $pcontent);
714 run_hooks(pagetemplate => sub {
715 shift->(page => $p, destpage => $page,
716 template => $itemtemplate);
719 $content.=$itemtemplate->output;
720 $itemtemplate->clear_params;
722 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
725 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
728 wikiname => $config{wikiname},
731 feeddesc => $feeddesc,
733 feeddate => date_3339($lasttime),
734 feeddate_822 => date_822($lasttime),
737 run_hooks(pagetemplate => sub {
738 shift->(page => $page, destpage => $page,
739 template => $template);
742 return $template->output;
746 return unless @{$config{pingurl}} && %toping;
748 eval q{require RPC::XML::Client};
750 debug(gettext("RPC::XML::Client not found, not pinging"));
754 # daemonize here so slow pings don't slow down wiki updates
755 defined(my $pid = fork) or error("Can't fork: $!");
758 POSIX::setsid() or error("Can't start a new session: $!");
759 open STDIN, '/dev/null';
760 open STDOUT, '>/dev/null';
761 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
763 # Don't need to keep a lock on the wiki as a daemon.
764 IkiWiki::unlockwiki();
766 foreach my $page (keys %toping) {
767 my $title=pagetitle(basename($page), 0);
768 my $url=urlto($page, "", 1);
769 foreach my $pingurl (@{$config{pingurl}}) {
770 debug("Pinging $pingurl for $page");
772 my $client = RPC::XML::Client->new($pingurl);
773 my $req = RPC::XML::request->new('weblogUpdates.ping',
775 my $res = $client->send_request($req);
777 error("Did not receive response to ping");
780 if (! exists $r->{flerror} || $r->{flerror}) {
781 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
785 error "Ping failed: $@";
790 exit 0; # daemon done
798 if (exists $params{rootpage}) {
799 $rootpage=bestlink($params{page}, $params{rootpage});
800 if (!length $rootpage) {
801 $rootpage=$params{rootpage};
805 $rootpage=$params{page};