Merge branch 'bugfix'
[ikiwiki.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use Encode;
8 use IkiWiki 3.00;
9 use URI;
10
11 my %knownfeeds;
12 my %page_numfeeds;
13 my @inline;
14 my $nested=0;
15
16 sub import {
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);
30 }
31
32 sub getopt () {
33         eval q{use Getopt::Long};
34         error($@) if $@;
35         Getopt::Long::Configure('pass_through');
36         GetOptions(
37                 "rss!" => \$config{rss},
38                 "atom!" => \$config{atom},
39                 "allowrss!" => \$config{allowrss},
40                 "allowatom!" => \$config{allowatom},
41                 "pingurl=s" => sub {
42                         push @{$config{pingurl}}, $_[1];
43                 },      
44         );
45 }
46
47 sub getsetup () {
48         return
49                 plugin => {
50                         safe => 1,
51                         rebuild => undef,
52                         section => "core",
53                 },
54                 rss => {
55                         type => "boolean",
56                         example => 0,
57                         description => "enable rss feeds by default?",
58                         safe => 1,
59                         rebuild => 1,
60                 },
61                 atom => {
62                         type => "boolean",
63                         example => 0,
64                         description => "enable atom feeds by default?",
65                         safe => 1,
66                         rebuild => 1,
67                 },
68                 allowrss => {
69                         type => "boolean",
70                         example => 0,
71                         description => "allow rss feeds to be used?",
72                         safe => 1,
73                         rebuild => 1,
74                 },
75                 allowatom => {
76                         type => "boolean",
77                         example => 0,
78                         description => "allow atom feeds to be used?",
79                         safe => 1,
80                         rebuild => 1,
81                 },
82                 pingurl => {
83                         type => "string",
84                         example => "http://rpc.technorati.com/rpc/ping",
85                         description => "urls to ping (using XML-RPC) on feed update",
86                         safe => 1,
87                         rebuild => 0,
88                 },
89 }
90
91 sub checkconfig () {
92         if (($config{rss} || $config{atom}) && ! length $config{url}) {
93                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
94         }
95         if ($config{rss}) {
96                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
97         }
98         if ($config{atom}) {
99                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
100         }
101         if (! exists $config{pingurl}) {
102                 $config{pingurl}=[];
103         }
104 }
105
106 sub format (@) {
107         my %params=@_;
108
109         # Fill in the inline content generated earlier. This is actually an
110         # optimisation.
111         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
112                 delete @inline[$1,]
113         }eg;
114         return $params{content};
115 }
116
117 sub sessioncgi ($$) {
118         my $q=shift;
119         my $session=shift;
120
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');
126                 my $add="";
127                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
128                         $add=1 unless length $add;
129                         $add++;
130                 }
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);
137                 }
138                 else {
139                         error(gettext("page editing not allowed"));
140                 }
141                 exit;
142         }
143 }
144
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.
147 package IkiWiki;
148
149 my %toping;
150 my %feedlinks;
151
152 sub preprocess_inline (@) {
153         my %params=@_;
154         
155         if (! exists $params{pages} && ! exists $params{pagenames}) {
156                 error gettext("missing pages parameter");
157         }
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) {
167                 $params{show}=10;
168         }
169         if (! exists $params{feedshow} && exists $params{show}) {
170                 $params{feedshow}=$params{show};
171         }
172         my $desc;
173         if (exists $params{description}) {
174                 $desc = $params{description} 
175         }
176         else {
177                 $desc = $config{wikiname};
178         }
179         my $actions=yesno($params{actions});
180         if (exists $params{template}) {
181                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
182         }
183         else {
184                 $params{template} = $archive ? "archivepage" : "inlinepage";
185         }
186
187         my @list;
188
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"),
193                                         "pagenames", $p);
194                         }
195                 }
196
197                 @list = map { bestlink($params{page}, $_) }
198                         split ' ', $params{pagenames};
199
200                 if (yesno($params{reverse})) {
201                         @list=reverse(@list);
202                 }
203
204                 foreach my $p (@list) {
205                         add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
206                 }
207         }
208         else {
209                 my $num=0;
210                 if ($params{show}) {
211                         $num=$params{show};
212                 }
213                 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
214                         $num=$params{feedshow};
215                 }
216                 if ($params{skip} && $num) {
217                         $num+=$params{skip};
218                 }
219
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) : ()),
226                 );
227         }
228
229         if (exists $params{skip}) {
230                 @list=@list[$params{skip} .. $#list];
231         }
232         
233         my @feedlist;
234         if ($feeds) {
235                 if (exists $params{feedshow} &&
236                     $params{feedshow} && @list > $params{feedshow}) {
237                         @feedlist=@list[0..$params{feedshow} - 1];
238                 }
239                 else {
240                         @feedlist=@list;
241                 }
242         }
243         
244         if ($params{show} && @list > $params{show}) {
245                 @list=@list[0..$params{show} - 1];
246         }
247
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"),
252                         list => \@feedlist,
253                 );
254         }
255
256         my ($feedbase, $feednum);
257         if ($feeds) {
258                 # Ensure that multiple feeds on a page go to unique files.
259                 
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
264                 # and conflicts.
265                 if (defined $params{feedfile}) {
266                         if ($params{feedfile} =~ /\// ||
267                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
268                                 error("illegal feedfile");
269                         }
270                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
271                 }
272                 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
273
274                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
275                 if (exists $knownfeeds{$feedid}) {
276                         $feednum=$knownfeeds{$feedid};
277                 }
278                 else {
279                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
280                                 if ($feeds) {
281                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
282                                 }
283                         }
284                         else {
285                                 $feednum=$knownfeeds{$feedid}="";
286                                 if ($feeds) {
287                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
288                                 }
289                         }
290                 }
291         }
292
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;
295
296         my $ret="";
297
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 => IkiWiki::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});
310                 }
311                 else {
312                         $formtemplate->param(postformtext =>
313                                 gettext("Add a new post titled:"));
314                 }
315                 $ret.=$formtemplate->output;
316                 
317                 # The post form includes the feed buttons, so
318                 # emptyfeeds cannot be hidden.
319                 $emptyfeeds=1;
320         }
321         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
322                 # Add feed buttons.
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;
327         }
328         
329         if (! $feedonly) {
330                 my $template;
331                 if (! $raw) {
332                         # cannot use wiki pages as templates; template not sanitized due to
333                         # format hook hack
334                         eval {
335                                 $template=template_depends($params{template}.".tmpl", $params{page},
336                                         blind_cache => 1);
337                         };
338                         if ($@) {
339                                 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
340                         }
341                 }
342                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
343         
344                 foreach my $page (@list) {
345                         my $file = $pagesources{$page};
346                         my $type = pagetype($file);
347                         if (! $raw) {
348                                 if ($needcontent) {
349                                         # Get the content before populating the
350                                         # template, since getting the content uses
351                                         # the same template if inlines are nested.
352                                         my $content=get_inline_content($page, $params{destpage});
353                                         $template->param(content => $content);
354                                 }
355                                 $template->param(pageurl => urlto($page, $params{destpage}));
356                                 $template->param(inlinepage => $page);
357                                 $template->param(title => pagetitle(basename($page)));
358                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
359                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
360                                 $template->param(first => 1) if $page eq $list[0];
361                                 $template->param(last => 1) if $page eq $list[$#list];
362                                 $template->param(html5 => $config{html5});
363         
364                                 if ($actions) {
365                                         my $file = $pagesources{$page};
366                                         my $type = pagetype($file);
367                                         if ($config{discussion}) {
368                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
369                                                     (length $config{cgiurl} ||
370                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
371                                                         $template->param(have_actions => 1);
372                                                         $template->param(discussionlink =>
373                                                                 htmllink($page,
374                                                                         $params{destpage},
375                                                                         $config{discussionpage},
376                                                                         noimageinline => 1,
377                                                                         forcesubpage => 1));
378                                                 }
379                                         }
380                                         if (length $config{cgiurl} &&
381                                             defined $type &&
382                                             IkiWiki->can("cgi_editpage")) {
383                                                 $template->param(have_actions => 1);
384                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
385
386                                         }
387                                 }
388         
389                                 run_hooks(pagetemplate => sub {
390                                         shift->(page => $page, destpage => $params{destpage},
391                                                 template => $template,);
392                                 });
393         
394                                 $ret.=$template->output;
395                                 $template->clear_params;
396                         }
397                         else {
398                                 if (defined $type) {
399                                         $ret.="\n".
400                                               linkify($page, $params{destpage},
401                                               preprocess($page, $params{destpage},
402                                               filter($page, $params{destpage},
403                                               readfile(srcfile($file)))));
404                                 }
405                                 else {
406                                         $ret.="\n".
407                                               readfile(srcfile($file));
408                                 }
409                         }
410                 }
411         }
412         
413         if ($feeds && ($emptyfeeds || @feedlist)) {
414                 if ($rss) {
415                         my $rssp=$feedbase."rss".$feednum;
416                         will_render($params{destpage}, $rssp);
417                         if (! $params{preview}) {
418                                 writefile($rssp, $config{destdir},
419                                         genfeed("rss",
420                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
421                                 $toping{$params{destpage}}=1 unless $config{rebuild};
422                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
423                         }
424                 }
425                 if ($atom) {
426                         my $atomp=$feedbase."atom".$feednum;
427                         will_render($params{destpage}, $atomp);
428                         if (! $params{preview}) {
429                                 writefile($atomp, $config{destdir},
430                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
431                                 $toping{$params{destpage}}=1 unless $config{rebuild};
432                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
433                         }
434                 }
435         }
436         
437         clear_inline_content_cache();
438
439         return $ret if $raw || $nested;
440         push @inline, $ret;
441         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
442 }
443
444 sub pagetemplate_inline (@) {
445         my %params=@_;
446         my $page=$params{page};
447         my $template=$params{template};
448
449         $template->param(feedlinks => $feedlinks{$page})
450                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
451 }
452
453 {
454 my %inline_content;
455 my $cached_destpage="";
456
457 sub get_inline_content ($$) {
458         my $page=shift;
459         my $destpage=shift;
460         
461         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
462                 return $inline_content{$page};
463         }
464
465         my $file=$pagesources{$page};
466         my $type=pagetype($file);
467         my $ret="";
468         if (defined $type) {
469                 $nested++;
470                 $ret=htmlize($page, $destpage, $type,
471                        linkify($page, $destpage,
472                        preprocess($page, $destpage,
473                        filter($page, $destpage,
474                        readfile(srcfile($file))))));
475                 $nested--;
476                 if (isinternal($page)) {
477                         # make inlined text of internal pages searchable
478                         run_hooks(indexhtml => sub {
479                                 shift->(page => $page, destpage => $page,
480                                         content => $ret);
481                         });
482                 }
483         }
484         
485         if ($cached_destpage ne $destpage) {
486                 clear_inline_content_cache();
487                 $cached_destpage=$destpage;
488         }
489         return $inline_content{$page}=$ret;
490 }
491
492 sub clear_inline_content_cache () {
493         %inline_content=();
494 }
495
496 }
497
498 sub date_822 ($) {
499         my $time=shift;
500
501         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
502         POSIX::setlocale(&POSIX::LC_TIME, "C");
503         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
504         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
505         return $ret;
506 }
507
508 sub absolute_urls ($$) {
509         # needed because rss sucks
510         my $html=shift;
511         my $baseurl=shift;
512
513         my $url=$baseurl;
514         $url=~s/[^\/]+$//;
515         my $urltop; # calculated if needed
516
517         my $ret="";
518
519         eval q{use HTML::Parser; use HTML::Tagset};
520         die $@ if $@;
521         my $p = HTML::Parser->new(api_version => 3);
522         $p->handler(default => sub { $ret.=join("", @_) }, "text");
523         $p->handler(start => sub {
524                 my ($tagname, $pos, $text) = @_;
525                 if (ref $HTML::Tagset::linkElements{$tagname}) {
526                         while (4 <= @$pos) {
527                                 # use attribute sets from right to left
528                                 # to avoid invalidating the offsets
529                                 # when replacing the values
530                                 my ($k_offset, $k_len, $v_offset, $v_len) =
531                                         splice(@$pos, -4);
532                                 my $attrname = lc(substr($text, $k_offset, $k_len));
533                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
534                                 next unless $v_offset; # 0 v_offset means no value
535                                 my $v = substr($text, $v_offset, $v_len);
536                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
537                                 if ($v=~/^#/) {
538                                         $v=$baseurl.$v; # anchor
539                                 }
540                                 elsif ($v=~/^(?!\w+:)[^\/]/) {
541                                         $v=$url.$v; # relative url
542                                 }
543                                 elsif ($v=~/^\//) {
544                                         if (! defined $urltop) {
545                                                 # what is the non path part of the url?
546                                                 my $top_uri = URI->new($url);
547                                                 $top_uri->path_query(""); # reset the path
548                                                 $urltop = $top_uri->as_string;
549                                         }
550                                         $v=$urltop.$v; # url relative to top of site
551                                 }
552                                 $v =~ s/\"/&quot;/g; # since we quote with ""
553                                 substr($text, $v_offset, $v_len) = qq("$v");
554                         }
555                 }
556                 $ret.=$text;
557         }, "tagname, tokenpos, text");
558         $p->parse($html);
559         $p->eof;
560
561         return $ret;
562 }
563
564 sub genfeed ($$$$$@) {
565         my $feedtype=shift;
566         my $feedurl=shift;
567         my $feeddesc=shift;
568         my $guid=shift;
569         my $page=shift;
570         my @pages=@_;
571         
572         my $url=URI->new(encode_utf8(urlto($page,"",1)));
573         
574         my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
575         my $content="";
576         my $lasttime = 0;
577         foreach my $p (@pages) {
578                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
579                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
580
581                 $itemtemplate->param(
582                         title => pagetitle(basename($p)),
583                         url => $u,
584                         permalink => $u,
585                         cdate_822 => date_822($pagectime{$p}),
586                         mdate_822 => date_822($pagemtime{$p}),
587                         cdate_3339 => date_3339($pagectime{$p}),
588                         mdate_3339 => date_3339($pagemtime{$p}),
589                 );
590
591                 if (exists $pagestate{$p}) {
592                         if (exists $pagestate{$p}{meta}{guid}) {
593                                 eval q{use HTML::Entities};
594                                 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
595                         }
596
597                         if (exists $pagestate{$p}{meta}{updated}) {
598                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
599                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
600                         }
601                 }
602
603                 if ($itemtemplate->query(name => "enclosure")) {
604                         my $file=$pagesources{$p};
605                         my $type=pagetype($file);
606                         if (defined $type) {
607                                 $itemtemplate->param(content => $pcontent);
608                         }
609                         else {
610                                 my $size=(srcfile_stat($file))[8];
611                                 my $mime="unknown";
612                                 eval q{use File::MimeInfo};
613                                 if (! $@) {
614                                         $mime = mimetype($file);
615                                 }
616                                 $itemtemplate->param(
617                                         enclosure => $u,
618                                         type => $mime,
619                                         length => $size,
620                                 );
621                         }
622                 }
623                 else {
624                         $itemtemplate->param(content => $pcontent);
625                 }
626
627                 run_hooks(pagetemplate => sub {
628                         shift->(page => $p, destpage => $page,
629                                 template => $itemtemplate);
630                 });
631
632                 $content.=$itemtemplate->output;
633                 $itemtemplate->clear_params;
634
635                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
636         }
637
638         my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
639         $template->param(
640                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
641                 wikiname => $config{wikiname},
642                 pageurl => $url,
643                 content => $content,
644                 feeddesc => $feeddesc,
645                 guid => $guid,
646                 feeddate => date_3339($lasttime),
647                 feedurl => $feedurl,
648                 version => $IkiWiki::version,
649         );
650         run_hooks(pagetemplate => sub {
651                 shift->(page => $page, destpage => $page,
652                         template => $template);
653         });
654         
655         return $template->output;
656 }
657
658 sub pingurl (@) {
659         return unless @{$config{pingurl}} && %toping;
660
661         eval q{require RPC::XML::Client};
662         if ($@) {
663                 debug(gettext("RPC::XML::Client not found, not pinging"));
664                 return;
665         }
666
667         # daemonize here so slow pings don't slow down wiki updates
668         defined(my $pid = fork) or error("Can't fork: $!");
669         return if $pid;
670         chdir '/';
671         POSIX::setsid() or error("Can't start a new session: $!");
672         open STDIN, '/dev/null';
673         open STDOUT, '>/dev/null';
674         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
675
676         # Don't need to keep a lock on the wiki as a daemon.
677         IkiWiki::unlockwiki();
678
679         foreach my $page (keys %toping) {
680                 my $title=pagetitle(basename($page), 0);
681                 my $url=urlto($page, "", 1);
682                 foreach my $pingurl (@{$config{pingurl}}) {
683                         debug("Pinging $pingurl for $page");
684                         eval {
685                                 my $client = RPC::XML::Client->new($pingurl);
686                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
687                                         $title, $url);
688                                 my $res = $client->send_request($req);
689                                 if (! ref $res) {
690                                         error("Did not receive response to ping");
691                                 }
692                                 my $r=$res->value;
693                                 if (! exists $r->{flerror} || $r->{flerror}) {
694                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
695                                 }
696                         };
697                         if ($@) {
698                                 error "Ping failed: $@";
699                         }
700                 }
701         }
702
703         exit 0; # daemon done
704 }
705
706
707 sub rootpage (@) {
708         my %params=@_;
709
710         my $rootpage;
711         if (exists $params{rootpage}) {
712                 $rootpage=bestlink($params{page}, $params{rootpage});
713                 if (!length $rootpage) {
714                         $rootpage=$params{rootpage};
715                 }
716         }
717         else {
718                 $rootpage=$params{page};
719         }
720         return $rootpage;
721 }
722
723 1