summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/aggregate.pm
blob: 85b398063007237eb478cfd6dbaf696372254441 (plain)
  1. #!/usr/bin/perl
  2. # Feed aggregation plugin.
  3. package IkiWiki::Plugin::aggregate;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. use HTML::Parser;
  8. use HTML::Tagset;
  9. use HTML::Entities;
  10. use URI;
  11. use open qw{:utf8 :std};
  12. my %feeds;
  13. my %guids;
  14. sub import { #{{{
  15. hook(type => "getopt", id => "aggregate", call => \&getopt);
  16. hook(type => "getsetup", id => "aggregate", call => \&getsetup);
  17. hook(type => "checkconfig", id => "aggregate", call => \&checkconfig);
  18. hook(type => "needsbuild", id => "aggregate", call => \&needsbuild);
  19. hook(type => "preprocess", id => "aggregate", call => \&preprocess);
  20. hook(type => "delete", id => "aggregate", call => \&delete);
  21. hook(type => "savestate", id => "aggregate", call => \&savestate);
  22. hook(type => "htmlize", id => "_aggregated", call => \&htmlize);
  23. if (exists $config{aggregate_webtrigger} && $config{aggregate_webtrigger}) {
  24. hook(type => "cgi", id => "aggregate", call => \&cgi);
  25. }
  26. } # }}}
  27. sub getopt () { #{{{
  28. eval q{use Getopt::Long};
  29. error($@) if $@;
  30. Getopt::Long::Configure('pass_through');
  31. GetOptions(
  32. "aggregate" => \$config{aggregate},
  33. "aggregateinternal!" => \$config{aggregateinternal},
  34. );
  35. } #}}}
  36. sub getsetup () { #{{{
  37. return
  38. plugin => {
  39. safe => 1,
  40. rebuild => undef,
  41. },
  42. aggregateinternal => {
  43. type => "boolean",
  44. example => 0,
  45. description => "enable aggregation to internal pages?",
  46. safe => 0, # enabling needs manual transition
  47. rebuild => 0,
  48. },
  49. aggregate_webtrigger => {
  50. type => "boolean",
  51. example => 0,
  52. description => "allow aggregation to be triggered via the web?",
  53. safe => 1,
  54. rebuild => 0,
  55. },
  56. } #}}}
  57. sub checkconfig () { #{{{
  58. if ($config{aggregate} && ! ($config{post_commit} &&
  59. IkiWiki::commit_hook_enabled())) {
  60. launchaggregation();
  61. }
  62. } #}}}
  63. sub cgi ($) { #{{{
  64. my $cgi=shift;
  65. if (defined $cgi->param('do') &&
  66. $cgi->param("do") eq "aggregate_webtrigger") {
  67. $|=1;
  68. print "Content-Type: text/plain\n\n";
  69. $config{cgi}=0;
  70. $config{verbose}=1;
  71. $config{syslog}=0;
  72. print gettext("Aggregation triggered via web.")."\n\n";
  73. if (launchaggregation()) {
  74. IkiWiki::lockwiki();
  75. IkiWiki::loadindex();
  76. require IkiWiki::Render;
  77. IkiWiki::refresh();
  78. IkiWiki::saveindex();
  79. }
  80. else {
  81. print gettext("Nothing to do right now, all feeds are up-to-date!")."\n";
  82. }
  83. exit 0;
  84. }
  85. } #}}}
  86. sub launchaggregation () { #{{{
  87. # See if any feeds need aggregation.
  88. loadstate();
  89. my @feeds=needsaggregate();
  90. return unless @feeds;
  91. if (! lockaggregate()) {
  92. debug("an aggregation process is already running");
  93. return;
  94. }
  95. # force a later rebuild of source pages
  96. $IkiWiki::forcerebuild{$_->{sourcepage}}=1
  97. foreach @feeds;
  98. # Fork a child process to handle the aggregation.
  99. # The parent process will then handle building the
  100. # result. This avoids messy code to clear state
  101. # accumulated while aggregating.
  102. defined(my $pid = fork) or error("Can't fork: $!");
  103. if (! $pid) {
  104. IkiWiki::loadindex();
  105. # Aggregation happens without the main wiki lock
  106. # being held. This allows editing pages etc while
  107. # aggregation is running.
  108. aggregate(@feeds);
  109. IkiWiki::lockwiki;
  110. # Merge changes, since aggregation state may have
  111. # changed on disk while the aggregation was happening.
  112. mergestate();
  113. expire();
  114. savestate();
  115. IkiWiki::unlockwiki;
  116. exit 0;
  117. }
  118. waitpid($pid,0);
  119. if ($?) {
  120. error "aggregation failed with code $?";
  121. }
  122. clearstate();
  123. unlockaggregate();
  124. return 1;
  125. } #}}}
  126. # Pages with extension _aggregated have plain html markup, pass through.
  127. sub htmlize (@) { #{{{
  128. my %params=@_;
  129. return $params{content};
  130. } #}}}
  131. # Used by ikiwiki-transition aggregateinternal.
  132. sub migrate_to_internal { #{{{
  133. if (! lockaggregate()) {
  134. error("an aggregation process is currently running");
  135. }
  136. IkiWiki::lockwiki();
  137. loadstate();
  138. $config{verbose}=1;
  139. foreach my $data (values %guids) {
  140. next unless $data->{page};
  141. next if $data->{expired};
  142. $config{aggregateinternal} = 0;
  143. my $oldname = pagefile($data->{page});
  144. my $oldoutput = $config{destdir}."/".IkiWiki::htmlpage($data->{page});
  145. $config{aggregateinternal} = 1;
  146. my $newname = pagefile($data->{page});
  147. debug "moving $oldname -> $newname";
  148. if (-e $newname) {
  149. if (-e $oldname) {
  150. error("$newname already exists");
  151. }
  152. else {
  153. debug("already renamed to $newname?");
  154. }
  155. }
  156. elsif (-e $oldname) {
  157. rename($oldname, $newname) || error("$!");
  158. }
  159. else {
  160. debug("$oldname not found");
  161. }
  162. if (-e $oldoutput) {
  163. require IkiWiki::Render;
  164. debug("removing output file $oldoutput");
  165. IkiWiki::prune($oldoutput);
  166. }
  167. }
  168. savestate();
  169. IkiWiki::unlockwiki;
  170. unlockaggregate();
  171. } #}}}
  172. sub needsbuild (@) { #{{{
  173. my $needsbuild=shift;
  174. loadstate();
  175. foreach my $feed (values %feeds) {
  176. if (exists $pagesources{$feed->{sourcepage}} &&
  177. grep { $_ eq $pagesources{$feed->{sourcepage}} } @$needsbuild) {
  178. # Mark all feeds originating on this page as
  179. # not yet seen; preprocess will unmark those that
  180. # still exist.
  181. markunseen($feed->{sourcepage});
  182. }
  183. }
  184. } # }}}
  185. sub preprocess (@) { #{{{
  186. my %params=@_;
  187. foreach my $required (qw{name url}) {
  188. if (! exists $params{$required}) {
  189. error sprintf(gettext("missing %s parameter"), $required)
  190. }
  191. }
  192. my $feed={};
  193. my $name=$params{name};
  194. if (exists $feeds{$name}) {
  195. $feed=$feeds{$name};
  196. }
  197. else {
  198. $feeds{$name}=$feed;
  199. }
  200. $feed->{name}=$name;
  201. $feed->{sourcepage}=$params{page};
  202. $feed->{url}=$params{url};
  203. my $dir=exists $params{dir} ? $params{dir} : $params{page}."/".IkiWiki::titlepage($params{name});
  204. $dir=~s/^\/+//;
  205. ($dir)=$dir=~/$config{wiki_file_regexp}/;
  206. $feed->{dir}=$dir;
  207. $feed->{feedurl}=defined $params{feedurl} ? $params{feedurl} : "";
  208. $feed->{updateinterval}=defined $params{updateinterval} ? $params{updateinterval} * 60 : 15 * 60;
  209. $feed->{expireage}=defined $params{expireage} ? $params{expireage} : 0;
  210. $feed->{expirecount}=defined $params{expirecount} ? $params{expirecount} : 0;
  211. if (exists $params{template}) {
  212. $params{template}=~s/[^-_a-zA-Z0-9]+//g;
  213. }
  214. else {
  215. $params{template} = "aggregatepost"
  216. }
  217. $feed->{template}=$params{template} . ".tmpl";
  218. delete $feed->{unseen};
  219. $feed->{lastupdate}=0 unless defined $feed->{lastupdate};
  220. $feed->{numposts}=0 unless defined $feed->{numposts};
  221. $feed->{newposts}=0 unless defined $feed->{newposts};
  222. $feed->{message}=gettext("new feed") unless defined $feed->{message};
  223. $feed->{error}=0 unless defined $feed->{error};
  224. $feed->{tags}=[];
  225. while (@_) {
  226. my $key=shift;
  227. my $value=shift;
  228. if ($key eq 'tag') {
  229. push @{$feed->{tags}}, $value;
  230. }
  231. }
  232. return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
  233. ($feed->{error} ? "<em>" : "").$feed->{message}.
  234. ($feed->{error} ? "</em>" : "").
  235. " (".$feed->{numposts}." ".gettext("posts").
  236. ($feed->{newposts} ? "; ".$feed->{newposts}.
  237. " ".gettext("new") : "").
  238. ")";
  239. } # }}}
  240. sub delete (@) { #{{{
  241. my @files=@_;
  242. # Remove feed data for removed pages.
  243. foreach my $file (@files) {
  244. my $page=pagename($file);
  245. markunseen($page);
  246. }
  247. } #}}}
  248. sub markunseen ($) { #{{{
  249. my $page=shift;
  250. foreach my $id (keys %feeds) {
  251. if ($feeds{$id}->{sourcepage} eq $page) {
  252. $feeds{$id}->{unseen}=1;
  253. }
  254. }
  255. } #}}}
  256. my $state_loaded=0;
  257. sub loadstate () { #{{{
  258. return if $state_loaded;
  259. $state_loaded=1;
  260. if (-e "$config{wikistatedir}/aggregate") {
  261. open(IN, "$config{wikistatedir}/aggregate") ||
  262. die "$config{wikistatedir}/aggregate: $!";
  263. while (<IN>) {
  264. $_=IkiWiki::possibly_foolish_untaint($_);
  265. chomp;
  266. my $data={};
  267. foreach my $i (split(/ /, $_)) {
  268. my ($field, $val)=split(/=/, $i, 2);
  269. if ($field eq "name" || $field eq "feed" ||
  270. $field eq "guid" || $field eq "message") {
  271. $data->{$field}=decode_entities($val, " \t\n");
  272. }
  273. elsif ($field eq "tag") {
  274. push @{$data->{tags}}, $val;
  275. }
  276. else {
  277. $data->{$field}=$val;
  278. }
  279. }
  280. if (exists $data->{name}) {
  281. $feeds{$data->{name}}=$data;
  282. }
  283. elsif (exists $data->{guid}) {
  284. $guids{$data->{guid}}=$data;
  285. }
  286. }
  287. close IN;
  288. }
  289. } #}}}
  290. sub savestate () { #{{{
  291. return unless $state_loaded;
  292. garbage_collect();
  293. my $newfile="$config{wikistatedir}/aggregate.new";
  294. my $cleanup = sub { unlink($newfile) };
  295. open (OUT, ">$newfile") || error("open $newfile: $!", $cleanup);
  296. foreach my $data (values %feeds, values %guids) {
  297. my @line;
  298. foreach my $field (keys %$data) {
  299. if ($field eq "name" || $field eq "feed" ||
  300. $field eq "guid" || $field eq "message") {
  301. push @line, "$field=".encode_entities($data->{$field}, " \t\n");
  302. }
  303. elsif ($field eq "tags") {
  304. push @line, "tag=$_" foreach @{$data->{tags}};
  305. }
  306. else {
  307. push @line, "$field=".$data->{$field};
  308. }
  309. }
  310. print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
  311. }
  312. close OUT || error("save $newfile: $!", $cleanup);
  313. rename($newfile, "$config{wikistatedir}/aggregate") ||
  314. error("rename $newfile: $!", $cleanup);
  315. } #}}}
  316. sub garbage_collect () { #{{{
  317. foreach my $name (keys %feeds) {
  318. # remove any feeds that were not seen while building the pages
  319. # that used to contain them
  320. if ($feeds{$name}->{unseen}) {
  321. delete $feeds{$name};
  322. }
  323. }
  324. foreach my $guid (values %guids) {
  325. # any guid whose feed is gone should be removed
  326. if (! exists $feeds{$guid->{feed}}) {
  327. unlink pagefile($guid->{page})
  328. if exists $guid->{page};
  329. delete $guids{$guid->{guid}};
  330. }
  331. # handle expired guids
  332. elsif ($guid->{expired} && exists $guid->{page}) {
  333. unlink pagefile($guid->{page});
  334. delete $guid->{page};
  335. delete $guid->{md5};
  336. }
  337. }
  338. } #}}}
  339. sub mergestate () { #{{{
  340. # Load the current state in from disk, and merge into it
  341. # values from the state in memory that might have changed
  342. # during aggregation.
  343. my %myfeeds=%feeds;
  344. my %myguids=%guids;
  345. clearstate();
  346. loadstate();
  347. # All that can change in feed state during aggregation is a few
  348. # fields.
  349. foreach my $name (keys %myfeeds) {
  350. if (exists $feeds{$name}) {
  351. foreach my $field (qw{message lastupdate numposts
  352. newposts error}) {
  353. $feeds{$name}->{$field}=$myfeeds{$name}->{$field};
  354. }
  355. }
  356. }
  357. # New guids can be created during aggregation.
  358. # It's also possible that guids were removed from the on-disk state
  359. # while the aggregation was in process. That would only happen if
  360. # their feed was also removed, so any removed guids added back here
  361. # will be garbage collected later.
  362. foreach my $guid (keys %myguids) {
  363. if (! exists $guids{$guid}) {
  364. $guids{$guid}=$myguids{$guid};
  365. }
  366. }
  367. } #}}}
  368. sub clearstate () { #{{{
  369. %feeds=();
  370. %guids=();
  371. $state_loaded=0;
  372. } #}}}
  373. sub expire () { #{{{
  374. foreach my $feed (values %feeds) {
  375. next unless $feed->{expireage} || $feed->{expirecount};
  376. my $count=0;
  377. my %seen;
  378. foreach my $item (sort { $IkiWiki::pagectime{$b->{page}} <=> $IkiWiki::pagectime{$a->{page}} }
  379. grep { exists $_->{page} && $_->{feed} eq $feed->{name} && $IkiWiki::pagectime{$_->{page}} }
  380. values %guids) {
  381. if ($feed->{expireage}) {
  382. my $days_old = (time - $IkiWiki::pagectime{$item->{page}}) / 60 / 60 / 24;
  383. if ($days_old > $feed->{expireage}) {
  384. debug(sprintf(gettext("expiring %s (%s days old)"),
  385. $item->{page}, int($days_old)));
  386. $item->{expired}=1;
  387. }
  388. }
  389. elsif ($feed->{expirecount} &&
  390. $count >= $feed->{expirecount}) {
  391. debug(sprintf(gettext("expiring %s"), $item->{page}));
  392. $item->{expired}=1;
  393. }
  394. else {
  395. if (! $seen{$item->{page}}) {
  396. $seen{$item->{page}}=1;
  397. $count++;
  398. }
  399. }
  400. }
  401. }
  402. } #}}}
  403. sub needsaggregate () { #{{{
  404. return values %feeds if $config{rebuild};
  405. return grep { time - $_->{lastupdate} >= $_->{updateinterval} } values %feeds;
  406. } #}}}
  407. sub aggregate (@) { #{{{
  408. eval q{use XML::Feed};
  409. error($@) if $@;
  410. eval q{use URI::Fetch};
  411. error($@) if $@;
  412. foreach my $feed (@_) {
  413. $feed->{lastupdate}=time;
  414. $feed->{newposts}=0;
  415. $feed->{message}=sprintf(gettext("processed ok at %s"),
  416. displaytime($feed->{lastupdate}));
  417. $feed->{error}=0;
  418. debug(sprintf(gettext("checking feed %s ..."), $feed->{name}));
  419. if (! length $feed->{feedurl}) {
  420. my @urls=XML::Feed->find_feeds($feed->{url});
  421. if (! @urls) {
  422. $feed->{message}=sprintf(gettext("could not find feed at %s"), $feed->{url});
  423. $feed->{error}=1;
  424. debug($feed->{message});
  425. next;
  426. }
  427. $feed->{feedurl}=pop @urls;
  428. }
  429. my $res=URI::Fetch->fetch($feed->{feedurl});
  430. if (! $res) {
  431. $feed->{message}=URI::Fetch->errstr;
  432. $feed->{error}=1;
  433. debug($feed->{message});
  434. next;
  435. }
  436. if ($res->status == URI::Fetch::URI_GONE()) {
  437. $feed->{message}=gettext("feed not found");
  438. $feed->{error}=1;
  439. debug($feed->{message});
  440. next;
  441. }
  442. my $content=$res->content;
  443. my $f=eval{XML::Feed->parse(\$content)};
  444. if ($@) {
  445. # One common cause of XML::Feed crashing is a feed
  446. # that contains invalid UTF-8 sequences. Convert
  447. # feed to ascii to try to work around.
  448. $feed->{message}.=" ".sprintf(gettext("(invalid UTF-8 stripped from feed)"));
  449. $content=Encode::decode_utf8($content, 0);
  450. $f=eval{XML::Feed->parse(\$content)};
  451. }
  452. if ($@) {
  453. # Another possibility is badly escaped entities.
  454. $feed->{message}.=" ".sprintf(gettext("(feed entities escaped)"));
  455. $content=~s/\&(?!amp)(\w+);/&amp;$1;/g;
  456. $content=Encode::decode_utf8($content, 0);
  457. $f=eval{XML::Feed->parse(\$content)};
  458. }
  459. if ($@) {
  460. $feed->{message}=gettext("feed crashed XML::Feed!")." ($@)";
  461. $feed->{error}=1;
  462. debug($feed->{message});
  463. next;
  464. }
  465. if (! $f) {
  466. $feed->{message}=XML::Feed->errstr;
  467. $feed->{error}=1;
  468. debug($feed->{message});
  469. next;
  470. }
  471. foreach my $entry ($f->entries) {
  472. add_page(
  473. feed => $feed,
  474. copyright => $f->copyright,
  475. title => defined $entry->title ? decode_entities($entry->title) : "untitled",
  476. link => $entry->link,
  477. content => defined $entry->content->body ? $entry->content->body : "",
  478. guid => defined $entry->id ? $entry->id : time."_".$feed->{name},
  479. ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
  480. );
  481. }
  482. }
  483. } #}}}
  484. sub add_page (@) { #{{{
  485. my %params=@_;
  486. my $feed=$params{feed};
  487. my $guid={};
  488. my $mtime;
  489. if (exists $guids{$params{guid}}) {
  490. # updating an existing post
  491. $guid=$guids{$params{guid}};
  492. return if $guid->{expired};
  493. }
  494. else {
  495. # new post
  496. $guid->{guid}=$params{guid};
  497. $guids{$params{guid}}=$guid;
  498. $mtime=$params{ctime};
  499. $feed->{numposts}++;
  500. $feed->{newposts}++;
  501. # assign it an unused page
  502. my $page=IkiWiki::titlepage($params{title});
  503. # escape slashes and periods in title so it doesn't specify
  504. # directory name or trigger ".." disallowing code.
  505. $page=~s!([/.])!"__".ord($1)."__"!eg;
  506. $page=$feed->{dir}."/".$page;
  507. ($page)=$page=~/$config{wiki_file_regexp}/;
  508. if (! defined $page || ! length $page) {
  509. $page=$feed->{dir}."/item";
  510. }
  511. my $c="";
  512. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  513. -e pagefile($page.$c)) {
  514. $c++
  515. }
  516. # Make sure that the file name isn't too long.
  517. # NB: This doesn't check for path length limits.
  518. my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
  519. if (defined $max && length(htmlfn($page)) >= $max) {
  520. $c="";
  521. $page=$feed->{dir}."/item";
  522. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  523. -e pagefile($page.$c)) {
  524. $c++
  525. }
  526. }
  527. $guid->{page}=$page;
  528. debug(sprintf(gettext("creating new page %s"), $page));
  529. }
  530. $guid->{feed}=$feed->{name};
  531. # To write or not to write? Need to avoid writing unchanged pages
  532. # to avoid unneccessary rebuilding. The mtime from rss cannot be
  533. # trusted; let's use a digest.
  534. eval q{use Digest::MD5 'md5_hex'};
  535. error($@) if $@;
  536. require Encode;
  537. my $digest=md5_hex(Encode::encode_utf8($params{content}));
  538. return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
  539. $guid->{md5}=$digest;
  540. # Create the page.
  541. my $template=template($feed->{template}, blind_cache => 1);
  542. $template->param(title => $params{title})
  543. if defined $params{title} && length($params{title});
  544. $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
  545. $template->param(name => $feed->{name});
  546. $template->param(url => $feed->{url});
  547. $template->param(copyright => $params{copyright})
  548. if defined $params{copyright} && length $params{copyright};
  549. $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
  550. if defined $params{link};
  551. if (ref $feed->{tags}) {
  552. $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
  553. }
  554. writefile(htmlfn($guid->{page}), $config{srcdir},
  555. $template->output);
  556. # Set the mtime, this lets the build process get the right creation
  557. # time on record for the new page.
  558. utime $mtime, $mtime, pagefile($guid->{page})
  559. if defined $mtime && $mtime <= time;
  560. } #}}}
  561. sub htmlescape ($) { #{{{
  562. # escape accidental wikilinks and preprocessor stuff
  563. my $html=shift;
  564. $html=~s/(?<!\\)\[\[/\\\[\[/g;
  565. return $html;
  566. } #}}}
  567. sub urlabs ($$) { #{{{
  568. my $url=shift;
  569. my $urlbase=shift;
  570. URI->new_abs($url, $urlbase)->as_string;
  571. } #}}}
  572. sub htmlabs ($$) { #{{{
  573. # Convert links in html from relative to absolute.
  574. # Note that this is a heuristic, which is not specified by the rss
  575. # spec and may not be right for all feeds. Also, see Debian
  576. # bug #381359.
  577. my $html=shift;
  578. my $urlbase=shift;
  579. my $ret="";
  580. my $p = HTML::Parser->new(api_version => 3);
  581. $p->handler(default => sub { $ret.=join("", @_) }, "text");
  582. $p->handler(start => sub {
  583. my ($tagname, $pos, $text) = @_;
  584. if (ref $HTML::Tagset::linkElements{$tagname}) {
  585. while (4 <= @$pos) {
  586. # use attribute sets from right to left
  587. # to avoid invalidating the offsets
  588. # when replacing the values
  589. my($k_offset, $k_len, $v_offset, $v_len) =
  590. splice(@$pos, -4);
  591. my $attrname = lc(substr($text, $k_offset, $k_len));
  592. next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
  593. next unless $v_offset; # 0 v_offset means no value
  594. my $v = substr($text, $v_offset, $v_len);
  595. $v =~ s/^([\'\"])(.*)\1$/$2/;
  596. my $new_v=urlabs($v, $urlbase);
  597. $new_v =~ s/\"/&quot;/g; # since we quote with ""
  598. substr($text, $v_offset, $v_len) = qq("$new_v");
  599. }
  600. }
  601. $ret.=$text;
  602. }, "tagname, tokenpos, text");
  603. $p->parse($html);
  604. $p->eof;
  605. return $ret;
  606. } #}}}
  607. sub pagefile ($) { #{{{
  608. my $page=shift;
  609. return "$config{srcdir}/".htmlfn($page);
  610. } #}}}
  611. sub htmlfn ($) { #{{{
  612. return shift().".".($config{aggregateinternal} ? "_aggregated" : $config{htmlext});
  613. } #}}}
  614. my $aggregatelock;
  615. sub lockaggregate () { #{{{
  616. # Take an exclusive lock to prevent multiple concurrent aggregators.
  617. # Returns true if the lock was aquired.
  618. if (! -d $config{wikistatedir}) {
  619. mkdir($config{wikistatedir});
  620. }
  621. open($aggregatelock, '>', "$config{wikistatedir}/aggregatelock") ||
  622. error ("cannot open to $config{wikistatedir}/aggregatelock: $!");
  623. if (! flock($aggregatelock, 2 | 4)) { # LOCK_EX | LOCK_NB
  624. close($aggregatelock) || error("failed closing aggregatelock: $!");
  625. return 0;
  626. }
  627. return 1;
  628. } #}}}
  629. sub unlockaggregate () { #{{{
  630. return close($aggregatelock) if $aggregatelock;
  631. return;
  632. } #}}}
  633. 1