summaryrefslogtreecommitdiffstats
path: root/opmlexporter.php
blob: d9d404ff1804e58d200b99d978f7d53bc6d20a65 (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
78
79
80
81
82
83
84
<?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
*
*/

namespace OCA\News;

/**
* Exports the OPML
*/
class OPMLExporter {
	
	private $api;
	private $trans;

	public function __construct($api){
		$this->api = $api;
		$this->trans = $api->getTrans();
	}


	/**
	 * Generates the OPML for the active user
	 * @return the OPML as string
	 */
	public function buildOPML($feeds){
		$dom = new \DomDocument('1.0', 'UTF-8');
		$dom->formatOutput = true;

		$opml_el = $dom->createElement('opml');
		$opml_el->setAttribute('version', '2.0');

		$head_el = $dom->createElement('head');

		$title = $this->api->getUserId() . ' ' . 
					$this->trans->t('subscriptions in ownCloud - News');
		$title_el = $dom->createElement('title', $title);

		$head_el->appendChild( $title_el );
		$opml_el->appendChild( $head_el );
		$body_el = $dom->createElement('body');

		$this->feedsToXML($feeds, $body_el, $dom);

		$opml_el->appendChild( $body_el );
		$dom->appendChild( $opml_el );

		return $dom->saveXML();
	}


	/**
	 * Creates the OPML content recursively
	 */
	protected function feedsToXML($data, $xml_el, $dom) {

		foreach($data as $collection) {
			$outline_el = $dom->createElement('outline');
			if ($collection instanceOf Folder) {
				$outline_el->setAttribute('title', $collection->getName());
				$outline_el->setAttribute('text', $collection->getName());
				$this->feedsToXML($collection->getChildren(), $outline_el, $dom);
			}
			elseif ($collection instanceOf Feed) {
				$outline_el->setAttribute('title', $collection->getTitle());
				$outline_el->setAttribute('text', $collection->getTitle());
				$outline_el->setAttribute('type', 'rss');
				$outline_el->setAttribute('xmlUrl', $collection->getUrl());
			}
			$xml_el->appendChild( $outline_el );
		}
	}


}