2016-09-05 18:05:19 +02:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
|
|
|
/**
|
|
|
|
* Extension of BridgeAbstract allowing caching of files downloaded over http.
|
|
|
|
* TODO allow file cache invalidation by touching files on access, and removing
|
|
|
|
* files/directories which have not been touched since ... a long time
|
|
|
|
*/
|
|
|
|
abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintain locally cached versions of pages to download, to avoid multiple downloads.
|
|
|
|
* @param url url to cache
|
2016-09-09 22:14:49 +02:00
|
|
|
* @param duration duration of the cache file in seconds (default: 24h/86400s)
|
2016-09-05 18:05:19 +02:00
|
|
|
* @return content of the file as string
|
|
|
|
*/
|
2016-09-09 22:14:49 +02:00
|
|
|
public function get_cached($url, $duration = 86400){
|
2016-09-09 22:23:40 +02:00
|
|
|
$filepath = $this->buildCacheFilePath($url);
|
2016-09-05 18:05:19 +02:00
|
|
|
|
2016-09-09 22:14:49 +02:00
|
|
|
if(file_exists($filepath) && filectime($filepath) < time() - $duration){
|
|
|
|
$this->debugMessage('Cache file ' . $filepath . ' exceeded duration of ' . $duration . ' seconds.');
|
|
|
|
unlink ($filepath);
|
|
|
|
$this->debugMessage('Cached file deleted: ' . $filepath);
|
|
|
|
}
|
|
|
|
|
2016-09-05 18:05:19 +02:00
|
|
|
if(file_exists($filepath)){
|
|
|
|
$this->debugMessage('loading cached file from ' . $filepath . ' for page at url ' . $url);
|
|
|
|
// TODO touch file and its parent, and try to do neighbour deletion
|
2016-09-09 22:23:40 +02:00
|
|
|
$this->refresh_in_cache($filepath);
|
2016-09-05 18:05:19 +02:00
|
|
|
$content = file_get_contents($filepath);
|
|
|
|
} else {
|
|
|
|
$this->debugMessage('we have no local copy of ' . $url . ' Downloading to ' . $filepath);
|
|
|
|
$dir = substr($filepath, 0, strrpos($filepath, '/'));
|
|
|
|
|
|
|
|
if(!is_dir($dir)){
|
|
|
|
$this->debugMessage('creating directories for ' . $dir);
|
|
|
|
mkdir($dir, 0777, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $this->getContents($url);
|
|
|
|
if($content !== false){
|
|
|
|
file_put_contents($filepath, $content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_get_html($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_cached_time($url){
|
2016-09-09 22:23:40 +02:00
|
|
|
$filepath = $this->buildCacheFilePath($url);
|
2016-09-05 18:05:19 +02:00
|
|
|
|
|
|
|
if(!file_exists($filepath)){
|
|
|
|
$this->get_cached($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filectime($filepath);
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:23:40 +02:00
|
|
|
private function refresh_in_cache($filepath){
|
|
|
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
2016-09-05 18:05:19 +02:00
|
|
|
$currentPath = $filepath;
|
|
|
|
while(!$cacheDir == $currentPath){
|
|
|
|
touch($currentPath);
|
|
|
|
$currentPath = dirname($currentPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:23:40 +02:00
|
|
|
private function buildCacheFilePath($url){
|
|
|
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
|
|
|
|
2016-09-05 18:05:19 +02:00
|
|
|
$simplified_url = str_replace(
|
|
|
|
['http://', 'https://', '?', '&', '='],
|
|
|
|
['', '', '/', '/', '/'],
|
|
|
|
$url);
|
|
|
|
|
|
|
|
if(substr($cacheDir, -1) !== '/'){
|
|
|
|
$cacheDir .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$filepath = $cacheDir . $simplified_url;
|
|
|
|
|
|
|
|
if(substr($filepath, -1) === '/'){
|
|
|
|
$filepath .= 'index.html';
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:23:40 +02:00
|
|
|
return realpath($filepath);
|
2016-09-05 18:05:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remove_from_cache($url){
|
2016-09-09 22:23:40 +02:00
|
|
|
$filepath = $this->buildCacheFilePath($url);
|
2016-09-05 18:05:19 +02:00
|
|
|
$this->debugMessage('removing from cache \'' . $filepath . '\'');
|
|
|
|
unlink($filepath);
|
|
|
|
}
|
|
|
|
}
|