]> git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
de21dbf32ae65fa76ad33169d5c63defc7fcb552
[tt-rss.git] / plugins / import_export / init.php
1 <?php
2 class Import_Export extends Plugin implements IHandler {
3
4 private $link;
5 private $host;
6
7 function init($host) {
8 $this->link = $host->get_link();
9 $this->host = $host;
10
11 $host->add_hook($host::HOOK_PREFS_TAB, $this);
12 $host->add_command("xml-import", "USER FILE: import articles from XML", $this);
13 }
14
15 function about() {
16 return array(1.0,
17 "Imports and exports user data using neutral XML format",
18 "fox");
19 }
20
21 function xml_import($args) {
22 array_shift($args);
23
24 $username = $args[count($args) - 2];
25 $filename = $args[count($args) - 1];
26
27 if (!$username) {
28 print "error: please specify username.\n";
29 return;
30 }
31
32 if (!is_file($filename)) {
33 print "error: input filename ($filename) doesn't exist.\n";
34 return;
35 }
36
37 _debug("importing $filename for user $username...\n");
38
39 $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$username'");
40
41 if (db_num_rows($result) == 0) {
42 print "error: could not find user $username.\n";
43 return;
44 }
45
46 $owner_uid = db_fetch_result($result, 0, "id");
47
48 $this->perform_data_import($this->link, $filename, $owner_uid);
49 }
50
51 function save() {
52 $example_value = db_escape_string($_POST["example_value"]);
53
54 echo "Value set to $example_value (not really)";
55 }
56
57 function get_prefs_js() {
58 return file_get_contents(dirname(__FILE__) . "/import_export.js");
59 }
60
61 function hook_prefs_tab($args) {
62 if ($args != "prefFeeds") return;
63
64 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
65
66 print "<h3>" . __("Article archive") . "</h3>";
67
68 print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
69
70 print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
71 __('Export my data')."</button> ";
72
73 print "<hr>";
74
75 print "<iframe id=\"data_upload_iframe\"
76 name=\"data_upload_iframe\" onload=\"dataImportComplete(this)\"
77 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
78
79 print "<form name=\"import_form\" style='display : block' target=\"data_upload_iframe\"
80 enctype=\"multipart/form-data\" method=\"POST\"
81 action=\"backend.php\">
82 <input id=\"export_file\" name=\"export_file\" type=\"file\">&nbsp;
83 <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
84 <input type=\"hidden\" name=\"plugin\" value=\"import_export\">
85 <input type=\"hidden\" name=\"method\" value=\"dataimport\">
86 <button dojoType=\"dijit.form.Button\" onclick=\"return importData();\" type=\"submit\">" .
87 __('Import') . "</button>";
88
89
90 print "</div>"; # pane
91 }
92
93 function csrf_ignore($method) {
94 return in_array($method, array("exportget"));
95 }
96
97 function before($method) {
98 return $_SESSION["uid"] != false;
99 }
100
101 function after() {
102 return true;
103 }
104
105 function exportget() {
106 $exportname = CACHE_DIR . "/export/" .
107 sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
108
109 if (file_exists($exportname)) {
110 header("Content-type: text/xml");
111
112 if (function_exists('gzencode')) {
113 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
114 echo gzencode(file_get_contents($exportname));
115 } else {
116 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
117 echo file_get_contents($exportname);
118 }
119 } else {
120 echo "File not found.";
121 }
122 }
123
124 function exportrun() {
125 $offset = (int) db_escape_string($_REQUEST['offset']);
126 $exported = 0;
127 $limit = 250;
128
129 if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
130 $result = db_query($this->link, "SELECT
131 ttrss_entries.guid,
132 ttrss_entries.title,
133 content,
134 marked,
135 published,
136 score,
137 note,
138 link,
139 tag_cache,
140 label_cache,
141 ttrss_feeds.title AS feed_title,
142 ttrss_feeds.feed_url AS feed_url,
143 ttrss_entries.updated
144 FROM
145 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
146 ttrss_entries
147 WHERE
148 (marked = true OR feed_id IS NULL) AND
149 ref_id = ttrss_entries.id AND
150 ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
151 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
152
153 $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
154
155 if ($offset == 0) {
156 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
157 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
158 } else {
159 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
160 }
161
162 if ($fp) {
163
164 while ($line = db_fetch_assoc($result)) {
165 fputs($fp, "<article>");
166
167 foreach ($line as $k => $v) {
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
188 function perform_data_import($link, $filename, $owner_uid) {
189
190 $num_imported = 0;
191 $num_processed = 0;
192 $num_feeds_created = 0;
193
194 $doc = @DOMDocument::load($filename);
195
196 if (!$doc) {
197 $contents = file_get_contents($filename);
198
199 if ($contents) {
200 $data = @gzuncompress($contents);
201 }
202
203 if (!$data) {
204 $data = @gzdecode($contents);
205 }
206
207 if ($data)
208 $doc = DOMDocument::loadXML($data);
209 }
210
211 if ($doc) {
212
213 $xpath = new DOMXpath($doc);
214
215 $container = $doc->firstChild;
216
217 if ($container && $container->hasAttribute('schema-version')) {
218 $schema_version = $container->getAttribute('schema-version');
219
220 if ($schema_version != SCHEMA_VERSION) {
221 print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
222 return;
223 }
224
225 } else {
226 print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
227 return;
228 }
229
230 $articles = $xpath->query("//article");
231
232 foreach ($articles as $article_node) {
233 if ($article_node->childNodes) {
234
235 $ref_id = 0;
236
237 $article = array();
238
239 foreach ($article_node->childNodes as $child) {
240 if ($child->nodeName != 'label_cache')
241 $article[$child->nodeName] = db_escape_string($child->nodeValue);
242 else
243 $article[$child->nodeName] = $child->nodeValue;
244 }
245
246 //print_r($article);
247
248 if ($article['guid']) {
249
250 ++$num_processed;
251
252 //db_query($link, "BEGIN");
253
254 //print 'GUID:' . $article['guid'] . "\n";
255
256 $result = db_query($link, "SELECT id FROM ttrss_entries
257 WHERE guid = '".$article['guid']."'");
258
259 if (db_num_rows($result) == 0) {
260
261 $result = db_query($link,
262 "INSERT INTO ttrss_entries
263 (title,
264 guid,
265 link,
266 updated,
267 content,
268 content_hash,
269 no_orig_date,
270 date_updated,
271 date_entered,
272 comments,
273 num_comments,
274 author)
275 VALUES
276 ('".$article['title']."',
277 '".$article['guid']."',
278 '".$article['link']."',
279 '".$article['updated']."',
280 '".$article['content']."',
281 '".sha1($article['content'])."',
282 false,
283 NOW(),
284 NOW(),
285 '',
286 '0',
287 '')");
288
289 $result = db_query($link, "SELECT id FROM ttrss_entries
290 WHERE guid = '".$article['guid']."'");
291
292 if (db_num_rows($result) != 0) {
293 $ref_id = db_fetch_result($result, 0, "id");
294 }
295
296 } else {
297 $ref_id = db_fetch_result($result, 0, "id");
298 }
299
300 //print "Got ref ID: $ref_id\n";
301
302 if ($ref_id) {
303
304 $feed_url = $article['feed_url'];
305 $feed_title = $article['feed_title'];
306
307 $feed = 'NULL';
308
309 if ($feed_url && $feed_title) {
310 $result = db_query($link, "SELECT id FROM ttrss_feeds
311 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
312
313 if (db_num_rows($result) != 0) {
314 $feed = db_fetch_result($result, 0, "id");
315 } else {
316 // try autocreating feed in Uncategorized...
317
318 $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid,
319 feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
320
321 $result = db_query($link, "SELECT id FROM ttrss_feeds
322 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
323
324 if (db_num_rows($result) != 0) {
325 ++$num_feeds_created;
326
327 $feed = db_fetch_result($result, 0, "id");
328 }
329 }
330 }
331
332 if ($feed != 'NULL')
333 $feed_qpart = "feed_id = $feed";
334 else
335 $feed_qpart = "feed_id IS NULL";
336
337 //print "$ref_id / $feed / " . $article['title'] . "\n";
338
339 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries
340 WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart");
341
342 if (db_num_rows($result) == 0) {
343
344 $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked']));
345 $published = bool_to_sql_bool(sql_bool_to_bool($article['published']));
346 $score = (int) $article['score'];
347
348 $tag_cache = $article['tag_cache'];
349 $label_cache = db_escape_string($article['label_cache']);
350 $note = $article['note'];
351
352 //print "Importing " . $article['title'] . "<br/>";
353
354 ++$num_imported;
355
356 $result = db_query($link,
357 "INSERT INTO ttrss_user_entries
358 (ref_id, owner_uid, feed_id, unread, last_read, marked,
359 published, score, tag_cache, label_cache, uuid, note)
360 VALUES ($ref_id, $owner_uid, $feed, false,
361 NULL, $marked, $published, $score, '$tag_cache',
362 '$label_cache', '', '$note')");
363
364 $label_cache = json_decode($label_cache, true);
365
366 if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
367 foreach ($label_cache as $label) {
368
369 label_create($link, $label[1],
370 $label[2], $label[3], $owner_uid);
371
372 label_add_article($link, $ref_id, $label[1], $owner_uid);
373
374 }
375 }
376
377 //db_query($link, "COMMIT");
378 }
379 }
380 }
381 }
382 }
383
384 print "<p>" .
385 T_sprintf("Finished: %d articles processed, %d imported, %d feeds created.",
386 $num_processed, $num_imported, $num_feeds_created) .
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 {
424 print "<p>" . T_sprintf("Could not upload file. You might need to adjust upload_max_filesize
425 in PHP.ini (current value = %s)", ini_get("upload_max_filesize")) . " or use CLI import tool.</p>";
426
427 }
428
429 print "<button dojoType=\"dijit.form.Button\"
430 onclick=\"dijit.byId('dataImportDlg').hide()\">".
431 __('Close this window')."</button>";
432
433 print "</div>";
434
435 }
436
437
438 }
439 ?>