From a051d38df9094ccb6cac8e62587d08c477cd09df Mon Sep 17 00:00:00 2001 From: bastei Date: Fri, 6 Sep 2013 17:45:52 +0200 Subject: try detect encoding of documents in ArticleEnhancer and convert them --- utility/articleenhancer/articleenhancer.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/utility/articleenhancer/articleenhancer.php b/utility/articleenhancer/articleenhancer.php index 76bb0fa9f..c9b61a135 100644 --- a/utility/articleenhancer/articleenhancer.php +++ b/utility/articleenhancer/articleenhancer.php @@ -60,8 +60,16 @@ abstract class ArticleEnhancer { if(preg_match($regex, $item->getUrl())) { $file = $this->fileFactory->getFile($item->getUrl(), $this->maximumTimeout); + + // convert encoding by detecting charset from header + $contentType = $file->headers['content-type']; + if( preg_match( '/(?<=charset=)[^;]*/', $contentType, $matches ) ) + $body = mb_convert_encoding($file->body, 'HTML-ENTITIES', $matches[0]); + else + $body = $file->body; + $dom = new \DOMDocument(); - @$dom->loadHTML($file->body); + @$dom->loadHTML($body); $xpath = new \DOMXpath($dom); $xpathResult = $xpath->evaluate($search); -- cgit v1.2.3 From e57fe91d2af61367f5eb78cf4051172e8335b8bc Mon Sep 17 00:00:00 2001 From: bastei Date: Fri, 6 Sep 2013 18:40:24 +0200 Subject: unit tests for commit a051d38 --- tests/unit/utility/articleenhancer/ArticleEnhancerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php b/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php index a59bf9485..bb3c9e53d 100644 --- a/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php +++ b/tests/unit/utility/articleenhancer/ArticleEnhancerTest.php @@ -74,6 +74,7 @@ class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { public function testDoesModifiyArticlesThatMatch() { $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = '
@@ -103,6 +104,7 @@ class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { public function testDoesModifiyAllArticlesThatMatch() { $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = '
@@ -132,6 +134,7 @@ class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { public function testModificationHandlesEmptyResults() { $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = '
@@ -159,6 +162,7 @@ class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { public function testModificationDoesNotBreakOnEmptyDom() { $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = ''; $item = new Item(); $item->setUrl('https://www.explosm.net/comics/312'); @@ -181,6 +185,7 @@ class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility { public function testModificationDoesNotBreakOnBrokenDom() { $file = new \stdClass; + $file->headers = array("content-type"=>"text/html; charset=utf-8"); $file->body = '

-- cgit v1.2.3 From a73b7da2ec8ceaf6716ed8e6a3041bfee726f71a Mon Sep 17 00:00:00 2001 From: bastei Date: Fri, 6 Sep 2013 19:06:22 +0200 Subject: curly braces are beautiful --- utility/articleenhancer/articleenhancer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utility/articleenhancer/articleenhancer.php b/utility/articleenhancer/articleenhancer.php index c9b61a135..e0d60d4c4 100644 --- a/utility/articleenhancer/articleenhancer.php +++ b/utility/articleenhancer/articleenhancer.php @@ -63,10 +63,11 @@ abstract class ArticleEnhancer { // convert encoding by detecting charset from header $contentType = $file->headers['content-type']; - if( preg_match( '/(?<=charset=)[^;]*/', $contentType, $matches ) ) + if( preg_match( '/(?<=charset=)[^;]*/', $contentType, $matches ) ) { $body = mb_convert_encoding($file->body, 'HTML-ENTITIES', $matches[0]); - else + } else { $body = $file->body; + } $dom = new \DOMDocument(); @$dom->loadHTML($body); -- cgit v1.2.3