From 38829e7739c8c16b9d6d419d0695919925b4d6e2 Mon Sep 17 00:00:00 2001 From: teromene Date: Thu, 5 Nov 2015 20:26:48 +0000 Subject: [PATCH] Little refactoring to reduce logic on index.php. Moved RssExpander as a core logic system to lib/Bridge.php Signed-off-by: teromene --- bridges/Freenews.php | 6 +- bridges/Gawker.php | 4 +- bridges/Les400Culs.php | 9 +-- bridges/RssExpander.php | 68 ------------------ bridges/TheOatMealBridge.php | 6 +- index.php | 134 +++-------------------------------- lib/Bridge.php | 66 +++++++++++++++++ lib/HTMLUtils.php | 119 +++++++++++++++++++++++++++++++ lib/RssBridge.php | 1 + 9 files changed, 201 insertions(+), 212 deletions(-) delete mode 100644 bridges/RssExpander.php create mode 100644 lib/HTMLUtils.php diff --git a/bridges/Freenews.php b/bridges/Freenews.php index e1cf094c..ecdd4610 100644 --- a/bridges/Freenews.php +++ b/bridges/Freenews.php @@ -1,6 +1,5 @@ toURI(strtolower($param['site'])); } // $this->message("loading feed from ".$this->getURI()); - parent::collectData($param); + parent::collectExpandableDatas($param, $name); } protected function parseRSSItem($newsItem) { diff --git a/bridges/Les400Culs.php b/bridges/Les400Culs.php index 4694f406..36dc0a80 100644 --- a/bridges/Les400Culs.php +++ b/bridges/Les400Culs.php @@ -1,11 +1,7 @@ returnError('There is no $param[\'url\'] for this RSS expander', 404); - } -// $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 - $rssContent = simplexml_load_file($param['url']) or $this->returnError('Could not request '.$param['url'], 404); -// $this->message("loaded RSS from ".$param['url']); - // TODO insert RSS format detection - // we suppose for now, we have some RSS 2.0 - $this->collect_RSS_2_0_data($rssContent); - } - - protected function collect_RSS_2_0_data($rssContent) { - $rssContent = $rssContent->channel[0]; -// $this->message("RSS content is ===========\n".var_export($rssContent, true)."==========="); - $this->load_RSS_2_0_feed_data($rssContent); - foreach($rssContent->item as $item) { -// $this->message("parsing item ".var_export($item, true)); - $this->items[] = $this->parseRSSItem($item); - } - } - - protected function RSS_2_0_time_to_timestamp($item) { - return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp(); - } - - // TODO set title, link, description, language, and so on - protected function load_RSS_2_0_feed_data($rssContent) { - $this->name = trim($rssContent->title); - $this->uri = trim($rssContent->link); - $this->description = trim($rssContent->description); - } - - /** - * Method should return, from a source RSS item given by lastRSS, one of our Items objects - * @param $item the input rss item - * @return a RSS-Bridge Item, with (hopefully) the whole content) - */ - abstract protected function parseRSSItem($item); - - - public function getName(){ - return $this->name; - } - - public function getURI(){ - return $this->uri; - } - - public function getDescription() { - return $this->description; - } -} diff --git a/bridges/TheOatMealBridge.php b/bridges/TheOatMealBridge.php index ba431c88..97aae693 100644 --- a/bridges/TheOatMealBridge.php +++ b/bridges/TheOatMealBridge.php @@ -1,7 +1,4 @@ getMessage()); } -function getHelperButtonFormat($value, $name){ - return ''; -} - -function getHelperButtonsFormat($formats){ - $buttons = ''; - foreach( $formats as $name => $infos ) - { - if ( isset($infos['name']) ) - { - $buttons .= getHelperButtonFormat($name, $infos['name']) . PHP_EOL; - } - } - return $buttons; -} - -function displayBridgeCard($bridgeName, $formats, $isActive = true) -{ - - - $bridgeElement = Bridge::create($bridgeName); - if($bridgeElement == false) { - return ""; - } - $bridgeElement->loadMetadatas(); - - $name = ''.$bridgeElement->name.''; - $description = $bridgeElement->description; - - $card = << -

