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{title}) {
191 $title = $params{title};
194 $title = $params{page} ne "index" ? pagetitle($params{page}) : $config{wikiname};
197 if (exists $params{description}) {
198 $desc = $params{description}
201 $desc = $config{wikiname};
203 my $actions=yesno($params{actions});
204 if (exists $params{template}) {
205 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
208 $params{template} = $archive ? "archivepage" : "inlinepage";
213 if (exists $params{pagenames}) {
214 foreach my $p (qw(sort pages)) {
215 if (exists $params{$p}) {
216 error sprintf(gettext("the %s and %s parameters cannot be used together"),
221 @list = split ' ', $params{pagenames};
223 if (yesno($params{reverse})) {
224 @list=reverse(@list);
227 foreach my $p (@list) {
228 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
231 @list = grep { exists $pagesources{$_} } @list;
238 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
239 $num=$params{feedshow};
241 if ($params{skip} && $num) {
245 @list = pagespec_match_list($params{page}, $params{pages},
246 deptype => deptype($quick ? "presence" : "content"),
247 filter => sub { $_[0] eq $params{page} },
248 sort => exists $params{sort} ? $params{sort} : "age",
249 reverse => yesno($params{reverse}),
250 ($num ? (num => $num) : ()),
254 if (exists $params{skip}) {
255 @list=@list[$params{skip} .. $#list];
260 if (exists $params{feedshow} &&
261 $params{feedshow} && @list > $params{feedshow}) {
262 @feedlist=@list[0..$params{feedshow} - 1];
269 if ($params{show} && @list > $params{show}) {
270 @list=@list[0..$params{show} - 1];
273 if ($feeds && exists $params{feedpages}) {
274 @feedlist = pagespec_match_list(
275 $params{page}, "($params{pages}) and ($params{feedpages})",
276 deptype => deptype($quick ? "presence" : "content"),
281 my ($feedbase, $feednum);
283 # Ensure that multiple feeds on a page go to unique files.
285 # Feedfile can lead to conflicts if usedirs is not enabled,
286 # so avoid supporting it in that case.
287 delete $params{feedfile} if ! $config{usedirs};
288 # Tight limits on legal feedfiles, to avoid security issues
290 if (defined $params{feedfile}) {
291 if ($params{feedfile} =~ /\// ||
292 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
293 error("illegal feedfile");
295 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
297 $feedbase=targetpage($params{page}, "", $params{feedfile});
299 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
300 if (exists $knownfeeds{$feedid}) {
301 $feednum=$knownfeeds{$feedid};
304 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
306 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
310 $feednum=$knownfeeds{$feedid}="";
312 $page_numfeeds{$params{destpage}}{$feedbase}=1;
318 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
321 $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
322 $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
325 $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
326 $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
332 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
333 (exists $params{postform} && yesno($params{postform}))) &&
334 IkiWiki->can("cgi_editpage")) {
335 # Add a blog post form, with feed buttons.
336 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
337 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
338 $formtemplate->param(rootpage => rootpage(%params));
341 $formtemplate->param(rssurl => $rssurl);
342 $formtemplate->param(rssdesc => $rssdesc);
345 $formtemplate->param(atomurl => $atomurl);
346 $formtemplate->param(atomdesc => $atomdesc);
349 if (exists $params{postformtext}) {
350 $formtemplate->param(postformtext =>
351 $params{postformtext});
354 $formtemplate->param(postformtext =>
355 gettext("Add a new post titled:"));
357 if (exists $params{id}) {
358 $formtemplate->param(postformid =>
361 $ret.=$formtemplate->output;
363 # The post form includes the feed buttons, so
364 # emptyfeeds cannot be hidden.
367 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
369 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
371 $linktemplate->param(rssurl => $rssurl);
372 $linktemplate->param(rssdesc => $rssdesc);
375 $linktemplate->param(atomurl => $atomurl);
376 $linktemplate->param(atomdesc => $atomdesc);
378 if (exists $params{id}) {
379 $linktemplate->param(id => $params{id});
381 $ret.=$linktemplate->output;
387 # cannot use wiki pages as templates; template not sanitized due to
390 $template=template_depends($params{template}.".tmpl", $params{page},
394 # gettext can clobber $@
396 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $error";
399 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
401 foreach my $page (@list) {
402 my $file = $pagesources{$page};
403 my $type = pagetype($file);
406 # Get the content before populating the
407 # template, since getting the content uses
408 # the same template if inlines are nested.
409 my $content=get_inline_content($page, $params{destpage});
410 $template->param(content => $content);
412 $template->param(pageurl => urlto($page, $params{destpage}));
413 $template->param(inlinepage => $page);
414 $template->param(title => pagetitle(basename($page)));
415 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
416 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
417 $template->param(first => 1) if $page eq $list[0];
418 $template->param(last => 1) if $page eq $list[$#list];
419 $template->param(html5 => $config{html5});
422 my $file = $pagesources{$page};
423 my $type = pagetype($file);
424 if ($config{discussion}) {
425 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
426 (length $config{cgiurl} ||
427 exists $pagesources{$page."/".lc($config{discussionpage})})) {
428 $template->param(have_actions => 1);
429 $template->param(discussionlink =>
432 $config{discussionpage},
437 if (length $config{cgiurl} &&
439 IkiWiki->can("cgi_editpage")) {
440 $template->param(have_actions => 1);
441 $template->param(editurl => cgiurl(do => "edit", page => $page));
446 run_hooks(pagetemplate => sub {
447 shift->(page => $page, destpage => $params{destpage},
448 template => $template,);
451 $ret.=$template->output;
452 $template->clear_params;
457 linkify($page, $params{destpage},
458 preprocess($page, $params{destpage},
459 filter($page, $params{destpage},
460 readfile(srcfile($file)))));
464 readfile(srcfile($file));
470 if ($feeds && ($emptyfeeds || @feedlist)) {
472 my $rssp=$feedbase."rss".$feednum;
473 will_render($params{destpage}, $rssp);
474 if (! $params{preview}) {
475 writefile($rssp, $config{destdir},
477 $config{url}."/".$rssp, $title, $desc, $params{guid}, $params{page}, @feedlist));
478 $toping{$params{destpage}}=1 unless $config{rebuild};
479 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
483 my $atomp=$feedbase."atom".$feednum;
484 will_render($params{destpage}, $atomp);
485 if (! $params{preview}) {
486 writefile($atomp, $config{destdir},
487 genfeed("atom", $config{url}."/".$atomp, $title, $desc, $params{guid}, $params{page}, @feedlist));
488 $toping{$params{destpage}}=1 unless $config{rebuild};
489 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
494 clear_inline_content_cache();
496 return $ret if $raw || $nested;
498 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
501 sub pagetemplate_inline (@) {
503 my $page=$params{page};
504 my $template=$params{template};
506 $template->param(feedlinks => $feedlinks{$page})
507 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
512 my $cached_destpage="";
514 sub get_inline_content ($$) {
518 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
519 return $inline_content{$page};
522 my $file=$pagesources{$page};
523 my $type=pagetype($file);
527 $ret=htmlize($page, $destpage, $type,
528 linkify($page, $destpage,
529 preprocess($page, $destpage,
530 filter($page, $destpage,
531 readfile(srcfile($file))))));
533 if (isinternal($page)) {
534 # make inlined text of internal pages searchable
535 run_hooks(indexhtml => sub {
536 shift->(page => $page, destpage => $destpage,
542 if ($cached_destpage ne $destpage) {
543 clear_inline_content_cache();
544 $cached_destpage=$destpage;
546 return $inline_content{$page}=$ret;
549 sub clear_inline_content_cache () {
558 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
559 POSIX::setlocale(&POSIX::LC_TIME, "C");
560 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
561 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
565 sub absolute_urls ($$) {
566 # needed because rss sucks
572 my $urltop; # calculated if needed
576 eval q{use HTML::Parser; use HTML::Tagset};
578 my $p = HTML::Parser->new(api_version => 3);
579 $p->handler(default => sub { $ret.=join("", @_) }, "text");
580 $p->handler(start => sub {
581 my ($tagname, $pos, $text) = @_;
582 if (ref $HTML::Tagset::linkElements{$tagname}) {
584 # use attribute sets from right to left
585 # to avoid invalidating the offsets
586 # when replacing the values
587 my ($k_offset, $k_len, $v_offset, $v_len) =
589 my $attrname = lc(substr($text, $k_offset, $k_len));
590 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
591 next unless $v_offset; # 0 v_offset means no value
592 my $v = substr($text, $v_offset, $v_len);
593 $v =~ s/^([\'\"])(.*)\1$/$2/;
594 eval q{use HTML::Entities};
595 my $dv = decode_entities($v);
597 $v=$baseurl.$v; # anchor
599 elsif ($dv=~/^(?!\w+:)[^\/]/) {
600 $v=$url.$v; # relative url
603 if (! defined $urltop) {
604 # what is the non path part of the url?
605 my $top_uri = URI->new($url);
606 $top_uri->path_query(""); # reset the path
607 $urltop = $top_uri->as_string;
609 $v=$urltop.$v; # url relative to top of site
611 $v =~ s/\"/"/g; # since we quote with ""
612 substr($text, $v_offset, $v_len) = qq("$v");
616 }, "tagname, tokenpos, text");
624 my $itemtemplate=shift;
628 return unless $itemtemplate->query(name => "enclosure");
630 my $size=(srcfile_stat($file))[8];
632 eval q{use File::MimeInfo};
634 $mime = mimetype($file);
636 $itemtemplate->param(
643 sub genfeed ($$$$$@) {
652 my $url=URI->new(encode_utf8(urlto($page,"",1)));
654 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
657 foreach my $p (@pages) {
658 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
659 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
660 my $fancy_enclosure_seen = 0;
662 $itemtemplate->param(
663 title => pagetitle(basename($p)),
666 cdate_822 => date_822($pagectime{$p}),
667 mdate_822 => date_822($pagemtime{$p}),
668 cdate_3339 => date_3339($pagectime{$p}),
669 mdate_3339 => date_3339($pagemtime{$p}),
672 if (exists $pagestate{$p}) {
673 if (exists $pagestate{$p}{meta}{guid}) {
674 eval q{use HTML::Entities};
675 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
678 if (exists $pagestate{$p}{meta}{updated}) {
679 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
680 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
683 if (exists $pagestate{$p}{meta}{enclosure}) {
684 my $absurl = $pagestate{$p}{meta}{enclosure};
685 my $file = $pagestate{$p}{meta}{enclosurefile};
686 genenclosure($itemtemplate, $absurl, $file);
687 $fancy_enclosure_seen = 1;
691 my $file=$pagesources{$p};
692 unless ($fancy_enclosure_seen || defined(pagetype($file))) {
693 genenclosure($itemtemplate, $u, $file);
694 $itemtemplate->param(simplepodcast => 1);
697 $itemtemplate->param(content => $pcontent);
699 run_hooks(pagetemplate => sub {
700 shift->(page => $p, destpage => $page,
701 template => $itemtemplate);
704 $content.=$itemtemplate->output;
705 $itemtemplate->clear_params;
707 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
710 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
713 wikiname => $config{wikiname},
716 feeddesc => $feeddesc,
718 feeddate => date_3339($lasttime),
719 feeddate_822 => date_822($lasttime),
722 run_hooks(pagetemplate => sub {
723 shift->(page => $page, destpage => $page,
724 template => $template);
727 return $template->output;
731 return unless @{$config{pingurl}} && %toping;
733 eval q{require RPC::XML::Client};
735 debug(gettext("RPC::XML::Client not found, not pinging"));
739 # daemonize here so slow pings don't slow down wiki updates
740 defined(my $pid = fork) or error("Can't fork: $!");
743 POSIX::setsid() or error("Can't start a new session: $!");
744 open STDIN, '/dev/null';
745 open STDOUT, '>/dev/null';
746 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
748 # Don't need to keep a lock on the wiki as a daemon.
749 IkiWiki::unlockwiki();
751 foreach my $page (keys %toping) {
752 my $title=pagetitle(basename($page), 0);
753 my $url=urlto($page, "", 1);
754 foreach my $pingurl (@{$config{pingurl}}) {
755 debug("Pinging $pingurl for $page");
757 my $client = RPC::XML::Client->new($pingurl);
758 my $req = RPC::XML::request->new('weblogUpdates.ping',
760 my $res = $client->send_request($req);
762 error("Did not receive response to ping");
765 if (! exists $r->{flerror} || $r->{flerror}) {
766 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
770 error "Ping failed: $@";
775 exit 0; # daemon done
783 if (exists $params{rootpage}) {
784 $rootpage=bestlink($params{page}, $params{rootpage});
785 if (!length $rootpage) {
786 $rootpage=$params{rootpage};
790 $rootpage=$params{page};