]>
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 | ||
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\"> | |
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 | ||
94 | function before($method) { | |
95 | return $_SESSION["uid"] != false; | |
96 | } | |
97 | ||
98 | function after() { | |
99 | return true; | |
100 | } | |
101 | ||
102 | function exportget() { | |
103 | $exportname = CACHE_DIR . "/export/" . | |
104 | sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml"; | |
105 | ||
106 | if (file_exists($exportname)) { | |
107 | header("Content-type: text/xml"); | |
108 | ||
77e81006 AD |
109 | $timestamp_suffix = date("Y-m-d", filemtime($exportname)); |
110 | ||
6c2637d9 | 111 | if (function_exists('gzencode')) { |
77e81006 | 112 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz"); |
6c2637d9 AD |
113 | echo gzencode(file_get_contents($exportname)); |
114 | } else { | |
77e81006 | 115 | header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml"); |
6c2637d9 AD |
116 | echo file_get_contents($exportname); |
117 | } | |
118 | } else { | |
119 | echo "File not found."; | |
120 | } | |
121 | } | |
122 | ||
123 | function exportrun() { | |
a42c55f0 | 124 | $offset = (int) db_escape_string($_REQUEST['offset']); |
6c2637d9 AD |
125 | $exported = 0; |
126 | $limit = 250; | |
127 | ||
128 | if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) { | |
a42c55f0 | 129 | $result = db_query("SELECT |
6c2637d9 AD |
130 | ttrss_entries.guid, |
131 | ttrss_entries.title, | |
132 | content, | |
133 | marked, | |
134 | published, | |
135 | score, | |
136 | note, | |
137 | link, | |
138 | tag_cache, | |
139 | label_cache, | |
140 | ttrss_feeds.title AS feed_title, | |
141 | ttrss_feeds.feed_url AS feed_url, | |
142 | ttrss_entries.updated | |
143 | FROM | |
144 | ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id), | |
145 | ttrss_entries | |
146 | WHERE | |
147 | (marked = true OR feed_id IS NULL) AND | |
148 | ref_id = ttrss_entries.id AND | |
149 | ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . " | |
150 | ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset"); | |
151 | ||
152 | $exportname = sha1($_SESSION['uid'] . $_SESSION['login']); | |
153 | ||
154 | if ($offset == 0) { | |
155 | $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w"); | |
156 | fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">"); | |
157 | } else { | |
158 | $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a"); | |
159 | } | |
160 | ||
161 | if ($fp) { | |
162 | ||
163 | while ($line = db_fetch_assoc($result)) { | |
164 | fputs($fp, "<article>"); | |
165 | ||
166 | foreach ($line as $k => $v) { | |
c541d3a5 | 167 | $v = str_replace("]]>", "]]]]><![CDATA[>", $v); |
6c2637d9 AD |
168 | fputs($fp, "<$k><![CDATA[$v]]></$k>"); |
169 | } | |
170 | ||
171 | fputs($fp, "</article>"); | |
172 | } | |
173 | ||
174 | $exported = db_num_rows($result); | |
175 | ||
176 | if ($exported < $limit && $exported > 0) { | |
177 | fputs($fp, "</articles>"); | |
178 | } | |
179 | ||
180 | fclose($fp); | |
181 | } | |
182 | ||
183 | } | |
184 | ||
185 | print json_encode(array("exported" => $exported)); | |
186 | } | |
187 | ||
a42c55f0 | 188 | function perform_data_import($filename, $owner_uid) { |
6c2637d9 AD |
189 | |
190 | $num_imported = 0; | |
191 | $num_processed = 0; | |
192 | $num_feeds_created = 0; | |
193 | ||
4262e001 | 194 | libxml_disable_entity_loader(false); |
195 | ||
6c2637d9 AD |
196 | $doc = @DOMDocument::load($filename); |
197 | ||
198 | if (!$doc) { | |
199 | $contents = file_get_contents($filename); | |
200 | ||
201 | if ($contents) { | |
202 | $data = @gzuncompress($contents); | |
203 | } | |
204 | ||
205 | if (!$data) { | |
206 | $data = @gzdecode($contents); | |
207 | } | |
208 | ||
209 | if ($data) | |
210 | $doc = DOMDocument::loadXML($data); | |
211 | } | |
212 | ||
4262e001 | 213 | libxml_disable_entity_loader(true); |
214 | ||
6c2637d9 AD |
215 | if ($doc) { |
216 | ||
217 | $xpath = new DOMXpath($doc); | |
218 | ||
219 | $container = $doc->firstChild; | |
220 | ||
221 | if ($container && $container->hasAttribute('schema-version')) { | |
222 | $schema_version = $container->getAttribute('schema-version'); | |
223 | ||
224 | if ($schema_version != SCHEMA_VERSION) { | |
225 | print "<p>" .__("Could not import: incorrect schema version.") . "</p>"; | |
226 | return; | |
227 | } | |
228 | ||
229 | } else { | |
230 | print "<p>" . __("Could not import: unrecognized document format.") . "</p>"; | |
231 | return; | |
232 | } | |
233 | ||
234 | $articles = $xpath->query("//article"); | |
235 | ||
236 | foreach ($articles as $article_node) { | |
237 | if ($article_node->childNodes) { | |
238 | ||
239 | $ref_id = 0; | |
240 | ||
241 | $article = array(); | |
242 | ||
243 | foreach ($article_node->childNodes as $child) { | |
ee4c4602 AD |
244 | if ($child->nodeName == 'content') { |
245 | $article[$child->nodeName] = db_escape_string($child->nodeValue, false); | |
246 | } else if ($child->nodeName == 'label_cache') { | |
6c2637d9 | 247 | $article[$child->nodeName] = $child->nodeValue; |
ee4c4602 AD |
248 | } else { |
249 | $article[$child->nodeName] = db_escape_string($child->nodeValue); | |
250 | } | |
6c2637d9 AD |
251 | } |
252 | ||
253 | //print_r($article); | |
254 | ||
255 | if ($article['guid']) { | |
256 | ||
257 | ++$num_processed; | |
258 | ||
a42c55f0 | 259 | //db_query("BEGIN"); |
6c2637d9 AD |
260 | |
261 | //print 'GUID:' . $article['guid'] . "\n"; | |
262 | ||
a42c55f0 | 263 | $result = db_query("SELECT id FROM ttrss_entries |
6c2637d9 AD |
264 | WHERE guid = '".$article['guid']."'"); |
265 | ||
266 | if (db_num_rows($result) == 0) { | |
267 | ||
6322ac79 | 268 | $result = db_query( |
6c2637d9 AD |
269 | "INSERT INTO ttrss_entries |
270 | (title, | |
271 | guid, | |
272 | link, | |
273 | updated, | |
274 | content, | |
275 | content_hash, | |
276 | no_orig_date, | |
277 | date_updated, | |
278 | date_entered, | |
279 | comments, | |
280 | num_comments, | |
281 | author) | |
282 | VALUES | |
283 | ('".$article['title']."', | |
284 | '".$article['guid']."', | |
285 | '".$article['link']."', | |
286 | '".$article['updated']."', | |
287 | '".$article['content']."', | |
288 | '".sha1($article['content'])."', | |
289 | false, | |
290 | NOW(), | |
291 | NOW(), | |
292 | '', | |
293 | '0', | |
294 | '')"); | |
295 | ||
a42c55f0 | 296 | $result = db_query("SELECT id FROM ttrss_entries |
6c2637d9 AD |
297 | WHERE guid = '".$article['guid']."'"); |
298 | ||
299 | if (db_num_rows($result) != 0) { | |
300 | $ref_id = db_fetch_result($result, 0, "id"); | |
301 | } | |
302 | ||
303 | } else { | |
304 | $ref_id = db_fetch_result($result, 0, "id"); | |
305 | } | |
306 | ||
307 | //print "Got ref ID: $ref_id\n"; | |
308 | ||
309 | if ($ref_id) { | |
310 | ||
311 | $feed_url = $article['feed_url']; | |
312 | $feed_title = $article['feed_title']; | |
313 | ||
314 | $feed = 'NULL'; | |
315 | ||
316 | if ($feed_url && $feed_title) { | |
a42c55f0 | 317 | $result = db_query("SELECT id FROM ttrss_feeds |
6c2637d9 AD |
318 | WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); |
319 | ||
320 | if (db_num_rows($result) != 0) { | |
321 | $feed = db_fetch_result($result, 0, "id"); | |
322 | } else { | |
323 | // try autocreating feed in Uncategorized... | |
324 | ||
a42c55f0 | 325 | $result = db_query("INSERT INTO ttrss_feeds (owner_uid, |
6c2637d9 AD |
326 | feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')"); |
327 | ||
a42c55f0 | 328 | $result = db_query("SELECT id FROM ttrss_feeds |
6c2637d9 AD |
329 | WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); |
330 | ||
331 | if (db_num_rows($result) != 0) { | |
332 | ++$num_feeds_created; | |
333 | ||
334 | $feed = db_fetch_result($result, 0, "id"); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | if ($feed != 'NULL') | |
340 | $feed_qpart = "feed_id = $feed"; | |
341 | else | |
342 | $feed_qpart = "feed_id IS NULL"; | |
343 | ||
344 | //print "$ref_id / $feed / " . $article['title'] . "\n"; | |
345 | ||
a42c55f0 | 346 | $result = db_query("SELECT int_id FROM ttrss_user_entries |
6c2637d9 AD |
347 | WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart"); |
348 | ||
349 | if (db_num_rows($result) == 0) { | |
350 | ||
351 | $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked'])); | |
352 | $published = bool_to_sql_bool(sql_bool_to_bool($article['published'])); | |
353 | $score = (int) $article['score']; | |
354 | ||
355 | $tag_cache = $article['tag_cache']; | |
6c2637d9 AD |
356 | $note = $article['note']; |
357 | ||
358 | //print "Importing " . $article['title'] . "<br/>"; | |
359 | ||
360 | ++$num_imported; | |
361 | ||
6322ac79 | 362 | $result = db_query( |
6c2637d9 AD |
363 | "INSERT INTO ttrss_user_entries |
364 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
365 | published, score, tag_cache, label_cache, uuid, note) | |
366 | VALUES ($ref_id, $owner_uid, $feed, false, | |
367 | NULL, $marked, $published, $score, '$tag_cache', | |
ee4c4602 | 368 | '', '', '$note')"); |
6c2637d9 | 369 | |
ee4c4602 | 370 | $label_cache = json_decode($article['label_cache'], true); |
6c2637d9 AD |
371 | |
372 | if (is_array($label_cache) && $label_cache["no-labels"] != 1) { | |
373 | foreach ($label_cache as $label) { | |
374 | ||
a42c55f0 | 375 | label_create($label[1], |
6c2637d9 AD |
376 | $label[2], $label[3], $owner_uid); |
377 | ||
a42c55f0 | 378 | label_add_article($ref_id, $label[1], $owner_uid); |
6c2637d9 AD |
379 | |
380 | } | |
381 | } | |
382 | ||
a42c55f0 | 383 | //db_query("COMMIT"); |
6c2637d9 AD |
384 | } |
385 | } | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
390 | print "<p>" . | |
f58df872 | 391 | __("Finished: "). |
d3b0e348 AD |
392 | vsprintf(_ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed). |
393 | vsprintf(_ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported). | |
394 | vsprintf(_ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created). | |
6c2637d9 AD |
395 | "</p>"; |
396 | ||
397 | } else { | |
398 | ||
399 | print "<p>" . __("Could not load XML document.") . "</p>"; | |
400 | ||
401 | } | |
402 | } | |
403 | ||
404 | function exportData() { | |
405 | ||
406 | print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>"; | |
407 | ||
408 | print "<div align='center'>"; | |
409 | print "<button dojoType=\"dijit.form.Button\" | |
410 | onclick=\"dijit.byId('dataExportDlg').prepare()\">". | |
411 | __('Prepare data')."</button>"; | |
412 | ||
413 | print "<button dojoType=\"dijit.form.Button\" | |
414 | onclick=\"dijit.byId('dataExportDlg').hide()\">". | |
415 | __('Close this window')."</button>"; | |
416 | ||
417 | print "</div>"; | |
418 | ||
419 | ||
420 | } | |
421 | ||
422 | function dataImport() { | |
423 | header("Content-Type: text/html"); # required for iframe | |
424 | ||
425 | print "<div style='text-align : center'>"; | |
426 | ||
b229a184 AD |
427 | if ($_FILES['export_file']['error'] != 0) { |
428 | print_error(T_sprintf("Upload failed with error code %d", | |
429 | $_FILES['export_file']['error'])); | |
430 | return; | |
431 | } | |
432 | ||
433 | $tmp_file = false; | |
6c2637d9 | 434 | |
b229a184 AD |
435 | if (is_uploaded_file($_FILES['export_file']['tmp_name'])) { |
436 | $tmp_file = tempnam(CACHE_DIR . '/upload', 'export'); | |
6c2637d9 | 437 | |
b229a184 AD |
438 | $result = move_uploaded_file($_FILES['export_file']['tmp_name'], |
439 | $tmp_file); | |
440 | ||
441 | if (!$result) { | |
442 | print_error(__("Unable to move uploaded file.")); | |
443 | return; | |
444 | } | |
6c2637d9 | 445 | } else { |
b229a184 AD |
446 | print_error(__('Error: please upload OPML file.')); |
447 | return; | |
448 | } | |
6c2637d9 | 449 | |
b229a184 | 450 | if (is_file($tmp_file)) { |
a42c55f0 | 451 | $this->perform_data_import($tmp_file, $_SESSION['uid']); |
b229a184 AD |
452 | unlink($tmp_file); |
453 | } else { | |
454 | print_error(__('No file uploaded.')); | |
455 | return; | |
6c2637d9 AD |
456 | } |
457 | ||
458 | print "<button dojoType=\"dijit.form.Button\" | |
459 | onclick=\"dijit.byId('dataImportDlg').hide()\">". | |
460 | __('Close this window')."</button>"; | |
461 | ||
462 | print "</div>"; | |
463 | ||
464 | } | |
465 | ||
106a3de9 AD |
466 | function api_version() { |
467 | return 2; | |
468 | } | |
6c2637d9 AD |
469 | |
470 | } | |
471 | ?> |