From 55f34b811fef1f5e834fbcc65e41f6748e49ffd1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 27 Dec 2011 23:10:04 +0400 Subject: [PATCH] implement experimental personal data import --- classes/dlg.php | 23 ++++++ classes/pref_feeds.php | 26 +++++-- include/functions.php | 159 +++++++++++++++++++++++++++++++++++++++++ js/prefs.js | 52 ++++++++++++++ update.php | 44 ++++++++++-- 5 files changed, 293 insertions(+), 11 deletions(-) diff --git a/classes/dlg.php b/classes/dlg.php index 2d1cb9cc..55eb5e37 100644 --- a/classes/dlg.php +++ b/classes/dlg.php @@ -954,5 +954,28 @@ class Dlg extends Protected_Handler { return; } + function dataImport() { + header("Content-Type: text/html"); # required for iframe + + print "
"; + + if (is_file($_FILES['export_file']['tmp_name'])) { + + perform_data_import($this->link, $_FILES['export_file']['tmp_name'], $_SESSION['uid']); + + } else { + print "

" . 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.

"; + + } + + print ""; + + print "
"; + + } + } ?> diff --git a/classes/pref_feeds.php b/classes/pref_feeds.php index f9ed1b55..f5cb6e64 100644 --- a/classes/pref_feeds.php +++ b/classes/pref_feeds.php @@ -1408,13 +1408,13 @@ class Pref_Feeds extends Protected_Handler { print __("Only main settings profile can be migrated using OPML.") . "

"; - print "
"; print "
+ action=\"backend.php\">   @@ -1441,13 +1441,31 @@ class Pref_Feeds extends Protected_Handler { __('Display URL')." "; - print "

" . __("Data Export") . "

"; + print "

" . __("Article archive") . "

"; - print "

" . __("You can export your Starred and Archived articles using database-neutral format for safekeeping.") . "

"; + print "

" . __("You can export your Starred and Archived articles using neutral storage format. Please note that import and export of such data is only supported between same tt-rss versions.") . "

"; + + print "

" . __("Export") . "

"; print " "; + print "

" . __("Import") . "

"; + + print ""; + + print " +   + + + "; + + print ""; # pane if (strpos($_SERVER['HTTP_USER_AGENT'], "Firefox") !== false) { diff --git a/include/functions.php b/include/functions.php index 80581433..b2ff209a 100644 --- a/include/functions.php +++ b/include/functions.php @@ -5116,4 +5116,163 @@ return $rv; } + function perform_data_import($link, $filename, $owner_uid) { + + $num_imported = 0; + $num_processed = 0; + $num_feeds_created = 0; + + $doc = DOMDocument::load($filename); + + if ($doc) { + + $xpath = new DOMXpath($doc); + $articles = $xpath->query("//article"); + + foreach ($articles as $article_node) { + if ($article_node->childNodes) { + + $ref_id = 0; + + $article = array(); + + foreach ($article_node->childNodes as $child) { + $article[$child->nodeName] = db_escape_string($child->nodeValue); + } + + //print_r($article); + + if ($article['guid']) { + + ++$num_processed; + + //db_query($link, "BEGIN"); + + //print 'GUID:' . $article['guid'] . "\n"; + + $result = db_query($link, "SELECT id FROM ttrss_entries + WHERE guid = '".$article['guid']."'"); + + if (db_num_rows($result) == 0) { + + $result = db_query($link, + "INSERT INTO ttrss_entries + (title, + guid, + link, + updated, + content, + content_hash, + no_orig_date, + date_updated, + date_entered, + comments, + num_comments, + author) + VALUES + ('".$article['title']."', + '".$article['guid']."', + '".$article['link']."', + '".$article['updated']."', + '".$article['content']."', + '".sha1($article['content'])."', + false, + NOW(), + NOW(), + '', + '0', + '')"); + + $result = db_query($link, "SELECT id FROM ttrss_entries + WHERE guid = '".$article['guid']."'"); + + if (db_num_rows($result) != 0) { + $ref_id = db_fetch_result($result, 0, "id"); + } + + } else { + $ref_id = db_fetch_result($result, 0, "id"); + } + + //print "Got ref ID: $ref_id\n"; + + if ($ref_id) { + + $feed_url = $article['feed_url']; + $feed_title = $article['feed_title']; + + $feed = 'NULL'; + + if ($feed_url && $feed_title) { + $result = db_query($link, "SELECT id FROM ttrss_feeds + WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); + + if (db_num_rows($result) != 0) { + $feed = db_fetch_result($result, 0, "id"); + } else { + // try autocreating feed in Uncategorized... + + $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid, + feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')"); + + $result = db_query($link, "SELECT id FROM ttrss_feeds + WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); + + if (db_num_rows($result) != 0) { + ++$num_feeds_created; + + $feed = db_fetch_result($result, 0, "id"); + } + } + } + + if ($feed != 'NULL') + $feed_qpart = "feed_id = $feed"; + else + $feed_qpart = "feed_id IS NULL"; + + //print "$ref_id / $feed / " . $article['title'] . "\n"; + + $result = db_query($link, "SELECT int_id FROM ttrss_user_entries + WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart"); + + if (db_num_rows($result) == 0) { + + $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked'])); + $published = bool_to_sql_bool(sql_bool_to_bool($article['published'])); + $score = (int) $article['score']; + + $tag_cache = $article['tag_cache']; + $label_cache = $article['label_cache']; + + //print "Importing " . $article['title'] . "
"; + + ++$num_imported; + + $result = db_query($link, + "INSERT INTO ttrss_user_entries + (ref_id, owner_uid, feed_id, unread, last_read, marked, + published, score, tag_cache, label_cache, uuid) + VALUES ($ref_id, $owner_uid, $feed, false, + NULL, $marked, $published, $score, '$tag_cache', '$label_cache', '')"); + + //db_query($link, "COMMIT"); + } + } + } + } + } + + print "

" . + T_sprintf("Finished: %d articles processed, %d imported, %d feeds created.", + $num_processed, $num_imported, $num_feeds_created) . + "

"; + + } else { + + print "

" . __("Could not load XML document.") . "

"; + + } + } + ?> diff --git a/js/prefs.js b/js/prefs.js index 7f9e44ad..0048e09d 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -754,6 +754,8 @@ function opmlImportComplete(iframe) { try { if (!iframe.contentDocument.body.innerHTML) return false; + Element.show(iframe); + notify(''); if (dijit.byId('opmlImportDlg')) @@ -794,10 +796,30 @@ function opmlImport() { return false; } else { notify_progress("Importing, please wait...", true); + + Element.show("upload_iframe"); + return true; } } +function importData() { + + var file = $("export_file"); + + if (file.value.length == 0) { + alert(__("Please choose the file first.")); + return false; + } else { + notify_progress("Importing, please wait...", true); + + Element.show("data_upload_iframe"); + + return true; + } +} + + function updateFilterList() { new Ajax.Request("backend.php", { parameters: "?op=pref-filters", @@ -2013,3 +2035,33 @@ function exportData() { } } +function dataImportComplete(iframe) { + try { + if (!iframe.contentDocument.body.innerHTML) return false; + + Element.hide(iframe); + + notify(''); + + if (dijit.byId('dataImportDlg')) + dijit.byId('dataImportDlg').destroyRecursive(); + + var content = iframe.contentDocument.body.innerHTML; + + dialog = new dijit.Dialog({ + id: "dataImportDlg", + title: __("Data Import"), + style: "width: 600px", + onCancel: function() { + + }, + content: content}); + + dialog.show(); + + } catch (e) { + exception_error("dataImportComplete", e); + } +} + + diff --git a/update.php b/update.php index 28bcb3d1..68add9f0 100755 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@ #!/usr/bin/php