src/Extractor/ContentExtractor.php
private function extractBody($detectBody, $xpathExpression, \DOMNode $node, $type)
    {
        if (false === $detectBody) {
            return false;
        }

        // shut up operator as there is no pre-validation possible.
        $elems = @$this->xpath->query($xpathExpression, $node);

        if (false === $elems || false === $this->hasElements($elems)) {
            return $detectBody;
        }

        $this->logger->info($type . ': found "' . $elems->length . '" with ' . $xpathExpression);

        if (1 === $elems->length) {
            // body can't be an attribute
            if ($elems->item(0) instanceof \DOMAttr) {
                $this->logger->info('Body can not be an attribute');

                return true;
            }

            $this->body = $elems->item(0);

            // prune (clean up elements that may not be content)
            if ($this->siteConfig->prune()) {
                $this->logger->info('Pruning content');
                $this->readability->prepArticle($this->body);
            }

            return false;
        }

        $this->body = $this->readability->dom->createElement('div');
        $this->logger->info('{nb} body elems found', ['nb' => $elems->length]);
        $len = 0;

        foreach ($elems as $elem) {
            if (!isset($elem->parentNode)) {
                continue;
            }

            $isDescendant = false;
            foreach ($this->body->childNodes as $parent) {
                $node = $elem->parentNode;
                while (null !== $node) {
                    if ($node->isSameNode($parent)) {
                        $isDescendant = true;
                        break 2;
                    }
                    $node = $node->parentNode;
                }
            }

            if ($isDescendant) {
                $this->logger->info('...element is child of another body element, skipping.');
            } else {
                // prune (clean up elements that may not be content)
                if ($this->siteConfig->prune()) {
                    $this->logger->info('...pruning content');
                    $this->readability->prepArticle($elem);
                }

                if ($elem) {
                    ++$len;
                    $this->body->appendChild($elem);
                }
            }
        }

        $this->logger->info('...{len} elements added to body', ['len' => $len]);

        return false;
    }
private function extractBody($detectBody, $xpathExpression, \DOMNode $node, $type)
    {
        if (false === $detectBody) {
            return false;
        }

        // shut up operator as there is no pre-validation possible.
        $elems = @$this->xpath->query($xpathExpression, $node);

        if (false === $elems || false === $this->hasElements($elems)) {
            return $detectBody;
        }

        $this->logger->info($type . ': found "' . $elems->length . '" with ' . $xpathExpression);

        if (1 === $elems->length) {
            // body can't be an attribute
            if ($elems->item(0) instanceof \DOMAttr) {
                $this->logger->info('Body can not be an attribute');

                return true;
            }

            $this->body = $elems->item(0);

            // prune (clean up elements that may not be content)
            if ($this->siteConfig->prune()) {
                $this->logger->info('Pruning content');
                $this->readability->prepArticle($this->body);
            }

            return false;
        }

        $this->body = $this->readability->dom->createElement('div');
        $this->logger->info('{nb} body elems found', ['nb' => $elems->length]);
        $len = 0;

        foreach ($elems as $elem) {
            if (!isset($elem->parentNode)) {
                continue;
            }

            $isDescendant = false;
            foreach ($this->body->childNodes as $parent) {
                $node = $elem->parentNode;
                while (null !== $node) {
                    if ($node->isSameNode($parent)) {
                        $isDescendant = true;
                        break 2;
                    }
                    $node = $node->parentNode;
                }
            }

            if ($isDescendant) {
                $this->logger->info('...element is child of another body element, skipping.');
            } else {
                // prune (clean up elements that may not be content)
                if ($this->siteConfig->prune()) {
                    $this->logger->info('...pruning content');
                    $this->readability->prepArticle($elem);
                }

                if ($elem) {
                    ++$len;
                    $this->body->appendChild($elem);
                }
            }
        }

        $this->logger->info('...{len} elements added to body', ['len' => $len]);

        return false;
    }
private function extractBody ( $ detectBody , $ xpathExpression , \ DOMNode $ node , $ type ) { if ( false === $ detectBody ) { return false ; } // shut up operator as there is no pre-validation possible. $ elems = @ $ this -> xpath -> query ( $ xpathExpression , $ node ) ; if ( false === $ elems || false === $ this -> hasElements ( $ elems ) ) { return $ detectBody ; } $ this -> logger -> info ( $ type . ': found "' . $ elems -> length . '" with ' . $ xpathExpression ) ; if ( 1 === $ elems -> length ) { // body can't be an attribute if ( $ elems -> item ( 0 ) instanceof \ DOMAttr ) { $ this -> logger -> info ( 'Body can not be an attribute' ) ; return true ; } $ this -> body = $ elems -> item ( 0 ) ; // prune (clean up elements that may not be content) if ( $ this -> siteConfig -> prune ( ) ) { $ this -> logger -> info ( 'Pruning content' ) ; $ this -> readability -> prepArticle ( $ this -> body ) ; } return false ; } $ this -> body = $ this -> readability -> dom -> createElement ( 'div' ) ; $ this -> logger -> info ( '{nb} body elems found' , [ 'nb' => $ elems -> length ] ) ; $ len = 0 ; foreach ( $ elems as $ elem ) { if ( ! isset ( $ elem -> parentNode ) ) { continue ; } $ isDescendant = false ; foreach ( $ this -> body -> childNodes as $ parent ) { $ node = $ elem -> parentNode ; while ( null !== $ node ) { if ( $ node -> isSameNode ( $ parent ) ) { $ isDescendant = true ; break 2 ; } $ node = $ node -> parentNode ; } } if ( $ isDescendant ) { $ this -> logger -> info ( '...element is child of another body element, skipping.' ) ; } else { // prune (clean up elements that may not be content) if ( $ this -> siteConfig -> prune ( ) ) { $ this -> logger -> info ( '...pruning content' ) ; $ this -> readability -> prepArticle ( $ elem ) ; } if ( $ elem ) { ++ $ len ; $ this -> body -> appendChild ( $ elem ) ; } } } $ this -> logger -> info ( '...{len} elements added to body' , [ 'len' => $ len ] ) ; return false ; }
Extract body from a given CSS for a node.

@param bool     $detectBody      Do we have to detect body ?
@param string   $xpathExpression XPath expression to extract body
@param \DOMNode $node            DOMNode to look into
@param string   $type            Format type we are looking for, only used for log message

@return bool Telling if we have to detect body again or not
Extract body from a given CSS for a node .