]>
git.wh0rd.org - tt-rss.git/blob - plugins/import_export/init.php
a01a612a40b1fad9575365bbe8e12d73686ded6d
2 class Import_Export
extends Plugin
implements IHandler
{
8 $this->link
= $host->get_link();
11 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
12 $host->add_command("xml-import", "import articles from XML", $this, ":", "FILE");
17 "Imports and exports user data using neutral XML format",
21 function xml_import($args) {
23 $filename = $args['xml_import'];
25 if (!is_file($filename)) {
26 print "error: input filename ($filename) doesn't exist.\n";
30 _debug("please enter your username:");
32 $username = db_escape_string($this->link
, trim(read_stdin()));
34 _debug("importing $filename for user $username...\n");
36 $result = db_query($this->link
, "SELECT id FROM ttrss_users WHERE login = '$username'");
38 if (db_num_rows($result) == 0) {
39 print "error: could not find user $username.\n";
43 $owner_uid = db_fetch_result($result, 0, "id");
45 $this->perform_data_import($this->link
, $filename, $owner_uid);
49 $example_value = db_escape_string($this->link
, $_POST["example_value"]);
51 echo "Value set to $example_value (not really)";
54 function get_prefs_js() {
55 return file_get_contents(dirname(__FILE__
) . "/import_export.js");
58 function hook_prefs_tab($args) {
59 if ($args != "prefFeeds") return;
61 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
63 print "<h3>" . __("Article archive") . "</h3>";
65 print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
67 print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
68 __('Export my data')."</button> ";
72 print "<iframe id=\"data_upload_iframe\"
73 name=\"data_upload_iframe\" onload=\"dataImportComplete(this)\"
74 style=\"width: 400px; height: 100px; display: none;\"></iframe>";
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>";
88 print "</div>"; # pane
91 function csrf_ignore($method) {
92 return in_array($method, array("exportget"));
95 function before($method) {
96 return $_SESSION["uid"] != false;
103 function exportget() {
104 $exportname = CACHE_DIR
. "/export/" .
105 sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
107 if (file_exists($exportname)) {
108 header("Content-type: text/xml");
110 if (function_exists('gzencode')) {
111 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
112 echo gzencode(file_get_contents($exportname));
114 header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
115 echo file_get_contents($exportname);
118 echo "File not found.";
122 function exportrun() {
123 $offset = (int) db_escape_string($this->link
, $_REQUEST['offset']);
127 if ($offset < 10000 && is_writable(CACHE_DIR
. "/export")) {
128 $result = db_query($this->link
, "SELECT
139 ttrss_feeds.title AS feed_title,
140 ttrss_feeds.feed_url AS feed_url,
141 ttrss_entries.updated
143 ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
146 (marked = true OR feed_id IS NULL) AND
147 ref_id = ttrss_entries.id AND
148 ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
149 ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
151 $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
154 $fp = fopen(CACHE_DIR
. "/export/$exportname.xml", "w");
155 fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION
."\">");
157 $fp = fopen(CACHE_DIR
. "/export/$exportname.xml", "a");
162 while ($line = db_fetch_assoc($result)) {
163 fputs($fp, "<article>");
165 foreach ($line as $k => $v) {
166 $v = str_replace("]]>", "]]]]><![CDATA[>", $v);
167 fputs($fp, "<$k><![CDATA[$v]]></$k>");
170 fputs($fp, "</article>");
173 $exported = db_num_rows($result);
175 if ($exported < $limit && $exported > 0) {
176 fputs($fp, "</articles>");
184 print json_encode(array("exported" => $exported));
187 function perform_data_import($link, $filename, $owner_uid) {
191 $num_feeds_created = 0;
193 $doc = @DOMDocument
::load($filename);
196 $contents = file_get_contents($filename);
199 $data = @gzuncompress
($contents);
203 $data = @gzdecode
($contents);
207 $doc = DOMDocument
::loadXML($data);
212 $xpath = new DOMXpath($doc);
214 $container = $doc->firstChild
;
216 if ($container && $container->hasAttribute('schema-version')) {
217 $schema_version = $container->getAttribute('schema-version');
219 if ($schema_version != SCHEMA_VERSION
) {
220 print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
225 print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
229 $articles = $xpath->query("//article");
231 foreach ($articles as $article_node) {
232 if ($article_node->childNodes
) {
238 foreach ($article_node->childNodes
as $child) {
239 if ($child->nodeName
!= 'label_cache')
240 $article[$child->nodeName
] = db_escape_string($this->link
, $child->nodeValue
);
242 $article[$child->nodeName
] = $child->nodeValue
;
247 if ($article['guid']) {
251 //db_query($link, "BEGIN");
253 //print 'GUID:' . $article['guid'] . "\n";
255 $result = db_query($link, "SELECT id FROM ttrss_entries
256 WHERE guid = '".$article['guid']."'");
258 if (db_num_rows($result) == 0) {
260 $result = db_query($link,
261 "INSERT INTO ttrss_entries
275 ('".$article['title']."',
276 '".$article['guid']."',
277 '".$article['link']."',
278 '".$article['updated']."',
279 '".$article['content']."',
280 '".sha1($article['content'])."',
288 $result = db_query($link, "SELECT id FROM ttrss_entries
289 WHERE guid = '".$article['guid']."'");
291 if (db_num_rows($result) != 0) {
292 $ref_id = db_fetch_result($result, 0, "id");
296 $ref_id = db_fetch_result($result, 0, "id");
299 //print "Got ref ID: $ref_id\n";
303 $feed_url = $article['feed_url'];
304 $feed_title = $article['feed_title'];
308 if ($feed_url && $feed_title) {
309 $result = db_query($link, "SELECT id FROM ttrss_feeds
310 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
312 if (db_num_rows($result) != 0) {
313 $feed = db_fetch_result($result, 0, "id");
315 // try autocreating feed in Uncategorized...
317 $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid,
318 feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
320 $result = db_query($link, "SELECT id FROM ttrss_feeds
321 WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
323 if (db_num_rows($result) != 0) {
324 ++
$num_feeds_created;
326 $feed = db_fetch_result($result, 0, "id");
332 $feed_qpart = "feed_id = $feed";
334 $feed_qpart = "feed_id IS NULL";
336 //print "$ref_id / $feed / " . $article['title'] . "\n";
338 $result = db_query($link, "SELECT int_id FROM ttrss_user_entries
339 WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart");
341 if (db_num_rows($result) == 0) {
343 $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked']));
344 $published = bool_to_sql_bool(sql_bool_to_bool($article['published']));
345 $score = (int) $article['score'];
347 $tag_cache = $article['tag_cache'];
348 $label_cache = db_escape_string($this->link
, $article['label_cache']);
349 $note = $article['note'];
351 //print "Importing " . $article['title'] . "<br/>";
355 $result = db_query($link,
356 "INSERT INTO ttrss_user_entries
357 (ref_id, owner_uid, feed_id, unread, last_read, marked,
358 published, score, tag_cache, label_cache, uuid, note)
359 VALUES ($ref_id, $owner_uid, $feed, false,
360 NULL, $marked, $published, $score, '$tag_cache',
361 '$label_cache', '', '$note')");
363 $label_cache = json_decode($label_cache, true);
365 if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
366 foreach ($label_cache as $label) {
368 label_create($link, $label[1],
369 $label[2], $label[3], $owner_uid);
371 label_add_article($link, $ref_id, $label[1], $owner_uid);
376 //db_query($link, "COMMIT");
385 vsprintf(ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
386 vsprintf(ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
387 vsprintf(ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
392 print "<p>" . __("Could not load XML document.") . "</p>";
397 function exportData() {
399 print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>";
401 print "<div align='center'>";
402 print "<button dojoType=\"dijit.form.Button\"
403 onclick=\"dijit.byId('dataExportDlg').prepare()\">".
404 __('Prepare data')."</button>";
406 print "<button dojoType=\"dijit.form.Button\"
407 onclick=\"dijit.byId('dataExportDlg').hide()\">".
408 __('Close this window')."</button>";
415 function dataImport() {
416 header("Content-Type: text/html"); # required for iframe
418 print "<div style='text-align : center'>";
420 if ($_FILES['export_file']['error'] != 0) {
421 print_error(T_sprintf("Upload failed with error code %d",
422 $_FILES['export_file']['error']));
428 if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
429 $tmp_file = tempnam(CACHE_DIR
. '/upload', 'export');
431 $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
435 print_error(__("Unable to move uploaded file."));
439 print_error(__('Error: please upload OPML file.'));
443 if (is_file($tmp_file)) {
444 $this->perform_data_import($this->link
, $tmp_file, $_SESSION['uid']);
447 print_error(__('No file uploaded.'));
451 print "<button dojoType=\"dijit.form.Button\"
452 onclick=\"dijit.byId('dataImportDlg').hide()\">".
453 __('Close this window')."</button>";