summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/aggregate.pm
blob: 2e4c86f242ae0673899c6faa330896544218b705 (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->{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 "$config{srcdir}/".htmlfn($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 "$config{srcdir}/".htmlfn($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}} || 0) <=> ($IkiWiki::pagectime{$a->{page}} || 0) }
  379. grep { exists $_->{page} && $_->{feed} eq $feed->{name} }
  380. values %guids) {
  381. if ($feed->{expireage}) {
  382. my $days_old = (time - ($IkiWiki::pagectime{$item->{page}} || 0)) / 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("last checked %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. my $content=$content=$entry->content->body;
  473. # atom feeds may have no content, only a summary
  474. if (! defined $content && ref $entry->summary) {
  475. $content=$entry->summary->body;
  476. }
  477. add_page(
  478. feed => $feed,
  479. copyright => $f->copyright,
  480. title => defined $entry->title ? decode_entities($entry->title) : "untitled",
  481. link => $entry->link,
  482. content => defined $content ? $content : "",
  483. guid => defined $entry->id ? $entry->id : time."_".$feed->{name},
  484. ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
  485. );
  486. }
  487. }
  488. } #}}}
  489. sub add_page (@) { #{{{
  490. my %params=@_;
  491. my $feed=$params{feed};
  492. my $guid={};
  493. my $mtime;
  494. if (exists $guids{$params{guid}}) {
  495. # updating an existing post
  496. $guid=$guids{$params{guid}};
  497. return if $guid->{expired};
  498. }
  499. else {
  500. # new post
  501. $guid->{guid}=$params{guid};
  502. $guids{$params{guid}}=$guid;
  503. $mtime=$params{ctime};
  504. $feed->{numposts}++;
  505. $feed->{newposts}++;
  506. # assign it an unused page
  507. my $page=titlepage($params{title});
  508. # escape slashes and periods in title so it doesn't specify
  509. # directory name or trigger ".." disallowing code.
  510. $page=~s!([/.])!"__".ord($1)."__"!eg;
  511. $page=$feed->{dir}."/".$page;
  512. ($page)=$page=~/$config{wiki_file_regexp}/;
  513. if (! defined $page || ! length $page) {
  514. $page=$feed->{dir}."/item";
  515. }
  516. my $c="";
  517. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  518. -e "$config{srcdir}/".htmlfn($page.$c)) {
  519. $c++
  520. }
  521. # Make sure that the file name isn't too long.
  522. # NB: This doesn't check for path length limits.
  523. my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
  524. if (defined $max && length(htmlfn($page)) >= $max) {
  525. $c="";
  526. $page=$feed->{dir}."/item";
  527. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  528. -e "$config{srcdir}/".htmlfn($page.$c)) {
  529. $c++
  530. }
  531. }
  532. $guid->{page}=$page;
  533. debug(sprintf(gettext("creating new page %s"), $page));
  534. }
  535. $guid->{feed}=$feed->{name};
  536. # To write or not to write? Need to avoid writing unchanged pages
  537. # to avoid unneccessary rebuilding. The mtime from rss cannot be
  538. # trusted; let's use a digest.
  539. eval q{use Digest::MD5 'md5_hex'};
  540. error($@) if $@;
  541. require Encode;
  542. my $digest=md5_hex(Encode::encode_utf8($params{content}));
  543. return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
  544. $guid->{md5}=$digest;
  545. # Create the page.
  546. my $template=template($feed->{template}, blind_cache => 1);
  547. $template->param(title => $params{title})
  548. if defined $params{title} && length($params{title});
  549. $template->param(content => htmlescape(htmlabs($params{content}, $feed->{feedurl})));
  550. $template->param(name => $feed->{name});
  551. $template->param(url => $feed->{url});
  552. $template->param(copyright => $params{copyright})
  553. if defined $params{copyright} && length $params{copyright};
  554. $template->param(permalink => urlabs($params{link}, $feed->{feedurl}))
  555. if defined $params{link};
  556. if (ref $feed->{tags}) {
  557. $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
  558. }
  559. writefile(htmlfn($guid->{page}), $config{srcdir},
  560. $template->output);
  561. if (defined $mtime && $mtime <= time) {
  562. # Set the mtime, this lets the build process get the right
  563. # creation time on record for the new page.
  564. utime $mtime, $mtime, "$config{srcdir}/".htmlfn($guid->{page});
  565. # Store it in pagectime for expiry code to use also.
  566. $IkiWiki::pagectime{$guid->{page}}=$mtime;
  567. }
  568. else {
  569. # Dummy value for expiry code.
  570. $IkiWiki::pagectime{$guid->{page}}=time;
  571. }
  572. } #}}}
  573. sub htmlescape ($) { #{{{
  574. # escape accidental wikilinks and preprocessor stuff
  575. my $html=shift;
  576. $html=~s/(?<!\\)\[\[/\\\[\[/g;
  577. return $html;
  578. } #}}}
  579. sub urlabs ($$) { #{{{
  580. my $url=shift;
  581. my $urlbase=shift;
  582. URI->new_abs($url, $urlbase)->as_string;
  583. } #}}}
  584. sub htmlabs ($$) { #{{{
  585. # Convert links in html from relative to absolute.
  586. # Note that this is a heuristic, which is not specified by the rss
  587. # spec and may not be right for all feeds. Also, see Debian
  588. # bug #381359.
  589. my $html=shift;
  590. my $urlbase=shift;
  591. my $ret="";
  592. my $p = HTML::Parser->new(api_version => 3);
  593. $p->handler(default => sub { $ret.=join("", @_) }, "text");
  594. $p->handler(start => sub {
  595. my ($tagname, $pos, $text) = @_;
  596. if (ref $HTML::Tagset::linkElements{$tagname}) {
  597. while (4 <= @$pos) {
  598. # use attribute sets from right to left
  599. # to avoid invalidating the offsets
  600. # when replacing the values
  601. my($k_offset, $k_len, $v_offset, $v_len) =
  602. splice(@$pos, -4);
  603. my $attrname = lc(substr($text, $k_offset, $k_len));
  604. next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
  605. next unless $v_offset; # 0 v_offset means no value
  606. my $v = substr($text, $v_offset, $v_len);
  607. $v =~ s/^([\'\"])(.*)\1$/$2/;
  608. my $new_v=urlabs($v, $urlbase);
  609. $new_v =~ s/\"/&quot;/g; # since we quote with ""
  610. substr($text, $v_offset, $v_len) = qq("$new_v");
  611. }
  612. }
  613. $ret.=$text;
  614. }, "tagname, tokenpos, text");
  615. $p->parse($html);
  616. $p->eof;
  617. return $ret;
  618. } #}}}
  619. sub htmlfn ($) { #{{{
  620. return shift().".".($config{aggregateinternal} ? "_aggregated" : $config{htmlext});
  621. } #}}}
  622. my $aggregatelock;
  623. sub lockaggregate () { #{{{
  624. # Take an exclusive lock to prevent multiple concurrent aggregators.
  625. # Returns true if the lock was aquired.
  626. if (! -d $config{wikistatedir}) {
  627. mkdir($config{wikistatedir});
  628. }
  629. open($aggregatelock, '>', "$config{wikistatedir}/aggregatelock") ||
  630. error ("cannot open to $config{wikistatedir}/aggregatelock: $!");
  631. if (! flock($aggregatelock, 2 | 4)) { # LOCK_EX | LOCK_NB
  632. close($aggregatelock) || error("failed closing aggregatelock: $!");
  633. return 0;
  634. }
  635. return 1;
  636. } #}}}
  637. sub unlockaggregate () { #{{{
  638. return close($aggregatelock) if $aggregatelock;
  639. return;
  640. } #}}}
  641. 1