-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathExport.php
More file actions
160 lines (133 loc) · 6.01 KB
/
Export.php
File metadata and controls
160 lines (133 loc) · 6.01 KB
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
<?php
declare(strict_types=1);
namespace Selfoss\controllers\Opml;
use Monolog\Logger;
use Selfoss\daos;
use Selfoss\helpers\Authentication;
use Selfoss\helpers\Configuration;
use Selfoss\helpers\SpoutLoader;
use Selfoss\helpers\StringKeyedArray;
/**
* 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 <stuporglue@gmail.com>
* @author Sean Rand <asanernd@gmail.com>
*/
final readonly class Export {
public function __construct(
private Authentication $authentication,
private Configuration $configuration,
private Logger $logger,
private daos\Sources $sourcesDao,
private SpoutLoader $spoutLoader,
private daos\Tags $tagsDao,
private \XMLWriter $writer
) {
}
/**
* Generate an OPML outline element from a source
*
* @note Uses the selfoss namespace to store information about spouts
*
* @param array{title: string, spout: string, params: string} $source source
*/
private function writeSource(array $source): void {
// retrieve the feed url of the source
$params = json_decode(html_entity_decode($source['params']), true);
$feed = $this->spoutLoader->get($source['spout']);
$feedUrl = $feed !== null ? $feed->getXmlUrl($params) : null;
// 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
*/
public function export(): void {
$this->authentication->ensureIsPrivileged();
$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');
/** @var StringKeyedArray<array<array{id: int, title: string, tags: string[], spout: string, params: string, filter: ?string, error: ?string, lastupdate: ?int, lastentry: ?int}>> */
$taggedSources = new StringKeyedArray();
$untaggedSources = [];
foreach ($this->sourcesDao->getAll() as $source) {
if ($source['tags']) {
foreach ($source['tags'] as $tag) {
if (!isset($taggedSources[$tag])) {
$taggedSources[$tag] = [];
}
$taggedSources[$tag][] = $source;
}
} else {
$untaggedSources[] = $source;
}
}
/** @var StringKeyedArray<string> associate tag names with colors */
$tagColors = new StringKeyedArray();
foreach ($this->tagsDao->get() as $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 ($taggedSources 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 ($untaggedSources as $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();
}
}