2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
12 hook(type => "getopt", id => "inline", call => \&getopt);
13 hook(type => "checkconfig", id => "inline", call => \&checkconfig);
14 hook(type => "sessioncgi", id => "skeleton", call => \&sessioncgi);
15 hook(type => "preprocess", id => "inline",
16 call => \&IkiWiki::preprocess_inline);
17 hook(type => "pagetemplate", id => "inline",
18 call => \&IkiWiki::pagetemplate_inline);
19 # Hook to change to do pinging since it's called late.
20 # This ensures each page only pings once and prevents slow
21 # pings interrupting page builds.
22 hook(type => "change", id => "inline",
23 call => \&IkiWiki::pingurl);
28 eval q{use Getopt::Long};
30 Getopt::Long::Configure('pass_through');
32 "rss!" => \$config{rss},
33 "atom!" => \$config{atom},
37 sub checkconfig () { #{{{
38 if (($config{rss} || $config{atom}) && ! length $config{url}) {
39 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
42 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
45 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
49 sub sessioncgi () { #{{{
53 if ($q->param('do') eq 'blog') {
54 my $page=decode_utf8($q->param('title'));
55 $page=~s/\///g; # no slashes in blog posts
56 # if the page already exists, munge it to be unique
57 my $from=$q->param('from');
59 while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
60 $add=1 unless length $add;
63 $q->param('page', $page.$add);
64 # now go create the page
65 $q->param('do', 'create');
66 IkiWiki::cgi_editpage($q, $session);
71 # Back to ikiwiki namespace for the rest, this code is very much
72 # internal to ikiwiki even though it's separated into a plugin.
80 return (defined $val && lc($val) eq "yes");
83 sub preprocess_inline (@) { #{{{
86 if (! exists $params{pages}) {
89 my $raw=yesno($params{raw});
90 my $archive=yesno($params{archive});
91 my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
92 my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
93 my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
94 my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
95 $feeds=0 if $params{preview};
96 if (! exists $params{show} && ! $archive) {
100 if (exists $params{description}) {
101 $desc = $params{description}
103 $desc = $config{wikiname};
105 my $actions=yesno($params{actions});
106 if (exists $params{template}) {
107 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
110 $params{template} = $archive ? "archivepage" : "inlinepage";
114 foreach my $page (keys %pagesources) {
115 next if $page eq $params{page};
116 if (pagespec_match($page, $params{pages}, location => $params{page})) {
121 if (exists $params{sort} && $params{sort} eq 'title') {
124 elsif (exists $params{sort} && $params{sort} eq 'mtime') {
125 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
127 elsif (! exists $params{sort} || $params{sort} eq 'age') {
128 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
131 return sprintf(gettext("unknown sort type %s"), $params{sort});
134 if (yesno($params{reverse})) {
135 @list=reverse(@list);
138 if (exists $params{skip}) {
139 @list=@list[$params{skip} .. scalar @list - 1];
142 if ($params{show} && @list > $params{show}) {
143 @list=@list[0..$params{show} - 1];
146 add_depends($params{page}, $params{pages});
147 # Explicitly add all currently displayed pages as dependencies, so
148 # that if they are removed or otherwise changed, the inline will be
149 # sure to be updated.
150 add_depends($params{page}, join(" or ", @list));
152 my $rssurl=basename(rsspage($params{page}));
153 my $atomurl=basename(atompage($params{page}));
156 if ($config{cgiurl} && (exists $params{rootpage} ||
157 (exists $params{postform} && yesno($params{postform})))) {
158 # Add a blog post form, with feed buttons.
159 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
160 $formtemplate->param(cgiurl => $config{cgiurl});
161 $formtemplate->param(rootpage =>
162 exists $params{rootpage} ? $params{rootpage} : $params{page});
163 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
164 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
165 if (exists $params{postformtext}) {
166 $formtemplate->param(postformtext =>
167 $params{postformtext});
170 $formtemplate->param(postformtext =>
171 gettext("Add a new post titled:"));
173 $ret.=$formtemplate->output;
177 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
178 $linktemplate->param(rssurl => $rssurl) if $rss;
179 $linktemplate->param(atomurl => $atomurl) if $atom;
180 $ret.=$linktemplate->output;
183 require HTML::Template;
184 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
186 return sprintf(gettext("nonexistant template %s"), $params{template});
188 my $template=HTML::Template->new(@params) unless $raw;
190 foreach my $page (@list) {
191 my $file = $pagesources{$page};
192 my $type = pagetype($file);
193 if (! $raw || ($raw && ! defined $type)) {
194 unless ($archive && $quick) {
195 # Get the content before populating the
196 # template, since getting the content uses
197 # the same template if inlines are nested.
198 my $content=get_inline_content($page, $params{destpage});
199 $template->param(content => $content);
201 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
202 $template->param(title => pagetitle(basename($page)));
203 $template->param(ctime => displaytime($pagectime{$page}));
206 my $file = $pagesources{$page};
207 my $type = pagetype($file);
208 if ($config{discussion}) {
209 my $discussionlink=gettext("discussion");
210 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
211 (length $config{cgiurl} ||
212 exists $links{$page."/".$discussionlink})) {
213 $template->param(have_actions => 1);
214 $template->param(discussionlink =>
217 gettext("Discussion"),
222 if (length $config{cgiurl} && defined $type) {
223 $template->param(have_actions => 1);
224 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
228 run_hooks(pagetemplate => sub {
229 shift->(page => $page, destpage => $params{page},
230 template => $template,);
233 $ret.=$template->output;
234 $template->clear_params;
239 linkify($page, $params{page},
240 preprocess($page, $params{page},
241 filter($page, $params{page},
242 readfile(srcfile($file)))));
248 if (exists $params{feedshow} && @list > $params{feedshow}) {
249 @list=@list[0..$params{feedshow} - 1];
251 if (exists $params{feedpages}) {
252 @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
256 my $rssp=rsspage($params{page});
257 will_render($params{page}, $rssp);
258 writefile($rssp, $config{destdir},
259 genfeed("rss", $rssurl, $desc, $params{page}, @list));
260 $toping{$params{page}}=1 unless $config{rebuild};
261 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
264 my $atomp=atompage($params{page});
265 will_render($params{page}, $atomp);
266 writefile($atomp, $config{destdir},
267 genfeed("atom", $atomurl, $desc, $params{page}, @list));
268 $toping{$params{page}}=1 unless $config{rebuild};
269 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
276 sub pagetemplate_inline (@) { #{{{
278 my $page=$params{page};
279 my $template=$params{template};
281 $template->param(feedlinks => $feedlinks{$page})
282 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
285 sub get_inline_content ($$) { #{{{
289 my $file=$pagesources{$page};
290 my $type=pagetype($file);
292 return htmlize($page, $type,
293 linkify($page, $destpage,
294 preprocess($page, $destpage,
295 filter($page, $destpage,
296 readfile(srcfile($file))))));
303 sub date_822 ($) { #{{{
306 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
307 POSIX::setlocale(&POSIX::LC_TIME, "C");
308 my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
309 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
313 sub date_3339 ($) { #{{{
316 my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
317 POSIX::setlocale(&POSIX::LC_TIME, "C");
318 my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
319 POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
323 sub absolute_urls ($$) { #{{{
324 # sucky sub because rss sucks
331 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
332 $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
333 $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
337 sub rsspage ($) { #{{{
338 return targetpage(shift, "rss");
341 sub atompage ($) { #{{{
342 return targetpage(shift, "atom");
345 sub genfeed ($$$$@) { #{{{
352 my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
354 my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
357 foreach my $p (@pages) {
358 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
359 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
361 $itemtemplate->param(
362 title => pagetitle(basename($p)),
365 cdate_822 => date_822($pagectime{$p}),
366 mdate_822 => date_822($pagemtime{$p}),
367 cdate_3339 => date_3339($pagectime{$p}),
368 mdate_3339 => date_3339($pagemtime{$p}),
371 if ($itemtemplate->query(name => "enclosure")) {
372 my $file=$pagesources{$p};
373 my $type=pagetype($file);
375 $itemtemplate->param(content => $pcontent);
378 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
380 eval q{use File::MimeInfo};
382 $mime = mimetype($file);
384 $itemtemplate->param(
392 $itemtemplate->param(content => $pcontent);
395 run_hooks(pagetemplate => sub {
396 shift->(page => $p, destpage => $page,
397 template => $itemtemplate);
400 $content.=$itemtemplate->output;
401 $itemtemplate->clear_params;
403 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
406 my $template=template($feedtype."page.tmpl", blind_cache => 1);
408 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
409 wikiname => $config{wikiname},
412 feeddesc => $feeddesc,
413 feeddate => date_3339($lasttime),
415 version => $IkiWiki::version,
417 run_hooks(pagetemplate => sub {
418 shift->(page => $page, destpage => $page,
419 template => $template);
422 return $template->output;
425 sub pingurl (@) { #{{{
426 return unless @{$config{pingurl}} && %toping;
428 eval q{require RPC::XML::Client};
430 debug(gettext("RPC::XML::Client not found, not pinging"));
434 # daemonize here so slow pings don't slow down wiki updates
435 defined(my $pid = fork) or error("Can't fork: $!");
438 setsid() or error("Can't start a new session: $!");
439 open STDIN, '/dev/null';
440 open STDOUT, '>/dev/null';
441 open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
443 # Don't need to keep a lock on the wiki as a daemon.
444 IkiWiki::unlockwiki();
446 foreach my $page (keys %toping) {
447 my $title=pagetitle(basename($page), 0);
448 my $url="$config{url}/".urlto($page, "");
449 foreach my $pingurl (@{$config{pingurl}}) {
450 debug("Pinging $pingurl for $page");
452 my $client = RPC::XML::Client->new($pingurl);
453 my $req = RPC::XML::request->new('weblogUpdates.ping',
455 my $res = $client->send_request($req);
457 debug("Did not receive response to ping");
460 if (! exists $r->{flerror} || $r->{flerror}) {
461 debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
465 debug "Ping failed: $@";
470 exit 0; # daemon done