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