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 => "skeleton", 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));
158 my $feedid=join("\0", map { $_."\0".$params{$_} } sort keys %params);
159 if (exists $knownfeeds{$feedid}) {
160 $feednum=$knownfeeds{$feedid};
163 if (exists $page_numfeeds{$params{destpage}}) {
164 $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}};
167 $feednum=$knownfeeds{$feedid}="";
168 $page_numfeeds{$params{destpage}}=1;
172 my $rssurl=basename(rsspage($params{destpage}).$feednum);
173 my $atomurl=basename(atompage($params{destpage}).$feednum);
176 if ($config{cgiurl} && (exists $params{rootpage} ||
177 (exists $params{postform} && yesno($params{postform})))) {
178 # Add a blog post form, with feed buttons.
179 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
180 $formtemplate->param(cgiurl => $config{cgiurl});
181 $formtemplate->param(rootpage =>
182 exists $params{rootpage} ? $params{rootpage} : $params{page});
183 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
184 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
185 if (exists $params{postformtext}) {
186 $formtemplate->param(postformtext =>
187 $params{postformtext});
190 $formtemplate->param(postformtext =>
191 gettext("Add a new post titled:"));
193 $ret.=$formtemplate->output;
197 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
198 $linktemplate->param(rssurl => $rssurl) if $rss;
199 $linktemplate->param(atomurl => $atomurl) if $atom;
200 $ret.=$linktemplate->output;
204 require HTML::Template;
205 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
207 return sprintf(gettext("nonexistant template %s"), $params{template});
209 my $template=HTML::Template->new(@params) unless $raw;
211 foreach my $page (@list) {
212 my $file = $pagesources{$page};
213 my $type = pagetype($file);
214 if (! $raw || ($raw && ! defined $type)) {
215 unless ($archive && $quick) {
216 # Get the content before populating the
217 # template, since getting the content uses
218 # the same template if inlines are nested.
219 my $content=get_inline_content($page, $params{destpage});
220 $template->param(content => $content);
222 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
223 $template->param(title => pagetitle(basename($page)));
224 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
227 my $file = $pagesources{$page};
228 my $type = pagetype($file);
229 if ($config{discussion}) {
230 my $discussionlink=gettext("discussion");
231 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
232 (length $config{cgiurl} ||
233 exists $links{$page."/".$discussionlink})) {
234 $template->param(have_actions => 1);
235 $template->param(discussionlink =>
238 gettext("Discussion"),
243 if (length $config{cgiurl} && defined $type) {
244 $template->param(have_actions => 1);
245 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
249 run_hooks(pagetemplate => sub {
250 shift->(page => $page, destpage => $params{destpage},
251 template => $template,);
254 $ret.=$template->output;
255 $template->clear_params;
260 linkify($page, $params{destpage},
261 preprocess($page, $params{destpage},
262 filter($page, $params{destpage},
263 readfile(srcfile($file)))));
270 if (exists $params{feedshow} && @list > $params{feedshow}) {
271 @list=@list[0..$params{feedshow} - 1];
273 if (exists $params{feedpages}) {
274 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
278 my $rssp=rsspage($params{destpage}).$feednum;
279 will_render($params{destpage}, $rssp);
280 writefile($rssp, $config{destdir},
281 genfeed("rss", $rssurl, $desc, $params{destpage}, @list));
282 $toping{$params{destpage}}=1 unless $config{rebuild};
283 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
286 my $atomp=atompage($params{destpage}).$feednum;
287 will_render($params{destpage}, $atomp);
288 writefile($atomp, $config{destdir},
289 genfeed("atom", $atomurl, $desc, $params{destpage}, @list));
290 $toping{$params{destpage}}=1 unless $config{rebuild};
291 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
298 sub pagetemplate_inline (@) { #{{{
300 my $page=$params{page};
301 my $template=$params{template};
303 $template->param(feedlinks => $feedlinks{$page})
304 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
307 sub get_inline_content ($$) { #{{{
311 my $file=$pagesources{$page};
312 my $type=pagetype($file);
314 return htmlize($page, $type,
315 linkify($page, $destpage,
316 preprocess($page, $destpage,
317 filter($page, $destpage,
318 readfile(srcfile($file))))));
325 sub date_822 ($) { #{{{
328 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
329 POSIX::setlocale(&POSIX::LC_TIME, "C");
330 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
331 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
335 sub date_3339 ($) { #{{{
338 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
339 POSIX::setlocale(&POSIX::LC_TIME, "C");
340 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
341 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
345 sub absolute_urls ($$) { #{{{
346 # sucky sub because rss sucks
353 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
354 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
355 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
359 sub rsspage ($) { #{{{
360 return targetpage(shift, "rss");
363 sub atompage ($) { #{{{
364 return targetpage(shift, "atom");
367 sub genfeed ($$$$@) { #{{{
374 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
376 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
379 foreach my $p (@pages) {
380 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
381 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
383 $itemtemplate->param(
384 title => pagetitle(basename($p)),
387 cdate_822 => date_822($pagectime{$p}),
388 mdate_822 => date_822($pagemtime{$p}),
389 cdate_3339 => date_3339($pagectime{$p}),
390 mdate_3339 => date_3339($pagemtime{$p}),
393 if ($itemtemplate->query(name => "enclosure")) {
394 my $file=$pagesources{$p};
395 my $type=pagetype($file);
397 $itemtemplate->param(content => $pcontent);
400 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
402 eval q{use File::MimeInfo};
404 $mime = mimetype($file);
406 $itemtemplate->param(
414 $itemtemplate->param(content => $pcontent);
417 run_hooks(pagetemplate => sub {
418 shift->(page => $p, destpage => $page,
419 template => $itemtemplate);
422 $content.=$itemtemplate->output;
423 $itemtemplate->clear_params;
425 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
428 my $template=template($feedtype."page.tmpl", blind_cache => 1);
430 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
431 wikiname => $config{wikiname},
434 feeddesc => $feeddesc,
435 feeddate => date_3339($lasttime),
437 version => $IkiWiki::version,
439 run_hooks(pagetemplate => sub {
440 shift->(page => $page, destpage => $page,
441 template => $template);
444 return $template->output;
447 sub pingurl (@) { #{{{
448 return unless @{$config{pingurl}} && %toping;
450 eval q{require RPC::XML::Client};
452 debug(gettext("RPC::XML::Client not found, not pinging"));
456 # daemonize here so slow pings don't slow down wiki updates
457 defined(my $pid = fork) or error("Can't fork: $!");
460 setsid() or error("Can't start a new session: $!");
461 open STDIN, '/dev/null';
462 open STDOUT, '>/dev/null';
463 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
465 # Don't need to keep a lock on the wiki as a daemon.
466 IkiWiki::unlockwiki();
468 foreach my $page (keys %toping) {
469 my $title=pagetitle(basename($page), 0);
470 my $url="$config{url}/".urlto($page, "");
471 foreach my $pingurl (@{$config{pingurl}}) {
472 debug("Pinging $pingurl for $page");
474 my $client = RPC::XML::Client->new($pingurl);
475 my $req = RPC::XML::request->new('weblogUpdates.ping',
477 my $res = $client->send_request($req);
479 debug("Did not receive response to ping");
482 if (! exists $r->{flerror} || $r->{flerror}) {
483 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
487 debug "Ping failed: $@";
492 exit 0; # daemon done