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