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