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