summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/aggregate.pm
blob: c9c2880c5737a77bea1b4d268cb468a68b7cbc44 (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 $c=$entry->content;
  473. # atom feeds may have no content, only a summary
  474. if (! defined $c && ref $entry->summary) {
  475. $c=$entry->summary;
  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 $c ? $c->body : "",
  483. guid => defined $entry->id ? $entry->id : time."_".$feed->{name},
  484. ctime => $entry->issued ? ($entry->issued->epoch || time) : time,
  485. base => (defined $c && $c->can("base")) ? $c->base : undef,
  486. );
  487. }
  488. }
  489. } #}}}
  490. sub add_page (@) { #{{{
  491. my %params=@_;
  492. my $feed=$params{feed};
  493. my $guid={};
  494. my $mtime;
  495. if (exists $guids{$params{guid}}) {
  496. # updating an existing post
  497. $guid=$guids{$params{guid}};
  498. return if $guid->{expired};
  499. }
  500. else {
  501. # new post
  502. $guid->{guid}=$params{guid};
  503. $guids{$params{guid}}=$guid;
  504. $mtime=$params{ctime};
  505. $feed->{numposts}++;
  506. $feed->{newposts}++;
  507. # assign it an unused page
  508. my $page=titlepage($params{title});
  509. # escape slashes and periods in title so it doesn't specify
  510. # directory name or trigger ".." disallowing code.
  511. $page=~s!([/.])!"__".ord($1)."__"!eg;
  512. $page=$feed->{dir}."/".$page;
  513. ($page)=$page=~/$config{wiki_file_regexp}/;
  514. if (! defined $page || ! length $page) {
  515. $page=$feed->{dir}."/item";
  516. }
  517. my $c="";
  518. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  519. -e "$config{srcdir}/".htmlfn($page.$c)) {
  520. $c++
  521. }
  522. # Make sure that the file name isn't too long.
  523. # NB: This doesn't check for path length limits.
  524. my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
  525. if (defined $max && length(htmlfn($page)) >= $max) {
  526. $c="";
  527. $page=$feed->{dir}."/item";
  528. while (exists $IkiWiki::pagecase{lc $page.$c} ||
  529. -e "$config{srcdir}/".htmlfn($page.$c)) {
  530. $c++
  531. }
  532. }
  533. $guid->{page}=$page;
  534. debug(sprintf(gettext("creating new page %s"), $page));
  535. }
  536. $guid->{feed}=$feed->{name};
  537. # To write or not to write? Need to avoid writing unchanged pages
  538. # to avoid unneccessary rebuilding. The mtime from rss cannot be
  539. # trusted; let's use a digest.
  540. eval q{use Digest::MD5 'md5_hex'};
  541. error($@) if $@;
  542. require Encode;
  543. my $digest=md5_hex(Encode::encode_utf8($params{content}));
  544. return unless ! exists $guid->{md5} || $guid->{md5} ne $digest || $config{rebuild};
  545. $guid->{md5}=$digest;
  546. # Create the page.
  547. my $template=template($feed->{template}, blind_cache => 1);
  548. $template->param(title => $params{title})
  549. if defined $params{title} && length($params{title});
  550. $template->param(content => htmlescape(htmlabs($params{content},
  551. defined $params{base} ? $params{base} : $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