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