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