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