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