{$name}

-

- {$description} -

-CARD; - - // If we don't have any parameter for the bridge, we print a generic form to load it. - if(count($bridgeElement->parameters) == 0) { - - $card .= '
- - ' . PHP_EOL; - - if ($isActive) - { - $card .= getHelperButtonsFormat($formats); - } - else - { - $card .= 'Inactive'; - } - $card .= '
' . PHP_EOL; - - } - - foreach($bridgeElement->parameters as $parameterName => $parameter) - { - $card .= '
    ' . PHP_EOL; - if(!is_numeric($parameterName)) { - $card .= '
    '.$parameterName.'
    ' . PHP_EOL; - } - $card .= '
    - - ' . PHP_EOL; - - $parameter = json_decode($parameter, true); - - foreach($parameter as $inputEntry) { - - if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = ""; - - $idArg = 'arg-' . $bridgeName . '-' . $parameterName . '-' . $inputEntry['identifier']; - - $card .= '' . PHP_EOL; - - if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') { - $card .= '
    ' . PHP_EOL; - } else if($inputEntry['type'] == 'number') { - $card .= '
    ' . PHP_EOL; - } else if($inputEntry['type'] == 'list') { - $card .= '
    '; - } else if($inputEntry['type'] == 'checkbox') { - - $card .= '
    ' . PHP_EOL; - - } - - } - if ($isActive) - { - $card .= getHelperButtonsFormat($formats); - } - else - { - $card .= 'Inactive'; - } - $card .= '
    ' . PHP_EOL; - - } - - $card .= ''.$bridgeElement->maintainer.''; - $card .= ''; - - return $card; -} - $formats = Format::searchInformation(); ?> @@ -283,23 +164,24 @@ $formats = Format::searchInformation(); $activeFoundBridgeCount = 0; $showInactive = isset($_REQUEST['show_inactive']) && $_REQUEST['show_inactive'] == 1; $inactiveBridges = ''; - foreach(Bridge::listBridges() as $bridgeName) + $bridgeList = Bridge::listBridges(); + foreach($bridgeList as $bridgeName) { - if(BridgeWhitelist($whitelist_selection, $bridgeName)) + if(Bridge::isWhitelisted($whitelist_selection, $bridgeName)) { - echo displayBridgeCard($bridgeName, $formats); + echo HTMLUtils::displayBridgeCard($bridgeName, $formats); $activeFoundBridgeCount++; } elseif ($showInactive) { // inactive bridges - $inactiveBridges .= displayBridgeCard($bridgeName, $formats, false) . PHP_EOL; + $inactiveBridges .= HTMLUtils::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL; } } echo '
    ' . $inactiveBridges; ?> diff --git a/lib/Bridge.php b/lib/Bridge.php index 6ff1c389..93032100 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -293,6 +293,72 @@ class Bridge{ return $listBridge; } + static function isWhitelisted( $whitelist, $name ) { + if(in_array("$name", $whitelist) or in_array("$name.php", $whitelist)) + return TRUE; + else + return FALSE; + } } +abstract class RssExpander extends HttpCachingBridgeAbstract{ + + public $name; + public $uri; + public $description; + + public function collectExpandableDatas(array $param, $name){ + if (empty($name)) { + $this->returnError('There is no $param[\'url\'] for this RSS expander', 404); + } +// $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 + $rssContent = simplexml_load_file($name) or $this->returnError('Could not request '.$name, 404); +// $this->message("loaded RSS from ".$param['url']); + // TODO insert RSS format detection + // we suppose for now, we have some RSS 2.0 + $this->collect_RSS_2_0_data($rssContent); + } + + protected function collect_RSS_2_0_data($rssContent) { + $rssContent = $rssContent->channel[0]; +// $this->message("RSS content is ===========\n".var_export($rssContent, true)."==========="); + $this->load_RSS_2_0_feed_data($rssContent); + foreach($rssContent->item as $item) { +// $this->message("parsing item ".var_export($item, true)); + $this->items[] = $this->parseRSSItem($item); + } + } + + protected function RSS_2_0_time_to_timestamp($item) { + return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp(); + } + + // TODO set title, link, description, language, and so on + protected function load_RSS_2_0_feed_data($rssContent) { + $this->name = trim($rssContent->title); + $this->uri = trim($rssContent->link); + $this->description = trim($rssContent->description); + } + + /** + * Method should return, from a source RSS item given by lastRSS, one of our Items objects + * @param $item the input rss item + * @return a RSS-Bridge Item, with (hopefully) the whole content) + */ + abstract protected function parseRSSItem($item); + + + public function getName(){ + return $this->name; + } + + public function getURI(){ + return $this->uri; + } + + public function getDescription() { + return $this->description; + } +} diff --git a/lib/HTMLUtils.php b/lib/HTMLUtils.php new file mode 100644 index 00000000..85c0dbe2 --- /dev/null +++ b/lib/HTMLUtils.php @@ -0,0 +1,119 @@ +' . $name . ''; + } + + public static function getHelperButtonsFormat($formats){ + $buttons = ''; + foreach( $formats as $name => $infos ) + { + if ( isset($infos['name']) ) + { + $buttons .= HTMLUtils::getHelperButtonFormat($name, $infos['name']) . PHP_EOL; + } + } + return $buttons; + } + + public static function displayBridgeCard($bridgeName, $formats, $isActive = true) + { + + + $bridgeElement = Bridge::create($bridgeName); + if($bridgeElement == false) { + return ""; + } + $bridgeElement->loadMetadatas(); + + $name = ''.$bridgeElement->name.''; + $description = $bridgeElement->description; + + $card = << +

    {$name}

    +

    + {$description} +

    +CARD; + + // If we don't have any parameter for the bridge, we print a generic form to load it. + if(count($bridgeElement->parameters) == 0) { + + $card .= '
    + + ' . PHP_EOL; + + if ($isActive) + { + $card .= HTMLUtils::getHelperButtonsFormat($formats); + } + else + { + $card .= 'Inactive'; + } + $card .= '
    ' . PHP_EOL; + + } + + foreach($bridgeElement->parameters as $parameterName => $parameter) + { + $card .= '
      ' . PHP_EOL; + if(!is_numeric($parameterName)) { + $card .= '
      '.$parameterName.'
      ' . PHP_EOL; + } + $card .= '
      + + ' . PHP_EOL; + + $parameter = json_decode($parameter, true); + + foreach($parameter as $inputEntry) { + + if(!isset($inputEntry['exampleValue'])) $inputEntry['exampleValue'] = ""; + + $idArg = 'arg-' . $bridgeName . '-' . $parameterName . '-' . $inputEntry['identifier']; + + $card .= '' . PHP_EOL; + + if(!isset($inputEntry['type']) || $inputEntry['type'] == 'text') { + $card .= '
      ' . PHP_EOL; + } else if($inputEntry['type'] == 'number') { + $card .= '
      ' . PHP_EOL; + } else if($inputEntry['type'] == 'list') { + $card .= '
      '; + } else if($inputEntry['type'] == 'checkbox') { + + $card .= '
      ' . PHP_EOL; + + } + + } + if ($isActive) + { + $card .= HTMLUtils::getHelperButtonsFormat($formats); + } + else + { + $card .= 'Inactive'; + } + $card .= '
      ' . PHP_EOL; + + } + + $card .= ''.$bridgeElement->maintainer.''; + $card .= ''; + + return $card; + } + + +} +?> diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 5e0824ff..a515a4ff 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -12,6 +12,7 @@ require __DIR__ . '/Item.php'; require __DIR__ . '/Format.php'; require __DIR__ . '/Bridge.php'; require __DIR__ . '/Cache.php'; +require __DIR__ . '/HTMLUtils.php'; $vendorLibSimpleHtmlDom = __DIR__ . PATH_VENDOR . '/simplehtmldom/simple_html_dom.php'; if( !file_exists($vendorLibSimpleHtmlDom) ){