[Bridge] Cleanup file

- Remove unnecessary documentation
- Update/Clarify documentation where necessary
- Remove empty lines
- Put 'else' between closing and opening curly braces
- Make sure curly braces start right after closing brace on functions '(){...'
- Start lines with '.' and use proper indentation when using multi-line string combinations
- Add spaces for function definitions/calls and assignments
- Add space before opening curly brace after class definition 'class xyz {'
This commit is contained in:
logmanoriginal 2016-08-24 20:14:23 +02:00
parent a43703d32d
commit 429126e18a

View file

@ -1,9 +1,4 @@
<?php <?php
/**
* All bridge logic
* Note : adapter are store in other place
*/
interface BridgeInterface { interface BridgeInterface {
public function collectData(array $param); public function collectData(array $param);
public function getCacheDuration(); public function getCacheDuration();
@ -24,9 +19,6 @@ abstract class BridgeAbstract implements BridgeInterface{
public $useProxy = true; public $useProxy = true;
public $parameters = array(); public $parameters = array();
/**
* Launch probative exception
*/
protected function returnError($message, $code){ protected function returnError($message, $code){
throw new \HttpException($message, $code); throw new \HttpException($message, $code);
} }
@ -40,65 +32,49 @@ abstract class BridgeAbstract implements BridgeInterface{
} }
/** /**
* Return datas stored in the bridge * Return items stored in the bridge
* @return mixed * @return mixed
*/ */
public function getDatas(){ public function getDatas(){
return $this->items; return $this->items;
} }
/** /**
* Defined datas with parameters depending choose bridge * Defined datas with parameters depending choose bridge
* Note : you can define a cache before with "setCache" * Note : you can define a cache with "setCache"
* @param array $param $_REQUEST, $_GET, $_POST, or array with bridge expected paramters * @param array $param $_REQUEST, $_GET, $_POST, or array with bridge expected paramters
*/ */
public function setDatas(array $param){ public function setDatas(array $param){
if(!is_null($this->cache)){ if(!is_null($this->cache)){
$this->cache->prepare($param); $this->cache->prepare($param);
$time = $this->cache->getTime(); $time = $this->cache->getTime();
} } else {
else{ $time = false;
$time = false; // No cache ? No time !
} }
if( $time !== false && ( time() - $this->getCacheDuration() < $time ) ){ // Cache file has not expired. Serve it. if($time !== false && (time() - $this->getCacheDuration() < $time)){
$this->items = $this->cache->loadData(); $this->items = $this->cache->loadData();
} } else {
else{
$this->collectData($param); $this->collectData($param);
if( !is_null($this->cache) ){ // Cache defined ? We go to refresh is memory :D if(!is_null($this->cache)){
$this->cache->saveData($this->getDatas()); $this->cache->saveData($this->getDatas());
} }
} }
} }
/**
* Define default bridge name
*/
public function getName(){ public function getName(){
return $this->name; return $this->name;
} }
/**
* Define default bridge URI
*/
public function getURI(){ public function getURI(){
return $this->uri; return $this->uri;
} }
/**
* Define default duraction for cache
*/
public function getCacheDuration(){ public function getCacheDuration(){
return 3600; return 3600;
} }
/**
* Defined cache object to use
*/
public function setCache(\CacheAbstract $cache){ public function setCache(\CacheAbstract $cache){
$this->cache = $cache; $this->cache = $cache;
@ -109,11 +85,15 @@ abstract class BridgeAbstract implements BridgeInterface{
if(!file_exists('DEBUG')) { if(!file_exists('DEBUG')) {
return; return;
} }
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$calling = $backtrace[2]; $calling = $backtrace[2];
$message = $calling['file'].':'.$calling['line'] $message = $calling['file'] . ':'
.' class '.get_class($this).'->'.$calling['function'] . $calling['line'] . ' class '
.' - '.$text; . get_class($this) . '->'
. $calling['function'] . ' - '
. $text;
error_log($message); error_log($message);
} }
@ -134,7 +114,7 @@ abstract class BridgeAbstract implements BridgeInterface{
$prevContext=$context; $prevContext=$context;
if(!stream_context_set_option($context, $contextOptions)){ if(!stream_context_set_option($context, $contextOptions)){
$context = $prevContext; $context = $prevContext;
}; }
} }
} }
@ -144,9 +124,9 @@ abstract class BridgeAbstract implements BridgeInterface{
$content = @file_get_contents($url, $use_include_path, $context, $offset, $maxlen); $content = @file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
} }
if($content===false){ if($content === false)
$this->message('Cant\'t download ' . $url); $this->message('Cant\'t download ' . $url);
}
return $content; return $content;
} }
@ -154,33 +134,31 @@ abstract class BridgeAbstract implements BridgeInterface{
$content = $this->getContents($url, $use_include_path, $context, $offset, $maxLen); $content = $this->getContents($url, $use_include_path, $context, $offset, $maxLen);
return str_get_html($content, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText); return str_get_html($content, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
} }
} }
/** /**
* Extension of BridgeAbstract allowing caching of files downloaded over http files. * Extension of BridgeAbstract allowing caching of files downloaded over http.
* This is specially useful for sites from Gawker or Liberation networks, which allow pages excerpts top be viewed together on index, while full pages have to be downloaded * TODO allow file cache invalidation by touching files on access, and removing
* separately. * files/directories which have not been touched since ... a long time
* This class mainly provides a get_cached method which will will download the file from its remote location.
* TODO allow file cache invalidation by touching files on access, and removing files/directories which have not been touched since ... a long time
* After all, rss-bridge is not respaw, isn't it ?
*/ */
abstract class HttpCachingBridgeAbstract extends BridgeAbstract { abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
/** /**
* Maintain locally cached versions of pages to download to avoid multiple doiwnloads. * Maintain locally cached versions of pages to download, to avoid multiple downloads.
* A file name is generated by replacing all "/" by "_", and the file is saved below this bridge cache
* @param url url to cache * @param url url to cache
* @return content of file as string * @return content of the file as string
*/ */
public function get_cached($url){ public function get_cached($url){
$simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url); $simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url);
// TODO build this from the variable given to Cache // TODO build this from the variable given to Cache
$pageCacheDir = __DIR__ . '/../cache/'.'pages/'; $pageCacheDir = __DIR__ . '/../cache/pages/';
$filename = $pageCacheDir . $simplified_url; $filename = $pageCacheDir . $simplified_url;
if(substr($filename, -1) == '/'){ if(substr($filename, -1) == '/'){
$filename = $filename . 'index.html'; $filename = $filename . 'index.html';
} }
if(file_exists($filename)){ if(file_exists($filename)){
$this->message('loading cached file from ' . $filename . ' for page at url ' . $url); $this->message('loading cached file from ' . $filename . ' for page at url ' . $url);
// TODO touch file and its parent, and try to do neighbour deletion // TODO touch file and its parent, and try to do neighbour deletion
@ -189,29 +167,36 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
} else { } else {
$this->message('we have no local copy of ' . $url . ' Downloading to ' . $filename); $this->message('we have no local copy of ' . $url . ' Downloading to ' . $filename);
$dir = substr($filename, 0, strrpos($filename, '/')); $dir = substr($filename, 0, strrpos($filename, '/'));
if(!is_dir($dir)){ if(!is_dir($dir)){
$this->message('creating directories for ' . $dir); $this->message('creating directories for ' . $dir);
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
} }
$content = $this->getContents($url); $content = $this->getContents($url);
if($content!==false){ if($content!==false){
file_put_contents($filename,$content); file_put_contents($filename,$content);
} }
} }
return $content; return $content;
} }
public function get_cached_time($url){ public function get_cached_time($url){
$simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url); $simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url);
// TODO build this from the variable given to Cache // TODO build this from the variable given to Cache
$pageCacheDir = __DIR__ . '/../cache/'.'pages/'; $pageCacheDir = __DIR__ . '/../cache/pages/';
$filename = $pageCacheDir . $simplified_url; $filename = $pageCacheDir . $simplified_url;
if(substr($filename, -1) == '/'){ if(substr($filename, -1) == '/'){
$filename = $filename . 'index.html'; $filename = $filename . 'index.html';
} }
if(!file_exists($filename)){ if(!file_exists($filename)){
$this->get_cached($url); $this->get_cached($url);
} }
return filectime($filename); return filectime($filename);
} }
@ -225,14 +210,13 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
public function remove_from_cache($url){ public function remove_from_cache($url){
$simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url); $simplified_url = str_replace(['http://', 'https://', '?', '&', '='], ['', '', '/', '/', '/'], $url);
// TODO build this from the variable given to Cache // TODO build this from the variable given to Cache
$pageCacheDir = __DIR__ . '/../cache/'.'pages/'; $pageCacheDir = __DIR__ . '/../cache/pages/';
$filename = realpath($pageCacheDir . $simplified_url); $filename = realpath($pageCacheDir . $simplified_url);
$this->message('removing from cache \'' . $filename . '\' WELL, NOT REALLY'); $this->message('removing from cache \'' . $filename . '\' WELL, NOT REALLY');
// filename is NO GOOD
// unlink($filename); // unlink($filename);
} }
} }
class Bridge { class Bridge {
@ -249,13 +233,10 @@ class Bridge{
* @return true if it is an instantiable bridge, false otherwise. * @return true if it is an instantiable bridge, false otherwise.
*/ */
static public function isInstantiable($nameBridge){ static public function isInstantiable($nameBridge){
$re = new ReflectionClass($nameBridge); $re = new ReflectionClass($nameBridge);
return $re->IsInstantiable(); return $re->IsInstantiable();
} }
/** /**
* Create a new bridge object * Create a new bridge object
* @param string $nameBridge Defined bridge name you want use * @param string $nameBridge Defined bridge name you want use
@ -278,7 +259,7 @@ class Bridge{
if(Bridge::isInstantiable($nameBridge)){ if(Bridge::isInstantiable($nameBridge)){
return new $nameBridge(); return new $nameBridge();
} else { } else {
return FALSE; return false;
} }
} }
@ -309,7 +290,6 @@ class Bridge{
* @return array List of the bridges * @return array List of the bridges
*/ */
static public function listBridges(){ static public function listBridges(){
$pathDirBridge = self::getDir(); $pathDirBridge = self::getDir();
$listBridge = array(); $listBridge = array();
$dirFiles = scandir($pathDirBridge); $dirFiles = scandir($pathDirBridge);
@ -324,16 +304,18 @@ class Bridge{
return $listBridge; return $listBridge;
} }
static function isWhitelisted( $whitelist, $name ) {
if(in_array($name, $whitelist) or in_array($name.'.php', $whitelist) or
// DEPRECATED: the nameBridge notation will be removed in future releases
in_array($name.'Bridge', $whitelist) or in_array($name.'Bridge.php', $whitelist) or
count($whitelist) === 1 and trim($whitelist[0]) === '*')
return TRUE;
else
return FALSE;
}
static function isWhitelisted($whitelist, $name){
if(in_array($name, $whitelist)
or in_array($name . '.php', $whitelist)
or in_array($name . 'Bridge', $whitelist) // DEPRECATED
or in_array($name . 'Bridge.php', $whitelist) // DEPRECATED
or count($whitelist) === 1 and trim($whitelist[0]) === '*'){
return true;
} else {
return false;
}
}
} }
abstract class RssExpander extends HttpCachingBridgeAbstract { abstract class RssExpander extends HttpCachingBridgeAbstract {
@ -342,15 +324,18 @@ abstract class RssExpander extends HttpCachingBridgeAbstract{
if(empty($name)){ if(empty($name)){
$this->returnServerError('There is no $name for this RSS expander'); $this->returnServerError('There is no $name for this RSS expander');
} }
$this->message('Loading from ' . $param['url']); $this->message('Loading from ' . $param['url']);
// Notice WE DO NOT use cache here on purpose : we want a fresh view of the RSS stream each time
$content=$this->getContents($name) or /* Notice we do not use cache here on purpose:
$this->returnServerError('Could not request '.$name); * we want a fresh view of the RSS stream each time
*/
$content = $this->getContents($name) or $this->returnServerError('Could not request ' . $name);
$rssContent = simplexml_load_string($content); $rssContent = simplexml_load_string($content);
$this->message('loaded RSS from ' . $param['url']); $this->message('loaded RSS from ' . $param['url']);
// TODO insert RSS format detection // TODO insert RSS format detection
// we suppose for now, we have some RSS 2.0 // For now we always assume RSS 2.0
$this->collect_RSS_2_0_data($rssContent); $this->collect_RSS_2_0_data($rssContent);
} }
@ -386,5 +371,3 @@ abstract class RssExpander extends HttpCachingBridgeAbstract{
return $this->description; return $this->description;
} }
} }