summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/aggregate.pm
blob: 9b531b6128c1de09d81e9c1b60da25ab736e2732 (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 = "$config{srcdir}/".htmlfn($data->{page});
  144. my $oldoutput = $config{destdir}."/".IkiWiki::htmlpage($data->{page});
  145. $config{aggregateinternal} = 1;
  146. my $newname = "$config{srcdir}/".htmlfn($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}."/".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->{lasttry}=$feed->{lastupdate} unless defined $feed->{lasttry};
  221. $feed->{numposts}=0 unless defined $feed->{numposts};
  222. $feed->{newposts}=0 unless defined $feed->{newposts};
  223. $feed->{message}=gettext("new feed") unless defined $feed->{message};
  224. $feed->{error}=0 unless defined $feed->{error};
  225. $feed->{tags}=[];
  226. while (@_) {
  227. my $key=shift;
  228. my $value=shift;
  229. if ($key eq 'tag') {
  230. push @{$feed->{tags}}, $value;
  231. }
  232. }
  233. return "<a href=\"".$feed->{url}."\">".$feed->{name}."</a>: ".
  234. ($feed->{error} ? "<em>" : "").$feed->{message}.
  235. ($feed->{error} ? "</em>" : "").
  236. " (".$feed->{numposts}." ".gettext("posts").
  237. ($feed->{newposts} ? "; ".$feed->{newposts}.
  238. " ".gettext("new") : "").
  239. ")";
  240. }
  241. sub delete (@) {
  242. my @files=@_;
  243. # Remove feed data for removed pages.
  244. foreach my $file (@files) {
  245. my $page=pagename($file);
  246. markunseen($page);
  247. }
  248. }
  249. sub markunseen ($) {
  250. my $page=shift;
  251. foreach my $id (keys %feeds) {
  252. if ($feeds{$id}->{sourcepage} eq $page) {
  253. $feeds{$id}->{unseen}=1;
  254. }
  255. }
  256. }
  257. my $state_loaded=0;
  258. sub loadstate () {
  259. return if $state_loaded;
  260. $state_loaded=1;
  261. if (-e "$config{wikistatedir}/aggregate") {
  262. open(IN, "$config{wikistatedir}/aggregate") ||
  263. die "$config{wikistatedir}/aggregate: $!";
  264. while (<IN>) {
  265. $_=IkiWiki::possibly_foolish_untaint($_);
  266. chomp;
  267. my $data={};
  268. foreach my $i (split(/ /, $_)) {
  269. my ($field, $val)=split(/=/, $i, 2);
  270. if ($field eq "name" || $field eq "feed" ||
  271. $field eq "guid" || $field eq "message") {
  272. $data->{$field}=decode_entities($val, " \t\n");
  273. }
  274. elsif ($field eq "tag") {
  275. push @{$data->{tags}}, $val;
  276. }
  277. else {
  278. $data->{$field}=$val;
  279. }
  280. }
  281. if (exists $data->{name}) {
  282. $feeds{$data->{name}}=$data;
  283. }
  284. elsif (exists $data->{guid}) {
  285. $guids{$data->{guid}}=$data;
  286. }
  287. }
  288. close IN;
  289. }
  290. }
  291. sub savestate () {
  292. return unless $state_loaded;
  293. garbage_collect();
  294. my $newfile="$config{wikistatedir}/aggregate.new";
  295. my $cleanup = sub { unlink($newfile) };
  296. open (OUT, ">$newfile") || error("open $newfile: $!", $cleanup);
  297. foreach my $data (values %feeds, values %guids) {
  298. my @line;
  299. foreach my $field (keys %$data) {
  300. if ($field eq "name" || $field eq "feed" ||
  301. $field eq "guid" || $field eq "message") {
  302. push @line, "$field=".encode_entities($data->{$field}, " \t\n");
  303. }
  304. elsif ($field eq "tags") {
  305. push @line, "tag=$_" foreach @{$data->{tags}};
  306. }
  307. else {
  308. push @line, "$field=".$data->{$field};
  309. }
  310. }
  311. print OUT join(" ", @line)."\n" || error("write $newfile: $!", $cleanup);
  312. }
  313. close OUT || error("save $newfile: $!", $cleanup);
  314. rename($newfile, "$config{wikistatedir}/aggregate") ||
  315. error("rename $newfile: $!", $cleanup);
  316. }
  317. sub garbage_collect () {
  318. foreach my $name (keys %feeds) {
  319. # remove any feeds that were not seen while building the pages
  320. # that used to contain them
  321. if ($feeds{$name}->{unseen}) {
  322. delete $feeds{$name};
  323. }
  324. }
  325. foreach my $guid (values %guids) {
  326. # any guid whose feed is gone should be removed
  327. if (! exists $feeds{$guid->{feed}}) {
  328. unlink "$config{srcdir}/".htmlfn($guid->{page})
  329. if exists $guid->{page};
  330. delete $guids{$guid->{guid}};
  331. }
  332. # handle expired guids
  333. elsif ($guid->{expired} && exists $guid->{page}) {
  334. unlink "$config{srcdir}/".htmlfn($guid->{page});
  335. delete $guid->{page};
  336. delete $guid->{md5};
  337. }
  338. }
  339. }
  340. sub mergestate () {
  341. # Load the current state in from disk, and merge into it
  342. # values from the state in memory that might have changed
  343. # during aggregation.
  344. my %myfeeds=%feeds;
  345. my %myguids=%guids;
  346. clearstate();
  347. loadstate();
  348. # All that can change in feed state during aggregation is a few
  349. # fields.
  350. foreach my $name (keys %myfeeds) {
  351. if (exists $feeds{$name}) {
  352. foreach my $field (qw{message lastupdate lasttry
  353. numposts newposts error}) {
  354. $feeds{$name}->{$field}=$myfeeds{$name}->{$field};
  355. }
  356. }
  357. }
  358. # New guids can be created during aggregation.
  359. # It's also possible that guids were removed from the on-disk state
  360. # while the aggregation was in process. That would only happen if
  361. # their feed was also removed, so any removed guids added back here
  362. # will be garbage collected later.
  363. foreach my $guid (keys %myguids) {
  364. if (! exists $guids{$guid}) {
  365. $guids{$guid}=$myguids{$guid};
  366. }
  367. }
  368. }
  369. sub clearstate () {
  370. %feeds=();
  371. %guids=();
  372. $state_loaded=0;
  373. }
  374. sub expire () {
  375. foreach my $feed (values %feeds) {
  376. next unless $feed->{expireage} || $feed->{expirecount};
  377. my $count=0;
  378. my %seen;
  379. foreach my $item (sort { ($IkiWiki::pagectime{$b->{page}} || 0) <=> ($IkiWiki::pagectime{$a->{page}} || 0) }
  380. grep { exists $_->{page} && $_->{feed} eq $feed->{name} }
  381. values %guids) {
  382. if ($feed->{expireage}) {
  383. my $days_old = (time - ($IkiWiki::pagectime{$item->{page}} || 0)) / 60 / 60 / 24;
  384. if ($days_old > $feed->{expireage}) {
  385. debug(sprintf(gettext("expiring %s (%s days old)"),
  386. $item->{page}, int($days_old)));
  387. $item->{expired}=1;
  388. }
  389. }
  390. elsif ($feed->{expirecount} &&
  391. $count >= $feed->{expirecount}) {
  392. debug(sprintf(gettext("expiring %s"), $item->{page}));
  393. $item->{expired}=1;
  394. }
  395. else {
  396. if (! $seen{$item->{page}}) {
  397. $seen{$item->{page}}=1;
  398. $count++;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. sub needsaggregate () {
  405. return values %feeds if $config{rebuild};
  406. return grep { time - $_->{lastupdate} >= $_->{updateinterval} } values %feeds;
  407. }
  408. sub aggregate (@) {
  409. eval q{use XML::Feed};
  410. error($@) if $@;
  411. eval q{use URI::Fetch};
  412. error($@) if $@;
  413. foreach my $feed (@_) {
  414. $feed->{lasttry}=time;
  415. $feed->{newposts}=0;
  416. $feed->{message}=sprintf(gettext("last checked %s"),
  417. displaytime($feed->{lasttry}));
  418. $feed->{error}=0;
  419. debug(sprintf(gettext("checking feed %s ..."), $feed->{name}));
  420. if (! length $feed->{feedurl}) {
  421. my @urls=XML::Feed->find_feeds($feed->{url});
  422. if (! @urls) {
  423. $feed->{message}=sprintf(gettext("could not find feed at %s"), $feed->{url});
  424. $feed->{error}=1;
  425. debug($feed->{message});
  426. next;
  427. }
  428. $feed->{feedurl}=pop @urls;
  429. }
  430. my $res=URI::Fetch->fetch($feed->{feedurl});
  431. if (! $res) {
  432. $feed->{message}=URI::Fetch->errstr;
  433. $feed->{error}=1;
  434. debug($feed->{message});
  435. next;
  436. }
  437. # lastupdate is only set if we were able to contact the server
  438. $feed->{lastupdate}=$feed->{lasttry};
  439. if ($res->status == URI::Fetch::URI_GONE()) {
  440. $feed->{message}=gettext("feed not found");
  441. $feed->{error}=1;
  442. debug($feed->{message});
  443. next;
  444. }
  445. my $content=$res->content;
  446. my $f=eval{XML::Feed->parse(\$content)};
  447. if ($@) {
  448. # One common cause of XML::Feed crashing is a feed
  449. # that contains invalid UTF-8 sequences. Convert
  450. # feed to ascii to try to work around.
  451. $feed->{message}.=" ".sprintf(gettext("(invalid UTF-8 stripped from feed)"));
  452. $f=eval {
  453. $content=Encode::decode_utf8($content, 0);
  454. XML::Feed->parse(\$content)
  455. };
  456. }
  457. if ($@) {
  458. # Another possibility is badly escaped entities.
  459. $feed->{message}.=" ".sprintf(gettext("(feed entities escaped)"));
  460. $content=~s/\&(?!amp)(\w+);/&amp;$1;/g;
  461. $f=eval {
  462. $content=Encode::decode_utf8($content, 0);
  463. XML::Feed->parse(\$content)
  464. };
  465. }
  466. if ($@) {
  467. $feed->{message}=gettext("feed crashed XML::Feed!")." ($@)";
  468. $feed->{error}=1;
  469. debug($feed->{message});
  470. next;
  471. }
  472. if (! $f) {
  473. $feed->{message}=XML::Feed->errstr;
  474. $feed->{error}=1;
  475. debug($feed->{message});
  476. next;
  477. }
  478. foreach my $entry ($f->entries) {
  479. my $c=$entry->content;
  480. # atom feeds may have no content, only a summary
  481. if (! defined $c && ref $entry->summary) {
  482. $c=$entry->summary;
  483. }
  484. add_page(
  485. feed => $feed,
  486. copyright => $f->copyright,
  487. title => defined $entry->title ? decode_entities($entry->title) : "untitled",
  488. link => $entry->link,
  489. content => (defined $c && defined $c->body) ? $c->body : "",
  490. guid => defined $entry->id ? $entry->id : time."_".$feed->{name},
  491. ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
  492. base => (defined $c && $c->can("base")) ? $c->base : undef,
  493. );
  494. }
  495. }
  496. }
  497. sub add_page (@) {
  498. my %params=@_;
  499. my $feed=$params{feed};
  500. my $guid={};
  501. my $mtime;
  502. if (exists $guids{$params{guid}}) {
  503. # updating an existing post
  504. $guid=$guids{$params{guid}};
  505. return if $guid->{expired};
  506. }
  507. else {
  508. # new post
  509. $guid->{guid}=$params{guid};
  510. $guids{$params{guid}}=$guid;
  511. $mtime=$params{ctime};
  512. $feed->{numposts}++;
  513. $feed->{newposts}++;
  514. # assign it an unused page
  515. my $page=titlepage($params{title});
  516. # escape slashes and periods in title so it doesn't specify
  517. # directory name or trigger ".." disallowing code.
  518. $page=~s!([/.])!"__".ord($1)."__"!eg;
  519. $page=$feed->{dir}."/".$page;
  520. ($page)=$page=~/$config{wiki_file_regexp}/;
  521. if (! defined $page || ! length $page) {
  522. $page=$feed->{dir}."/item";
  523. }
  524. my $c="";
  525. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  526. -e "$config{srcdir}/".htmlfn($page.$c)) {
  527. $c++
  528. }
  529. # Make sure that the file name isn't too long.
  530. # NB: This doesn't check for path length limits.
  531. my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
  532. if (defined $max && length(htmlfn($page)) >= $max) {
  533. $c="";
  534. $page=$feed->{dir}."/item";
  535. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  536. -e "$config{srcdir}/".htmlfn($page.$c)) {
  537. $c++
  538. }
  539. }
  540. $guid->{page}=$page;
  541. debug(sprintf(gettext("creating new page %s"), $page));
  542. }
  543. $guid->{feed}=$feed->{name};
  544. # To write or not to write? Need to avoid writing unchanged pages
  545. # to avoid unneccessary rebuilding. The mtime from rss cannot be
  546. # trusted; let's use a digest.
  547. eval q{use Digest::MD5 'md5_hex'};
  548. error($@) if $@;
  549. require Encode;
  550. my $digest=md5_hex(Encode::encode_utf8($params{content}));
  551. return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
  552. $guid->{md5}=$digest;
  553. # Create the page.
  554. my $template=template($feed->{template}, blind_cache => 1);
  555. $template->param(title => $params{title})
  556. if defined $params{title} && length($params{title});
  557. $template->param(content => wikiescape(htmlabs($params{content},
  558. defined $params{base} ? $params{base} : $feed->{feedurl})));
  559. $template->param(name => $feed->{name});
  560. $template->param(url => $feed->{url});
  561. $template->param(copyright => $params{copyright})
  562. if defined $params{copyright} && length $params{copyright};
  563. $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
  564. if defined $params{link};
  565. if (ref $feed->{tags}) {
  566. $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
  567. }
  568. writefile(htmlfn($guid->{page}), $config{srcdir},
  569. $template->output);
  570. if (defined $mtime && $mtime <= time) {
  571. # Set the mtime, this lets the build process get the right
  572. # creation time on record for the new page.
  573. utime $mtime, $mtime, "$config{srcdir}/".htmlfn($guid->{page});
  574. # Store it in pagectime for expiry code to use also.
  575. $IkiWiki::pagectime{$guid->{page}}=$mtime;
  576. }
  577. else {
  578. # Dummy value for expiry code.
  579. $IkiWiki::pagectime{$guid->{page}}=time;
  580. }
  581. }
  582. sub wikiescape ($) {
  583. # escape accidental wikilinks and preprocessor stuff
  584. return encode_entities(shift, '\[\]');
  585. }
  586. sub urlabs ($$) {
  587. my $url=shift;
  588. my $urlbase=shift;
  589. URI->new_abs($url, $urlbase)->as_string;
  590. }
  591. sub htmlabs ($$) {
  592. # Convert links in html from relative to absolute.
  593. # Note that this is a heuristic, which is not specified by the rss
  594. # spec and may not be right for all feeds. Also, see Debian
  595. # bug #381359.
  596. my $html=shift;
  597. my $urlbase=shift;
  598. my $ret="";
  599. my $p = HTML::Parser->new(api_version => 3);
  600. $p->handler(default => sub { $ret.=join("", @_) }, "text");
  601. $p->handler(start => sub {
  602. my ($tagname, $pos, $text) = @_;
  603. if (ref $HTML::Tagset::linkElements{$tagname}) {
  604. while (4 <= @$pos) {
  605. # use attribute sets from right to left
  606. # to avoid invalidating the offsets
  607. # when replacing the values
  608. my($k_offset, $k_len, $v_offset, $v_len) =
  609. splice(@$pos, -4);
  610. my $attrname = lc(substr($text, $k_offset, $k_len));
  611. next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
  612. next unless $v_offset; # 0 v_offset means no value
  613. my $v = substr($text, $v_offset, $v_len);
  614. $v =~ s/^([\'\"])(.*)\1$/$2/;
  615. my $new_v=urlabs($v, $urlbase);
  616. $new_v =~ s/\"/&quot;/g; # since we quote with ""
  617. substr($text, $v_offset, $v_len) = qq("$new_v");
  618. }
  619. }
  620. $ret.=$text;
  621. }, "tagname, tokenpos, text");
  622. $p->parse($html);
  623. $p->eof;
  624. return $ret;
  625. }
  626. sub htmlfn ($) {
  627. return shift().".".($config{aggregateinternal} ? "_aggregated" : $config{htmlext});
  628. }
  629. my $aggregatelock;
  630. sub lockaggregate () {
  631. # Take an exclusive lock to prevent multiple concurrent aggregators.
  632. # Returns true if the lock was aquired.
  633. if (! -d $config{wikistatedir}) {
  634. mkdir($config{wikistatedir});
  635. }
  636. open($aggregatelock, '>', "$config{wikistatedir}/aggregatelock") ||
  637. error ("cannot open to $config{wikistatedir}/aggregatelock: $!");
  638. if (! flock($aggregatelock, 2 | 4)) { # LOCK_EX | LOCK_NB
  639. close($aggregatelock) || error("failed closing aggregatelock: $!");
  640. return 0;
  641. }
  642. return 1;
  643. }
  644. sub unlockaggregate () {
  645. return close($aggregatelock) if $aggregatelock;
  646. return;
  647. }
  648. 1