all: Use ->remove() instead of ->outertext = ''

simplehtmldom 1.9 introduced new functions to recursively remove
nodes from the DOM. This allows removing elements without the need
to re-load the document by using $html->load($html->save()), which
is very inefficient.

Find more information about remove() at
https://simplehtmldom.sourceforge.io/docs/1.9/api/simple_html_dom_node/remove/
This commit is contained in:
logmanoriginal 2019-06-01 21:15:30 +02:00
parent 014b698f67
commit 052844f5e1
21 changed files with 44 additions and 49 deletions

View file

@ -50,18 +50,18 @@ class AsahiShimbunAJWBridge extends BridgeAbstract {
$e_lead = $element->find('span.Lead', 0);
if ($e_lead) {
$item['content'] = $e_lead->innertext;
$e_lead->outertext = '';
$e_lead->remove();
} else {
$item['content'] = $element->innertext;
}
$e_date = $element->find('span.EnDate', 0);
if ($e_date) {
$item['timestamp'] = strtotime($e_date->innertext);
$e_date->outertext = '';
$e_date->remove();
}
$e_video = $element->find('span.EnVideo', 0);
if ($e_video) {
$e_video->outertext = '';
$e_video->remove();
$element->innertext = "VIDEO: $element->innertext";
}
$item['title'] = $element->innertext;

View file

@ -55,7 +55,7 @@ class BundesbankBridge extends BridgeAbstract {
$title = $study->find('.teasable__title div.h2', 0);
foreach($title->children as &$child) {
$child->outertext = '';
$child->remove();
}
$item['title'] = $title->innertext;

View file

@ -58,7 +58,7 @@ class CastorusBridge extends BridgeAbstract {
returnServerError('Cannot find nodes!');
foreach($nodes as $node) {
$node->outertext = '';
$node->remove();
}
return strtotime($activity->innertext);

View file

@ -50,7 +50,7 @@ class DauphineLibereBridge extends FeedExpander {
private function extractContent($url){
$html2 = getSimpleHTMLDOMCached($url);
foreach ($html2->find('.noprint, link, script, iframe, .shareTool, .contentInfo') as $remove) {
$remove->outertext = '';
$remove->remove();
}
return $html2->find('div.content', 0)->innertext;
}

View file

@ -29,16 +29,16 @@ class EconomistBridge extends BridgeAbstract {
// Remove newsletter subscription box
$newsletter = $content->find('div[class="newsletter-form__message"]', 0);
if ($newsletter)
$newsletter->outertext = '';
$newsletter->remove();
$newsletterForm = $content->find('form', 0);
if ($newsletterForm)
$newsletterForm->outertext = '';
$newsletterForm->remove();
// Remove next and previous article URLs at the bottom
$nextprev = $content->find('div[class="blog-post__next-previous-wrapper"]', 0);
if ($nextprev)
$nextprev->outertext = '';
$nextprev->remove();
$section = [ $article->find('h3[itemprop="articleSection"]', 0)->plaintext ];

View file

@ -584,7 +584,7 @@ EOD;
foreach($content_filters as $filter) {
foreach($content->find($filter) as $subject) {
$subject->outertext = '';
$subject->remove();
}
}

View file

@ -50,7 +50,7 @@ class HaveIBeenPwnedBridge extends BridgeAbstract {
$permalink = $breach->find('p', 1)->find('a', 0)->href;
// Remove permalink
$breach->find('p', 1)->find('a', 0)->outertext = '';
$breach->find('p', 1)->find('a', 0)->remove();
$item['title'] = $breach->find('h3', 0)->plaintext . ' - ' . $accounts[1] . ' breached accounts';
$item['dateAdded'] = strtotime($dateAdded[1]);

View file

@ -239,16 +239,16 @@ class JustETFBridge extends BridgeAbstract {
or returnServerError('Article body not found!');
// Remove teaser image
$element->find('img.teaser-img', 0)->outertext = '';
$element->find('img.teaser-img', 0)->remove();
// Remove self advertisements
foreach($element->find('.call-action') as $adv) {
$adv->outertext = '';
$adv->remove();
}
// Remove tips
foreach($element->find('.panel-edu') as $tip) {
$tip->outertext = '';
$tip->remove();
}
// Remove inline scripts (used for i.e. interactive graphs) as they are
@ -318,7 +318,7 @@ class JustETFBridge extends BridgeAbstract {
$description = $description->parent();
foreach($description->find('div') as $div) {
$div->outertext = '';
$div->remove();
}
$quote = $html->find('div.infobox div.val', 0)

View file

@ -61,7 +61,7 @@ class NextgovBridge extends FeedExpander {
return 'Could not request Nextgov: ' . $url;
$contents = $article->find('div.wysiwyg', 0);
$contents->find('svg.content-tombstone', 0)->outertext = '';
$contents->find('svg.content-tombstone', 0)->remove();
$contents = $contents->innertext;
$contents = stripWithDelimiters($contents, '<div class="ad-container">', '</div>');
$contents = stripWithDelimiters($contents, '<div', '</div>'); //ad outer div

View file

@ -51,7 +51,7 @@ class OsmAndBlogBridge extends BridgeAbstract {
private function cleanupContent($content, ...$removeItems) {
foreach ($removeItems as $obj) {
if ($obj) $obj->outertext = '';
if ($obj) $obj->remove();
}
foreach ($content->find('img') as $obj) {
$obj->src = $this->filterURL($obj->src);

View file

@ -63,7 +63,7 @@ class PikabuBridge extends BridgeAbstract {
foreach($el_to_remove_selectors as $el_to_remove_selector) {
foreach($post->find($el_to_remove_selector) as $el) {
$el->outertext = '';
$el->remove();
}
}

View file

@ -38,20 +38,17 @@ class RadioMelodieBridge extends BridgeAbstract {
$imgs = $textDOM->find('img[src^="http://www.radiomelodie.com/image.php]');
foreach($imgs as $img) {
$img->src = $this->rewriteImage($img->src);
$article->save();
}
// Remove Google Ads
$ads = $article->find('div[class=adInline]');
foreach($ads as $ad) {
$ad->outertext = '';
$article->save();
$ad->remove();
}
// Remove Radio Melodie Logo
$logoHTML = $article->find('div[id=logoArticleRM]', 0);
$logoHTML->outertext = '';
$article->save();
$logoHTML->remove();
$author = $article->find('p[class=AuthorName]', 0)->plaintext;
@ -65,8 +62,7 @@ class RadioMelodieBridge extends BridgeAbstract {
$header = '<img src="' . $picture[0] . '"/>';
// Remove the Date and Author part
$textDOM->find('div[class=AuthorDate]', 0)->outertext = '';
$article->save();
$textDOM->find('div[class=AuthorDate]', 0)->remove();
$text = $textDOM->innertext;
$item['content'] = '<h1>' . $item['title'] . '</h1>' . $date . '<br/>' . $header . $text;
$this->items[] = $item;

View file

@ -48,7 +48,7 @@ class SIMARBridge extends BridgeAbstract {
foreach($e_item->find('p') as $paragraph) {
/* Remove empty paragraphs */
if (preg_match('/^(\W|&nbsp;)+$/', $paragraph->innertext) == 1) {
$paragraph->outertext = '';
$paragraph->remove();
}
}
if ($e_item) {

View file

@ -18,7 +18,7 @@ class ScmbBridge extends BridgeAbstract {
$item['title'] = $article->find('header h1 a', 0)->innertext;
// remove text "En savoir plus" from anecdote content
$article->find('span.read-more', 0)->outertext = '';
$article->find('span.read-more', 0)->remove();
$content = $article->find('p.summary a', 0)->innertext;
// remove superfluous spaces at the end

View file

@ -171,7 +171,7 @@ class TwitterBridge extends BridgeAbstract {
// remove 'invisible' content
foreach($tweet->find('.invisible') as $invisible) {
$invisible->outertext = '';
$invisible->remove();
}
// Skip protmoted tweets

View file

@ -62,9 +62,8 @@ class VkBridge extends BridgeAbstract
$this->pageName = htmlspecialchars_decode($pageName);
}
foreach ($html->find('div.replies') as $comment_block) {
$comment_block->outertext = '';
$comment_block->remove();
}
$html->load($html->save());
$pinned_post_item = null;
$last_post_id = 0;
@ -82,7 +81,7 @@ class VkBridge extends BridgeAbstract
if (is_object($post->find('a.wall_post_more', 0))) {
//delete link "show full" in content
$post->find('a.wall_post_more', 0)->outertext = '';
$post->find('a.wall_post_more', 0)->remove();
}
$content_suffix = '';
@ -114,7 +113,7 @@ class VkBridge extends BridgeAbstract
foreach($external_link_selectors_to_remove as $sel) {
if (is_object($post->find($sel, 0))) {
$post->find($sel, 0)->outertext = '';
$post->find($sel, 0)->remove();
}
}
@ -140,7 +139,7 @@ class VkBridge extends BridgeAbstract
$content_suffix .= "<br><img src='" . $matches[1] . "'>";
}
$content_suffix .= "<br>Article: <a href='$article_link'>$article_title ($article_author)</a>";
$article->outertext = '';
$article->remove();
}
// get video on post
@ -150,7 +149,7 @@ class VkBridge extends BridgeAbstract
$video_title = $video->find('div.post_video_title', 0)->plaintext;
$video_link = $video->find('a.lnk', 0)->getAttribute('href');
$this->appendVideo($video_title, $video_link, $content_suffix, $post_videos);
$video->outertext = '';
$video->remove();
$main_video_link = $video_link;
}
@ -161,14 +160,14 @@ class VkBridge extends BridgeAbstract
if (count($temp) > 1) $video_title = $temp[1];
$video_link = $a->getAttribute('href');
if ($video_link != $main_video_link) $this->appendVideo($video_title, $video_link, $content_suffix, $post_videos);
$a->outertext = '';
$a->remove();
}
// get all photos
foreach($post->find('div.wall_text > a.page_post_thumb_wrap') as $a) {
$result = $this->getPhoto($a);
if ($result == null) continue;
$a->outertext = '';
$a->remove();
$content_suffix .= "<br>$result";
}
@ -177,7 +176,7 @@ class VkBridge extends BridgeAbstract
$a = $el->find('.page_album_link', 0);
$album_title = $a->find('.page_album_title_text', 0)->getAttribute('title');
$album_link = $a->getAttribute('href');
$el->outertext = '';
$el->remove();
$content_suffix .= "<br>Album: <a href='$album_link'>$album_title</a>";
}
@ -200,7 +199,7 @@ class VkBridge extends BridgeAbstract
}
$a->outertext = '';
$a->remove();
}
// get other documents
@ -217,7 +216,7 @@ class VkBridge extends BridgeAbstract
}
$div->outertext = '';
$div->remove();
}
// get polls
@ -227,14 +226,14 @@ class VkBridge extends BridgeAbstract
foreach($div->find('div.page_poll_text') as $poll_stat_title) {
$content_suffix .= '<br>- ' . $poll_stat_title->innertext;
}
$div->outertext = '';
$div->remove();
}
// get sign
$post_author = $pageName;
foreach($post->find('a.wall_signed_by') as $a) {
$post_author = $a->innertext;
$a->outertext = '';
$a->remove();
}
if (is_object($post->find('div.copy_quote', 0))) {
@ -243,7 +242,7 @@ class VkBridge extends BridgeAbstract
}
$copy_quote = $post->find('div.copy_quote', 0);
if ($copy_post_header = $copy_quote->find('div.copy_post_header', 0)) {
$copy_post_header->outertext = '';
$copy_post_header->remove();
}
$copy_quote_content = $copy_quote->innertext;
$copy_quote->outertext = "<br>Reposted: <br>$copy_quote_content";

View file

@ -141,7 +141,7 @@ class WikipediaBridge extends BridgeAbstract {
$anchorFallbackIndex = 0){
// Clean the bottom of the featured article
if ($element->find('div', -1))
$element->find('div', -1)->outertext = '';
$element->find('div', -1)->remove();
// The title and URI of the article can be found in an anchor containing
// the string '...' in most wikis ('full article ...')
@ -202,10 +202,10 @@ class WikipediaBridge extends BridgeAbstract {
// Let's remove a couple of things from the article
$table = $content->find('#toc', 0); // Table of contents
if(!$table === false)
$table->outertext = '';
$table->remove();
foreach($content->find('ol.references') as $reference) // References
$reference->outertext = '';
$reference->remove();
return str_replace('href="/', 'href="' . $this->getURI() . '/', $content->innertext);
}

View file

@ -50,7 +50,7 @@ class WordPressBridge extends FeedExpander {
foreach ($article->find('h1.entry-title') as $title)
if ($title->plaintext == $item['title'])
$title->outertext = '';
$title->remove();
$article_image = $article_html->find('img.wp-post-image', 0);
if(!empty($item['content']) && (!is_object($article_image) || empty($article_image->src))) {

View file

@ -44,7 +44,7 @@ class WorldOfTanksBridge extends FeedExpander {
// Remove the scripts, please
foreach($content->find('script') as $script) {
$script->outertext = '';
$script->remove();
}
return $content->innertext;

View file

@ -193,7 +193,7 @@ class XenForoBridge extends BridgeAbstract {
// Remove script tags
foreach($content->find('script') as $script) {
$script->outertext = '';
$script->remove();
}
$item['content'] = $content->innertext;

View file

@ -36,7 +36,7 @@ function sanitize($html,
if(in_array($element->tag, $text_to_keep)) {
$element->outertext = $element->plaintext;
} elseif(in_array($element->tag, $tags_to_remove)) {
$element->outertext = '';
$element->remove();
} else {
foreach($element->getAllAttributes() as $attributeName => $attribute) {
if(!in_array($attributeName, $attributes_to_keep))