]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
fix pagediff to not display as "preview"
[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 => $config{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 gettext("failed to process template:")." $@";
340                         }
341                         if (! $template) {
342                                 error sprintf(gettext("template %s not found"), $params{template}.".tmpl");
343                         }
344                 }
345                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
346         
347                 foreach my $page (@list) {
348                         my $file = $pagesources{$page};
349                         my $type = pagetype($file);
350                         if (! $raw) {
351                                 if ($needcontent) {
352                                         # Get the content before populating the
353                                         # template, since getting the content uses
354                                         # the same template if inlines are nested.
355                                         my $content=get_inline_content($page, $params{destpage});
356                                         $template->param(content => $content);
357                                 }
358                                 $template->param(pageurl => urlto($page, $params{destpage}));
359                                 $template->param(inlinepage => $page);
360                                 $template->param(title => pagetitle(basename($page)));
361                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
362                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
363                                 $template->param(first => 1) if $page eq $list[0];
364                                 $template->param(last => 1) if $page eq $list[$#list];
365                                 $template->param(html5 => $config{html5});
366         
367                                 if ($actions) {
368                                         my $file = $pagesources{$page};
369                                         my $type = pagetype($file);
370                                         if ($config{discussion}) {
371                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
372                                                     (length $config{cgiurl} ||
373                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
374                                                         $template->param(have_actions => 1);
375                                                         $template->param(discussionlink =>
376                                                                 htmllink($page,
377                                                                         $params{destpage},
378                                                                         $config{discussionpage},
379                                                                         noimageinline => 1,
380                                                                         forcesubpage => 1));
381                                                 }
382                                         }
383                                         if (length $config{cgiurl} &&
384                                             defined $type &&
385                                             IkiWiki->can("cgi_editpage")) {
386                                                 $template->param(have_actions => 1);
387                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
388
389                                         }
390                                 }
391         
392                                 run_hooks(pagetemplate => sub {
393                                         shift->(page => $page, destpage => $params{destpage},
394                                                 template => $template,);
395                                 });
396         
397                                 $ret.=$template->output;
398                                 $template->clear_params;
399                         }
400                         else {
401                                 if (defined $type) {
402                                         $ret.="\n".
403                                               linkify($page, $params{destpage},
404                                               preprocess($page, $params{destpage},
405                                               filter($page, $params{destpage},
406                                               readfile(srcfile($file)))));
407                                 }
408                                 else {
409                                         $ret.="\n".
410                                               readfile(srcfile($file));
411                                 }
412                         }
413                 }
414         }
415         
416         if ($feeds && ($emptyfeeds || @feedlist)) {
417                 if ($rss) {
418                         my $rssp=$feedbase."rss".$feednum;
419                         will_render($params{destpage}, $rssp);
420                         if (! $params{preview}) {
421                                 writefile($rssp, $config{destdir},
422                                         genfeed("rss",
423                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
424                                 $toping{$params{destpage}}=1 unless $config{rebuild};
425                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
426                         }
427                 }
428                 if ($atom) {
429                         my $atomp=$feedbase."atom".$feednum;
430                         will_render($params{destpage}, $atomp);
431                         if (! $params{preview}) {
432                                 writefile($atomp, $config{destdir},
433                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
434                                 $toping{$params{destpage}}=1 unless $config{rebuild};
435                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
436                         }
437                 }
438         }
439         
440         clear_inline_content_cache();
441
442         return $ret if $raw || $nested;
443         push @inline, $ret;
444         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
445 }
446
447 sub pagetemplate_inline (@) {
448         my %params=@_;
449         my $page=$params{page};
450         my $template=$params{template};
451
452         $template->param(feedlinks => $feedlinks{$page})
453                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
454 }
455
456 {
457 my %inline_content;
458 my $cached_destpage="";
459
460 sub get_inline_content ($$) {
461         my $page=shift;
462         my $destpage=shift;
463         
464         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
465                 return $inline_content{$page};
466         }
467
468         my $file=$pagesources{$page};
469         my $type=pagetype($file);
470         my $ret="";
471         if (defined $type) {
472                 $nested++;
473                 $ret=htmlize($page, $destpage, $type,
474                        linkify($page, $destpage,
475                        preprocess($page, $destpage,
476                        filter($page, $destpage,
477                        readfile(srcfile($file))))));
478                 $nested--;
479         }
480         
481         if ($cached_destpage ne $destpage) {
482                 clear_inline_content_cache();
483                 $cached_destpage=$destpage;
484         }
485         return $inline_content{$page}=$ret;
486 }
487
488 sub clear_inline_content_cache () {
489         %inline_content=();
490 }
491
492 }
493
494 sub date_822 ($) {
495         my $time=shift;
496
497         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
498         POSIX::setlocale(&POSIX::LC_TIME, "C");
499         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
500         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
501         return $ret;
502 }
503
504 sub absolute_urls ($$) {
505         # sucky sub because rss sucks
506         my $content=shift;
507         my $baseurl=shift;
508
509         my $url=$baseurl;
510         $url=~s/[^\/]+$//;
511
512         # what is the non path part of the url?
513         my $top_uri = URI->new($url);
514         $top_uri->path_query(""); # reset the path
515         my $urltop = $top_uri->as_string;
516
517         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
518         # relative to another wiki page
519         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
520         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
521         # relative to the top of the site
522         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
523         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
524         return $content;
525 }
526
527 sub genfeed ($$$$$@) {
528         my $feedtype=shift;
529         my $feedurl=shift;
530         my $feeddesc=shift;
531         my $guid=shift;
532         my $page=shift;
533         my @pages=@_;
534         
535         my $url=URI->new(encode_utf8(urlto($page,"",1)));
536         
537         my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
538         my $content="";
539         my $lasttime = 0;
540         foreach my $p (@pages) {
541                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
542                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
543
544                 $itemtemplate->param(
545                         title => pagetitle(basename($p)),
546                         url => $u,
547                         permalink => $u,
548                         cdate_822 => date_822($pagectime{$p}),
549                         mdate_822 => date_822($pagemtime{$p}),
550                         cdate_3339 => date_3339($pagectime{$p}),
551                         mdate_3339 => date_3339($pagemtime{$p}),
552                 );
553
554                 if (exists $pagestate{$p}) {
555                         if (exists $pagestate{$p}{meta}{guid}) {
556                                 eval q{use HTML::Entities};
557                                 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
558                         }
559
560                         if (exists $pagestate{$p}{meta}{updated}) {
561                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
562                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
563                         }
564                 }
565
566                 if ($itemtemplate->query(name => "enclosure")) {
567                         my $file=$pagesources{$p};
568                         my $type=pagetype($file);
569                         if (defined $type) {
570                                 $itemtemplate->param(content => $pcontent);
571                         }
572                         else {
573                                 my $size=(srcfile_stat($file))[8];
574                                 my $mime="unknown";
575                                 eval q{use File::MimeInfo};
576                                 if (! $@) {
577                                         $mime = mimetype($file);
578                                 }
579                                 $itemtemplate->param(
580                                         enclosure => $u,
581                                         type => $mime,
582                                         length => $size,
583                                 );
584                         }
585                 }
586                 else {
587                         $itemtemplate->param(content => $pcontent);
588                 }
589
590                 run_hooks(pagetemplate => sub {
591                         shift->(page => $p, destpage => $page,
592                                 template => $itemtemplate);
593                 });
594
595                 $content.=$itemtemplate->output;
596                 $itemtemplate->clear_params;
597
598                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
599         }
600
601         my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
602         $template->param(
603                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
604                 wikiname => $config{wikiname},
605                 pageurl => $url,
606                 content => $content,
607                 feeddesc => $feeddesc,
608                 guid => $guid,
609                 feeddate => date_3339($lasttime),
610                 feedurl => $feedurl,
611                 version => $IkiWiki::version,
612         );
613         run_hooks(pagetemplate => sub {
614                 shift->(page => $page, destpage => $page,
615                         template => $template);
616         });
617         
618         return $template->output;
619 }
620
621 sub pingurl (@) {
622         return unless @{$config{pingurl}} && %toping;
623
624         eval q{require RPC::XML::Client};
625         if ($@) {
626                 debug(gettext("RPC::XML::Client not found, not pinging"));
627                 return;
628         }
629
630         # daemonize here so slow pings don't slow down wiki updates
631         defined(my $pid = fork) or error("Can't fork: $!");
632         return if $pid;
633         chdir '/';
634         POSIX::setsid() or error("Can't start a new session: $!");
635         open STDIN, '/dev/null';
636         open STDOUT, '>/dev/null';
637         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
638
639         # Don't need to keep a lock on the wiki as a daemon.
640         IkiWiki::unlockwiki();
641
642         foreach my $page (keys %toping) {
643                 my $title=pagetitle(basename($page), 0);
644                 my $url=urlto($page, "", 1);
645                 foreach my $pingurl (@{$config{pingurl}}) {
646                         debug("Pinging $pingurl for $page");
647                         eval {
648                                 my $client = RPC::XML::Client->new($pingurl);
649                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
650                                         $title, $url);
651                                 my $res = $client->send_request($req);
652                                 if (! ref $res) {
653                                         error("Did not receive response to ping");
654                                 }
655                                 my $r=$res->value;
656                                 if (! exists $r->{flerror} || $r->{flerror}) {
657                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
658                                 }
659                         };
660                         if ($@) {
661                                 error "Ping failed: $@";
662                         }
663                 }
664         }
665
666         exit 0; # daemon done
667 }
668
669
670 sub rootpage (@) {
671         my %params=@_;
672
673         my $rootpage;
674         if (exists $params{rootpage}) {
675                 $rootpage=bestlink($params{page}, $params{rootpage});
676                 if (!length $rootpage) {
677                         $rootpage=$params{rootpage};
678                 }
679         }
680         else {
681                 $rootpage=$params{page};
682         }
683         return $rootpage;
684 }
685
686 1