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 => "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];
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 = map { bestlink($params{page}, $_) }
215 split ' ', $params{pagenames};
217 if (yesno($params{reverse})) {
218 @list=reverse(@list);
221 foreach my $p (@list) {
222 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
230 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
231 $num=$params{feedshow};
233 if ($params{skip} && $num) {
237 @list = pagespec_match_list($params{page}, $params{pages},
238 deptype => deptype($quick ? "presence" : "content"),
239 filter => sub { $_[0] eq $params{page} },
240 sort => exists $params{sort} ? $params{sort} : "age",
241 reverse => yesno($params{reverse}),
242 ($num ? (num => $num) : ()),
246 if (exists $params{skip}) {
247 @list=@list[$params{skip} .. $#list];
252 if (exists $params{feedshow} &&
253 $params{feedshow} && @list > $params{feedshow}) {
254 @feedlist=@list[0..$params{feedshow} - 1];
261 if ($params{show} && @list > $params{show}) {
262 @list=@list[0..$params{show} - 1];
265 if ($feeds && exists $params{feedpages}) {
266 @feedlist = pagespec_match_list(
267 $params{page}, "($params{pages}) and ($params{feedpages})",
268 deptype => deptype($quick ? "presence" : "content"),
273 my ($feedbase, $feednum);
275 # Ensure that multiple feeds on a page go to unique files.
277 # Feedfile can lead to conflicts if usedirs is not enabled,
278 # so avoid supporting it in that case.
279 delete $params{feedfile} if ! $config{usedirs};
280 # Tight limits on legal feedfiles, to avoid security issues
282 if (defined $params{feedfile}) {
283 if ($params{feedfile} =~ /\// ||
284 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
285 error("illegal feedfile");
287 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
289 $feedbase=targetpage($params{page}, "", $params{feedfile});
291 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
292 if (exists $knownfeeds{$feedid}) {
293 $feednum=$knownfeeds{$feedid};
296 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
298 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
302 $feednum=$knownfeeds{$feedid}="";
304 $page_numfeeds{$params{destpage}}{$feedbase}=1;
310 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
313 $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
314 $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
317 $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
318 $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
324 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
325 (exists $params{postform} && yesno($params{postform}))) &&
326 IkiWiki->can("cgi_editpage")) {
327 # Add a blog post form, with feed buttons.
328 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
329 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
330 $formtemplate->param(rootpage => rootpage(%params));
333 $formtemplate->param(rssurl => $rssurl);
334 $formtemplate->param(rssdesc => $rssdesc);
337 $formtemplate->param(atomurl => $atomurl);
338 $formtemplate->param(atomdesc => $atomdesc);
341 if (exists $params{postformtext}) {
342 $formtemplate->param(postformtext =>
343 $params{postformtext});
346 $formtemplate->param(postformtext =>
347 gettext("Add a new post titled:"));
349 if (exists $params{id}) {
350 $formtemplate->param(postformid =>
353 $ret.=$formtemplate->output;
355 # The post form includes the feed buttons, so
356 # emptyfeeds cannot be hidden.
359 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
361 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
363 $linktemplate->param(rssurl => $rssurl);
364 $linktemplate->param(rssdesc => $rssdesc);
367 $linktemplate->param(atomurl => $atomurl);
368 $linktemplate->param(atomdesc => $atomdesc);
370 if (exists $params{id}) {
371 $linktemplate->param(id => $params{id});
373 $ret.=$linktemplate->output;
379 # cannot use wiki pages as templates; template not sanitized due to
382 $template=template_depends($params{template}.".tmpl", $params{page},
386 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
389 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
391 foreach my $page (@list) {
392 my $file = $pagesources{$page};
393 my $type = pagetype($file);
396 # Get the content before populating the
397 # template, since getting the content uses
398 # the same template if inlines are nested.
399 my $content=get_inline_content($page, $params{destpage});
400 $template->param(content => $content);
402 $template->param(pageurl => urlto($page, $params{destpage}));
403 $template->param(inlinepage => $page);
404 $template->param(title => pagetitle(basename($page)));
405 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
406 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
407 $template->param(first => 1) if $page eq $list[0];
408 $template->param(last => 1) if $page eq $list[$#list];
409 $template->param(html5 => $config{html5});
412 my $file = $pagesources{$page};
413 my $type = pagetype($file);
414 if ($config{discussion}) {
415 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
416 (length $config{cgiurl} ||
417 exists $pagesources{$page."/".lc($config{discussionpage})})) {
418 $template->param(have_actions => 1);
419 $template->param(discussionlink =>
422 $config{discussionpage},
427 if (length $config{cgiurl} &&
429 IkiWiki->can("cgi_editpage")) {
430 $template->param(have_actions => 1);
431 $template->param(editurl => cgiurl(do => "edit", page => $page));
436 run_hooks(pagetemplate => sub {
437 shift->(page => $page, destpage => $params{destpage},
438 template => $template,);
441 $ret.=$template->output;
442 $template->clear_params;
447 linkify($page, $params{destpage},
448 preprocess($page, $params{destpage},
449 filter($page, $params{destpage},
450 readfile(srcfile($file)))));
454 readfile(srcfile($file));
460 if ($feeds && ($emptyfeeds || @feedlist)) {
462 my $rssp=$feedbase."rss".$feednum;
463 will_render($params{destpage}, $rssp);
464 if (! $params{preview}) {
465 writefile($rssp, $config{destdir},
467 $config{url}."/".$rssp, $desc, $params{guid}, $params{page}, @feedlist));
468 $toping{$params{destpage}}=1 unless $config{rebuild};
469 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
473 my $atomp=$feedbase."atom".$feednum;
474 will_render($params{destpage}, $atomp);
475 if (! $params{preview}) {
476 writefile($atomp, $config{destdir},
477 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{page}, @feedlist));
478 $toping{$params{destpage}}=1 unless $config{rebuild};
479 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
484 clear_inline_content_cache();
486 return $ret if $raw || $nested;
488 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
491 sub pagetemplate_inline (@) {
493 my $page=$params{page};
494 my $template=$params{template};
496 $template->param(feedlinks => $feedlinks{$page})
497 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
502 my $cached_destpage="";
504 sub get_inline_content ($$) {
508 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
509 return $inline_content{$page};
512 my $file=$pagesources{$page};
513 my $type=pagetype($file);
517 $ret=htmlize($page, $destpage, $type,
518 linkify($page, $destpage,
519 preprocess($page, $destpage,
520 filter($page, $destpage,
521 readfile(srcfile($file))))));
523 if (isinternal($page)) {
524 # make inlined text of internal pages searchable
525 run_hooks(indexhtml => sub {
526 shift->(page => $page, destpage => $destpage,
532 if ($cached_destpage ne $destpage) {
533 clear_inline_content_cache();
534 $cached_destpage=$destpage;
536 return $inline_content{$page}=$ret;
539 sub clear_inline_content_cache () {
548 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
549 POSIX::setlocale(&POSIX::LC_TIME, "C");
550 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
551 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
555 sub absolute_urls ($$) {
556 # needed because rss sucks
562 my $urltop; # calculated if needed
566 eval q{use HTML::Parser; use HTML::Tagset};
568 my $p = HTML::Parser->new(api_version => 3);
569 $p->handler(default => sub { $ret.=join("", @_) }, "text");
570 $p->handler(start => sub {
571 my ($tagname, $pos, $text) = @_;
572 if (ref $HTML::Tagset::linkElements{$tagname}) {
574 # use attribute sets from right to left
575 # to avoid invalidating the offsets
576 # when replacing the values
577 my ($k_offset, $k_len, $v_offset, $v_len) =
579 my $attrname = lc(substr($text, $k_offset, $k_len));
580 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
581 next unless $v_offset; # 0 v_offset means no value
582 my $v = substr($text, $v_offset, $v_len);
583 $v =~ s/^([\'\"])(.*)\1$/$2/;
584 eval q{use HTML::Entities};
585 my $dv = decode_entities($v);
587 $v=$baseurl.$v; # anchor
589 elsif ($dv=~/^(?!\w+:)[^\/]/) {
590 $v=$url.$v; # relative url
593 if (! defined $urltop) {
594 # what is the non path part of the url?
595 my $top_uri = URI->new($url);
596 $top_uri->path_query(""); # reset the path
597 $urltop = $top_uri->as_string;
599 $v=$urltop.$v; # url relative to top of site
601 $v =~ s/\"/"/g; # since we quote with ""
602 substr($text, $v_offset, $v_len) = qq("$v");
606 }, "tagname, tokenpos, text");
613 sub genfeed ($$$$$@) {
621 my $url=URI->new(encode_utf8(urlto($page,"",1)));
623 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
626 foreach my $p (@pages) {
627 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
628 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
630 $itemtemplate->param(
631 title => pagetitle(basename($p)),
634 cdate_822 => date_822($pagectime{$p}),
635 mdate_822 => date_822($pagemtime{$p}),
636 cdate_3339 => date_3339($pagectime{$p}),
637 mdate_3339 => date_3339($pagemtime{$p}),
640 if (exists $pagestate{$p}) {
641 if (exists $pagestate{$p}{meta}{guid}) {
642 eval q{use HTML::Entities};
643 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
646 if (exists $pagestate{$p}{meta}{updated}) {
647 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
648 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
652 if ($itemtemplate->query(name => "enclosure")) {
653 my $file=$pagesources{$p};
654 my $type=pagetype($file);
656 $itemtemplate->param(content => $pcontent);
659 my $size=(srcfile_stat($file))[8];
661 eval q{use File::MimeInfo};
663 $mime = mimetype($file);
665 $itemtemplate->param(
673 $itemtemplate->param(content => $pcontent);
676 run_hooks(pagetemplate => sub {
677 shift->(page => $p, destpage => $page,
678 template => $itemtemplate);
681 $content.=$itemtemplate->output;
682 $itemtemplate->clear_params;
684 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
687 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
689 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
690 wikiname => $config{wikiname},
693 feeddesc => $feeddesc,
695 feeddate => date_3339($lasttime),
698 run_hooks(pagetemplate => sub {
699 shift->(page => $page, destpage => $page,
700 template => $template);
703 return $template->output;
707 return unless @{$config{pingurl}} && %toping;
709 eval q{require RPC::XML::Client};
711 debug(gettext("RPC::XML::Client not found, not pinging"));
715 # daemonize here so slow pings don't slow down wiki updates
716 defined(my $pid = fork) or error("Can't fork: $!");
719 POSIX::setsid() or error("Can't start a new session: $!");
720 open STDIN, '/dev/null';
721 open STDOUT, '>/dev/null';
722 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
724 # Don't need to keep a lock on the wiki as a daemon.
725 IkiWiki::unlockwiki();
727 foreach my $page (keys %toping) {
728 my $title=pagetitle(basename($page), 0);
729 my $url=urlto($page, "", 1);
730 foreach my $pingurl (@{$config{pingurl}}) {
731 debug("Pinging $pingurl for $page");
733 my $client = RPC::XML::Client->new($pingurl);
734 my $req = RPC::XML::request->new('weblogUpdates.ping',
736 my $res = $client->send_request($req);
738 error("Did not receive response to ping");
741 if (! exists $r->{flerror} || $r->{flerror}) {
742 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
746 error "Ping failed: $@";
751 exit 0; # daemon done
759 if (exists $params{rootpage}) {
760 $rootpage=bestlink($params{page}, $params{rootpage});
761 if (!length $rootpage) {
762 $rootpage=$params{rootpage};
766 $rootpage=$params{page};