diff options
author | nashe <thomas@chauchefoin.fr> | 2017-10-21 13:30:15 +0200 |
---|---|---|
committer | nashe <thomas@chauchefoin.fr> | 2017-10-21 13:30:15 +0200 |
commit | 351009241bbe0864670c79fd78b5e95271cc6c2f (patch) | |
tree | 1fe11d960daa3da772971386b87480a9854962bc | |
parent | 1b341502ba68092b444a7870d601537eec68b344 (diff) | |
download | planet-3510092.tar planet-3510092.tar.gz planet-3510092.tar.bz2 planet-3510092.tar.xz planet-3510092.zip |
Improve project structure
-rw-r--r-- | app/classes/Cache.php (renamed from app/lib/Cache.php) | 0 | ||||
-rw-r--r-- | app/classes/Opml.php | 70 | ||||
-rw-r--r-- | app/classes/OpmlManager.php | 50 | ||||
-rw-r--r-- | app/classes/Planet.php | 67 | ||||
-rw-r--r-- | app/lib/lib.opml.php | 110 | ||||
-rw-r--r-- | composer.json | 4 |
6 files changed, 157 insertions, 144 deletions
diff --git a/app/lib/Cache.php b/app/classes/Cache.php index b73182e..b73182e 100644 --- a/app/lib/Cache.php +++ b/app/classes/Cache.php diff --git a/app/classes/Opml.php b/app/classes/Opml.php index e69de29..ae9e8b1 100644 --- a/app/classes/Opml.php +++ b/app/classes/Opml.php @@ -0,0 +1,70 @@ +<?php + +class Opml +{ + var $_xml = null; + var $_currentTag = ''; + + var $title = ''; + var $entries = array(); + var $map = + array( + 'URL' => 'website', + 'HTMLURL' => 'website', + 'TEXT' => 'name', + 'TITLE' => 'name', + 'XMLURL' => 'feed', + 'DESCRIPTION' => 'description', + 'ISDOWN' => 'isDown' + ); + + + function parse($data) + { + $this->_xml = xml_parser_create('UTF-8'); + //xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false); + //xml_parser_set_option($this->_xml, XML_OPTION_SKIP_WHITE, true); + xml_set_object($this->_xml, $this); + xml_set_element_handler($this->_xml,'_openTag','_closeTag'); + xml_set_character_data_handler ($this->_xml, '_cData'); + + xml_parse($this->_xml,$data); + xml_parser_free($this->_xml); + return $this->entries; + } + + + function _openTag($p,$tag,$attrs) + { + $this->_currentTag = $tag; + + if ($tag == 'OUTLINE') + { + $i = count($this->entries); + foreach (array_keys($this->map) as $key) + { + if (isset($attrs[$key])) { + $this->entries[$i][$this->map[$key]] = $attrs[$key]; + } + } + } + } + + function _closeTag($p, $tag){ + $this->_currentTag = ''; + } + + function _cData($p, $cdata){ + if ($this->_currentTag == 'TITLE'){ + $this->title = $cdata; + } + } + + function getTitle(){ + return $this->title; + } + + function getPeople(){ + return $this->entries; + } +}
\ No newline at end of file diff --git a/app/classes/OpmlManager.php b/app/classes/OpmlManager.php new file mode 100644 index 0000000..02491a2 --- /dev/null +++ b/app/classes/OpmlManager.php @@ -0,0 +1,50 @@ +<?php + + +class OpmlManager +{ + public static function load($file) + { + if (!file_exists($file)) { + throw new Exception('OPML file not found!'); + } + + $opml = new opml(); + + //Remove BOM if needed + $BOM = '/^/'; + $fileContent = file_get_contents($file); + $fileContent = preg_replace($BOM, '', $fileContent, 1); + + //Parse + $opml->parse($fileContent); + + return $opml; + } + + /** + * @param Opml $opml + * @param string $file + */ + public static function save($opml, $file){ + $out = '<?xml version="1.0"?>'."\n"; + $out.= '<opml version="1.1">'."\n"; + $out.= '<head>'."\n"; + $out.= '<title>'.htmlspecialchars($opml->getTitle()).'</title>'."\n"; + $out.= '<dateCreated>'.date('c').'</dateCreated>'."\n"; + $out.= '<dateModified>'.date('c').'</dateModified>'."\n"; + $out.= '</head>'."\n"; + $out.= '<body>'."\n"; + foreach ($opml->entries as $person) { + $out.= '<outline text="' . htmlspecialchars($person['name'], ENT_QUOTES) . '" htmlUrl="' . htmlspecialchars($person['website'], ENT_QUOTES) . '" xmlUrl="' . htmlspecialchars($person['feed'], ENT_QUOTES) . '" isDown="' . htmlspecialchars($person['isDown'], ENT_QUOTES) . '"/>'."\n"; + } + $out.= '</body>'."\n"; + $out.= '</opml>'; + + file_put_contents($file, $out); + } + + public static function backup($file){ + copy($file, $file.'.bak'); + } +} diff --git a/app/classes/Planet.php b/app/classes/Planet.php index 39b55f6..c287843 100644 --- a/app/classes/Planet.php +++ b/app/classes/Planet.php @@ -34,23 +34,30 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ class Planet { + /** @var PlanetConfig */ public $config; + + /** @var PlanetItem[] */ public $items; + + /** @var PlanetFeed[] */ public $people; + + /** @var PlanetError[] */ public $errors; + /** + * Planet constructor. + * + * @param PlanetConfig $config + */ public function __construct($config=null) { + $this->config = $config === null ? new PlanetConfig() : $config; - if ($config == null) { - $this->config = new PlanetConfig(array()); - } else { - $this->config = $config; - } - - $this->items = array(); - $this->people = array(); - $this->errors = array(); + $this->items = []; + $this->people = []; + $this->errors = []; } /** @@ -111,8 +118,9 @@ class Planet } /** - * Adds a feed to the planet - * @param PlanetFeed feed + * Adds a feed to the planet. + * + * @param PlanetFeed $feed */ public function addPerson(&$feed) { @@ -120,8 +128,10 @@ class Planet } /** - * Load people from an OPML - * @return integer Number of people loaded + * Load people from an OPML. + * + * @param string $file File to load the OPML from. + * @return integer Number of people loaded. */ public function loadOpml($file) { @@ -162,17 +172,16 @@ class Planet } /** - * Download - * @var $max_load percentage of feeds to load + * Fetch feeds and see if new data is present. + * + * @param float $max_load Percentage of feeds to load */ public function download($max_load=0.1) { $max_load_feeds = ceil(count($this->people) * $max_load); - $opml = OpmlManager::load(__DIR__.'/../../custom/people.opml'); - foreach ($this->people as $feed) { + foreach ($this->people as &$feed) { //Avoid mass loading with variable cache duration - //$feed->set_cache_duration($this->config->getCacheTimeout()+rand(0,30)); $feed->set_cache_duration($this->config->getCacheTimeout()); //Load only a few feeds, force other to fetch from the cache @@ -181,6 +190,8 @@ class Planet $this->errors[] = new PlanetError(1, 'Forced from cache : '.$feed->getFeed()); } + // Bypass remote's SSL/TLS certificate if the user explicitly + // asked for it in the configuration. if ($this->config->checkcerts === false) { $feed->set_curl_options([ CURLOPT_SSL_VERIFYHOST => false, @@ -188,28 +199,22 @@ class Planet ]); } - //Load feed $feed->init(); - $isDown = ''; // http://simplepie.org/wiki/reference/simplepie/merge_items ? - //Add items to index if (($feed->data) && ($feed->get_item_quantity() > 0)){ $items = $feed->get_items(); $this->items = array_merge($this->items, $items); + $feed['isUp'] = true; } else { - $this->errors[] = new PlanetError(1, 'No items : '.$feed->getFeed()); - $isDown = '1'; - } - - //Mark if the feed is temporary unavailable - foreach ($opml->entries as $key => $entrie) { - if ($feed->getFeed() === $entrie['feed']) { - $opml->entries[$key]['isDown'] = $isDown; - } + $this->errors[] = new PlanetError(1, 'No items or down : ' . $feed->getFeed()); + $feed['isDown'] = false; } } - OpmlManager::save($opml, __DIR__.'/../../custom/people.opml'); + + // FIXME: not sure that it's $this->people? + // FIXME: make sure we made a change + OpmlManager::save($this->people, __DIR__.'/../../custom/people.opml'); } public function sort() diff --git a/app/lib/lib.opml.php b/app/lib/lib.opml.php deleted file mode 100644 index 7a20321..0000000 --- a/app/lib/lib.opml.php +++ /dev/null @@ -1,110 +0,0 @@ -<?php -class opml -{ - var $_xml = null; - var $_currentTag = ''; - - var $title = ''; - var $entries = array(); - var $map = - array( - 'URL' => 'website', - 'HTMLURL' => 'website', - 'TEXT' => 'name', - 'TITLE' => 'name', - 'XMLURL' => 'feed', - 'DESCRIPTION' => 'description', - 'ISDOWN' => 'isDown' - ); - - - function parse($data) - { - $this->_xml = xml_parser_create('UTF-8'); - //xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false); - //xml_parser_set_option($this->_xml, XML_OPTION_SKIP_WHITE, true); - xml_set_object($this->_xml, $this); - xml_set_element_handler($this->_xml,'_openTag','_closeTag'); - xml_set_character_data_handler ($this->_xml, '_cData'); - - xml_parse($this->_xml,$data); - xml_parser_free($this->_xml); - return $this->entries; - } - - - function _openTag($p,$tag,$attrs) - { - $this->_currentTag = $tag; - - if ($tag == 'OUTLINE') - { - $i = count($this->entries); - foreach (array_keys($this->map) as $key) - { - if (isset($attrs[$key])) { - $this->entries[$i][$this->map[$key]] = $attrs[$key]; - } - } - } - } - - function _closeTag($p, $tag){ - $this->_currentTag = ''; - } - - function _cData($p, $cdata){ - if ($this->_currentTag == 'TITLE'){ - $this->title = $cdata; - } - } - - function getTitle(){ - return $this->title; - } - - function getPeople(){ - return $this->entries; - } -} - -class OpmlManager -{ - static function load($file) { - if (@file_exists($file)) { - $opml = new opml(); - - //Remove BOM if needed - $BOM = '/^/'; - $fileContent = file_get_contents($file); - $fileContent = preg_replace($BOM, '', $fileContent, 1); - - //Parse - $opml->parse($fileContent); - - return $opml; - } - } - - public static function save($opml, $file){ - $out = '<?xml version="1.0"?>'."\n"; - $out.= '<opml version="1.1">'."\n"; - $out.= '<head>'."\n"; - $out.= '<title>'.htmlspecialchars($opml->getTitle()).'</title>'."\n"; - $out.= '<dateCreated>'.date('c').'</dateCreated>'."\n"; - $out.= '<dateModified>'.date('c').'</dateModified>'."\n"; - $out.= '</head>'."\n"; - $out.= '<body>'."\n"; - foreach ($opml->entries as $person) { - $out.= '<outline text="' . htmlspecialchars($person['name'], ENT_QUOTES) . '" htmlUrl="' . htmlspecialchars($person['website'], ENT_QUOTES) . '" xmlUrl="' . htmlspecialchars($person['feed'], ENT_QUOTES) . '" isDown="' . htmlspecialchars($person['isDown'], ENT_QUOTES) . '"/>'."\n"; - } - $out.= '</body>'."\n"; - $out.= '</opml>'; - - file_put_contents($file, $out); - } - - public static function backup($file){ - copy($file, $file.'.bak'); - } -} diff --git a/composer.json b/composer.json index ed06489..c43b48e 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,7 @@ "": "app/classes/" }, "files": [ - "app/lib/lib.opml.php", - "app/helpers.php", - "app/lib/Cache.php" + "app/helpers.php" ] } } |