]> git.wh0rd.org - tt-rss.git/blame - plugins/import_export/init.php
debug logging system rework:
[tt-rss.git] / plugins / import_export / init.php
CommitLineData
6c2637d9
AD
1<?php
2class Import_Export extends Plugin implements IHandler {
6c2637d9
AD
3 private $host;
4
d2a421e3 5 function init($host) {
6c2637d9
AD
6 $this->host = $host;
7
8 $host->add_hook($host::HOOK_PREFS_TAB, $this);
f58df872 9 $host->add_command("xml-import", "import articles from XML", $this, ":", "FILE");
6c2637d9
AD
10 }
11
d2a421e3 12 function about() {
7a866114 13 return array(1.0,
0ac22f29 14 "Imports and exports user data using neutral XML format",
7a866114
AD
15 "fox");
16 }
17
6c2637d9 18 function xml_import($args) {
6c2637d9 19
f58df872 20 $filename = $args['xml_import'];
6c2637d9
AD
21
22 if (!is_file($filename)) {
23 print "error: input filename ($filename) doesn't exist.\n";
24 return;
25 }
26
c10a4306 27 Debug::log("please enter your username:");
f58df872 28
a369a6d9 29 $username = trim(read_stdin());
f58df872 30
c10a4306 31 Debug::log("importing $filename for user $username...\n");
6c2637d9 32
a369a6d9
AD
33 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
34 $sth->execute($username);
6c2637d9 35
a369a6d9
AD
36 if ($row = $sth->fetch()) {
37 $owner_uid = $row['id'];
38
39 $this->perform_data_import($filename, $owner_uid);
40 } else {
6c2637d9
AD
41 print "error: could not find user $username.\n";
42 return;
43 }
6c2637d9
AD
44 }
45
46 function get_prefs_js() {
47 return file_get_contents(dirname(__FILE__) . "/import_export.js");
48 }
49
50 function hook_prefs_tab($args) {
51 if ($args != "prefFeeds") return;
52
53 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
54
11334fdf 55 print_notice(__("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version."));
6c2637d9 56
11334fdf 57 print "<p>";
6c2637d9
AD
58
59 print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
60 __('Export my data')."</button> ";
61
62 print "<hr>";
63
64 print "<iframe id=\"data_upload_iframe\"
65 name=\"data_upload_iframe\" onload=\"dataImportComplete(this)\"
66 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
67
68 print "<form name=\"import_form\" style='display : block' target=\"data_upload_iframe\"
69 enctype=\"multipart/form-data\" method=\"POST\"
70 action=\"backend.php\">
a369a6d9
AD
71 <label class=\"dijitButton\">".__("Choose file...")."
72 <input style=\"display : none\" id=\"export_file\" name=\"export_file\" type=\"file\">&nbsp;
73 </label>
6c2637d9
AD
74 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
75 <input type=\"hidden\" name=\"plugin\" value=\"import_export\">
76 <input type=\"hidden\" name=\"method\" value=\"dataimport\">
77 <button dojoType=\"dijit.form.Button\" onclick=\"return importData();\" type=\"submit\">" .
78 __('Import') . "</button>";
79
b229a184 80 print "</form>";
6c2637d9 81
11334fdf
AD
82 print "</p>";
83
6c2637d9
AD
84 print "</div>"; # pane
85 }
86
87 function csrf_ignore($method) {
88 return in_array($method, array("exportget"));
89 }
90
21ce7d9e
AD
91 /**
92 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
93 */
6c2637d9
AD
94 function before($method) {
95 return $_SESSION["uid"] != false;
96 }
97
98 function after() {
99 return true;
100 }
101
21ce7d9e
AD
102 /**
103 * @SuppressWarnings(unused)
104 */
6c2637d9
AD
105 function exportget() {
106 $exportname = CACHE_DIR . "/export/" .
107 sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
108
109 if (file_exists($exportname)) {
110 header("Content-type: text/xml");
111
77e81006
AD
112 $timestamp_suffix = date("Y-m-d", filemtime($exportname));
113
6c2637d9 114 if (function_exists('gzencode')) {
77e81006 115 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz");
6c2637d9
AD
116 echo gzencode(file_get_contents($exportname));
117 } else {
77e81006 118 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml");
6c2637d9
AD
119 echo file_get_contents($exportname);
120 }
121 } else {
122 echo "File not found.";
123 }
124 }
125
126 function exportrun() {
a369a6d9 127 $offset = (int) $_REQUEST['offset'];
6c2637d9
AD
128 $exported = 0;
129 $limit = 250;
130
131 if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
a369a6d9
AD
132
133 $sth = $this->pdo->prepare("SELECT
6c2637d9
AD
134 ttrss_entries.guid,
135 ttrss_entries.title,
136 content,
137 marked,
138 published,
139 score,
140 note,
141 link,
142 tag_cache,
143 label_cache,
144 ttrss_feeds.title AS feed_title,
145 ttrss_feeds.feed_url AS feed_url,
146 ttrss_entries.updated
147 FROM
148 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
149 ttrss_entries
150 WHERE
151 (marked = true OR feed_id IS NULL) AND
152 ref_id = ttrss_entries.id AND
a369a6d9 153 ttrss_user_entries.owner_uid = ?
63616430 154 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
a369a6d9 155
63616430 156 $sth->execute([$_SESSION['uid']]);
6c2637d9
AD
157
158 $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
159
160 if ($offset == 0) {
161 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
162 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
163 } else {
164 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
165 }
166
167 if ($fp) {
168
a369a6d9
AD
169 $exported = 0;
170 while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
171 ++$exported;
172
173 fputs($fp, "<article>\n");
6c2637d9
AD
174
175 foreach ($line as $k => $v) {
a369a6d9
AD
176
177 fputs($fp, " ");
178
179 if (is_bool($v))
180 $v = (int) $v;
181
182 if (!$v || is_numeric($v)) {
183 fputs($fp, "<$k>$v</$k>\n");
184 } else {
185 $v = str_replace("]]>", "]]]]><![CDATA[>", $v);
186 fputs($fp, "<$k><![CDATA[$v]]></$k>\n");
187 }
6c2637d9
AD
188 }
189
a369a6d9 190 fputs($fp, "</article>\n");
6c2637d9
AD
191 }
192
6c2637d9
AD
193 if ($exported < $limit && $exported > 0) {
194 fputs($fp, "</articles>");
195 }
196
197 fclose($fp);
198 }
199
200 }
201
202 print json_encode(array("exported" => $exported));
203 }
204
a42c55f0 205 function perform_data_import($filename, $owner_uid) {
6c2637d9
AD
206
207 $num_imported = 0;
208 $num_processed = 0;
209 $num_feeds_created = 0;
210
4262e001 211 libxml_disable_entity_loader(false);
212
a369a6d9 213 $doc = new DOMDocument();
6c2637d9 214
a369a6d9 215 if (!$doc_loaded = @$doc->load($filename)) {
6c2637d9
AD
216 $contents = file_get_contents($filename);
217
218 if ($contents) {
219 $data = @gzuncompress($contents);
220 }
221
222 if (!$data) {
223 $data = @gzdecode($contents);
224 }
225
226 if ($data)
a369a6d9 227 $doc_loaded = $doc->loadXML($data);
6c2637d9
AD
228 }
229
4262e001 230 libxml_disable_entity_loader(true);
231
a369a6d9 232 if ($doc_loaded) {
6c2637d9
AD
233
234 $xpath = new DOMXpath($doc);
235
236 $container = $doc->firstChild;
237
238 if ($container && $container->hasAttribute('schema-version')) {
239 $schema_version = $container->getAttribute('schema-version');
240
241 if ($schema_version != SCHEMA_VERSION) {
242 print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
243 return;
244 }
245
246 } else {
247 print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
248 return;
249 }
250
251 $articles = $xpath->query("//article");
252
253 foreach ($articles as $article_node) {
254 if ($article_node->childNodes) {
255
256 $ref_id = 0;
257
258 $article = array();
259
260 foreach ($article_node->childNodes as $child) {
a369a6d9 261 if ($child->nodeName == 'content' || $child->nodeName == 'label_cache') {
6c2637d9 262 $article[$child->nodeName] = $child->nodeValue;
ee4c4602 263 } else {
a369a6d9 264 $article[$child->nodeName] = clean($child->nodeValue);
ee4c4602 265 }
6c2637d9
AD
266 }
267
268 //print_r($article);
269
270 if ($article['guid']) {
271
272 ++$num_processed;
273
a369a6d9 274 $this->pdo->beginTransaction();
6c2637d9
AD
275
276 //print 'GUID:' . $article['guid'] . "\n";
277
a369a6d9
AD
278 $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
279 WHERE guid = ?");
280 $sth->execute([$article['guid']]);
6c2637d9 281
a369a6d9
AD
282 if ($row = $sth->fetch()) {
283 $ref_id = $row['id'];
284 } else {
285 $sth = $this->pdo->prepare(
6c2637d9
AD
286 "INSERT INTO ttrss_entries
287 (title,
288 guid,
289 link,
290 updated,
291 content,
292 content_hash,
293 no_orig_date,
294 date_updated,
295 date_entered,
296 comments,
297 num_comments,
298 author)
299 VALUES
a369a6d9 300 (?, ?, ?, ?, ?, ?,
6c2637d9
AD
301 false,
302 NOW(),
303 NOW(),
304 '',
305 '0',
306 '')");
82152bdc 307
a369a6d9
AD
308 $sth->execute([
309 $article['title'],
310 $article['guid'],
311 $article['link'],
312 $article['updated'],
313 $article['content'],
314 sha1($article['content'])
315 ]);
316
317 $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
318 WHERE guid = ?");
319 $sth->execute([$article['guid']]);
320
321 if ($row = $sth->fetch()) {
322 $ref_id = $row['id'];
6c2637d9 323 }
6c2637d9
AD
324 }
325
326 //print "Got ref ID: $ref_id\n";
327
328 if ($ref_id) {
329
a369a6d9 330 $feed = NULL;
6c2637d9 331
a369a6d9 332 if ($article['feed_url'] && $article['feed_title']) {
6c2637d9 333
a369a6d9
AD
334 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
335 WHERE feed_url = ? AND owner_uid = ?");
336 $sth->execute([$article['feed_url'], $owner_uid]);
6c2637d9 337
a369a6d9
AD
338 if ($row = $sth->fetch()) {
339 $feed = $row['id'];
6c2637d9
AD
340 } else {
341 // try autocreating feed in Uncategorized...
342
a369a6d9
AD
343 $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds (owner_uid,
344 feed_url, title) VALUES (?, ?, ?)");
345 $res = $sth->execute([$owner_uid, $article['feed_url'], $article['feed_title']]);
6c2637d9 346
a369a6d9
AD
347 if ($res) {
348 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
349 WHERE feed_url = ? AND owner_uid = ?");
350 $sth->execute([$article['feed_url'], $owner_uid]);
6c2637d9 351
a369a6d9
AD
352 if ($row = $sth->fetch()) {
353 ++$num_feeds_created;
6c2637d9 354
a369a6d9
AD
355 $feed = $row['id'];
356 }
6c2637d9
AD
357 }
358 }
359 }
360
a369a6d9
AD
361 if ($feed)
362 $feed_qpart = "feed_id = " . (int) $feed;
6c2637d9
AD
363 else
364 $feed_qpart = "feed_id IS NULL";
365
366 //print "$ref_id / $feed / " . $article['title'] . "\n";
367
a369a6d9
AD
368 $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries
369 WHERE ref_id = ? AND owner_uid = ? AND $feed_qpart");
370 $sth->execute([$ref_id, $owner_uid]);
6c2637d9 371
a369a6d9 372 if (!$sth->fetch()) {
6c2637d9 373
6c2637d9
AD
374 $score = (int) $article['score'];
375
376 $tag_cache = $article['tag_cache'];
6c2637d9
AD
377 $note = $article['note'];
378
379 //print "Importing " . $article['title'] . "<br/>";
380
381 ++$num_imported;
382
a369a6d9 383 $sth = $this->pdo->prepare(
6c2637d9
AD
384 "INSERT INTO ttrss_user_entries
385 (ref_id, owner_uid, feed_id, unread, last_read, marked,
386 published, score, tag_cache, label_cache, uuid, note)
a369a6d9
AD
387 VALUES (?, ?, ?, false,
388 NULL, ?, ?, ?, ?, '', '', ?)");
389
390 $res = $sth->execute([
391 $ref_id,
392 $owner_uid,
393 $feed,
394 (int)sql_bool_to_bool($article['marked']),
395 (int)sql_bool_to_bool($article['published']),
396 $score,
397 $tag_cache,
398 $note]);
399
400 if ($res) {
401
402 $label_cache = json_decode($article['label_cache'], true);
403
404 if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
405 foreach ($label_cache as $label) {
406 Labels::create($label[1],
407 $label[2], $label[3], $owner_uid);
408
409 Labels::add_article($ref_id, $label[1], $owner_uid);
410 }
6c2637d9
AD
411 }
412 }
6c2637d9
AD
413 }
414 }
a369a6d9
AD
415
416 $this->pdo->commit();
6c2637d9
AD
417 }
418 }
419 }
420
421 print "<p>" .
f58df872 422 __("Finished: ").
d3b0e348
AD
423 vsprintf(_ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
424 vsprintf(_ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
425 vsprintf(_ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
6c2637d9
AD
426 "</p>";
427
428 } else {
429
430 print "<p>" . __("Could not load XML document.") . "</p>";
431
432 }
433 }
434
435 function exportData() {
436
437 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>";
438
439 print "<div align='center'>";
440 print "<button dojoType=\"dijit.form.Button\"
441 onclick=\"dijit.byId('dataExportDlg').prepare()\">".
442 __('Prepare data')."</button>";
443
444 print "<button dojoType=\"dijit.form.Button\"
445 onclick=\"dijit.byId('dataExportDlg').hide()\">".
446 __('Close this window')."</button>";
447
448 print "</div>";
449
450
451 }
452
453 function dataImport() {
454 header("Content-Type: text/html"); # required for iframe
455
456 print "<div style='text-align : center'>";
457
b229a184 458 if ($_FILES['export_file']['error'] != 0) {
1a322ff3
AD
459 print_error(T_sprintf("Upload failed with error code %d (%s)",
460 $_FILES['export_file']['error'],
461 get_upload_error_message($_FILES['export_file']['error'])));
462 } else {
b229a184 463
1a322ff3
AD
464 if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
465 $tmp_file = tempnam(CACHE_DIR . '/upload', 'export');
6c2637d9 466
1a322ff3
AD
467 $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
468 $tmp_file);
b229a184 469
1a322ff3
AD
470 if (!$result) {
471 print_error(__("Unable to move uploaded file."));
472 return;
473 }
474 } else {
475 print_error(__('Error: please upload OPML file.'));
b229a184
AD
476 return;
477 }
6c2637d9 478
1a322ff3
AD
479 if (is_file($tmp_file)) {
480 $this->perform_data_import($tmp_file, $_SESSION['uid']);
481 unlink($tmp_file);
482 } else {
483 print_error(__('No file uploaded.'));
484 return;
485 }
6c2637d9
AD
486 }
487
488 print "<button dojoType=\"dijit.form.Button\"
489 onclick=\"dijit.byId('dataImportDlg').hide()\">".
490 __('Close this window')."</button>";
491
492 print "</div>";
493
494 }
495
106a3de9
AD
496 function api_version() {
497 return 2;
498 }
6c2637d9 499
365f5c8b 500}