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];
56 description => "enable rss feeds by default?",
63 description => "enable atom feeds by default?",
70 description => "allow rss feeds to be used?",
77 description => "allow atom feeds to be used?",
83 example => "http://rpc.technorati.com/rpc/ping",
84 description => "urls to ping (using XML-RPC) on feed update",
91 if (($config{rss} || $config{atom}) && ! length $config{url}) {
92 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
95 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
98 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
100 if (! exists $config{pingurl}) {
108 # Fill in the inline content generated earlier. This is actually an
110 $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
113 return $params{content};
116 sub sessioncgi ($$) {
120 if ($q->param('do') eq 'blog') {
121 my $page=titlepage(decode_utf8($q->param('title')));
122 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
123 # if the page already exists, munge it to be unique
124 my $from=$q->param('from');
126 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
127 $add=1 unless length $add;
130 $q->param('page', $page.$add);
131 # now go create the page
132 $q->param('do', 'create');
133 # make sure the editpage plugin in loaded
134 if (IkiWiki->can("cgi_editpage")) {
135 IkiWiki::cgi_editpage($q, $session);
138 error(gettext("page editing not allowed"));
144 # Back to ikiwiki namespace for the rest, this code is very much
145 # internal to ikiwiki even though it's separated into a plugin.
151 sub preprocess_inline (@) {
154 if (! exists $params{pages} && ! exists $params{pagenames}) {
155 error gettext("missing pages parameter");
157 my $raw=yesno($params{raw});
158 my $archive=yesno($params{archive});
159 my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
160 my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
161 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
162 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
163 my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
164 my $feedonly=yesno($params{feedonly});
165 if (! exists $params{show} && ! $archive) {
168 if (! exists $params{feedshow} && exists $params{show}) {
169 $params{feedshow}=$params{show};
172 if (exists $params{description}) {
173 $desc = $params{description}
176 $desc = $config{wikiname};
178 my $actions=yesno($params{actions});
179 if (exists $params{template}) {
180 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
183 $params{template} = $archive ? "archivepage" : "inlinepage";
188 if (exists $params{pagenames}) {
189 foreach my $p (qw(sort pages)) {
190 if (exists $params{$p}) {
191 error sprintf(gettext("the %s and %s parameters cannot be used together"),
196 @list = map { bestlink($params{page}, $_) }
197 split ' ', $params{pagenames};
199 $params{pages} = join(" or ", @list);
202 @list = pagespec_match_list(
203 [ grep { $_ ne $params{page} } keys %pagesources ],
204 $params{pages}, location => $params{page});
206 if (exists $params{sort} && $params{sort} eq 'title') {
207 @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
209 elsif (exists $params{sort} && $params{sort} eq 'title_natural') {
210 eval q{use Sort::Naturally};
212 error(gettext("Sort::Naturally needed for title_natural sort"));
214 @list=sort { Sort::Naturally::ncmp(pagetitle(basename($a)), pagetitle(basename($b))) } @list;
216 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
217 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
219 elsif (! exists $params{sort} || $params{sort} eq 'age') {
220 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
223 error sprintf(gettext("unknown sort type %s"), $params{sort});
227 if (yesno($params{reverse})) {
228 @list=reverse(@list);
231 if (exists $params{skip}) {
232 @list=@list[$params{skip} .. scalar @list - 1];
237 if (exists $params{feedshow} &&
238 $params{feedshow} && @list > $params{feedshow}) {
239 @feedlist=@list[0..$params{feedshow} - 1];
246 if ($params{show} && @list > $params{show}) {
247 @list=@list[0..$params{show} - 1];
250 add_depends($params{page}, $params{pages});
251 # Explicitly add all currently displayed pages as dependencies, so
252 # that if they are removed or otherwise changed, the inline will be
253 # sure to be updated.
254 add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
256 if ($feeds && exists $params{feedpages}) {
257 @feedlist=pagespec_match_list(\@feedlist, $params{feedpages}, location => $params{page});
260 my ($feedbase, $feednum);
262 # Ensure that multiple feeds on a page go to unique files.
264 # Feedfile can lead to conflicts if usedirs is not enabled,
265 # so avoid supporting it in that case.
266 delete $params{feedfile} if ! $config{usedirs};
267 # Tight limits on legal feedfiles, to avoid security issues
269 if (defined $params{feedfile}) {
270 if ($params{feedfile} =~ /\// ||
271 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
272 error("illegal feedfile");
274 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
276 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
278 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
279 if (exists $knownfeeds{$feedid}) {
280 $feednum=$knownfeeds{$feedid};
283 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
285 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
289 $feednum=$knownfeeds{$feedid}="";
291 $page_numfeeds{$params{destpage}}{$feedbase}=1;
297 my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
298 my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
302 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
303 (exists $params{postform} && yesno($params{postform}))) &&
304 IkiWiki->can("cgi_editpage")) {
305 # Add a blog post form, with feed buttons.
306 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
307 $formtemplate->param(cgiurl => $config{cgiurl});
309 if (exists $params{rootpage}) {
310 $rootpage=bestlink($params{page}, $params{rootpage});
311 if (!length $rootpage) {
312 $rootpage=$params{rootpage};
316 $rootpage=$params{page};
318 $formtemplate->param(rootpage => $rootpage);
319 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
320 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
321 if (exists $params{postformtext}) {
322 $formtemplate->param(postformtext =>
323 $params{postformtext});
326 $formtemplate->param(postformtext =>
327 gettext("Add a new post titled:"));
329 $ret.=$formtemplate->output;
331 # The post form includes the feed buttons, so
332 # emptyfeeds cannot be hidden.
335 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
337 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
338 $linktemplate->param(rssurl => $rssurl) if $rss;
339 $linktemplate->param(atomurl => $atomurl) if $atom;
340 $ret.=$linktemplate->output;
344 require HTML::Template;
345 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
347 error sprintf(gettext("nonexistant template %s"), $params{template});
349 my $template=HTML::Template->new(@params) unless $raw;
351 foreach my $page (@list) {
352 my $file = $pagesources{$page};
353 my $type = pagetype($file);
354 if (! $raw || ($raw && ! defined $type)) {
355 unless ($archive && $quick) {
356 # Get the content before populating the
357 # template, since getting the content uses
358 # the same template if inlines are nested.
359 my $content=get_inline_content($page, $params{destpage});
360 $template->param(content => $content);
362 $template->param(pageurl => urlto($page, $params{destpage}));
363 $template->param(inlinepage => $page);
364 $template->param(title => pagetitle(basename($page)));
365 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
366 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
367 $template->param(first => 1) if $page eq $list[0];
368 $template->param(last => 1) if $page eq $list[$#list];
371 my $file = $pagesources{$page};
372 my $type = pagetype($file);
373 if ($config{discussion}) {
374 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
375 (length $config{cgiurl} ||
376 exists $links{$page."/".$config{discussionpage}})) {
377 $template->param(have_actions => 1);
378 $template->param(discussionlink =>
381 $config{discussionpage},
386 if (length $config{cgiurl} && defined $type) {
387 $template->param(have_actions => 1);
388 $template->param(editurl => cgiurl(do => "edit", page => $page));
392 run_hooks(pagetemplate => sub {
393 shift->(page => $page, destpage => $params{destpage},
394 template => $template,);
397 $ret.=$template->output;
398 $template->clear_params;
403 linkify($page, $params{destpage},
404 preprocess($page, $params{destpage},
405 filter($page, $params{destpage},
406 readfile(srcfile($file)))));
412 if ($feeds && ($emptyfeeds || @feedlist)) {
414 my $rssp=$feedbase."rss".$feednum;
415 will_render($params{destpage}, $rssp);
416 if (! $params{preview}) {
417 writefile($rssp, $config{destdir},
419 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
420 $toping{$params{destpage}}=1 unless $config{rebuild};
421 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
425 my $atomp=$feedbase."atom".$feednum;
426 will_render($params{destpage}, $atomp);
427 if (! $params{preview}) {
428 writefile($atomp, $config{destdir},
429 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
430 $toping{$params{destpage}}=1 unless $config{rebuild};
431 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
436 return $ret if $raw || $nested;
438 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
441 sub pagetemplate_inline (@) {
443 my $page=$params{page};
444 my $template=$params{template};
446 $template->param(feedlinks => $feedlinks{$page})
447 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
450 sub get_inline_content ($$) {
454 my $file=$pagesources{$page};
455 my $type=pagetype($file);
458 my $ret=htmlize($page, $destpage, $type,
459 linkify($page, $destpage,
460 preprocess($page, $destpage,
461 filter($page, $destpage,
462 readfile(srcfile($file))))));
474 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
475 POSIX::setlocale(&POSIX::LC_TIME, "C");
476 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
477 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
484 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
485 POSIX::setlocale(&POSIX::LC_TIME, "C");
486 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
487 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
491 sub absolute_urls ($$) {
492 # sucky sub because rss sucks
499 # what is the non path part of the url?
500 my $top_uri = URI->new($url);
501 $top_uri->path_query(""); # reset the path
502 my $urltop = $top_uri->as_string;
504 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
505 # relative to another wiki page
506 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
507 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
508 # relative to the top of the site
509 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
510 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
514 sub genfeed ($$$$$@) {
522 my $url=URI->new(encode_utf8(urlto($page,"",1)));
524 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
527 foreach my $p (@pages) {
528 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
529 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
531 $itemtemplate->param(
532 title => pagetitle(basename($p)),
535 cdate_822 => date_822($pagectime{$p}),
536 mdate_822 => date_822($pagemtime{$p}),
537 cdate_3339 => date_3339($pagectime{$p}),
538 mdate_3339 => date_3339($pagemtime{$p}),
541 if (exists $pagestate{$p}) {
542 if (exists $pagestate{$p}{meta}{guid}) {
543 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
546 if (exists $pagestate{$p}{meta}{updated}) {
547 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
548 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
552 if ($itemtemplate->query(name => "enclosure")) {
553 my $file=$pagesources{$p};
554 my $type=pagetype($file);
556 $itemtemplate->param(content => $pcontent);
559 my $size=(srcfile_stat($file))[8];
561 eval q{use File::MimeInfo};
563 $mime = mimetype($file);
565 $itemtemplate->param(
573 $itemtemplate->param(content => $pcontent);
576 run_hooks(pagetemplate => sub {
577 shift->(page => $p, destpage => $page,
578 template => $itemtemplate);
581 $content.=$itemtemplate->output;
582 $itemtemplate->clear_params;
584 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
587 my $template=template($feedtype."page.tmpl", blind_cache => 1);
589 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
590 wikiname => $config{wikiname},
593 feeddesc => $feeddesc,
595 feeddate => date_3339($lasttime),
597 version => $IkiWiki::version,
599 run_hooks(pagetemplate => sub {
600 shift->(page => $page, destpage => $page,
601 template => $template);
604 return $template->output;
608 return unless @{$config{pingurl}} && %toping;
610 eval q{require RPC::XML::Client};
612 debug(gettext("RPC::XML::Client not found, not pinging"));
616 # daemonize here so slow pings don't slow down wiki updates
617 defined(my $pid = fork) or error("Can't fork: $!");
620 POSIX::setsid() or error("Can't start a new session: $!");
621 open STDIN, '/dev/null';
622 open STDOUT, '>/dev/null';
623 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
625 # Don't need to keep a lock on the wiki as a daemon.
626 IkiWiki::unlockwiki();
628 foreach my $page (keys %toping) {
629 my $title=pagetitle(basename($page), 0);
630 my $url=urlto($page, "", 1);
631 foreach my $pingurl (@{$config{pingurl}}) {
632 debug("Pinging $pingurl for $page");
634 my $client = RPC::XML::Client->new($pingurl);
635 my $req = RPC::XML::request->new('weblogUpdates.ping',
637 my $res = $client->send_request($req);
639 error("Did not receive response to ping");
642 if (! exists $r->{flerror} || $r->{flerror}) {
643 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
647 error "Ping failed: $@";
652 exit 0; # daemon done