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);
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");
158 my $raw=yesno($params{raw});
159 my $archive=yesno($params{archive});
160 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
161 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
162 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
163 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick && ! $raw;
164 my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
165 my $feedonly=yesno($params{feedonly});
166 if (! exists $params{show} && ! $archive) {
169 if (! exists $params{feedshow} && exists $params{show}) {
170 $params{feedshow}=$params{show};
173 if (exists $params{description}) {
174 $desc = $params{description}
177 $desc = $config{wikiname};
179 my $actions=yesno($params{actions});
180 if (exists $params{template}) {
181 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
184 $params{template} = $archive ? "archivepage" : "inlinepage";
189 if (exists $params{pagenames}) {
190 foreach my $p (qw(sort pages)) {
191 if (exists $params{$p}) {
192 error sprintf(gettext("the %s and %s parameters cannot be used together"),
197 @list = grep { $_ ne '' }
198 map { bestlink($params{page}, $_) }
199 split ' ', $params{pagenames};
201 if (yesno($params{reverse})) {
202 @list=reverse(@list);
205 foreach my $p (@list) {
206 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
214 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
215 $num=$params{feedshow};
217 if ($params{skip} && $num) {
221 @list = pagespec_match_list($params{page}, $params{pages},
222 deptype => deptype($quick ? "presence" : "content"),
223 filter => sub { $_[0] eq $params{page} },
224 sort => exists $params{sort} ? $params{sort} : "age",
225 reverse => yesno($params{reverse}),
226 ($num ? (num => $num) : ()),
230 if (exists $params{skip}) {
231 @list=@list[$params{skip} .. $#list];
236 if (exists $params{feedshow} &&
237 $params{feedshow} && @list > $params{feedshow}) {
238 @feedlist=@list[0..$params{feedshow} - 1];
245 if ($params{show} && @list > $params{show}) {
246 @list=@list[0..$params{show} - 1];
249 if ($feeds && exists $params{feedpages}) {
250 @feedlist = pagespec_match_list(
251 $params{page}, "($params{pages}) and ($params{feedpages})",
252 deptype => deptype($quick ? "presence" : "content"),
257 my ($feedbase, $feednum);
259 # Ensure that multiple feeds on a page go to unique files.
261 # Feedfile can lead to conflicts if usedirs is not enabled,
262 # so avoid supporting it in that case.
263 delete $params{feedfile} if ! $config{usedirs};
264 # Tight limits on legal feedfiles, to avoid security issues
266 if (defined $params{feedfile}) {
267 if ($params{feedfile} =~ /\// ||
268 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
269 error("illegal feedfile");
271 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
273 $feedbase=targetpage($params{page}, "", $params{feedfile});
275 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
276 if (exists $knownfeeds{$feedid}) {
277 $feednum=$knownfeeds{$feedid};
280 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
282 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
286 $feednum=$knownfeeds{$feedid}="";
288 $page_numfeeds{$params{destpage}}{$feedbase}=1;
294 my ($rssurl, $atomurl, $rssdesc, $atomdesc);
297 $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
298 $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
301 $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
302 $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
308 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
309 (exists $params{postform} && yesno($params{postform}))) &&
310 IkiWiki->can("cgi_editpage")) {
311 # Add a blog post form, with feed buttons.
312 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
313 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
314 $formtemplate->param(rootpage => rootpage(%params));
317 $formtemplate->param(rssurl => $rssurl);
318 $formtemplate->param(rssdesc => $rssdesc);
321 $formtemplate->param(atomurl => $atomurl);
322 $formtemplate->param(atomdesc => $atomdesc);
325 if (exists $params{postformtext}) {
326 $formtemplate->param(postformtext =>
327 $params{postformtext});
330 $formtemplate->param(postformtext =>
331 gettext("Add a new post titled:"));
333 if (exists $params{id}) {
334 $formtemplate->param(postformid =>
337 $ret.=$formtemplate->output;
339 # The post form includes the feed buttons, so
340 # emptyfeeds cannot be hidden.
343 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
345 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
347 $linktemplate->param(rssurl => $rssurl);
348 $linktemplate->param(rssdesc => $rssdesc);
351 $linktemplate->param(atomurl => $atomurl);
352 $linktemplate->param(atomdesc => $atomdesc);
354 if (exists $params{id}) {
355 $linktemplate->param(id => $params{id});
357 $ret.=$linktemplate->output;
363 # cannot use wiki pages as templates; template not sanitized due to
366 $template=template_depends($params{template}.".tmpl", $params{page},
370 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
373 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
375 foreach my $page (@list) {
376 my $file = $pagesources{$page};
377 my $type = pagetype($file);
380 # Get the content before populating the
381 # template, since getting the content uses
382 # the same template if inlines are nested.
383 my $content=get_inline_content($page, $params{destpage});
384 $template->param(content => $content);
386 $template->param(pageurl => urlto($page, $params{destpage}));
387 $template->param(inlinepage => $page);
388 $template->param(title => pagetitle(basename($page)));
389 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
390 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
391 $template->param(first => 1) if $page eq $list[0];
392 $template->param(last => 1) if $page eq $list[$#list];
393 $template->param(html5 => $config{html5});
396 my $file = $pagesources{$page};
397 my $type = pagetype($file);
398 if ($config{discussion}) {
399 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
400 (length $config{cgiurl} ||
401 exists $pagesources{$page."/".lc($config{discussionpage})})) {
402 $template->param(have_actions => 1);
403 $template->param(discussionlink =>
406 $config{discussionpage},
411 if (length $config{cgiurl} &&
413 IkiWiki->can("cgi_editpage")) {
414 $template->param(have_actions => 1);
415 $template->param(editurl => cgiurl(do => "edit", page => $page));
420 run_hooks(pagetemplate => sub {
421 shift->(page => $page, destpage => $params{destpage},
422 template => $template,);
425 $ret.=$template->output;
426 $template->clear_params;
431 linkify($page, $params{destpage},
432 preprocess($page, $params{destpage},
433 filter($page, $params{destpage},
434 readfile(srcfile($file)))));
438 readfile(srcfile($file));
444 if ($feeds && ($emptyfeeds || @feedlist)) {
446 my $rssp=$feedbase."rss".$feednum;
447 will_render($params{destpage}, $rssp);
448 if (! $params{preview}) {
449 writefile($rssp, $config{destdir},
451 $config{url}."/".$rssp, $desc, $params{guid}, $params{page}, @feedlist));
452 $toping{$params{destpage}}=1 unless $config{rebuild};
453 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
457 my $atomp=$feedbase."atom".$feednum;
458 will_render($params{destpage}, $atomp);
459 if (! $params{preview}) {
460 writefile($atomp, $config{destdir},
461 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{page}, @feedlist));
462 $toping{$params{destpage}}=1 unless $config{rebuild};
463 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
468 clear_inline_content_cache();
470 return $ret if $raw || $nested;
472 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
475 sub pagetemplate_inline (@) {
477 my $page=$params{page};
478 my $template=$params{template};
480 $template->param(feedlinks => $feedlinks{$page})
481 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
486 my $cached_destpage="";
488 sub get_inline_content ($$) {
492 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
493 return $inline_content{$page};
496 my $file=$pagesources{$page};
497 my $type=pagetype($file);
501 $ret=htmlize($page, $destpage, $type,
502 linkify($page, $destpage,
503 preprocess($page, $destpage,
504 filter($page, $destpage,
505 readfile(srcfile($file))))));
507 if (isinternal($page)) {
508 # make inlined text of internal pages searchable
509 run_hooks(indexhtml => sub {
510 shift->(page => $page, destpage => $destpage,
516 if ($cached_destpage ne $destpage) {
517 clear_inline_content_cache();
518 $cached_destpage=$destpage;
520 return $inline_content{$page}=$ret;
523 sub clear_inline_content_cache () {
532 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
533 POSIX::setlocale(&POSIX::LC_TIME, "C");
534 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
535 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
539 sub absolute_urls ($$) {
540 # needed because rss sucks
546 my $urltop; # calculated if needed
550 eval q{use HTML::Parser; use HTML::Tagset};
552 my $p = HTML::Parser->new(api_version => 3);
553 $p->handler(default => sub { $ret.=join("", @_) }, "text");
554 $p->handler(start => sub {
555 my ($tagname, $pos, $text) = @_;
556 if (ref $HTML::Tagset::linkElements{$tagname}) {
558 # use attribute sets from right to left
559 # to avoid invalidating the offsets
560 # when replacing the values
561 my ($k_offset, $k_len, $v_offset, $v_len) =
563 my $attrname = lc(substr($text, $k_offset, $k_len));
564 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
565 next unless $v_offset; # 0 v_offset means no value
566 my $v = substr($text, $v_offset, $v_len);
567 $v =~ s/^([\'\"])(.*)\1$/$2/;
568 eval q{use HTML::Entities};
569 my $dv = decode_entities($v);
571 $v=$baseurl.$v; # anchor
573 elsif ($dv=~/^(?!\w+:)[^\/]/) {
574 $v=$url.$v; # relative url
577 if (! defined $urltop) {
578 # what is the non path part of the url?
579 my $top_uri = URI->new($url);
580 $top_uri->path_query(""); # reset the path
581 $urltop = $top_uri->as_string;
583 $v=$urltop.$v; # url relative to top of site
585 $v =~ s/\"/"/g; # since we quote with ""
586 substr($text, $v_offset, $v_len) = qq("$v");
590 }, "tagname, tokenpos, text");
597 sub genfeed ($$$$$@) {
605 my $url=URI->new(encode_utf8(urlto($page,"",1)));
607 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
610 foreach my $p (@pages) {
611 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
612 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
614 $itemtemplate->param(
615 title => pagetitle(basename($p)),
618 cdate_822 => date_822($pagectime{$p}),
619 mdate_822 => date_822($pagemtime{$p}),
620 cdate_3339 => date_3339($pagectime{$p}),
621 mdate_3339 => date_3339($pagemtime{$p}),
624 if (exists $pagestate{$p}) {
625 if (exists $pagestate{$p}{meta}{guid}) {
626 eval q{use HTML::Entities};
627 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
630 if (exists $pagestate{$p}{meta}{updated}) {
631 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
632 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
636 if ($itemtemplate->query(name => "enclosure")) {
637 my $file=$pagesources{$p};
638 my $type=pagetype($file);
640 $itemtemplate->param(content => $pcontent);
643 my $size=(srcfile_stat($file))[8];
645 eval q{use File::MimeInfo};
647 $mime = mimetype($file);
649 $itemtemplate->param(
657 $itemtemplate->param(content => $pcontent);
660 run_hooks(pagetemplate => sub {
661 shift->(page => $p, destpage => $page,
662 template => $itemtemplate);
665 $content.=$itemtemplate->output;
666 $itemtemplate->clear_params;
668 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
671 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
673 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
674 wikiname => $config{wikiname},
677 feeddesc => $feeddesc,
679 feeddate => date_3339($lasttime),
682 run_hooks(pagetemplate => sub {
683 shift->(page => $page, destpage => $page,
684 template => $template);
687 return $template->output;
691 return unless @{$config{pingurl}} && %toping;
693 eval q{require RPC::XML::Client};
695 debug(gettext("RPC::XML::Client not found, not pinging"));
699 # daemonize here so slow pings don't slow down wiki updates
700 defined(my $pid = fork) or error("Can't fork: $!");
703 POSIX::setsid() or error("Can't start a new session: $!");
704 open STDIN, '/dev/null';
705 open STDOUT, '>/dev/null';
706 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
708 # Don't need to keep a lock on the wiki as a daemon.
709 IkiWiki::unlockwiki();
711 foreach my $page (keys %toping) {
712 my $title=pagetitle(basename($page), 0);
713 my $url=urlto($page, "", 1);
714 foreach my $pingurl (@{$config{pingurl}}) {
715 debug("Pinging $pingurl for $page");
717 my $client = RPC::XML::Client->new($pingurl);
718 my $req = RPC::XML::request->new('weblogUpdates.ping',
720 my $res = $client->send_request($req);
722 error("Did not receive response to ping");
725 if (! exists $r->{flerror} || $r->{flerror}) {
726 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
730 error "Ping failed: $@";
735 exit 0; # daemon done
743 if (exists $params{rootpage}) {
744 $rootpage=bestlink($params{page}, $params{rootpage});
745 if (!length $rootpage) {
746 $rootpage=$params{rootpage};
750 $rootpage=$params{page};