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