1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - News |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Alessandro Cosentino <[email protected]> |
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
10
|
|
|
* @copyright Alessandro Cosentino 2012 |
11
|
|
|
* @copyright Bernhard Posselt 2012, 2014 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OCA\News\Utility; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Exports the OPML |
18
|
|
|
*/ |
19
|
|
|
class OPMLExporter { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generates the OPML for the active user |
23
|
|
|
* |
24
|
|
|
* @param \OCA\News\Db\Folder[] $folders |
25
|
|
|
* @param \OCA\News\Db\Feed[] $feeds |
26
|
|
|
* @return \DomDocument the document |
27
|
|
|
*/ |
28
|
|
|
public function build($folders, $feeds){ |
29
|
|
|
$document = new \DomDocument('1.0', 'UTF-8'); |
30
|
|
|
$document->formatOutput = true; |
31
|
|
|
|
32
|
|
|
$root = $document->createElement('opml'); |
33
|
|
|
$root->setAttribute('version', '2.0'); |
34
|
|
|
|
35
|
|
|
// head |
36
|
|
|
$head = $document->createElement('head'); |
37
|
|
|
|
38
|
|
|
$title = $document->createElement('title', 'Subscriptions'); |
39
|
|
|
$head->appendChild($title); |
40
|
|
|
|
41
|
|
|
$root->appendChild($head); |
42
|
|
|
|
43
|
|
|
// body |
44
|
|
|
$body = $document->createElement('body'); |
45
|
|
|
|
46
|
|
|
// feeds with folders |
47
|
|
|
foreach($folders as $folder) { |
48
|
|
|
$folderOutline = $document->createElement('outline'); |
49
|
|
|
$folderOutline->setAttribute('title', $folder->getName()); |
50
|
|
|
$folderOutline->setAttribute('text', $folder->getName()); |
51
|
|
|
|
52
|
|
|
// feeds in folders |
53
|
|
|
foreach ($feeds as $feed) { |
54
|
|
|
if ($feed->getFolderId() === $folder->getId()) { |
55
|
|
|
$feedOutline = $this->createFeedOutline($feed, $document); |
56
|
|
|
$folderOutline->appendChild($feedOutline); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$body->appendChild($folderOutline); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// feeds without folders |
64
|
|
|
foreach ($feeds as $feed) { |
65
|
|
|
if ($feed->getFolderId() === 0) { |
66
|
|
|
$feedOutline = $this->createFeedOutline($feed, $document); |
67
|
|
|
$body->appendChild($feedOutline); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$root->appendChild($body); |
72
|
|
|
|
73
|
|
|
$document->appendChild($root); |
74
|
|
|
|
75
|
|
|
return $document; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
protected function createFeedOutline($feed, $document) { |
80
|
|
|
$feedOutline = $document->createElement('outline'); |
81
|
|
|
$feedOutline->setAttribute('title', $feed->getTitle()); |
82
|
|
|
$feedOutline->setAttribute('text', $feed->getTitle()); |
83
|
|
|
$feedOutline->setAttribute('type', 'rss'); |
84
|
|
|
$feedOutline->setAttribute('xmlUrl', $feed->getUrl()); |
85
|
|
|
$feedOutline->setAttribute('htmlUrl', $feed->getLink()); |
86
|
|
|
return $feedOutline; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|