]> git.wh0rd.org - tt-rss.git/blame - plugins/import_export/import_export.php
fix share plugin icon location
[tt-rss.git] / plugins / import_export / import_export.php
CommitLineData
6c2637d9
AD
1<?php
2class Import_Export extends Plugin implements IHandler {
3
4 private $link;
5 private $host;
6
7 function __construct($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 xml_import($args) {
16 array_shift($args);
17
18 $username = $args[count($args) - 2];
19 $filename = $args[count($args) - 1];
20
21 if (!$username) {
22 print "error: please specify username.\n";
23 return;
24 }
25
26 if (!is_file($filename)) {
27 print "error: input filename ($filename) doesn't exist.\n";
28 return;
29 }
30
31 _debug("importing $filename for user $username...\n");
32
33 $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$username'");
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
42 $this->perform_data_import($this->link, $filename, $owner_uid);
43 }
44
45 function save() {
46 $example_value = db_escape_string($_POST["example_value"]);
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
60 print "<h3>" . __("Article archive") . "</h3>";
61
62 print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
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\">&nbsp;
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
83
84 print "</div>"; # pane
85 }
86
87 function csrf_ignore($method) {
88 return in_array($method, array("exportget"));
89 }
90
91 function before($method) {
92 return $_SESSION["uid"] != false;
93 }
94
95 function after() {
96 return true;
97 }
98
99 function exportget() {
100 $exportname = CACHE_DIR . "/export/" .
101 sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
102
103 if (file_exists($exportname)) {
104 header("Content-type: text/xml");
105
106 if (function_exists('gzencode')) {
107 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
108 echo gzencode(file_get_contents($exportname));
109 } else {
110 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
111 echo file_get_contents($exportname);
112 }
113 } else {
114 echo "File not found.";
115 }
116 }
117
118 function exportrun() {
119 $offset = (int) db_escape_string($_REQUEST['offset']);
120 $exported = 0;
121 $limit = 250;
122
123 if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
124 $result = db_query($this->link, "SELECT
125 ttrss_entries.guid,
126 ttrss_entries.title,
127 content,
128 marked,
129 published,
130 score,
131 note,
132 link,
133 tag_cache,
134 label_cache,
135 ttrss_feeds.title AS feed_title,
136 ttrss_feeds.feed_url AS feed_url,
137 ttrss_entries.updated
138 FROM
139 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
140 ttrss_entries
141 WHERE
142 (marked = true OR feed_id IS NULL) AND
143 ref_id = ttrss_entries.id AND
144 ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
145 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
146
147 $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
148
149 if ($offset == 0) {
150 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
151 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
152 } else {
153 $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
154 }
155
156 if ($fp) {
157
158 while ($line = db_fetch_assoc($result)) {
159 fputs($fp, "<article>");
160
161 foreach ($line as $k => $v) {
162 fputs($fp, "<$k><![CDATA[$v]]></$k>");
163 }
164
165 fputs($fp, "</article>");
166 }
167
168 $exported = db_num_rows($result);
169
170 if ($exported < $limit && $exported > 0) {
171 fputs($fp, "</articles>");
172 }
173
174 fclose($fp);
175 }
176
177 }
178
179 print json_encode(array("exported" => $exported));
180 }
181
182 function perform_data_import($link, $filename, $owner_uid) {
183
184 $num_imported = 0;
185 $num_processed = 0;
186 $num_feeds_created = 0;
187
188 $doc = @DOMDocument::load($filename);
189
190 if (!$doc) {
191 $contents = file_get_contents($filename);
192
193 if ($contents) {
194 $data = @gzuncompress($contents);
195 }
196
197 if (!$data) {
198 $data = @gzdecode($contents);
199 }
200
201 if ($data)
202 $doc = DOMDocument::loadXML($data);
203 }
204
205 if ($doc) {
206
207 $xpath = new DOMXpath($doc);
208
209 $container = $doc->firstChild;
210
211 if ($container && $container->hasAttribute('schema-version')) {
212 $schema_version = $container->getAttribute('schema-version');
213
214 if ($schema_version != SCHEMA_VERSION) {
215 print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
216 return;
217 }
218
219 } else {
220 print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
221 return;
222 }
223
224 $articles = $xpath->query("//article");
225
226 foreach ($articles as $article_node) {
227 if ($article_node->childNodes) {
228
229 $ref_id = 0;
230
231 $article = array();
232
233 foreach ($article_node->childNodes as $child) {
234 if ($child->nodeName != 'label_cache')
235 $article[$child->nodeName] = db_escape_string($child->nodeValue);
236 else
237 $article[$child->nodeName] = $child->nodeValue;
238 }
239
240 //print_r($article);
241
242 if ($article['guid']) {
243
244 ++$num_processed;
245
246 //db_query($link, "BEGIN");
247
248 //print 'GUID:' . $article['guid'] . "\n";
249
250 $result = db_query($link, "SELECT id FROM ttrss_entries
251 WHERE guid = '".$article['guid']."'");
252
253 if (db_num_rows($result) == 0) {
254
255 $result = db_query($link,
256 "INSERT INTO ttrss_entries
257 (title,
258 guid,
259 link,
260 updated,
261 content,
262 content_hash,
263 no_orig_date,
264 date_updated,
265 date_entered,
266 comments,
267 num_comments,
268 author)
269 VALUES
270 ('".$article['title']."',
271 '".$article['guid']."',
272 '".$article['link']."',
273 '".$article['updated']."',
274 '".$article['content']."',
275 '".sha1($article['content'])."',
276 false,
277 NOW(),
278 NOW(),
279 '',
280 '0',
281 '')");
282
283 $result = db_query($link, "SELECT id FROM ttrss_entries
284 WHERE guid = '".$article['guid']."'");
285
286 if (db_num_rows($result) != 0) {
287 $ref_id = db_fetch_result($result, 0, "id");
288 }
289
290 } else {
291 $ref_id = db_fetch_result($result, 0, "id");
292 }
293
294 //print "Got ref ID: $ref_id\n";
295
296 if ($ref_id) {
297
298 $feed_url = $article['feed_url'];
299 $feed_title = $article['feed_title'];
300
301 $feed = 'NULL';
302
303 if ($feed_url && $feed_title) {
304 $result = db_query($link, "SELECT id FROM ttrss_feeds
305 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
306
307 if (db_num_rows($result) != 0) {
308 $feed = db_fetch_result($result, 0, "id");
309 } else {
310 // try autocreating feed in Uncategorized...
311
312 $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid,
313 feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
314
315 $result = db_query($link, "SELECT id FROM ttrss_feeds
316 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
317
318 if (db_num_rows($result) != 0) {
319 ++$num_feeds_created;
320
321 $feed = db_fetch_result($result, 0, "id");
322 }
323 }
324 }
325
326 if ($feed != 'NULL')
327 $feed_qpart = "feed_id = $feed";
328 else
329 $feed_qpart = "feed_id IS NULL";
330
331 //print "$ref_id / $feed / " . $article['title'] . "\n";
332
333 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries
334 WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart");
335
336 if (db_num_rows($result) == 0) {
337
338 $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked']));
339 $published = bool_to_sql_bool(sql_bool_to_bool($article['published']));
340 $score = (int) $article['score'];
341
342 $tag_cache = $article['tag_cache'];
343 $label_cache = db_escape_string($article['label_cache']);
344 $note = $article['note'];
345
346 //print "Importing " . $article['title'] . "<br/>";
347
348 ++$num_imported;
349
350 $result = db_query($link,
351 "INSERT INTO ttrss_user_entries
352 (ref_id, owner_uid, feed_id, unread, last_read, marked,
353 published, score, tag_cache, label_cache, uuid, note)
354 VALUES ($ref_id, $owner_uid, $feed, false,
355 NULL, $marked, $published, $score, '$tag_cache',
356 '$label_cache', '', '$note')");
357
358 $label_cache = json_decode($label_cache, true);
359
360 if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
361 foreach ($label_cache as $label) {
362
363 label_create($link, $label[1],
364 $label[2], $label[3], $owner_uid);
365
366 label_add_article($link, $ref_id, $label[1], $owner_uid);
367
368 }
369 }
370
371 //db_query($link, "COMMIT");
372 }
373 }
374 }
375 }
376 }
377
378 print "<p>" .
379 T_sprintf("Finished: %d articles processed, %d imported, %d feeds created.",
380 $num_processed, $num_imported, $num_feeds_created) .
381 "</p>";
382
383 } else {
384
385 print "<p>" . __("Could not load XML document.") . "</p>";
386
387 }
388 }
389
390 function exportData() {
391
392 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>";
393
394 print "<div align='center'>";
395 print "<button dojoType=\"dijit.form.Button\"
396 onclick=\"dijit.byId('dataExportDlg').prepare()\">".
397 __('Prepare data')."</button>";
398
399 print "<button dojoType=\"dijit.form.Button\"
400 onclick=\"dijit.byId('dataExportDlg').hide()\">".
401 __('Close this window')."</button>";
402
403 print "</div>";
404
405
406 }
407
408 function dataImport() {
409 header("Content-Type: text/html"); # required for iframe
410
411 print "<div style='text-align : center'>";
412
413 if (is_file($_FILES['export_file']['tmp_name'])) {
414
415 $this->perform_data_import($this->link, $_FILES['export_file']['tmp_name'], $_SESSION['uid']);
416
417 } else {
418 print "<p>" . T_sprintf("Could not upload file. You might need to adjust upload_max_filesize
419 in PHP.ini (current value = %s)", ini_get("upload_max_filesize")) . " or use CLI import tool.</p>";
420
421 }
422
423 print "<button dojoType=\"dijit.form.Button\"
424 onclick=\"dijit.byId('dataImportDlg').hide()\">".
425 __('Close this window')."</button>";
426
427 print "</div>";
428
429 }
430
431
432}
433?>