From 9f2dd4868469a4362cd0ec8ef1aeae0ff9bf2b04 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 10 Sep 2016 19:04:01 +0200 Subject: [PATCH] [BridgeAbstract] Add getSimpleHTMLDOMCached This function is a copy of the get_cached function from HttpCachingBridgeAbstract, adding all parameters of getSimpleHTMLDOM in order to replace the need of HttpCachingBridgeAbstract entirely --- lib/BridgeAbstract.php | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index f58e0566..abcda79d 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -386,4 +386,60 @@ abstract class BridgeAbstract implements BridgeInterface { , $defaultBRText , $defaultSpanText); } + + /** + * Maintain locally cached versions of pages to avoid multiple downloads. + * @param url url to cache + * @param duration duration of the cache file in seconds (default: 24h/86400s) + * @return content of the file as string + */ + public function getSimpleHTMLDOMCached($url + , $duration = 86400 + , $use_include_path = false + , $context = null + , $offset = 0 + , $maxLen = null + , $lowercase = true + , $forceTagsClosed = true + , $target_charset = DEFAULT_TARGET_CHARSET + , $stripRN = true + , $defaultBRText = DEFAULT_BR_TEXT + , $defaultSpanText = DEFAULT_SPAN_TEXT){ + $this->debugMessage('Caching url ' . $url . ', duration ' . $duration); + + $filepath = __DIR__ . '/../cache/pages/' . sha1($url) . '.cache'; + $this->debugMessage('Cache file ' . $filepath); + + if(file_exists($filepath) && filectime($filepath) < time() - $duration){ + unlink ($filepath); + $this->debugMessage('Cached file deleted: ' . $filepath); + } + + if(file_exists($filepath)){ + $this->debugMessage('Loading cached file ' . $filepath); + touch($filepath); + $content = file_get_contents($filepath); + } else { + $this->debugMessage('Caching ' . $url . ' to ' . $filepath); + $dir = substr($filepath, 0, strrpos($filepath, '/')); + + if(!is_dir($dir)){ + $this->debugMessage('Creating directory ' . $dir); + mkdir($dir, 0777, true); + } + + $content = $this->getContents($url, $use_include_path, $context, $offset, $maxLen); + if($content !== false){ + file_put_contents($filepath, $content); + } + } + + return str_get_html($content + , $lowercase + , $forceTagsClosed + , $target_charset + , $stripRN + , $defaultBRText + , $defaultSpanText); + } }