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
176
177
178
|
<?php
/**
* Provide useful methods to generate files to export.
*/
class FreshRSS_Export_Service {
/** @var string */
private $username;
/** @var FreshRSS_CategoryDAO */
private $category_dao;
/** @var FreshRSS_FeedDAO */
private $feed_dao;
/** @var FreshRSS_EntryDAO */
private $entry_dao;
/** @var FreshRSS_TagDAO */
private $tag_dao;
public const FRSS_NAMESPACE = 'https://freshrss.org/opml';
public const TYPE_HTML_XPATH = 'HTML+XPath';
public const TYPE_XML_XPATH = 'XML+XPath';
public const TYPE_RSS_ATOM = 'rss';
/**
* Initialize the service for the given user.
*/
public function __construct(string $username) {
$this->username = $username;
$this->category_dao = FreshRSS_Factory::createCategoryDao($username);
$this->feed_dao = FreshRSS_Factory::createFeedDao($username);
$this->entry_dao = FreshRSS_Factory::createEntryDao($username);
$this->tag_dao = FreshRSS_Factory::createTagDao();
}
/**
* Generate OPML file content.
* @return array{0:string,1:string} First item is the filename, second item is the content
*/
public function generateOpml(): array {
$view = new FreshRSS_View();
$day = date('Y-m-d');
$view->categories = $this->category_dao->listCategories(true, true) ?: [];
$view->excludeMutedFeeds = false;
return [
"feeds_{$day}.opml.xml",
$view->helperToString('export/opml')
];
}
/**
* Generate the starred and labelled entries file content.
*
* Both starred and labelled entries are put into a "starred" file, that’s
* why there is only one method for both.
*
* @phpstan-param 'S'|'T'|'ST' $type
* @param string $type must be one of:
* 'S' (starred/favourite),
* 'T' (taggued/labelled),
* 'ST' (starred or labelled)
* @return array{0:string,1:string} First item is the filename, second item is the content
*/
public function generateStarredEntries(string $type): array {
$view = new FreshRSS_View();
$view->categories = $this->category_dao->listCategories(true) ?: [];
$day = date('Y-m-d');
$view->list_title = _t('sub.import_export.starred_list');
$view->type = 'starred';
$entriesId = $this->entry_dao->listIdsWhere($type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1) ?? [];
$view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
// The following is a streamable query, i.e. must be last
$view->entries = $this->entry_dao->listWhere(
$type, 0, FreshRSS_Entry::STATE_ALL, 'ASC', -1
);
return [
"starred_{$day}.json",
$view->helperToString('export/articles')
];
}
/**
* Generate the entries file content for the given feed.
* @param int $feed_id
* @param int $max_number_entries
* @return array{0:string,1:string}|null First item is the filename, second item is the content.
* It also can return null if the feed doesn’t exist.
*/
public function generateFeedEntries(int $feed_id, int $max_number_entries): ?array {
$feed = $this->feed_dao->searchById($feed_id);
if ($feed === null) {
return null;
}
$view = new FreshRSS_View();
$view->categories = $this->category_dao->listCategories(true) ?: [];
$view->feed = $feed;
$day = date('Y-m-d');
$filename = "feed_{$day}_" . $feed->categoryId() . '_' . $feed->id() . '.json';
$view->list_title = _t('sub.import_export.feed_list', $feed->name());
$view->type = 'feed/' . $feed->id();
$entriesId = $this->entry_dao->listIdsWhere(
'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
) ?? [];
$view->entryIdsTagNames = $this->tag_dao->getEntryIdsTagNames($entriesId);
// The following is a streamable query, i.e. must be last
$view->entries = $this->entry_dao->listWhere(
'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC', $max_number_entries
);
return [
$filename,
$view->helperToString('export/articles')
];
}
/**
* Generate the entries file content for all the feeds.
* @param int $max_number_entries
* @return array<string,string> Keys are filenames and values are contents.
*/
public function generateAllFeedEntries(int $max_number_entries): array {
$feed_ids = $this->feed_dao->listFeedsIds();
$exported_files = [];
foreach ($feed_ids as $feed_id) {
$result = $this->generateFeedEntries($feed_id, $max_number_entries);
if (!$result) {
continue;
}
[$filename, $content] = $result;
$exported_files[$filename] = $content;
}
return $exported_files;
}
/**
* Compress several files in a Zip file.
* @param array<string,string> $files where the key is the filename, the value is the content
* @return array{0:string,1:string|false} First item is the zip filename, second item is the zip content
*/
public function zip(array $files): array {
$day = date('Y-m-d');
$zip_filename = 'freshrss_' . $this->username . '_' . $day . '_export.zip';
// From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
$zip_file = tempnam('/tmp', 'zip');
if ($zip_file == false) {
return [$zip_filename, false];
}
$zip_archive = new ZipArchive();
$zip_archive->open($zip_file, ZipArchive::OVERWRITE);
foreach ($files as $filename => $content) {
$zip_archive->addFromString($filename, $content);
}
$zip_archive->close();
$content = file_get_contents($zip_file);
unlink($zip_file);
return [
$zip_filename,
$content,
];
}
}
|