response
[ikiwiki] / 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                 },
53                 rss => {
54                         type => "boolean",
55                         example => 0,
56                         description => "enable rss feeds by default?",
57                         safe => 1,
58                         rebuild => 1,
59                 },
60                 atom => {
61                         type => "boolean",
62                         example => 0,
63                         description => "enable atom feeds by default?",
64                         safe => 1,
65                         rebuild => 1,
66                 },
67                 allowrss => {
68                         type => "boolean",
69                         example => 0,
70                         description => "allow rss feeds to be used?",
71                         safe => 1,
72                         rebuild => 1,
73                 },
74                 allowatom => {
75                         type => "boolean",
76                         example => 0,
77                         description => "allow atom feeds to be used?",
78                         safe => 1,
79                         rebuild => 1,
80                 },
81                 pingurl => {
82                         type => "string",
83                         example => "http://rpc.technorati.com/rpc/ping",
84                         description => "urls to ping (using XML-RPC) on feed update",
85                         safe => 1,
86                         rebuild => 0,
87                 },
88 }
89
90 sub checkconfig () {
91         if (($config{rss} || $config{atom}) && ! length $config{url}) {
92                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
93         }
94         if ($config{rss}) {
95                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
96         }
97         if ($config{atom}) {
98                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
99         }
100         if (! exists $config{pingurl}) {
101                 $config{pingurl}=[];
102         }
103 }
104
105 sub format (@) {
106         my %params=@_;
107
108         # Fill in the inline content generated earlier. This is actually an
109         # optimisation.
110         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
111                 delete @inline[$1,]
112         }eg;
113         return $params{content};
114 }
115
116 sub sessioncgi ($$) {
117         my $q=shift;
118         my $session=shift;
119
120         if ($q->param('do') eq 'blog') {
121                 my $page=titlepage(decode_utf8($q->param('title')));
122                 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
123                 # if the page already exists, munge it to be unique
124                 my $from=$q->param('from');
125                 my $add="";
126                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
127                         $add=1 unless length $add;
128                         $add++;
129                 }
130                 $q->param('page', $page.$add);
131                 # now go create the page
132                 $q->param('do', 'create');
133                 # make sure the editpage plugin in loaded
134                 if (IkiWiki->can("cgi_editpage")) {
135                         IkiWiki::cgi_editpage($q, $session);
136                 }
137                 else {
138                         error(gettext("page editing not allowed"));
139                 }
140                 exit;
141         }
142 }
143
144 # Back to ikiwiki namespace for the rest, this code is very much
145 # internal to ikiwiki even though it's separated into a plugin.
146 package IkiWiki;
147
148 my %toping;
149 my %feedlinks;
150
151 sub preprocess_inline (@) {
152         my %params=@_;
153         
154         if (! exists $params{pages} && ! exists $params{pagenames}) {
155                 error gettext("missing pages parameter");
156         }
157         my $raw=yesno($params{raw});
158         my $archive=yesno($params{archive});
159         my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
160         my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
161         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
162         my $feeds=! $nested && (exists $params{feeds} ? yesno($params{feeds}) : !$quick);
163         my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
164         my $feedonly=yesno($params{feedonly});
165         if (! exists $params{show} && ! $archive) {
166                 $params{show}=10;
167         }
168         if (! exists $params{feedshow} && exists $params{show}) {
169                 $params{feedshow}=$params{show};
170         }
171         my $desc;
172         if (exists $params{description}) {
173                 $desc = $params{description} 
174         }
175         else {
176                 $desc = $config{wikiname};
177         }
178         my $actions=yesno($params{actions});
179         if (exists $params{template}) {
180                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
181         }
182         else {
183                 $params{template} = $archive ? "archivepage" : "inlinepage";
184         }
185
186         my @list;
187
188         if (exists $params{pagenames}) {
189                 foreach my $p (qw(sort pages)) {
190                         if (exists $params{$p}) {
191                                 error sprintf(gettext("the %s and %s parameters cannot be used together"),
192                                         "pagenames", $p);
193                         }
194                 }
195
196                 @list = map { bestlink($params{page}, $_) }
197                         split ' ', $params{pagenames};
198
199                 if (yesno($params{reverse})) {
200                         @list=reverse(@list);
201                 }
202
203                 foreach my $p (@list) {
204                         add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
205                 }
206         }
207         else {
208                 my $num=0;
209                 if ($params{show}) {
210                         $num=$params{show};
211                 }
212                 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
213                         $num=$params{feedshow};
214                 }
215                 if ($params{skip} && $num) {
216                         $num+=$params{skip};
217                 }
218
219                 @list = pagespec_match_list($params{page}, $params{pages},
220                         deptype => deptype($quick ? "presence" : "content"),
221                         filter => sub { $_[0] eq $params{page} },
222                         sort => exists $params{sort} ? $params{sort} : "age",
223                         reverse => yesno($params{reverse}),
224                         ($num ? (num => $num) : ()),
225                 );
226         }
227
228         if (exists $params{skip}) {
229                 @list=@list[$params{skip} .. $#list];
230         }
231         
232         my @feedlist;
233         if ($feeds) {
234                 if (exists $params{feedshow} &&
235                     $params{feedshow} && @list > $params{feedshow}) {
236                         @feedlist=@list[0..$params{feedshow} - 1];
237                 }
238                 else {
239                         @feedlist=@list;
240                 }
241         }
242         
243         if ($params{show} && @list > $params{show}) {
244                 @list=@list[0..$params{show} - 1];
245         }
246
247         if ($feeds && exists $params{feedpages}) {
248                 @feedlist = pagespec_match_list(
249                         $params{page}, "($params{pages}) and ($params{feedpages})",
250                         deptype => deptype($quick ? "presence" : "content"),
251                         list => \@feedlist,
252                 );
253         }
254
255         my ($feedbase, $feednum);
256         if ($feeds) {
257                 # Ensure that multiple feeds on a page go to unique files.
258                 
259                 # Feedfile can lead to conflicts if usedirs is not enabled,
260                 # so avoid supporting it in that case.
261                 delete $params{feedfile} if ! $config{usedirs};
262                 # Tight limits on legal feedfiles, to avoid security issues
263                 # and conflicts.
264                 if (defined $params{feedfile}) {
265                         if ($params{feedfile} =~ /\// ||
266                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
267                                 error("illegal feedfile");
268                         }
269                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
270                 }
271                 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
272
273                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
274                 if (exists $knownfeeds{$feedid}) {
275                         $feednum=$knownfeeds{$feedid};
276                 }
277                 else {
278                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
279                                 if ($feeds) {
280                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
281                                 }
282                         }
283                         else {
284                                 $feednum=$knownfeeds{$feedid}="";
285                                 if ($feeds) {
286                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
287                                 }
288                         }
289                 }
290         }
291
292         my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
293         my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
294
295         my $ret="";
296
297         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
298             (exists $params{postform} && yesno($params{postform}))) &&
299             IkiWiki->can("cgi_editpage")) {
300                 # Add a blog post form, with feed buttons.
301                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
302                 $formtemplate->param(cgiurl => $config{cgiurl});
303                 $formtemplate->param(rootpage => rootpage(%params));
304                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
305                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
306                 if (exists $params{postformtext}) {
307                         $formtemplate->param(postformtext =>
308                                 $params{postformtext});
309                 }
310                 else {
311                         $formtemplate->param(postformtext =>
312                                 gettext("Add a new post titled:"));
313                 }
314                 $ret.=$formtemplate->output;
315                 
316                 # The post form includes the feed buttons, so
317                 # emptyfeeds cannot be hidden.
318                 $emptyfeeds=1;
319         }
320         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
321                 # Add feed buttons.
322                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
323                 $linktemplate->param(rssurl => $rssurl) if $rss;
324                 $linktemplate->param(atomurl => $atomurl) if $atom;
325                 $ret.=$linktemplate->output;
326         }
327         
328         if (! $feedonly) {
329                 require HTML::Template;
330                 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
331                 if (! @params) {
332                         error sprintf(gettext("nonexistant template %s"), $params{template});
333                 }
334                 my $template=HTML::Template->new(@params) unless $raw;
335                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
336         
337                 foreach my $page (@list) {
338                         my $file = $pagesources{$page};
339                         my $type = pagetype($file);
340                         if (! $raw) {
341                                 if ($needcontent) {
342                                         # Get the content before populating the
343                                         # template, since getting the content uses
344                                         # the same template if inlines are nested.
345                                         my $content=get_inline_content($page, $params{destpage});
346                                         $template->param(content => $content);
347                                 }
348                                 $template->param(pageurl => urlto($page, $params{destpage}));
349                                 $template->param(inlinepage => $page);
350                                 $template->param(title => pagetitle(basename($page)));
351                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
352                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
353                                 $template->param(first => 1) if $page eq $list[0];
354                                 $template->param(last => 1) if $page eq $list[$#list];
355         
356                                 if ($actions) {
357                                         my $file = $pagesources{$page};
358                                         my $type = pagetype($file);
359                                         if ($config{discussion}) {
360                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
361                                                     (length $config{cgiurl} ||
362                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
363                                                         $template->param(have_actions => 1);
364                                                         $template->param(discussionlink =>
365                                                                 htmllink($page,
366                                                                         $params{destpage},
367                                                                         $config{discussionpage},
368                                                                         noimageinline => 1,
369                                                                         forcesubpage => 1));
370                                                 }
371                                         }
372                                         if (length $config{cgiurl} &&
373                                             defined $type &&
374                                             IkiWiki->can("cgi_editpage")) {
375                                                 $template->param(have_actions => 1);
376                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
377
378                                         }
379                                 }
380         
381                                 run_hooks(pagetemplate => sub {
382                                         shift->(page => $page, destpage => $params{destpage},
383                                                 template => $template,);
384                                 });
385         
386                                 $ret.=$template->output;
387                                 $template->clear_params;
388                         }
389                         else {
390                                 if (defined $type) {
391                                         $ret.="\n".
392                                               linkify($page, $params{destpage},
393                                               preprocess($page, $params{destpage},
394                                               filter($page, $params{destpage},
395                                               readfile(srcfile($file)))));
396                                 }
397                                 else {
398                                         $ret.="\n".
399                                               readfile(srcfile($file));
400                                 }
401                         }
402                 }
403         }
404         
405         if ($feeds && ($emptyfeeds || @feedlist)) {
406                 if ($rss) {
407                         my $rssp=$feedbase."rss".$feednum;
408                         will_render($params{destpage}, $rssp);
409                         if (! $params{preview}) {
410                                 writefile($rssp, $config{destdir},
411                                         genfeed("rss",
412                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
413                                 $toping{$params{destpage}}=1 unless $config{rebuild};
414                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
415                         }
416                 }
417                 if ($atom) {
418                         my $atomp=$feedbase."atom".$feednum;
419                         will_render($params{destpage}, $atomp);
420                         if (! $params{preview}) {
421                                 writefile($atomp, $config{destdir},
422                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
423                                 $toping{$params{destpage}}=1 unless $config{rebuild};
424                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
425                         }
426                 }
427         }
428         
429         clear_inline_content_cache();
430
431         return $ret if $raw || $nested;
432         push @inline, $ret;
433         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
434 }
435
436 sub pagetemplate_inline (@) {
437         my %params=@_;
438         my $page=$params{page};
439         my $template=$params{template};
440
441         $template->param(feedlinks => $feedlinks{$page})
442                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
443 }
444
445 {
446 my %inline_content;
447 my $cached_destpage="";
448
449 sub get_inline_content ($$) {
450         my $page=shift;
451         my $destpage=shift;
452         
453         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
454                 return $inline_content{$page};
455         }
456
457         my $file=$pagesources{$page};
458         my $type=pagetype($file);
459         my $ret="";
460         if (defined $type) {
461                 $nested++;
462                 $ret=htmlize($page, $destpage, $type,
463                        linkify($page, $destpage,
464                        preprocess($page, $destpage,
465                        filter($page, $destpage,
466                        readfile(srcfile($file))))));
467                 $nested--;
468         }
469         
470         if ($cached_destpage ne $destpage) {
471                 clear_inline_content_cache();
472                 $cached_destpage=$destpage;
473         }
474         return $inline_content{$page}=$ret;
475 }
476
477 sub clear_inline_content_cache () {
478         %inline_content=();
479 }
480
481 }
482
483 sub date_822 ($) {
484         my $time=shift;
485
486         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
487         POSIX::setlocale(&POSIX::LC_TIME, "C");
488         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
489         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
490         return $ret;
491 }
492
493 sub date_3339 ($) {
494         my $time=shift;
495
496         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
497         POSIX::setlocale(&POSIX::LC_TIME, "C");
498         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
499         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
500         return $ret;
501 }
502
503 sub absolute_urls ($$) {
504         # sucky sub because rss sucks
505         my $content=shift;
506         my $baseurl=shift;
507
508         my $url=$baseurl;
509         $url=~s/[^\/]+$//;
510
511         # what is the non path part of the url?
512         my $top_uri = URI->new($url);
513         $top_uri->path_query(""); # reset the path
514         my $urltop = $top_uri->as_string;
515
516         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
517         # relative to another wiki page
518         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
519         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
520         # relative to the top of the site
521         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
522         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
523         return $content;
524 }
525
526 sub genfeed ($$$$$@) {
527         my $feedtype=shift;
528         my $feedurl=shift;
529         my $feeddesc=shift;
530         my $guid=shift;
531         my $page=shift;
532         my @pages=@_;
533         
534         my $url=URI->new(encode_utf8(urlto($page,"",1)));
535         
536         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
537         my $content="";
538         my $lasttime = 0;
539         foreach my $p (@pages) {
540                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
541                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
542
543                 $itemtemplate->param(
544                         title => pagetitle(basename($p)),
545                         url => $u,
546                         permalink => $u,
547                         cdate_822 => date_822($pagectime{$p}),
548                         mdate_822 => date_822($pagemtime{$p}),
549                         cdate_3339 => date_3339($pagectime{$p}),
550                         mdate_3339 => date_3339($pagemtime{$p}),
551                 );
552
553                 if (exists $pagestate{$p}) {
554                         if (exists $pagestate{$p}{meta}{guid}) {
555                                 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
556                         }
557
558                         if (exists $pagestate{$p}{meta}{updated}) {
559                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
560                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
561                         }
562                 }
563
564                 if ($itemtemplate->query(name => "enclosure")) {
565                         my $file=$pagesources{$p};
566                         my $type=pagetype($file);
567                         if (defined $type) {
568                                 $itemtemplate->param(content => $pcontent);
569                         }
570                         else {
571                                 my $size=(srcfile_stat($file))[8];
572                                 my $mime="unknown";
573                                 eval q{use File::MimeInfo};
574                                 if (! $@) {
575                                         $mime = mimetype($file);
576                                 }
577                                 $itemtemplate->param(
578                                         enclosure => $u,
579                                         type => $mime,
580                                         length => $size,
581                                 );
582                         }
583                 }
584                 else {
585                         $itemtemplate->param(content => $pcontent);
586                 }
587
588                 run_hooks(pagetemplate => sub {
589                         shift->(page => $p, destpage => $page,
590                                 template => $itemtemplate);
591                 });
592
593                 $content.=$itemtemplate->output;
594                 $itemtemplate->clear_params;
595
596                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
597         }
598
599         my $template=template($feedtype."page.tmpl", blind_cache => 1);
600         $template->param(
601                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
602                 wikiname => $config{wikiname},
603                 pageurl => $url,
604                 content => $content,
605                 feeddesc => $feeddesc,
606                 guid => $guid,
607                 feeddate => date_3339($lasttime),
608                 feedurl => $feedurl,
609                 version => $IkiWiki::version,
610         );
611         run_hooks(pagetemplate => sub {
612                 shift->(page => $page, destpage => $page,
613                         template => $template);
614         });
615         
616         return $template->output;
617 }
618
619 sub pingurl (@) {
620         return unless @{$config{pingurl}} && %toping;
621
622         eval q{require RPC::XML::Client};
623         if ($@) {
624                 debug(gettext("RPC::XML::Client not found, not pinging"));
625                 return;
626         }
627
628         # daemonize here so slow pings don't slow down wiki updates
629         defined(my $pid = fork) or error("Can't fork: $!");
630         return if $pid;
631         chdir '/';
632         POSIX::setsid() or error("Can't start a new session: $!");
633         open STDIN, '/dev/null';
634         open STDOUT, '>/dev/null';
635         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
636
637         # Don't need to keep a lock on the wiki as a daemon.
638         IkiWiki::unlockwiki();
639
640         foreach my $page (keys %toping) {
641                 my $title=pagetitle(basename($page), 0);
642                 my $url=urlto($page, "", 1);
643                 foreach my $pingurl (@{$config{pingurl}}) {
644                         debug("Pinging $pingurl for $page");
645                         eval {
646                                 my $client = RPC::XML::Client->new($pingurl);
647                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
648                                         $title, $url);
649                                 my $res = $client->send_request($req);
650                                 if (! ref $res) {
651                                         error("Did not receive response to ping");
652                                 }
653                                 my $r=$res->value;
654                                 if (! exists $r->{flerror} || $r->{flerror}) {
655                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
656                                 }
657                         };
658                         if ($@) {
659                                 error "Ping failed: $@";
660                         }
661                 }
662         }
663
664         exit 0; # daemon done
665 }
666
667
668 sub rootpage (@) {
669         my %params=@_;
670
671         my $rootpage;
672         if (exists $params{rootpage}) {
673                 $rootpage=bestlink($params{page}, $params{rootpage});
674                 if (!length $rootpage) {
675                         $rootpage=$params{rootpage};
676                 }
677         }
678         else {
679                 $rootpage=$params{page};
680         }
681         return $rootpage;
682 }
683
684 1