-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
330 lines (292 loc) · 10.7 KB
/
init.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
require 'SchemaManager.php';
class more_feed_data extends Plugin {
static $REQUIRED_SCHEMA_VERSION = 3;
static $KEY_SCHEMA_VERSION = "schema_version";
static $PLUGIN_FOLDER = "plugins.local/more_feed_data";
function drop_table() {
print "Dropping table";
$sth = $this->pdo->prepare("DROP TABLE IF EXISTS ttrss_plugin_more_feed_data;");
if ($sth->execute()) {
print "<p>Table dropped.</p>";
return true;
}
else {
print "<p>Table drop failed.</p>";
return false;
}
}
function create_table() {
print "Creating table";
$sth = $this->pdo->prepare(file_get_contents("plugins.local/more_feed_data/schema/more_feed_data_schema_pgsql.sql"));
if ($sth->execute()) {
print "<p>Table created, setting installed schema version.</p>";
$this->host->set($this, $this::$KEY_SCHEMA_VERSION, 1);
print "<p>Installed version set: " . $this->host->get($this, $this::$KEY_SCHEMA_VERSION) . "</p>";
return true;
}
else {
print "<p>Table creation failed.</p>";
return false;
}
}
function check_database() {
$sth = $this->pdo->prepare("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name = 'ttrss_plugin_more_feed_data');");
$sth->execute();
$database_table_exists = $sth->fetch(PDO::FETCH_ASSOC)['exists'];
if (!$database_table_exists) {
$database_table_exists = $this->create_table();
}
if ($database_table_exists) {
if(!$this->host->get($this, $this::$KEY_SCHEMA_VERSION)) {
// Database exists but version key is gone.
$this->drop_table();
$database_table_exists = $this->create_table();
}
print "<p>Database table exists.</p>";
$installed_version = $this->host->get($this, $this::$KEY_SCHEMA_VERSION);
print "<p>Installed version: " . $installed_version . "</p>";
printf("<p>Required version: %d</p>", $this::$REQUIRED_SCHEMA_VERSION);
while ($installed_version < $this::$REQUIRED_SCHEMA_VERSION) {
$version_to_install = $installed_version + 1;
$upgrade_file = "plugins.local/more_feed_data/schema/versions/pgsql/" . $version_to_install . ".sql";
printf("<p>Running upgrade %d -> %d: %s</p>", $installed_version, $version_to_install, $upgrade_file);
$sth = $this->pdo->prepare(file_get_contents($upgrade_file));
if ($sth->execute()) {
$installed_version = $version_to_install;
$this->host->set($this, $this::$KEY_SCHEMA_VERSION, $installed_version);
printf("Schema upgraded to %d", $this->host->get($this, $this::$KEY_SCHEMA_VERSION));
}
else {
printf("<p>Schema failed to upgrade to %d.", $version_to_install);
}
}
}
}
function processFeed($feed_data, $fetch_url) {
$doc = new DOMDocument();
$doc->loadXML($feed_data);
$root = $doc->firstChild;
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
$xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
$xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
$root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
$generator;
$generatorUri;
$generatorVersion;
$cleanGenerator;
$cleanGeneratorVersion;
if ($root && $root->length > 0) {
$root = $root->item(0);
if ($root) {
$generatorNode;
switch (mb_strtolower($root->tagName)) {
case "rdf:rdf":
break;
case "channel":
$generatorNode = $xpath->query("//channel/generator")->item(0);
break;
case "feed":
case "atom:feed":
$generatorNode = $xpath->query("//atom:feed/atom:generator")->item(0);
if (!$generatorNode) {
$generatorNode = $xpath->query("//atom03:feed/atom03:generator")->item(0);
}
break;
default:
//printf("Unknown/unsupported feed type");
return;
}
if ($generatorNode) {
$generator = $generatorNode->nodeValue;
$generatorUri = $generatorNode->getAttribute("uri");
$generatorVersion = $generatorNode->getAttribute("version");
// If a version is supplied, we can keep it as "clean".
if($generatorVersion) {
$cleanGeneratorVersion = $generatorVersion;
}
if(strpos($generator, "https://wordpress.org/?v=") === 0) {
// https://wordpress.org/?v=5.4.2
$cleanGenerator = "Wordpress";
$cleanGeneratorVersion = str_replace("https://wordpress.org/?v=", "", $generator);
}
else if(strpos($generator, "http://wordpress.com/") === 0) {
// http://wordpress.com/
$cleanGenerator = "Wordpress";
}
else if(strpos($generator, "Blogger") === 0) {
// Blogger
$cleanGenerator = "Blogger";
}
else if(strpos($generator, "GatsbyJS") === 0) {
// GatsbyJS
$cleanGenerator = "GatsbyJS";
}
else if(strpos($generator, "Ghost ") === 0) {
// Ghost 3.26
$cleanGenerator = "Ghost";
$cleanGeneratorVersion = str_replace("Ghost ", "", $generator);
}
else if(strpos($generator, "Hexo") === 0) {
// Hexo
$cleanGenerator = "Hexo";
}
else if(strpos($generator, "Hugo") === 0) {
// Hugo -- gohugo.io
$cleanGenerator = "Hugo";
}
else if(strpos($generator, "Jekyll") === 0) {
$cleanGenerator = "Jekyll";
}
else if(strpos($generator, "Jekyll v") === 0) {
$cleanGenerator = "Jekyll";
$cleanGeneratorVersion = str_replace("Jekyll v", "", $generator);
}
else if(strpos($generator, "JSitemap Pro") === 0) {
// JSitemap Pro
$cleanGenerator = "Joomla!";
}
else if(strpos($generator, "Medium") === 0) {
// Medium
$cleanGenerator = "Medium";
}
else if(strpos($generator, "Movable Type Pro") === 0) {
// Movable Type Pro
$cleanGenerator = "Movable Type";
}
else if(strpos($generator, "newtelligence dasBlog ") === 0) {
// newtelligence dasBlog 4.0.0.0
$cleanGenerator = "dasBlog";
$cleanGeneratorVersion = str_replace("newtelligence dasBlog ", "", $generator);
}
else if(strpos($generator, "oldSchool v") === 0) {
// oldSchool v0.5.59
$cleanGenerator = "oldSchool";
$cleanGeneratorVersion = str_replace("oldSchool v", "", $generator);
}
else if(strpos($generator, "PyNITLog") === 0) {
// PyNITLog
$cleanGenerator = "PyNITLog";
}
else if(strpos($generator, "Site-Server v") === 0) {
// Site-Server v6.0.0-25071-25071 (http://www.squarespace.com)
$cleanGenerator = "SquareSpace Site-Server";
$trimmedStart = str_replace("Site-Server v", "", $generator);
$cleanGeneratorVersion = substr($trimmedStart, 0, strpos($trimmedStart, " (http://www.squarespace.com)"));
}
else if(strpos($generator, "Substack") === 0) {
// Substack
$cleanGenerator = "Substack";
}
else if(strpos($generator, "Svbtle.com") === 0) {
// Svbtle.com
$cleanGenerator = "Svbtle";
}
else if(strpos($generator, "TypePad") === 0) {
// TypePad
$cleanGenerator = "TypePad";
}
$sth = $this->pdo->prepare("
WITH feed AS (
SELECT id FROM ttrss_feeds WHERE feed_url = :feedUrl)
INSERT INTO ttrss_plugin_more_feed_data (feedid, generator, generatoruri, generatorversion, cleanGenerator, cleanGeneratorVersion)
SELECT id, :generator, :generatorUri, :generatorVersion, :cleanGenerator, :cleanGeneratorVersion
FROM feed
ON CONFLICT (feedId) DO UPDATE SET
generator = EXCLUDED.generator,
generatorUri = EXCLUDED.generatorUri,
generatorVersion = EXCLUDED.generatorVersion,
cleanGenerator = EXCLUDED.cleanGenerator,
cleanGeneratorVersion = EXCLUDED.cleanGeneratorVersion;");
if ($sth->execute(array(
':feedUrl' => $fetch_url,
':generator' => $generator,
':generatorUri' => $generatorUri,
':generatorVersion' => $generatorVersion,
':cleanGenerator' => $cleanGenerator,
':cleanGeneratorVersion' => $cleanGeneratorVersion))) {
//print("<p>Inserted/Updated</p>");
}
}
else {
// TODO: blank the values in the database.
//print("<p>Generator node not found.</p>");
}
}
} else {
user_error("Unknown/unsupported feed type", E_USER_NOTICE);
}
}
function about() {
return array(1.2,
"Stores more feed data",
"Aaron Axvig",
false,
"https://github.com/aaronaxvig/ttrss-plugin-more-feed-data");
}
function init($host) {
$this->host = $host;
$host->add_hook($host::HOOK_FEED_FETCHED, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
$host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
}
function hook_prefs_edit_feed($feed_id) {
printf("<h2>More feed data</h2>");
$sth = $this->pdo->prepare("SELECT * FROM public.ttrss_plugin_more_feed_data WHERE feedId = :feedId;");
if ($sth->execute(array(
':feedId' => $feed_id))) {
$row = $sth->fetch();
printf("<ol>");
printf("<li>Generator: %s</li>", $row['generator']);
printf("<li>Generator URI: %s</li>", $row['generatoruri']);
printf("<li>Generator version: %s</li>", $row['generatorversion']);
printf("<li>Generator (clean): %s</li>", $row['cleangenerator']);
printf("<li>Generator version (clean): %s</li>", $row['cleangeneratorversion']);
printf("</ol>");
}
else {
printf("<p>Failed to retrieve data for feedId %s</p>", $feed_id);
}
}
function hook_feed_fetched($feed_data, $fetch_url, $owner_uid, $feed) {
$this->processFeed($feed_data, $fetch_url);
return $feed_data;
}
function hook_prefs_tab($args) {
if ($args != "prefPrefs") return;
print "<div dojoType='dijit.layout.AccordionPane' title=\"<i class='material-icons'>storage</i> More Feed Data plugin\">";
print "<h2>Database schema</h2>";
$this->check_database();
print "<h2>Feed generator stats</h2>";
$sth = $this->pdo->prepare("SELECT COUNT(cleanGenerator) AS count, cleanGenerator
FROM ttrss_plugin_more_feed_data
GROUP BY cleanGenerator
ORDER BY COUNT(cleanGenerator) DESC;");
if ($sth->execute()) {
printf("<table><thead><th>Count</th><th>Generator</th></thead><tbody>");
while($row = $sth->fetch()) {
printf("<tr><td>%s</td><td>%s</td></tr>", $row['count'], $row['cleangenerator']);
}
printf("</tbody></table>");
}
else {
printf("<p>Failed to retrieve data for feedId %s</p>", $feed_id);
}
// For testing...
$fetch_url = "https://wordpress.org/feed/";
$feed_data = file_get_contents("plugins.local/more_feed_data/sampleFeed.xml");
//$this->processFeed($feed_data, $fetch_url);
// End for testing...
print "</div>";
}
function api_version() {
return 2;
}
}
?>