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 my $feedonly=yesno($params{feedonly});
99 if (! exists $params{show} && ! $archive) {
103 if (exists $params{description}) {
104 $desc = $params{description}
106 $desc = $config{wikiname};
108 my $actions=yesno($params{actions});
109 if (exists $params{template}) {
110 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
113 $params{template} = $archive ? "archivepage" : "inlinepage";
117 foreach my $page (keys %pagesources) {
118 next if $page eq $params{page};
119 if (pagespec_match($page, $params{pages}, location => $params{page})) {
124 if (exists $params{sort} && $params{sort} eq 'title') {
127 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
128 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
130 elsif (! exists $params{sort} || $params{sort} eq 'age') {
131 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
134 return sprintf(gettext("unknown sort type %s"), $params{sort});
137 if (yesno($params{reverse})) {
138 @list=reverse(@list);
141 if (exists $params{skip}) {
142 @list=@list[$params{skip} .. scalar @list - 1];
145 if ($params{show} && @list > $params{show}) {
146 @list=@list[0..$params{show} - 1];
149 add_depends($params{page}, $params{pages});
150 # Explicitly add all currently displayed pages as dependencies, so
151 # that if they are removed or otherwise changed, the inline will be
152 # sure to be updated.
153 add_depends($params{page}, join(" or ", @list));
154 # Force a scan of this page so any metadata that appears after this
155 # inline directive is available when inlining. The page normally
156 # wouldn't be scanned if it's only being rebuilt because of a
158 IkiWiki::scan($pagesources{$params{page}});
162 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
163 if (exists $knownfeeds{$feedid}) {
164 $feednum=$knownfeeds{$feedid};
167 if (exists $page_numfeeds{$params{destpage}}) {
169 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
173 $feednum=$knownfeeds{$feedid}="";
175 $page_numfeeds{$params{destpage}}=1;
180 my $rssurl=basename(rsspage($params{destpage}).$feednum) if $feeds && $rss;
181 my $atomurl=basename(atompage($params{destpage}).$feednum) if $feeds && $atom;
184 if ($config{cgiurl} && (exists $params{rootpage} ||
185 (exists $params{postform} && yesno($params{postform})))) {
186 # Add a blog post form, with feed buttons.
187 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
188 $formtemplate->param(cgiurl => $config{cgiurl});
189 $formtemplate->param(rootpage =>
190 exists $params{rootpage} ? $params{rootpage} : $params{page});
191 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
192 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
193 if (exists $params{postformtext}) {
194 $formtemplate->param(postformtext =>
195 $params{postformtext});
198 $formtemplate->param(postformtext =>
199 gettext("Add a new post titled:"));
201 $ret.=$formtemplate->output;
205 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
206 $linktemplate->param(rssurl => $rssurl) if $rss;
207 $linktemplate->param(atomurl => $atomurl) if $atom;
208 $ret.=$linktemplate->output;
212 require HTML::Template;
213 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
215 return sprintf(gettext("nonexistant template %s"), $params{template});
217 my $template=HTML::Template->new(@params) unless $raw;
219 foreach my $page (@list) {
220 my $file = $pagesources{$page};
221 my $type = pagetype($file);
222 if (! $raw || ($raw && ! defined $type)) {
223 unless ($archive && $quick) {
224 # Get the content before populating the
225 # template, since getting the content uses
226 # the same template if inlines are nested.
227 my $content=get_inline_content($page, $params{destpage});
228 $template->param(content => $content);
230 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
231 $template->param(title => pagetitle(basename($page)));
232 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
233 $template->param(first => 1) if $page eq $list[0];
234 $template->param(last => 1) if $page eq $list[$#list];
237 my $file = $pagesources{$page};
238 my $type = pagetype($file);
239 if ($config{discussion}) {
240 my $discussionlink=gettext("discussion");
241 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
242 (length $config{cgiurl} ||
243 exists $links{$page."/".$discussionlink})) {
244 $template->param(have_actions => 1);
245 $template->param(discussionlink =>
248 gettext("Discussion"),
253 if (length $config{cgiurl} && defined $type) {
254 $template->param(have_actions => 1);
255 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
259 run_hooks(pagetemplate => sub {
260 shift->(page => $page, destpage => $params{destpage},
261 template => $template,);
264 $ret.=$template->output;
265 $template->clear_params;
270 linkify($page, $params{destpage},
271 preprocess($page, $params{destpage},
272 filter($page, $params{destpage},
273 readfile(srcfile($file)))));
280 if (exists $params{feedshow} && @list > $params{feedshow}) {
281 @list=@list[0..$params{feedshow} - 1];
283 if (exists $params{feedpages}) {
284 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
287 if ($rss && ! $params{preview}) {
288 my $rssp=rsspage($params{destpage}).$feednum;
289 will_render($params{destpage}, $rssp);
290 writefile($rssp, $config{destdir},
291 genfeed("rss", $rssurl, $desc, $params{destpage}, @list));
292 $toping{$params{destpage}}=1 unless $config{rebuild};
293 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
295 if ($atom && ! $params{preview}) {
296 my $atomp=atompage($params{destpage}).$feednum;
297 will_render($params{destpage}, $atomp);
298 writefile($atomp, $config{destdir},
299 genfeed("atom", $atomurl, $desc, $params{destpage}, @list));
300 $toping{$params{destpage}}=1 unless $config{rebuild};
301 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
308 sub pagetemplate_inline (@) { #{{{
310 my $page=$params{page};
311 my $template=$params{template};
313 $template->param(feedlinks => $feedlinks{$page})
314 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
317 sub get_inline_content ($$) { #{{{
321 my $file=$pagesources{$page};
322 my $type=pagetype($file);
324 return htmlize($page, $type,
325 linkify($page, $destpage,
326 preprocess($page, $destpage,
327 filter($page, $destpage,
328 readfile(srcfile($file))))));
335 sub date_822 ($) { #{{{
338 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
339 POSIX::setlocale(&POSIX::LC_TIME, "C");
340 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
341 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
345 sub date_3339 ($) { #{{{
348 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
349 POSIX::setlocale(&POSIX::LC_TIME, "C");
350 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
351 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
355 sub absolute_urls ($$) { #{{{
356 # sucky sub because rss sucks
363 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
364 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
365 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
369 sub rsspage ($) { #{{{
370 return targetpage(shift, "rss");
373 sub atompage ($) { #{{{
374 return targetpage(shift, "atom");
377 sub genfeed ($$$$@) { #{{{
384 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
386 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
389 foreach my $p (@pages) {
390 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
391 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
393 $itemtemplate->param(
394 title => pagetitle(basename($p)),
397 cdate_822 => date_822($pagectime{$p}),
398 mdate_822 => date_822($pagemtime{$p}),
399 cdate_3339 => date_3339($pagectime{$p}),
400 mdate_3339 => date_3339($pagemtime{$p}),
403 if ($itemtemplate->query(name => "enclosure")) {
404 my $file=$pagesources{$p};
405 my $type=pagetype($file);
407 $itemtemplate->param(content => $pcontent);
410 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
412 eval q{use File::MimeInfo};
414 $mime = mimetype($file);
416 $itemtemplate->param(
424 $itemtemplate->param(content => $pcontent);
427 run_hooks(pagetemplate => sub {
428 shift->(page => $p, destpage => $page,
429 template => $itemtemplate);
432 $content.=$itemtemplate->output;
433 $itemtemplate->clear_params;
435 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
438 my $template=template($feedtype."page.tmpl", blind_cache => 1);
440 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
441 wikiname => $config{wikiname},
444 feeddesc => $feeddesc,
445 feeddate => date_3339($lasttime),
447 version => $IkiWiki::version,
449 run_hooks(pagetemplate => sub {
450 shift->(page => $page, destpage => $page,
451 template => $template);
454 return $template->output;
457 sub pingurl (@) { #{{{
458 return unless @{$config{pingurl}} && %toping;
460 eval q{require RPC::XML::Client};
462 debug(gettext("RPC::XML::Client not found, not pinging"));
466 # daemonize here so slow pings don't slow down wiki updates
467 defined(my $pid = fork) or error("Can't fork: $!");
470 setsid() or error("Can't start a new session: $!");
471 open STDIN, '/dev/null';
472 open STDOUT, '>/dev/null';
473 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
475 # Don't need to keep a lock on the wiki as a daemon.
476 IkiWiki::unlockwiki();
478 foreach my $page (keys %toping) {
479 my $title=pagetitle(basename($page), 0);
480 my $url="$config{url}/".urlto($page, "");
481 foreach my $pingurl (@{$config{pingurl}}) {
482 debug("Pinging $pingurl for $page");
484 my $client = RPC::XML::Client->new($pingurl);
485 my $req = RPC::XML::request->new('weblogUpdates.ping',
487 my $res = $client->send_request($req);
489 debug("Did not receive response to ping");
492 if (! exists $r->{flerror} || $r->{flerror}) {
493 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
497 debug "Ping failed: $@";
502 exit 0; # daemon done