diff --git a/bridges/AcrimedBridge.php b/bridges/AcrimedBridge.php index 3ca8e80..8b40d1d 100644 --- a/bridges/AcrimedBridge.php +++ b/bridges/AcrimedBridge.php @@ -16,7 +16,7 @@ class AcrimedBridge extends FeedExpander { $articlePage = getSimpleHTMLDOM($newsItem->link); $article = sanitize($articlePage->find('article.article1', 0)->innertext); - $article = defaultImageSrcTo($article, static::URI); + $article = defaultLinkTo($article, static::URI); $item['content'] = $article; return $item; diff --git a/bridges/WorldOfTanksBridge.php b/bridges/WorldOfTanksBridge.php index 1cc41b7..a894f6e 100644 --- a/bridges/WorldOfTanksBridge.php +++ b/bridges/WorldOfTanksBridge.php @@ -63,7 +63,7 @@ class WorldOfTanksBridge extends BridgeAbstract { debugMessage('loading page ' . $item['uri']); $articlePage = getSimpleHTMLDOMCached($item['uri']); $content = $articlePage->find('.l-content', 0); - defaultImageSrcTo($content, self::URI); + defaultLinkTo($content, self::URI); $item['title'] = $content->find('h1', 0)->innertext; $item['content'] = $content->find('.b-content', 0)->innertext; $item['timestamp'] = $content->find('.b-statistic_time', 0)->getAttribute("data-timestamp"); diff --git a/caches/FileCache.php b/caches/FileCache.php index c8e4982..59bced9 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -8,11 +8,13 @@ class FileCache implements CacheInterface { protected $param; public function loadData(){ - return json_decode(file_get_contents($this->getCacheFile()), true); + return unserialize(file_get_contents($this->getCacheFile())); } public function saveData($datas){ - $writeStream = file_put_contents($this->getCacheFile(), json_encode($datas, JSON_PRETTY_PRINT)); + // Notice: We use plain serialize() here to reduce memory footprint on + // large input data. + $writeStream = file_put_contents($this->getCacheFile(), serialize($datas)); if($writeStream === false) { throw new \Exception("Cannot write the cache... Do you have the right permissions ?"); @@ -39,7 +41,7 @@ class FileCache implements CacheInterface { ); foreach($cacheIterator as $cacheFile){ - if(in_array($cacheFile->getBasename(), array('.', '..'))) + if(in_array($cacheFile->getBasename(), array('.', '..', '.gitkeep'))) continue; elseif($cacheFile->isFile()){ if(filemtime($cacheFile->getPathname()) < time() - $duration) @@ -110,6 +112,8 @@ class FileCache implements CacheInterface { throw new \Exception('Call "setParameters" first!'); } - return hash('md5', http_build_query($this->param)) . '.cache'; + // Change character when making incompatible changes to prevent loading + // errors due to incompatible file contents \|/ + return hash('md5', http_build_query($this->param) . 'A') . '.cache'; } } diff --git a/lib/html.php b/lib/html.php index 1b9b5ab..d5f6667 100644 --- a/lib/html.php +++ b/lib/html.php @@ -278,12 +278,21 @@ $keptText = array()){ return $htmlContent; } -function defaultImageSrcTo($content, $server){ +function defaultLinkTo($content, $server){ foreach($content->find('img') as $image){ - if(is_null(strpos($image->src, "http")) - && is_null(strpos($image->src, "//")) - && is_null(strpos($image->src, "data:"))) + if(strpos($image->src, 'http') === false + && strpos($image->src, '//') === false + && strpos($image->src, 'data:') === false) $image->src = $server . $image->src; } + + foreach($content->find('a') as $anchor){ + if(strpos($anchor->href, 'http') === false + && strpos($anchor->href, '//') === false + && strpos($anchor->href, '#') !== 0 + && strpos($anchor->href, '?') !== 0) + $anchor->href = $server . $anchor->href; + } + return $content; }