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