Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP Error/Warnning #279

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Extractor/ContentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ public function process(string $html, string $url, SiteConfig $siteConfig = null
$this->readability = $this->getReadability($html, $url, $parser, $this->siteConfig->tidy() && $smartTidy);
$tidied = $this->readability->tidied;

$this->logger->info('Body size after Readability: {length}', ['length' => \strlen((string) $this->readability->dom->saveXML())]);
$this->logger->debug('Body after Readability', ['dom_saveXML' => $this->readability->dom->saveXML()]);
$this->logger->info('Body size after Readability: {length}', ['length' => \strlen((string) $this->readability->dom->saveXML($this->readability->dom->documentElement))]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If https://www.php.net/manual/en/domdocument.savexml.php#44422 is correct, this would just save a page from unknown encoding as UTF-8. No idea how would it try to do that, possibly leading to some corruption.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but that's not really a problem as it's inside the log, maybe the log will only be unreadable?

$this->logger->debug('Body after Readability', ['dom_saveXML' => $this->readability->dom->saveXML($this->readability->dom->documentElement)]);

// we use xpath to find elements in the given HTML document
$this->xpath = new \DOMXPath($this->readability->dom);
Expand Down Expand Up @@ -373,11 +373,11 @@ public function process(string $html, string $url, SiteConfig $siteConfig = null

$this->removeElements($elems, 'Stripping {length} empty a elements');

$this->logger->debug('DOM after site config stripping', ['dom_saveXML' => $this->readability->dom->saveXML()]);
$this->logger->debug('DOM after site config stripping', ['dom_saveXML' => $this->readability->dom->saveXML($this->readability->dom->documentElement)]);

// try to get body
foreach ($this->siteConfig->body as $pattern) {
$this->logger->info('Trying {pattern} for body (content length: {content_length})', ['pattern' => $pattern, 'content_length' => \strlen((string) $this->readability->dom->saveXML())]);
$this->logger->info('Trying {pattern} for body (content length: {content_length})', ['pattern' => $pattern, 'content_length' => \strlen((string) $this->readability->dom->saveXML($this->readability->dom->documentElement))]);

$res = $this->extractBody(
true,
Expand Down
13 changes: 9 additions & 4 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ public function mergeConfig(SiteConfig $currentConfig, SiteConfig $newConfig)
// find_string: <other-img
// replace_string: <img
// To fix that issue, we combine find & replace as key & value in one array, we merge them and then rebuild find & replace string in the current config

// merge http_header array from currentConfig into newConfig
// because final values override former values in case of named keys
$currentConfig->http_header = array_merge($newConfig->http_header, $currentConfig->http_header);

if (\count($currentConfig->find_string) !== \count($currentConfig->replace_string)) {
return $currentConfig;
}
Comment on lines +335 to +337
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird to me. Sohuld not we rather return $newConfig? And should not we check the variables from $newConfig? I would assume the $currentConfig has been already validated upon construction.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll move that check in parseLines instead


girishpanchal30 marked this conversation as resolved.
Show resolved Hide resolved
$findReplaceCurrentConfig = array_combine($currentConfig->find_string, $currentConfig->replace_string);
$findReplaceNewConfig = array_combine($newConfig->find_string, $newConfig->replace_string);
$findReplaceMerged = array_merge((array) $findReplaceCurrentConfig, (array) $findReplaceNewConfig);
Expand All @@ -340,10 +349,6 @@ public function mergeConfig(SiteConfig $currentConfig, SiteConfig $newConfig)
$currentConfig->replace_string[] = $replaceString;
}

// merge http_header array from currentConfig into newConfig
// because final values override former values in case of named keys
$currentConfig->http_header = array_merge($newConfig->http_header, $currentConfig->http_header);

return $currentConfig;
}

Expand Down