implement experimental personal data import

This commit is contained in:
Andrew Dolgov 2011-12-27 23:10:04 +04:00
parent 566faa1476
commit 55f34b811f
5 changed files with 293 additions and 11 deletions

View file

@ -954,5 +954,28 @@ class Dlg extends Protected_Handler {
return;
}
function dataImport() {
header("Content-Type: text/html"); # required for iframe
print "<div style='text-align : center'>";
if (is_file($_FILES['export_file']['tmp_name'])) {
perform_data_import($this->link, $_FILES['export_file']['tmp_name'], $_SESSION['uid']);
} else {
print "<p>" . 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.</p>";
}
print "<button dojoType=\"dijit.form.Button\"
onclick=\"dijit.byId('dataImportDlg').hide()\">".
__('Close this window')."</button>";
print "</div>";
}
}
?>

View file

@ -1408,13 +1408,13 @@ class Pref_Feeds extends Protected_Handler {
print __("Only main settings profile can be migrated using OPML.") . "</p>";
print "<br/><iframe id=\"upload_iframe\"
print "<iframe id=\"upload_iframe\"
name=\"upload_iframe\" onload=\"opmlImportComplete(this)\"
style=\"width: 400px; height: 100px; display: none;\"></iframe>";
print "<form name=\"opml_form\" style='display : block' target=\"upload_iframe\"
enctype=\"multipart/form-data\" method=\"POST\"
action=\"backend.php\">
action=\"backend.php\">
<input id=\"opml_file\" name=\"opml_file\" type=\"file\">&nbsp;
<input type=\"hidden\" name=\"op\" value=\"dlg\">
<input type=\"hidden\" name=\"method\" value=\"importOpml\">
@ -1441,13 +1441,31 @@ class Pref_Feeds extends Protected_Handler {
__('Display URL')."</button> ";
print "<h2>" . __("Data Export") . "</h2>";
print "<h2>" . __("Article archive") . "</h2>";
print "<p>" . __("You can export your Starred and Archived articles using database-neutral format for safekeeping.") . "</p>";
print "<p>" . __("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.") . "</p>";
print "<h3>" . __("Export") . "</h3>";
print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
__('Export my data')."</button> ";
print "<h3>" . __("Import") . "</h3>";
print "<iframe id=\"data_upload_iframe\"
name=\"data_upload_iframe\" onload=\"dataImportComplete(this)\"
style=\"width: 400px; height: 100px; display: none;\"></iframe>";
print "<form name=\"opml_form\" style='display : block' target=\"data_upload_iframe\"
enctype=\"multipart/form-data\" method=\"POST\"
action=\"backend.php\">
<input id=\"export_file\" name=\"export_file\" type=\"file\">&nbsp;
<input type=\"hidden\" name=\"op\" value=\"dlg\">
<input type=\"hidden\" name=\"method\" value=\"dataimport\">
<button dojoType=\"dijit.form.Button\" onclick=\"return importData();\" type=\"submit\">" .
__('Import') . "</button>";
print "</div>"; # pane
if (strpos($_SERVER['HTTP_USER_AGENT'], "Firefox") !== false) {

View file

@ -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'] . "<br/>";
++$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 "<p>" .
T_sprintf("Finished: %d articles processed, %d imported, %d feeds created.",
$num_processed, $num_imported, $num_feeds_created) .
"</p>";
} else {
print "<p>" . __("Could not load XML document.") . "</p>";
}
}
?>

View file

@ -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);
}
}

View file

@ -1,6 +1,6 @@
#!/usr/bin/php
<?php
set_include_path(get_include_path() . PATH_SEPARATOR .
set_include_path(get_include_path() . PATH_SEPARATOR .
dirname(__FILE__) . "/include");
define('DISABLE_SESSIONS', true);
@ -22,12 +22,13 @@
if (!$op || $op == "-help") {
print "Tiny Tiny RSS data update script.\n\n";
print "Options:\n";
print " -feeds - update feeds\n";
print " -feedbrowser - update feedbrowser\n";
print " -daemon - start single-process update daemon\n";
print " -cleanup-tags - perform tags table maintenance\n";
print " -get-feeds - receive popular feeds from linked instances\n";
print " -help - show this help\n";
print " -feeds - update feeds\n";
print " -feedbrowser - update feedbrowser\n";
print " -daemon - start single-process update daemon\n";
print " -cleanup-tags - perform tags table maintenance\n";
print " -get-feeds - receive popular feeds from linked instances\n";
print " -import USER FILE - import articles from XML\n";
print " -help - show this help\n";
return;
}
@ -115,6 +116,35 @@
get_linked_feeds($link);
}
if ($op == "-import") {
$username = $argv[2];
$filename = $argv[3];
if (!$username) {
print "error: please specify username.\n";
return;
}
if (!is_file($filename)) {
print "error: input filename ($filename) doesn't exist.\n";
return;
}
print "importing $filename for user $username...\n";
$result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
if (db_num_rows($result) == 0) {
print "error: could not find user $username.\n";
return;
}
$owner_uid = db_fetch_result($result, 0, "id");
perform_data_import($link, $filename, $owner_uid);
}
db_close($link);
if ($lock_handle != false) {