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', $page.$add);
132 # now go create the page
133 $q->param('do', 'create');
134 # make sure the editpage plugin in 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=! $nested && (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 = map { bestlink($params{page}, $_) }
198 split ' ', $params{pagenames};
200 if (yesno($params{reverse})) {
201 @list=reverse(@list);
204 foreach my $p (@list) {
205 add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
213 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
214 $num=$params{feedshow};
216 if ($params{skip} && $num) {
220 @list = pagespec_match_list($params{page}, $params{pages},
221 deptype => deptype($quick ? "presence" : "content"),
222 filter => sub { $_[0] eq $params{page} },
223 sort => exists $params{sort} ? $params{sort} : "age",
224 reverse => yesno($params{reverse}),
225 ($num ? (num => $num) : ()),
229 if (exists $params{skip}) {
230 @list=@list[$params{skip} .. $#list];
235 if (exists $params{feedshow} &&
236 $params{feedshow} && @list > $params{feedshow}) {
237 @feedlist=@list[0..$params{feedshow} - 1];
244 if ($params{show} && @list > $params{show}) {
245 @list=@list[0..$params{show} - 1];
248 if ($feeds && exists $params{feedpages}) {
249 @feedlist = pagespec_match_list(
250 $params{page}, "($params{pages}) and ($params{feedpages})",
251 deptype => deptype($quick ? "presence" : "content"),
256 my ($feedbase, $feednum);
258 # Ensure that multiple feeds on a page go to unique files.
260 # Feedfile can lead to conflicts if usedirs is not enabled,
261 # so avoid supporting it in that case.
262 delete $params{feedfile} if ! $config{usedirs};
263 # Tight limits on legal feedfiles, to avoid security issues
265 if (defined $params{feedfile}) {
266 if ($params{feedfile} =~ /\// ||
267 $params{feedfile} !~ /$config{wiki_file_regexp}/) {
268 error("illegal feedfile");
270 $params{feedfile}=possibly_foolish_untaint($params{feedfile});
272 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
274 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
275 if (exists $knownfeeds{$feedid}) {
276 $feednum=$knownfeeds{$feedid};
279 if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
281 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
285 $feednum=$knownfeeds{$feedid}="";
287 $page_numfeeds{$params{destpage}}{$feedbase}=1;
293 my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
294 my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
298 if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
299 (exists $params{postform} && yesno($params{postform}))) &&
300 IkiWiki->can("cgi_editpage")) {
301 # Add a blog post form, with feed buttons.
302 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
303 $formtemplate->param(cgiurl => $config{cgiurl});
304 $formtemplate->param(rootpage => rootpage(%params));
305 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
306 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
307 if (exists $params{postformtext}) {
308 $formtemplate->param(postformtext =>
309 $params{postformtext});
312 $formtemplate->param(postformtext =>
313 gettext("Add a new post titled:"));
315 $ret.=$formtemplate->output;
317 # The post form includes the feed buttons, so
318 # emptyfeeds cannot be hidden.
321 elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
323 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
324 $linktemplate->param(rssurl => $rssurl) if $rss;
325 $linktemplate->param(atomurl => $atomurl) if $atom;
326 $ret.=$linktemplate->output;
332 # cannot use wiki pages as templates; template not sanitized due to
335 $template=template_depends($params{template}.".tmpl", $params{page},
339 error gettext("failed to process template:")." $@";
342 error sprintf(gettext("template %s not found"), $params{template}.".tmpl");
345 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
347 foreach my $page (@list) {
348 my $file = $pagesources{$page};
349 my $type = pagetype($file);
352 # Get the content before populating the
353 # template, since getting the content uses
354 # the same template if inlines are nested.
355 my $content=get_inline_content($page, $params{destpage});
356 $template->param(content => $content);
358 $template->param(pageurl => urlto($page, $params{destpage}));
359 $template->param(inlinepage => $page);
360 $template->param(title => pagetitle(basename($page)));
361 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
362 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
363 $template->param(first => 1) if $page eq $list[0];
364 $template->param(last => 1) if $page eq $list[$#list];
367 my $file = $pagesources{$page};
368 my $type = pagetype($file);
369 if ($config{discussion}) {
370 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
371 (length $config{cgiurl} ||
372 exists $pagesources{$page."/".lc($config{discussionpage})})) {
373 $template->param(have_actions => 1);
374 $template->param(discussionlink =>
377 $config{discussionpage},
382 if (length $config{cgiurl} &&
384 IkiWiki->can("cgi_editpage")) {
385 $template->param(have_actions => 1);
386 $template->param(editurl => cgiurl(do => "edit", page => $page));
391 run_hooks(pagetemplate => sub {
392 shift->(page => $page, destpage => $params{destpage},
393 template => $template,);
396 $ret.=$template->output;
397 $template->clear_params;
402 linkify($page, $params{destpage},
403 preprocess($page, $params{destpage},
404 filter($page, $params{destpage},
405 readfile(srcfile($file)))));
409 readfile(srcfile($file));
415 if ($feeds && ($emptyfeeds || @feedlist)) {
417 my $rssp=$feedbase."rss".$feednum;
418 will_render($params{destpage}, $rssp);
419 if (! $params{preview}) {
420 writefile($rssp, $config{destdir},
422 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
423 $toping{$params{destpage}}=1 unless $config{rebuild};
424 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
428 my $atomp=$feedbase."atom".$feednum;
429 will_render($params{destpage}, $atomp);
430 if (! $params{preview}) {
431 writefile($atomp, $config{destdir},
432 genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
433 $toping{$params{destpage}}=1 unless $config{rebuild};
434 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
439 clear_inline_content_cache();
441 return $ret if $raw || $nested;
443 return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
446 sub pagetemplate_inline (@) {
448 my $page=$params{page};
449 my $template=$params{template};
451 $template->param(feedlinks => $feedlinks{$page})
452 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
457 my $cached_destpage="";
459 sub get_inline_content ($$) {
463 if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
464 return $inline_content{$page};
467 my $file=$pagesources{$page};
468 my $type=pagetype($file);
472 $ret=htmlize($page, $destpage, $type,
473 linkify($page, $destpage,
474 preprocess($page, $destpage,
475 filter($page, $destpage,
476 readfile(srcfile($file))))));
480 if ($cached_destpage ne $destpage) {
481 clear_inline_content_cache();
482 $cached_destpage=$destpage;
484 return $inline_content{$page}=$ret;
487 sub clear_inline_content_cache () {
496 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
497 POSIX::setlocale(&POSIX::LC_TIME, "C");
498 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
499 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
506 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
507 POSIX::setlocale(&POSIX::LC_TIME, "C");
508 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
509 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
513 sub absolute_urls ($$) {
514 # sucky sub because rss sucks
521 # what is the non path part of the url?
522 my $top_uri = URI->new($url);
523 $top_uri->path_query(""); # reset the path
524 my $urltop = $top_uri->as_string;
526 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
527 # relative to another wiki page
528 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
529 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
530 # relative to the top of the site
531 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
532 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
536 sub genfeed ($$$$$@) {
544 my $url=URI->new(encode_utf8(urlto($page,"",1)));
546 my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
549 foreach my $p (@pages) {
550 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
551 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
553 $itemtemplate->param(
554 title => pagetitle(basename($p)),
557 cdate_822 => date_822($pagectime{$p}),
558 mdate_822 => date_822($pagemtime{$p}),
559 cdate_3339 => date_3339($pagectime{$p}),
560 mdate_3339 => date_3339($pagemtime{$p}),
563 if (exists $pagestate{$p}) {
564 if (exists $pagestate{$p}{meta}{guid}) {
565 eval q{use HTML::Entities};
566 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
569 if (exists $pagestate{$p}{meta}{updated}) {
570 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
571 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
575 if ($itemtemplate->query(name => "enclosure")) {
576 my $file=$pagesources{$p};
577 my $type=pagetype($file);
579 $itemtemplate->param(content => $pcontent);
582 my $size=(srcfile_stat($file))[8];
584 eval q{use File::MimeInfo};
586 $mime = mimetype($file);
588 $itemtemplate->param(
596 $itemtemplate->param(content => $pcontent);
599 run_hooks(pagetemplate => sub {
600 shift->(page => $p, destpage => $page,
601 template => $itemtemplate);
604 $content.=$itemtemplate->output;
605 $itemtemplate->clear_params;
607 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
610 my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
612 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
613 wikiname => $config{wikiname},
616 feeddesc => $feeddesc,
618 feeddate => date_3339($lasttime),
620 version => $IkiWiki::version,
622 run_hooks(pagetemplate => sub {
623 shift->(page => $page, destpage => $page,
624 template => $template);
627 return $template->output;
631 return unless @{$config{pingurl}} && %toping;
633 eval q{require RPC::XML::Client};
635 debug(gettext("RPC::XML::Client not found, not pinging"));
639 # daemonize here so slow pings don't slow down wiki updates
640 defined(my $pid = fork) or error("Can't fork: $!");
643 POSIX::setsid() or error("Can't start a new session: $!");
644 open STDIN, '/dev/null';
645 open STDOUT, '>/dev/null';
646 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
648 # Don't need to keep a lock on the wiki as a daemon.
649 IkiWiki::unlockwiki();
651 foreach my $page (keys %toping) {
652 my $title=pagetitle(basename($page), 0);
653 my $url=urlto($page, "", 1);
654 foreach my $pingurl (@{$config{pingurl}}) {
655 debug("Pinging $pingurl for $page");
657 my $client = RPC::XML::Client->new($pingurl);
658 my $req = RPC::XML::request->new('weblogUpdates.ping',
660 my $res = $client->send_request($req);
662 error("Did not receive response to ping");
665 if (! exists $r->{flerror} || $r->{flerror}) {
666 error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
670 error "Ping failed: $@";
675 exit 0; # daemon done
683 if (exists $params{rootpage}) {
684 $rootpage=bestlink($params{page}, $params{rootpage});
685 if (!length $rootpage) {
686 $rootpage=$params{rootpage};
690 $rootpage=$params{page};