]>
Commit | Line | Data |
---|---|---|
6c2637d9 AD |
1 | <?php |
2 | class 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 | ||
365f5c8b AD |
18 | private function bool_to_sql_bool($s) { |
19 | return $s ? 'true' : 'false'; | |
20 | } | |
21 | ||
6c2637d9 | 22 | function xml_import($args) { |
6c2637d9 | 23 | |
f58df872 | 24 | $filename = $args['xml_import']; |
6c2637d9 AD |
25 | |
26 | if (!is_file($filename)) { | |
27 | print "error: input filename ($filename) doesn't exist.\n"; | |
28 | return; | |
29 | } | |
30 | ||
f58df872 AD |
31 | _debug("please enter your username:"); |
32 | ||
a42c55f0 | 33 | $username = db_escape_string(trim(read_stdin())); |
f58df872 | 34 | |
6c2637d9 AD |
35 | _debug("importing $filename for user $username...\n"); |
36 | ||
a42c55f0 | 37 | $result = db_query("SELECT id FROM ttrss_users WHERE login = '$username'"); |
6c2637d9 AD |
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 | ||
a42c55f0 | 46 | $this->perform_data_import($filename, $owner_uid); |
6c2637d9 AD |
47 | } |
48 | ||
49 | function save() { | |
a42c55f0 | 50 | $example_value = db_escape_string($_POST["example_value"]); |
6c2637d9 AD |
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 | ||
11334fdf | 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.")); |
6c2637d9 | 65 | |
11334fdf | 66 | print "<p>"; |
6c2637d9 AD |
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 | ||
b229a184 | 87 | print "</form>"; |
6c2637d9 | 88 | |
11334fdf AD |
89 | print "</p>"; |
90 | ||
6c2637d9 AD |
91 | print "</div>"; # pane |
92 | } | |
93 | ||
94 | function csrf_ignore($method) { | |
95 | return in_array($method, array("exportget")); | |
96 | } | |
97 | ||
21ce7d9e AD |
98 | /** |
99 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
100 | */ | |
6c2637d9 AD |
101 | function before($method) { |
102 | return $_SESSION["uid"] != false; | |
103 | } | |
104 | ||
105 | function after() { | |
106 | return true; | |
107 | } | |
108 | ||
21ce7d9e AD |
109 | /** |
110 | * @SuppressWarnings(unused) | |
111 | */ | |
6c2637d9 AD |
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 | ||
77e81006 AD |
119 | $timestamp_suffix = date("Y-m-d", filemtime($exportname)); |
120 | ||
6c2637d9 | 121 | if (function_exists('gzencode')) { |
77e81006 | 122 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz"); |
6c2637d9 AD |
123 | echo gzencode(file_get_contents($exportname)); |
124 | } else { | |
77e81006 | 125 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml"); |
6c2637d9 AD |
126 | echo file_get_contents($exportname); |
127 | } | |
128 | } else { | |
129 | echo "File not found."; | |
130 | } | |
131 | } | |
132 | ||
133 | function exportrun() { | |
a42c55f0 | 134 | $offset = (int) db_escape_string($_REQUEST['offset']); |
6c2637d9 AD |
135 | $exported = 0; |
136 | $limit = 250; | |
137 | ||
138 | if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) { | |
a42c55f0 | 139 | $result = db_query("SELECT |
6c2637d9 AD |
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) { | |
c541d3a5 | 177 | $v = str_replace("]]>", "]]]]><![CDATA[>", $v); |
6c2637d9 AD |
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 | ||
a42c55f0 | 198 | function perform_data_import($filename, $owner_uid) { |
6c2637d9 AD |
199 | |
200 | $num_imported = 0; | |
201 | $num_processed = 0; | |
202 | $num_feeds_created = 0; | |
203 | ||
4262e001 | 204 | libxml_disable_entity_loader(false); |
205 | ||
6c2637d9 AD |
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) | |
1ddf3a28 | 220 | $doc = (new DOMDocument)->loadXML($data); |
6c2637d9 AD |
221 | } |
222 | ||
4262e001 | 223 | libxml_disable_entity_loader(true); |
224 | ||
6c2637d9 AD |
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) { | |
ee4c4602 AD |
254 | if ($child->nodeName == 'content') { |
255 | $article[$child->nodeName] = db_escape_string($child->nodeValue, false); | |
256 | } else if ($child->nodeName == 'label_cache') { | |
6c2637d9 | 257 | $article[$child->nodeName] = $child->nodeValue; |
ee4c4602 AD |
258 | } else { |
259 | $article[$child->nodeName] = db_escape_string($child->nodeValue); | |
260 | } | |
6c2637d9 AD |
261 | } |
262 | ||
263 | //print_r($article); | |
264 | ||
265 | if ($article['guid']) { | |
266 | ||
267 | ++$num_processed; | |
268 | ||
a42c55f0 | 269 | //db_query("BEGIN"); |
6c2637d9 AD |
270 | |
271 | //print 'GUID:' . $article['guid'] . "\n"; | |
272 | ||
a42c55f0 | 273 | $result = db_query("SELECT id FROM ttrss_entries |
6c2637d9 AD |
274 | WHERE guid = '".$article['guid']."'"); |
275 | ||
276 | if (db_num_rows($result) == 0) { | |
277 | ||
6322ac79 | 278 | $result = db_query( |
6c2637d9 AD |
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 | ||
a42c55f0 | 306 | $result = db_query("SELECT id FROM ttrss_entries |
6c2637d9 AD |
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) { | |
a42c55f0 | 327 | $result = db_query("SELECT id FROM ttrss_feeds |
6c2637d9 AD |
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 | ||
a42c55f0 | 335 | $result = db_query("INSERT INTO ttrss_feeds (owner_uid, |
6c2637d9 AD |
336 | feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')"); |
337 | ||
a42c55f0 | 338 | $result = db_query("SELECT id FROM ttrss_feeds |
6c2637d9 AD |
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 | ||
a42c55f0 | 356 | $result = db_query("SELECT int_id FROM ttrss_user_entries |
6c2637d9 AD |
357 | WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart"); |
358 | ||
359 | if (db_num_rows($result) == 0) { | |
360 | ||
365f5c8b AD |
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'])); | |
6c2637d9 AD |
363 | $score = (int) $article['score']; |
364 | ||
365 | $tag_cache = $article['tag_cache']; | |
6c2637d9 AD |
366 | $note = $article['note']; |
367 | ||
368 | //print "Importing " . $article['title'] . "<br/>"; | |
369 | ||
370 | ++$num_imported; | |
371 | ||
6322ac79 | 372 | $result = db_query( |
6c2637d9 AD |
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', | |
ee4c4602 | 378 | '', '', '$note')"); |
6c2637d9 | 379 | |
ee4c4602 | 380 | $label_cache = json_decode($article['label_cache'], true); |
6c2637d9 AD |
381 | |
382 | if (is_array($label_cache) && $label_cache["no-labels"] != 1) { | |
383 | foreach ($label_cache as $label) { | |
384 | ||
7c9b5a3f | 385 | Labels::create($label[1], |
6c2637d9 AD |
386 | $label[2], $label[3], $owner_uid); |
387 | ||
7c9b5a3f | 388 | Labels::add_article($ref_id, $label[1], $owner_uid); |
6c2637d9 AD |
389 | |
390 | } | |
391 | } | |
392 | ||
a42c55f0 | 393 | //db_query("COMMIT"); |
6c2637d9 AD |
394 | } |
395 | } | |
396 | } | |
397 | } | |
398 | } | |
399 | ||
400 | print "<p>" . | |
f58df872 | 401 | __("Finished: "). |
d3b0e348 AD |
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). | |
6c2637d9 AD |
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 | ||
b229a184 | 437 | if ($_FILES['export_file']['error'] != 0) { |
1a322ff3 AD |
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 { | |
b229a184 | 442 | |
1a322ff3 | 443 | $tmp_file = false; |
6c2637d9 | 444 | |
1a322ff3 AD |
445 | if (is_uploaded_file($_FILES['export_file']['tmp_name'])) { |
446 | $tmp_file = tempnam(CACHE_DIR . '/upload', 'export'); | |
6c2637d9 | 447 | |
1a322ff3 AD |
448 | $result = move_uploaded_file($_FILES['export_file']['tmp_name'], |
449 | $tmp_file); | |
b229a184 | 450 | |
1a322ff3 AD |
451 | if (!$result) { |
452 | print_error(__("Unable to move uploaded file.")); | |
453 | return; | |
454 | } | |
455 | } else { | |
456 | print_error(__('Error: please upload OPML file.')); | |
b229a184 AD |
457 | return; |
458 | } | |
6c2637d9 | 459 | |
1a322ff3 AD |
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 | } | |
6c2637d9 AD |
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 | ||
106a3de9 AD |
477 | function api_version() { |
478 | return 2; | |
479 | } | |
6c2637d9 | 480 | |
365f5c8b | 481 | } |