-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathExport.php
175 lines (142 loc) · 6.13 KB
/
Export.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace controllers\Opml;
use helpers\Authentication;
use helpers\Configuration;
use helpers\SpoutLoader;
use Monolog\Logger;
/**
* OPML loading and exporting controller
*
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Michael Moore <[email protected]>
* @author Sean Rand <[email protected]>
*/
class Export {
/** @var Authentication authentication helper */
private $authentication;
/** @var Configuration configuration */
private $configuration;
/** @var Logger */
private $logger;
/** @var SpoutLoader */
private $spoutLoader;
/** @var \XMLWriter */
private $writer;
/** @var \daos\Sources */
private $sourcesDao;
/** @var \daos\Tags */
private $tagsDao;
public function __construct(Authentication $authentication, Configuration $configuration, Logger $logger, \daos\Sources $sourcesDao, SpoutLoader $spoutLoader, \daos\Tags $tagsDao, \XMLWriter $writer) {
$this->authentication = $authentication;
$this->configuration = $configuration;
$this->logger = $logger;
$this->sourcesDao = $sourcesDao;
$this->spoutLoader = $spoutLoader;
$this->tagsDao = $tagsDao;
$this->writer = $writer;
}
/**
* Generate an OPML outline element from a source
*
* @note Uses the selfoss namespace to store information about spouts
*
* @param array $source source
*
* @return void
*/
private function writeSource(array $source) {
// retrieve the feed url of the source
$params = json_decode(html_entity_decode($source['params']), true);
$feedUrl = $this->spoutLoader->get($source['spout'])->getXmlUrl($params);
// if the spout doesn't return a feed url, the source isn't an RSS feed
if ($feedUrl !== null) {
$this->writer->startElement('outline');
} else {
$this->writer->startElementNS('selfoss', 'outline', null);
}
$this->writer->writeAttribute('title', $source['title']);
$this->writer->writeAttribute('text', $source['title']);
if ($feedUrl !== null) {
$this->writer->writeAttribute('xmlUrl', $feedUrl);
$this->writer->writeAttribute('type', 'rss');
}
// write spout name and parameters in namespaced attributes
$this->writer->writeAttributeNS('selfoss', 'spout', null, $source['spout']);
$this->writer->writeAttributeNS('selfoss', 'params', null, html_entity_decode($source['params']));
$this->writer->endElement(); // outline
$this->logger->debug('done exporting source ' . $source['title']);
}
/**
* Export user's subscriptions to OPML file
*
* @note Uses the selfoss namespace to store selfoss-specific information
*
* @return void
*/
public function export() {
$this->authentication->needsLoggedIn();
$this->logger->debug('start OPML export');
$this->writer->openMemory();
$this->writer->setIndent(true);
$this->writer->setIndentString(' ');
$this->writer->startDocument('1.0', 'UTF-8');
$this->writer->startElement('opml');
$this->writer->writeAttribute('version', '2.0');
$this->writer->writeAttribute('xmlns:selfoss', 'https://selfoss.aditu.de/');
// selfoss version, XML format version and creation date
$this->writer->startElementNS('selfoss', 'meta', null);
$this->writer->writeAttribute('generatedBy', 'selfoss-' . SELFOSS_VERSION);
$this->writer->writeAttribute('version', '1.0');
$this->writer->writeAttribute('createdOn', date('r'));
$this->writer->endElement(); // meta
$this->logger->debug('OPML export: finished writing meta');
$this->writer->startElement('head');
$user = $this->configuration->username;
$this->writer->writeElement('title', ($user ? $user . '\'s' : 'My') . ' subscriptions in selfoss');
$this->writer->endElement(); // head
$this->logger->debug('OPML export: finished writing head');
$this->writer->startElement('body');
// create tree structure for tagged and untagged sources
$sources = ['tagged' => [], 'untagged' => []];
foreach ($this->sourcesDao->get() as $source) {
if ($source['tags']) {
foreach ($source['tags'] as $tag) {
$sources['tagged'][$tag][] = $source;
}
} else {
$sources['untagged'][] = $source;
}
}
// create associative array with tag names as keys, colors as values
$tagColors = [];
foreach ($this->tagsDao->get() as $key => $tag) {
$this->logger->debug('OPML export: tag ' . $tag['tag'] . ' has color ' . $tag['color']);
$tagColors[$tag['tag']] = $tag['color'];
}
// generate outline elements for all sources
foreach ($sources['tagged'] as $tag => $children) {
$this->logger->debug("OPML export: exporting tag $tag sources");
$this->writer->startElement('outline');
$this->writer->writeAttribute('title', $tag);
$this->writer->writeAttribute('text', $tag);
$this->writer->writeAttributeNS('selfoss', 'color', null, $tagColors[$tag]);
foreach ($children as $source) {
$this->writeSource($source);
}
$this->writer->endElement(); // outline
}
$this->logger->debug('OPML export: exporting untagged sources');
foreach ($sources['untagged'] as $key => $source) {
$this->writeSource($source);
}
$this->writer->endElement(); // body
$this->writer->endDocument();
$this->logger->debug('finished OPML export');
// save content as file and suggest file name
$exportName = 'selfoss-subscriptions-' . date('YmdHis') . '.xml';
header('Content-Disposition: attachment; filename="' . $exportName . '"');
header('Content-Type: text/xml; charset=UTF-8');
echo $this->writer->outputMemory();
}
}