summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: ffdf397f1f337af6cfcb6435f8aaf35fcc63faf7 (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', "/$from/$page$add");
  122. # now go create the page
  123. $q->param('do', 'create');
  124. # make sure the editpage plugin is 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=exists $params{feeds} ? yesno($params{feeds}) : !$quick && ! $raw;
  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{page}, "", $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, $atomurl, $rssdesc, $atomdesc);
  266. if ($feeds) {
  267. if ($rss) {
  268. $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
  269. $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
  270. }
  271. if ($atom) {
  272. $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
  273. $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
  274. }
  275. }
  276. my $ret="";
  277. if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
  278. (exists $params{postform} && yesno($params{postform}))) &&
  279. IkiWiki->can("cgi_editpage")) {
  280. # Add a blog post form, with feed buttons.
  281. my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
  282. $formtemplate->param(cgiurl => IkiWiki::cgiurl());
  283. $formtemplate->param(rootpage => rootpage(%params));
  284. if ($feeds) {
  285. if ($rss) {
  286. $formtemplate->param(rssurl => $rssurl);
  287. $formtemplate->param(rssdesc => $rssdesc);
  288. }
  289. if ($atom) {
  290. $formtemplate->param(atomurl => $atomurl);
  291. $formtemplate->param(atomdesc => $atomdesc);
  292. }
  293. }
  294. if (exists $params{postformtext}) {
  295. $formtemplate->param(postformtext =>
  296. $params{postformtext});
  297. }
  298. else {
  299. $formtemplate->param(postformtext =>
  300. gettext("Add a new post titled:"));
  301. }
  302. if (exists $params{id}) {
  303. $formtemplate->param(postformid =>
  304. $params{id});
  305. }
  306. $ret.=$formtemplate->output;
  307. # The post form includes the feed buttons, so
  308. # emptyfeeds cannot be hidden.
  309. $emptyfeeds=1;
  310. }
  311. elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
  312. # Add feed buttons.
  313. my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
  314. if ($rss) {
  315. $linktemplate->param(rssurl => $rssurl);
  316. $linktemplate->param(rssdesc => $rssdesc);
  317. }
  318. if ($atom) {
  319. $linktemplate->param(atomurl => $atomurl);
  320. $linktemplate->param(atomdesc => $atomdesc);
  321. }
  322. if (exists $params{id}) {
  323. $linktemplate->param(id => $params{id});
  324. }
  325. $ret.=$linktemplate->output;
  326. }
  327. if (! $feedonly) {
  328. my $template;
  329. if (! $raw) {
  330. # cannot use wiki pages as templates; template not sanitized due to
  331. # format hook hack
  332. eval {
  333. $template=template_depends($params{template}.".tmpl", $params{page},
  334. blind_cache => 1);
  335. };
  336. if ($@) {
  337. error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
  338. }
  339. }
  340. my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
  341. foreach my $page (@list) {
  342. my $file = $pagesources{$page};
  343. my $type = pagetype($file);
  344. if (! $raw) {
  345. if ($needcontent) {
  346. # Get the content before populating the
  347. # template, since getting the content uses
  348. # the same template if inlines are nested.
  349. my $content=get_inline_content($page, $params{destpage});
  350. $template->param(content => $content);
  351. }
  352. $template->param(pageurl => urlto($page, $params{destpage}));
  353. $template->param(inlinepage => $page);
  354. $template->param(title => pagetitle(basename($page)));
  355. $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
  356. $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
  357. $template->param(first => 1) if $page eq $list[0];
  358. $template->param(last => 1) if $page eq $list[$#list];
  359. $template->param(html5 => $config{html5});
  360. if ($actions) {
  361. my $file = $pagesources{$page};
  362. my $type = pagetype($file);
  363. if ($config{discussion}) {
  364. if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
  365. (length $config{cgiurl} ||
  366. exists $pagesources{$page."/".lc($config{discussionpage})})) {
  367. $template->param(have_actions => 1);
  368. $template->param(discussionlink =>
  369. htmllink($page,
  370. $params{destpage},
  371. $config{discussionpage},
  372. noimageinline => 1,
  373. forcesubpage => 1));
  374. }
  375. }
  376. if (length $config{cgiurl} &&
  377. defined $type &&
  378. IkiWiki->can("cgi_editpage")) {
  379. $template->param(have_actions => 1);
  380. $template->param(editurl => cgiurl(do => "edit", page => $page));
  381. }
  382. }
  383. run_hooks(pagetemplate => sub {
  384. shift->(page => $page, destpage => $params{destpage},
  385. template => $template,);
  386. });
  387. $ret.=$template->output;
  388. $template->clear_params;
  389. }
  390. else {
  391. if (defined $type) {
  392. $ret.="\n".
  393. linkify($page, $params{destpage},
  394. preprocess($page, $params{destpage},
  395. filter($page, $params{destpage},
  396. readfile(srcfile($file)))));
  397. }
  398. else {
  399. $ret.="\n".
  400. readfile(srcfile($file));
  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{page}, @feedlist));
  413. $toping{$params{destpage}}=1 unless $config{rebuild};
  414. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" 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{page}, @feedlist));
  423. $toping{$params{destpage}}=1 unless $config{rebuild};
  424. $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
  425. }
  426. }
  427. }
  428. clear_inline_content_cache();
  429. return $ret if $raw || $nested;
  430. push @inline, $ret;
  431. return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
  432. }
  433. sub pagetemplate_inline (@) {
  434. my %params=@_;
  435. my $page=$params{page};
  436. my $template=$params{template};
  437. $template->param(feedlinks => $feedlinks{$page})
  438. if exists $feedlinks{$page} && $template->query(name => "feedlinks");
  439. }
  440. {
  441. my %inline_content;
  442. my $cached_destpage="";
  443. sub get_inline_content ($$) {
  444. my $page=shift;
  445. my $destpage=shift;
  446. if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
  447. return $inline_content{$page};
  448. }
  449. my $file=$pagesources{$page};
  450. my $type=pagetype($file);
  451. my $ret="";
  452. if (defined $type) {
  453. $nested++;
  454. $ret=htmlize($page, $destpage, $type,
  455. linkify($page, $destpage,
  456. preprocess($page, $destpage,
  457. filter($page, $destpage,
  458. readfile(srcfile($file))))));
  459. $nested--;
  460. if (isinternal($page)) {
  461. # make inlined text of internal pages searchable
  462. run_hooks(indexhtml => sub {
  463. shift->(page => $page, destpage => $page,
  464. content => $ret);
  465. });
  466. }
  467. }
  468. if ($cached_destpage ne $destpage) {
  469. clear_inline_content_cache();
  470. $cached_destpage=$destpage;
  471. }
  472. return $inline_content{$page}=$ret;
  473. }
  474. sub clear_inline_content_cache () {
  475. %inline_content=();
  476. }
  477. }
  478. sub date_822 ($) {
  479. my $time=shift;
  480. my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
  481. POSIX::setlocale(&POSIX::LC_TIME, "C");
  482. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  483. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  484. return $ret;
  485. }
  486. sub absolute_urls ($$) {
  487. # needed because rss sucks
  488. my $html=shift;
  489. my $baseurl=shift;
  490. my $url=$baseurl;
  491. $url=~s/[^\/]+$//;
  492. my $urltop; # calculated if needed
  493. my $ret="";
  494. eval q{use HTML::Parser; use HTML::Tagset};
  495. die $@ if $@;
  496. my $p = HTML::Parser->new(api_version => 3);
  497. $p->handler(default => sub { $ret.=join("", @_) }, "text");
  498. $p->handler(start => sub {
  499. my ($tagname, $pos, $text) = @_;
  500. if (ref $HTML::Tagset::linkElements{$tagname}) {
  501. while (4 <= @$pos) {
  502. # use attribute sets from right to left
  503. # to avoid invalidating the offsets
  504. # when replacing the values
  505. my ($k_offset, $k_len, $v_offset, $v_len) =
  506. splice(@$pos, -4);
  507. my $attrname = lc(substr($text, $k_offset, $k_len));
  508. next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
  509. next unless $v_offset; # 0 v_offset means no value
  510. my $v = substr($text, $v_offset, $v_len);
  511. $v =~ s/^([\'\"])(.*)\1$/$2/;
  512. if ($v=~/^#/) {
  513. $v=$baseurl.$v; # anchor
  514. }
  515. elsif ($v=~/^(?!\w+:)[^\/]/) {
  516. $v=$url.$v; # relative url
  517. }
  518. elsif ($v=~/^\//) {
  519. if (! defined $urltop) {
  520. # what is the non path part of the url?
  521. my $top_uri = URI->new($url);
  522. $top_uri->path_query(""); # reset the path
  523. $urltop = $top_uri->as_string;
  524. }
  525. $v=$urltop.$v; # url relative to top of site
  526. }
  527. $v =~ s/\"/&quot;/g; # since we quote with ""
  528. substr($text, $v_offset, $v_len) = qq("$v");
  529. }
  530. }
  531. $ret.=$text;
  532. }, "tagname, tokenpos, text");
  533. $p->parse($html);
  534. $p->eof;
  535. return $ret;
  536. }
  537. sub genfeed ($$$$$@) {
  538. my $feedtype=shift;
  539. my $feedurl=shift;
  540. my $feeddesc=shift;
  541. my $guid=shift;
  542. my $page=shift;
  543. my @pages=@_;
  544. my $url=URI->new(encode_utf8(urlto($page,"",1)));
  545. my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
  546. my $content="";
  547. my $lasttime = 0;
  548. foreach my $p (@pages) {
  549. my $u=URI->new(encode_utf8(urlto($p, "", 1)));
  550. my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
  551. $itemtemplate->param(
  552. title => pagetitle(basename($p)),
  553. url => $u,
  554. permalink => $u,
  555. cdate_822 => date_822($pagectime{$p}),
  556. mdate_822 => date_822($pagemtime{$p}),
  557. cdate_3339 => date_3339($pagectime{$p}),
  558. mdate_3339 => date_3339($pagemtime{$p}),
  559. );
  560. if (exists $pagestate{$p}) {
  561. if (exists $pagestate{$p}{meta}{guid}) {
  562. eval q{use HTML::Entities};
  563. $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
  564. }
  565. if (exists $pagestate{$p}{meta}{updated}) {
  566. $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
  567. $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
  568. }
  569. }
  570. if ($itemtemplate->query(name => "enclosure")) {
  571. my $file=$pagesources{$p};
  572. my $type=pagetype($file);
  573. if (defined $type) {
  574. $itemtemplate->param(content => $pcontent);
  575. }
  576. else {
  577. my $size=(srcfile_stat($file))[8];
  578. my $mime="unknown";
  579. eval q{use File::MimeInfo};
  580. if (! $@) {
  581. $mime = mimetype($file);
  582. }
  583. $itemtemplate->param(
  584. enclosure => $u,
  585. type => $mime,
  586. length => $size,
  587. );
  588. }
  589. }
  590. else {
  591. $itemtemplate->param(content => $pcontent);
  592. }
  593. run_hooks(pagetemplate => sub {
  594. shift->(page => $p, destpage => $page,
  595. template => $itemtemplate);
  596. });
  597. $content.=$itemtemplate->output;
  598. $itemtemplate->clear_params;
  599. $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
  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. return $template->output;
  618. }
  619. sub pingurl (@) {
  620. return unless @{$config{pingurl}} && %toping;
  621. eval q{require RPC::XML::Client};
  622. if ($@) {
  623. debug(gettext("RPC::XML::Client not found, not pinging"));
  624. return;
  625. }
  626. # daemonize here so slow pings don't slow down wiki updates
  627. defined(my $pid = fork) or error("Can't fork: $!");
  628. return if $pid;
  629. chdir '/';
  630. POSIX::setsid() or error("Can't start a new session: $!");
  631. open STDIN, '/dev/null';
  632. open STDOUT, '>/dev/null';
  633. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  634. # Don't need to keep a lock on the wiki as a daemon.
  635. IkiWiki::unlockwiki();
  636. foreach my $page (keys %toping) {
  637. my $title=pagetitle(basename($page), 0);
  638. my $url=urlto($page, "", 1);
  639. foreach my $pingurl (@{$config{pingurl}}) {
  640. debug("Pinging $pingurl for $page");
  641. eval {
  642. my $client = RPC::XML::Client->new($pingurl);
  643. my $req = RPC::XML::request->new('weblogUpdates.ping',
  644. $title, $url);
  645. my $res = $client->send_request($req);
  646. if (! ref $res) {
  647. error("Did not receive response to ping");
  648. }
  649. my $r=$res->value;
  650. if (! exists $r->{flerror} || $r->{flerror}) {
  651. error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  652. }
  653. };
  654. if ($@) {
  655. error "Ping failed: $@";
  656. }
  657. }
  658. }
  659. exit 0; # daemon done
  660. }
  661. sub rootpage (@) {
  662. my %params=@_;
  663. my $rootpage;
  664. if (exists $params{rootpage}) {
  665. $rootpage=bestlink($params{page}, $params{rootpage});
  666. if (!length $rootpage) {
  667. $rootpage=$params{rootpage};
  668. }
  669. }
  670. else {
  671. $rootpage=$params{page};
  672. }
  673. return $rootpage;
  674. }
  675. 1