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 my $postform = (exists $params{rootpage});
333 if (exists $params{postform}) {
334 $postform = yesno($params{postform});
337 if (length $config{cgiurl} && ! $params{preview} && $postform &&
338 IkiWiki->can("cgi_editpage")) {
339 # Add a blog post form, with feed buttons.
340 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
341 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
342 $formtemplate->param(rootpage => rootpage(%params));
345 $formtemplate->param(rssurl => $rssurl);
346 $formtemplate->param(rssdesc => $rssdesc);
349 $formtemplate->param(atomurl => $atomurl);
350 $formtemplate->param(atomdesc => $atomdesc);
353 if (exists $params{postformtext}) {
354 $formtemplate->param(postformtext =>
355 $params{postformtext});
358 $formtemplate->param(postformtext =>
359 gettext("Add a new post titled:"));
361 if (exists $params{id}) {
362 $formtemplate->param(postformid =>
365 $ret.=$formtemplate->output;
367 # The post form includes the feed buttons, so
368 # emptyfeeds cannot be hidden.
371 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
373 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
375 $linktemplate->param(rssurl => $rssurl);
376 $linktemplate->param(rssdesc => $rssdesc);
379 $linktemplate->param(atomurl => $atomurl);
380 $linktemplate->param(atomdesc => $atomdesc);
382 if (exists $params{id}) {
383 $linktemplate->param(id => $params{id});
385 $ret.=$linktemplate->output;
391 # cannot use wiki pages as templates; template not sanitized due to
394 $template=template_depends($params{template}.".tmpl", $params{page},
398 # gettext can clobber $@
400 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $error";
403 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
405 foreach my $page (@list) {
406 my $file = $pagesources{$page};
407 my $type = pagetype($file);
410 # Get the content before populating the
411 # template, since getting the content uses
412 # the same template if inlines are nested.
413 my $content=get_inline_content($page, $params{destpage});
414 $template->param(content => $content);
416 $template->param(pageurl => urlto($page, $params{destpage}));
417 $template->param(inlinepage => $page);
418 $template->param(title => pagetitle(basename($page)));
419 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
420 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
421 $template->param(first => 1) if $page eq $list[0];
422 $template->param(last => 1) if $page eq $list[$#list];
423 $template->param(html5 => $config{html5});
426 my $file = $pagesources{$page};
427 my $type = pagetype($file);
428 if ($config{discussion}) {
429 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
430 (length $config{cgiurl} ||
431 exists $pagesources{$page."/".lc($config{discussionpage})})) {
432 $template->param(have_actions => 1);
433 $template->param(discussionlink =>
436 $config{discussionpage},
441 if (length $config{cgiurl} &&
443 IkiWiki->can("cgi_editpage")) {
444 $template->param(have_actions => 1);
445 $template->param(editurl => cgiurl(do => "edit", page => $page));
450 run_hooks(pagetemplate => sub {
451 shift->(page => $page, destpage => $params{destpage},
452 template => $template,);
455 $ret.=$template->output;
456 $template->clear_params;
461 linkify($page, $params{destpage},
462 preprocess($page, $params{destpage},
463 filter($page, $params{destpage},
464 readfile(srcfile($file)))));
468 readfile(srcfile($file));
474 if ($feeds && ($emptyfeeds || @feedlist)) {
476 my $rssp=$feedbase."rss".$feednum;
477 will_render($params{destpage}, $rssp);
478 if (! $params{preview}) {
479 writefile($rssp, $config{destdir},
481 $config{url}."/".$rssp, $title, $desc, $params{guid}, $params{page}, @feedlist));
482 $toping{$params{destpage}}=1 unless $config{rebuild};
483 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
487 my $atomp=$feedbase."atom".$feednum;
488 will_render($params{destpage}, $atomp);
489 if (! $params{preview}) {
490 writefile($atomp, $config{destdir},
491 genfeed("atom", $config{url}."/".$atomp, $title, $desc, $params{guid}, $params{page}, @feedlist));
492 $toping{$params{destpage}}=1 unless $config{rebuild};
493 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
498 clear_inline_content_cache();
500 return $ret if $raw || $nested;
502 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
505 sub pagetemplate_inline (@) {
507 my $page=$params{page};
508 my $template=$params{template};
510 $template->param(feedlinks => $feedlinks{$page})
511 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
516 my $cached_destpage="";
518 sub get_inline_content ($$) {
522 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
523 return $inline_content{$page};
526 my $file=$pagesources{$page};
527 my $type=pagetype($file);
531 $ret=htmlize($page, $destpage, $type,
532 linkify($page, $destpage,
533 preprocess($page, $destpage,
534 filter($page, $destpage,
535 readfile(srcfile($file))))));
537 if (isinternal($page)) {
538 # make inlined text of internal pages searchable
539 run_hooks(indexhtml => sub {
540 shift->(page => $page, destpage => $destpage,
546 if ($cached_destpage ne $destpage) {
547 clear_inline_content_cache();
548 $cached_destpage=$destpage;
550 return $inline_content{$page}=$ret;
553 sub clear_inline_content_cache () {
562 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
563 POSIX::setlocale(&POSIX::LC_TIME, "C");
564 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
565 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
569 sub absolute_urls ($$) {
570 # needed because rss sucks
576 my $urltop; # calculated if needed
580 eval q{use HTML::Parser; use HTML::Tagset};
582 my $p = HTML::Parser->new(api_version => 3);
583 $p->handler(default => sub { $ret.=join("", @_) }, "text");
584 $p->handler(start => sub {
585 my ($tagname, $pos, $text) = @_;
586 if (ref $HTML::Tagset::linkElements{$tagname}) {
588 # use attribute sets from right to left
589 # to avoid invalidating the offsets
590 # when replacing the values
591 my ($k_offset, $k_len, $v_offset, $v_len) =
593 my $attrname = lc(substr($text, $k_offset, $k_len));
594 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
595 next unless $v_offset; # 0 v_offset means no value
596 my $v = substr($text, $v_offset, $v_len);
597 $v =~ s/^([\'\"])(.*)\1$/$2/;
598 eval q{use HTML::Entities};
599 my $dv = decode_entities($v);
601 $v=$baseurl.$v; # anchor
603 elsif ($dv=~/^(?!\w+:)[^\/]/) {
604 $v=$url.$v; # relative url
607 if (! defined $urltop) {
608 # what is the non path part of the url?
609 my $top_uri = URI->new($url);
610 $top_uri->path_query(""); # reset the path
611 $urltop = $top_uri->as_string;
613 $v=$urltop.$v; # url relative to top of site
615 $v =~ s/\"/"/g; # since we quote with ""
616 substr($text, $v_offset, $v_len) = qq("$v");
620 }, "tagname, tokenpos, text");
628 my $itemtemplate=shift;
632 return unless $itemtemplate->query(name => "enclosure");
634 my $size=(srcfile_stat($file))[8];
636 eval q{use File::MimeInfo};
638 $mime = mimetype($file);
640 $itemtemplate->param(
647 sub genfeed ($$$$$@) {
656 my $url=URI->new(encode_utf8(urlto($page,"",1)));
658 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
661 foreach my $p (@pages) {
662 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
663 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
664 my $fancy_enclosure_seen = 0;
666 $itemtemplate->param(
667 title => pagetitle(basename($p)),
670 cdate_822 => date_822($pagectime{$p}),
671 mdate_822 => date_822($pagemtime{$p}),
672 cdate_3339 => date_3339($pagectime{$p}),
673 mdate_3339 => date_3339($pagemtime{$p}),
676 if (exists $pagestate{$p}) {
677 if (exists $pagestate{$p}{meta}{guid}) {
678 eval q{use HTML::Entities};
679 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
682 if (exists $pagestate{$p}{meta}{updated}) {
683 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
684 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
687 if (exists $pagestate{$p}{meta}{enclosure}) {
688 my $absurl = $pagestate{$p}{meta}{enclosure};
689 my $file = $pagestate{$p}{meta}{enclosurefile};
690 genenclosure($itemtemplate, $absurl, $file);
691 $fancy_enclosure_seen = 1;
695 my $file=$pagesources{$p};
696 unless ($fancy_enclosure_seen || defined(pagetype($file))) {
697 genenclosure($itemtemplate, $u, $file);
698 $itemtemplate->param(simplepodcast => 1);
701 $itemtemplate->param(content => $pcontent);
703 run_hooks(pagetemplate => sub {
704 shift->(page => $p, destpage => $page,
705 template => $itemtemplate);
708 $content.=$itemtemplate->output;
709 $itemtemplate->clear_params;
711 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
714 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
717 wikiname => $config{wikiname},
720 feeddesc => $feeddesc,
722 feeddate => date_3339($lasttime),
723 feeddate_822 => date_822($lasttime),
726 run_hooks(pagetemplate => sub {
727 shift->(page => $page, destpage => $page,
728 template => $template);
731 return $template->output;
735 return unless @{$config{pingurl}} && %toping;
737 eval q{require RPC::XML::Client};
739 debug(gettext("RPC::XML::Client not found, not pinging"));
743 # daemonize here so slow pings don't slow down wiki updates
744 defined(my $pid = fork) or error("Can't fork: $!");
747 POSIX::setsid() or error("Can't start a new session: $!");
748 open STDIN, '/dev/null';
749 open STDOUT, '>/dev/null';
750 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
752 # Don't need to keep a lock on the wiki as a daemon.
753 IkiWiki::unlockwiki();
755 foreach my $page (keys %toping) {
756 my $title=pagetitle(basename($page), 0);
757 my $url=urlto($page, "", 1);
758 foreach my $pingurl (@{$config{pingurl}}) {
759 debug("Pinging $pingurl for $page");
761 my $client = RPC::XML::Client->new($pingurl);
762 my $req = RPC::XML::request->new('weblogUpdates.ping',
764 my $res = $client->send_request($req);
766 error("Did not receive response to ping");
769 if (! exists $r->{flerror} || $r->{flerror}) {
770 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
774 error "Ping failed: $@";
779 exit 0; # daemon done
787 if (exists $params{rootpage}) {
788 $rootpage=bestlink($params{page}, $params{rootpage});
789 if (!length $rootpage) {
790 $rootpage=$params{rootpage};
794 $rootpage=$params{page};