2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
15 hook(type => "getopt", id => "inline", call => \&getopt);
16 hook(type => "checkconfig", id => "inline", call => \&checkconfig);
17 hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
18 hook(type => "preprocess", id => "inline",
19 call => \&IkiWiki::preprocess_inline);
20 hook(type => "pagetemplate", id => "inline",
21 call => \&IkiWiki::pagetemplate_inline);
22 # Hook to change to do pinging since it's called late.
23 # This ensures each page only pings once and prevents slow
24 # pings interrupting page builds.
25 hook(type => "change", id => "inline",
26 call => \&IkiWiki::pingurl);
31 eval q{use Getopt::Long};
33 Getopt::Long::Configure('pass_through');
35 "rss!" => \$config{rss},
36 "atom!" => \$config{atom},
40 sub checkconfig () { #{{{
41 if (($config{rss} || $config{atom}) && ! length $config{url}) {
42 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
45 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
48 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
52 sub sessioncgi () { #{{{
56 if ($q->param('do') eq 'blog') {
57 my $page=decode_utf8($q->param('title'));
58 $page=~s/\///g; # no slashes in blog posts
59 # if the page already exists, munge it to be unique
60 my $from=$q->param('from');
62 while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
63 $add=1 unless length $add;
66 $q->param('page', $page.$add);
67 # now go create the page
68 $q->param('do', 'create');
69 IkiWiki::cgi_editpage($q, $session);
74 # Back to ikiwiki namespace for the rest, this code is very much
75 # internal to ikiwiki even though it's separated into a plugin.
83 return (defined $val && lc($val) eq "yes");
86 sub preprocess_inline (@) { #{{{
89 if (! exists $params{pages}) {
92 my $raw=yesno($params{raw});
93 my $archive=yesno($params{archive});
94 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
95 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
96 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
97 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
98 $feeds=0 if $params{preview};
99 my $feedonly=yesno($params{feedonly});
100 if (! exists $params{show} && ! $archive) {
104 if (exists $params{description}) {
105 $desc = $params{description}
107 $desc = $config{wikiname};
109 my $actions=yesno($params{actions});
110 if (exists $params{template}) {
111 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
114 $params{template} = $archive ? "archivepage" : "inlinepage";
118 foreach my $page (keys %pagesources) {
119 next if $page eq $params{page};
120 if (pagespec_match($page, $params{pages}, location => $params{page})) {
125 if (exists $params{sort} && $params{sort} eq 'title') {
128 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
129 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
131 elsif (! exists $params{sort} || $params{sort} eq 'age') {
132 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
135 return sprintf(gettext("unknown sort type %s"), $params{sort});
138 if (yesno($params{reverse})) {
139 @list=reverse(@list);
142 if (exists $params{skip}) {
143 @list=@list[$params{skip} .. scalar @list - 1];
146 if ($params{show} && @list > $params{show}) {
147 @list=@list[0..$params{show} - 1];
150 add_depends($params{page}, $params{pages});
151 # Explicitly add all currently displayed pages as dependencies, so
152 # that if they are removed or otherwise changed, the inline will be
153 # sure to be updated.
154 add_depends($params{page}, join(" or ", @list));
155 # Force a scan of this page so any metadata that appears after this
156 # inline directive is available when inlining. The page normally
157 # wouldn't be scanned if it's only being rebuilt because of a
159 IkiWiki::scan($pagesources{$params{page}});
163 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
164 if (exists $knownfeeds{$feedid}) {
165 $feednum=$knownfeeds{$feedid};
168 if (exists $page_numfeeds{$params{destpage}}) {
170 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
174 $feednum=$knownfeeds{$feedid}="";
176 $page_numfeeds{$params{destpage}}=1;
181 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
182 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
185 if ($config{cgiurl} && (exists $params{rootpage} ||
186 (exists $params{postform} && yesno($params{postform})))) {
187 # Add a blog post form, with feed buttons.
188 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
189 $formtemplate->param(cgiurl => $config{cgiurl});
190 $formtemplate->param(rootpage =>
191 exists $params{rootpage} ? $params{rootpage} : $params{page});
192 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
193 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
194 if (exists $params{postformtext}) {
195 $formtemplate->param(postformtext =>
196 $params{postformtext});
199 $formtemplate->param(postformtext =>
200 gettext("Add a new post titled:"));
202 $ret.=$formtemplate->output;
206 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
207 $linktemplate->param(rssurl => $rssurl) if $rss;
208 $linktemplate->param(atomurl => $atomurl) if $atom;
209 $ret.=$linktemplate->output;
213 require HTML::Template;
214 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
216 return sprintf(gettext("nonexistant template %s"), $params{template});
218 my $template=HTML::Template->new(@params) unless $raw;
220 foreach my $page (@list) {
221 my $file = $pagesources{$page};
222 my $type = pagetype($file);
223 if (! $raw || ($raw && ! defined $type)) {
224 unless ($archive && $quick) {
225 # Get the content before populating the
226 # template, since getting the content uses
227 # the same template if inlines are nested.
228 my $content=get_inline_content($page, $params{destpage});
229 $template->param(content => $content);
231 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
232 $template->param(title => pagetitle(basename($page)));
233 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
236 my $file = $pagesources{$page};
237 my $type = pagetype($file);
238 if ($config{discussion}) {
239 my $discussionlink=gettext("discussion");
240 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
241 (length $config{cgiurl} ||
242 exists $links{$page."/".$discussionlink})) {
243 $template->param(have_actions => 1);
244 $template->param(discussionlink =>
247 gettext("Discussion"),
252 if (length $config{cgiurl} && defined $type) {
253 $template->param(have_actions => 1);
254 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
258 run_hooks(pagetemplate => sub {
259 shift->(page => $page, destpage => $params{destpage},
260 template => $template,);
263 $ret.=$template->output;
264 $template->clear_params;
269 linkify($page, $params{destpage},
270 preprocess($page, $params{destpage},
271 filter($page, $params{destpage},
272 readfile(srcfile($file)))));
279 if (exists $params{feedshow} && @list > $params{feedshow}) {
280 @list=@list[0..$params{feedshow} - 1];
282 if (exists $params{feedpages}) {
283 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
287 my $rssp=rsspage($params{destpage}).$feednum;
288 will_render($params{destpage}, $rssp);
289 writefile($rssp, $config{destdir},
290 genfeed("rss", $rssurl, $desc, $params{destpage}, @list));
291 $toping{$params{destpage}}=1 unless $config{rebuild};
292 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
295 my $atomp=atompage($params{destpage}).$feednum;
296 will_render($params{destpage}, $atomp);
297 writefile($atomp, $config{destdir},
298 genfeed("atom", $atomurl, $desc, $params{destpage}, @list));
299 $toping{$params{destpage}}=1 unless $config{rebuild};
300 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
307 sub pagetemplate_inline (@) { #{{{
309 my $page=$params{page};
310 my $template=$params{template};
312 $template->param(feedlinks => $feedlinks{$page})
313 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
316 sub get_inline_content ($$) { #{{{
320 my $file=$pagesources{$page};
321 my $type=pagetype($file);
323 return htmlize($page, $type,
324 linkify($page, $destpage,
325 preprocess($page, $destpage,
326 filter($page, $destpage,
327 readfile(srcfile($file))))));
334 sub date_822 ($) { #{{{
337 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
338 POSIX::setlocale(&POSIX::LC_TIME, "C");
339 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
340 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
344 sub date_3339 ($) { #{{{
347 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
348 POSIX::setlocale(&POSIX::LC_TIME, "C");
349 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
350 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
354 sub absolute_urls ($$) { #{{{
355 # sucky sub because rss sucks
362 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
363 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
364 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
368 sub rsspage ($) { #{{{
369 return targetpage(shift, "rss");
372 sub atompage ($) { #{{{
373 return targetpage(shift, "atom");
376 sub genfeed ($$$$@) { #{{{
383 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
385 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
388 foreach my $p (@pages) {
389 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
390 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
392 $itemtemplate->param(
393 title => pagetitle(basename($p)),
396 cdate_822 => date_822($pagectime{$p}),
397 mdate_822 => date_822($pagemtime{$p}),
398 cdate_3339 => date_3339($pagectime{$p}),
399 mdate_3339 => date_3339($pagemtime{$p}),
402 if ($itemtemplate->query(name => "enclosure")) {
403 my $file=$pagesources{$p};
404 my $type=pagetype($file);
406 $itemtemplate->param(content => $pcontent);
409 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
411 eval q{use File::MimeInfo};
413 $mime = mimetype($file);
415 $itemtemplate->param(
423 $itemtemplate->param(content => $pcontent);
426 run_hooks(pagetemplate => sub {
427 shift->(page => $p, destpage => $page,
428 template => $itemtemplate);
431 $content.=$itemtemplate->output;
432 $itemtemplate->clear_params;
434 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
437 my $template=template($feedtype."page.tmpl", blind_cache => 1);
439 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
440 wikiname => $config{wikiname},
443 feeddesc => $feeddesc,
444 feeddate => date_3339($lasttime),
446 version => $IkiWiki::version,
448 run_hooks(pagetemplate => sub {
449 shift->(page => $page, destpage => $page,
450 template => $template);
453 return $template->output;
456 sub pingurl (@) { #{{{
457 return unless @{$config{pingurl}} && %toping;
459 eval q{require RPC::XML::Client};
461 debug(gettext("RPC::XML::Client not found, not pinging"));
465 # daemonize here so slow pings don't slow down wiki updates
466 defined(my $pid = fork) or error("Can't fork: $!");
469 setsid() or error("Can't start a new session: $!");
470 open STDIN, '/dev/null';
471 open STDOUT, '>/dev/null';
472 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
474 # Don't need to keep a lock on the wiki as a daemon.
475 IkiWiki::unlockwiki();
477 foreach my $page (keys %toping) {
478 my $title=pagetitle(basename($page), 0);
479 my $url="$config{url}/".urlto($page, "");
480 foreach my $pingurl (@{$config{pingurl}}) {
481 debug("Pinging $pingurl for $page");
483 my $client = RPC::XML::Client->new($pingurl);
484 my $req = RPC::XML::request->new('weblogUpdates.ping',
486 my $res = $client->send_request($req);
488 debug("Did not receive response to ping");
491 if (! exists $r->{flerror} || $r->{flerror}) {
492 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
496 debug "Ping failed: $@";
501 exit 0; # daemon done