diff options
-rw-r--r-- | ajax/importopml.php | 19 | ||||
-rw-r--r-- | js/settings.js | 16 | ||||
-rw-r--r-- | lib/feed.php | 8 | ||||
-rw-r--r-- | opmlparser.php | 74 |
4 files changed, 67 insertions, 50 deletions
diff --git a/ajax/importopml.php b/ajax/importopml.php index 89e2a52f0..c2230c8b8 100644 --- a/ajax/importopml.php +++ b/ajax/importopml.php @@ -23,10 +23,6 @@ function bailOut($msg) { exit(); } -function debug($msg) { - OCP\Util::writeLog('news','ajax/importopml.php: '.$msg, OCP\Util::DEBUG); -} - if(!isset($_POST['path'])) { bailOut($l->t('No file path was submitted.')); } @@ -35,10 +31,15 @@ require_once('news/opmlparser.php'); $raw = file_get_contents($_POST['path']); -$parser = new OPMLParser($raw); -$title = $parser->getTitle(); -$data = $parser->parse(); -$count = 0; //number of feeds imported +try { + $parsed = OPMLParser::parse($raw); +} catch (Exception $e) { + bailOut($e->getMessage()); +} + +if ($parsed == null) { + bailOut($l->t('An error occurred while parsing the file.')); +} -OCP\JSON::success(array('data' => array('title'=>$title, 'count'=>$count))); +OCP\JSON::success(array('data' => array('title'=>$parsed->getTitle(), 'count'=>$parsed->getCount()))); diff --git a/js/settings.js b/js/settings.js index f686644c3..95fb14b6a 100644 --- a/js/settings.js +++ b/js/settings.js @@ -1,5 +1,7 @@ News = News || {} News.Settings={ + importkind: '', + importpath: '', IMPORTCLOUD:'cloud', IMPORTLOCAL:'local', cloudFileSelected:function(path){ @@ -7,8 +9,8 @@ News.Settings={ if(jsondata.status == 'success'){ $('#browsebtn, #cloudbtn, #importbtn').show(); $('#opml_file').text(t('news', 'File ') + path + t('news', ' loaded from cloud.')); - this.importkind = this.IMPORTCLOUD; - this.importpath = jsondata.data.tmp; + News.Settings.importkind = News.Settings.IMPORTCLOUD; + News.Settings.importpath = jsondata.data.tmp; } else{ OC.dialogs.alert(jsondata.data.message, t('news', 'Error')); @@ -30,10 +32,8 @@ News.Settings={ $(button).prop('value', t('news', 'Importing...')); var path = ''; - alert(this.importkind); - if (this.importkind == this.IMPORTCLOUD) { - path = this.importpath; - alert(this.IMPORTCLOUD); + if (News.Settings.importkind == News.Settings.IMPORTCLOUD) { + path = News.Settings.importpath; } else if (this.importkind == this.IMPORTLOCAL) { } else { @@ -43,7 +43,9 @@ News.Settings={ $.post(OC.filePath('news', 'ajax', 'importopml.php'), { path: path }, function(jsondata){ if (jsondata.status == 'success') { alert(jsondata.data.title); - } + } else { + OC.dialogs.alert(jsondata.data.message, t('news', 'Error')); + } }); $(button).prop('value', t('news', 'Import')); diff --git a/lib/feed.php b/lib/feed.php index 6b1409440..496faa224 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -20,10 +20,14 @@ class OC_News_Feed extends OC_News_Collection { private $items; //array that contains all the items of the feed private $favicon; - public function __construct($url, $title, $items, $id = null) { + // if $items = null, it means that feed has not been fetched yet + // if $id = null, it means that the feed has not been stored in the db yet + public function __construct($url, $title, $items = null, $id = null) { $this->url = $url; $this->title = $title; - $this->items = $items; + if ($items !== null) { + $this->items = $items; + } if ($id !== null) { parent::__construct($id); } diff --git a/opmlparser.php b/opmlparser.php index 34f8f115e..3a07383cb 100644 --- a/opmlparser.php +++ b/opmlparser.php @@ -13,37 +13,38 @@ class OPMLParser { - private $raw; - private $body; private $title; - private $error; + private $body; + private $data; + private $count; + + private function __construct() { + $this->data = array(); + $this->count = 0; + } - public function __construct($raw) { - $this->raw = $raw; - try { - $xml_parser = new SimpleXMLElement($this->raw, LIBXML_NOERROR); - $this->title = (string)$xml_parser->head->title; - $this->body = $xml_parser->body; - } - catch (Exception $e) { - $this->error = $e->getMessage(); - return; - } + public function getTitle() { + return $this->title; + } + + public function getData() { + return $this->data; } - public function parse(){ - return self::parseFolder($this->body); + public function getCount() { + return $this->count; } - private function parseFolder($rawfolder) { + private static function parseFolder($rawfolder, &$count) { $list = array(); foreach ($rawfolder->outline as $rawcollection) { if ($rawcollection['type'] == 'rss') { $collection = self::parseFeed($rawcollection); + $count++; } else { $name = (string)$rawcollection['text']; - $children = self::parseFolder($rawcollection); + $children = self::parseFolder($rawcollection, $count); $collection = new OC_News_Folder($name); $collection->addChildren($children); } @@ -54,24 +55,33 @@ class OPMLParser { return $list; } - private function parseFeed($rawfeed) { + private static function parseFeed($rawfeed) { $url = (string)$rawfeed['xmlUrl']; - echo $url; - - $feed = OC_News_Utils::fetch($url); + $title = (string)$rawfeed['title']; - if ($feed != null) { - $title = $rawfeed['title']; - $feed->setTitle($title); - } + $feed = new OC_News_Feed($url, $title); return $feed; } - public function getTitle() { - return $this->title; - } - - public function getError() { - return $this->error; + /** + * @param $raw the XML string to be parsed + * @return an object of the OPMLParser class itself + * or null if the parsing failed + * @throws + */ + public static function parse($raw){ + $parsed = new OPMLParser(); + + $xml_parser = new SimpleXMLElement($raw, LIBXML_NOERROR); + $parsed->title = (string)$xml_parser->head->title; + $parsed->body = $xml_parser->body; + + if ($parsed->body != null) { + $parsed->data = self::parseFolder($parsed->body, $parsed->count); + return $parsed; + } else { + return null; + } } + }
\ No newline at end of file |