]>
Commit | Line | Data |
---|---|---|
6c2637d9 AD |
1 | <?php |
2 | class 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\"> | |
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 | ||
86 | ||
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() { | |
3972bf59 | 122 | $offset = (int) db_escape_string($this->link, $_REQUEST['offset']); |
6c2637d9 AD |
123 | $exported = 0; |
124 | $limit = 250; | |
125 | ||
126 | if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) { | |
127 | $result = db_query($this->link, "SELECT | |
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 | ||
186 | function perform_data_import($link, $filename, $owner_uid) { | |
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') | |
3972bf59 | 239 | $article[$child->nodeName] = db_escape_string($this->link, $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 | ||
250 | //db_query($link, "BEGIN"); | |
251 | ||
252 | //print 'GUID:' . $article['guid'] . "\n"; | |
253 | ||
254 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
255 | WHERE guid = '".$article['guid']."'"); | |
256 | ||
257 | if (db_num_rows($result) == 0) { | |
258 | ||
259 | $result = db_query($link, | |
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 | ||
287 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
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) { | |
308 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
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 | ||
316 | $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid, | |
317 | feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')"); | |
318 | ||
319 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
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 | ||
337 | $result = db_query($link, "SELECT int_id FROM ttrss_user_entries | |
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']; | |
3972bf59 | 347 | $label_cache = db_escape_string($this->link, $article['label_cache']); |
6c2637d9 AD |
348 | $note = $article['note']; |
349 | ||
350 | //print "Importing " . $article['title'] . "<br/>"; | |
351 | ||
352 | ++$num_imported; | |
353 | ||
354 | $result = db_query($link, | |
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 | ||
367 | label_create($link, $label[1], | |
368 | $label[2], $label[3], $owner_uid); | |
369 | ||
370 | label_add_article($link, $ref_id, $label[1], $owner_uid); | |
371 | ||
372 | } | |
373 | } | |
374 | ||
375 | //db_query($link, "COMMIT"); | |
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 | ||
419 | if (is_file($_FILES['export_file']['tmp_name'])) { | |
420 | ||
421 | $this->perform_data_import($this->link, $_FILES['export_file']['tmp_name'], $_SESSION['uid']); | |
422 | ||
423 | } else { | |
e5c3f0b2 | 424 | print "<p>" . T_sprintf("Could not upload file. You might need to adjust upload_max_filesize in PHP.ini (current value = %s)", ini_get("upload_max_filesize")) . " or use CLI import tool.</p>"; |
6c2637d9 AD |
425 | |
426 | } | |
427 | ||
428 | print "<button dojoType=\"dijit.form.Button\" | |
429 | onclick=\"dijit.byId('dataImportDlg').hide()\">". | |
430 | __('Close this window')."</button>"; | |
431 | ||
432 | print "</div>"; | |
433 | ||
434 | } | |
435 | ||
436 | ||
437 | } | |
438 | ?> |