summaryrefslogtreecommitdiffstats
path: root/opmlparser.php
blob: 34f8f115ef9cc837919ebb6f5b6f9e146791a21e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php 

/**
* ownCloud - News app
*
* @author Alessandro Cosentino
* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
*
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING-README file
*
*/

class OPMLParser {

	private $raw;
	private $body;
	private $title;
	private $error;

	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 parse(){
		return self::parseFolder($this->body);
	}
	
	private function parseFolder($rawfolder) {
		$list = array();
		foreach ($rawfolder->outline as $rawcollection) {
			if ($rawcollection['type'] == 'rss') {
				$collection = self::parseFeed($rawcollection);
			}
			else {
				$name = (string)$rawcollection['text'];
				$children = self::parseFolder($rawcollection);
				$collection = new OC_News_Folder($name);
				$collection->addChildren($children);
			}
			if ($collection !== null) {
				$list[] = $collection;
			}
		}
		return $list;
	}
	
	private function parseFeed($rawfeed) {
		$url = (string)$rawfeed['xmlUrl'];
		echo $url;

		$feed = OC_News_Utils::fetch($url);

		if ($feed != null) {
			$title = $rawfeed['title'];
			$feed->setTitle($title);
		}
		return $feed;
	}
	
	public function getTitle() {
		return $this->title;
	}
	
	public function getError() {
		return $this->error;
	}
}