]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/import_export/init.php
import_export: various bugfixes
[tt-rss.git] / plugins / import_export / init.php
index 981a2b58151f9839a5a29badaef698d1b9a8409d..7c628909ff11de71df78981d4ec696df35495d73 100644 (file)
@@ -26,11 +26,11 @@ class Import_Export extends Plugin implements IHandler {
 
                _debug("please enter your username:");
 
-               $username = db_escape_string( trim(read_stdin()));
+               $username = db_escape_string(trim(read_stdin()));
 
                _debug("importing $filename for user $username...\n");
 
-               $result = db_query( "SELECT id FROM ttrss_users WHERE login = '$username'");
+               $result = db_query("SELECT id FROM ttrss_users WHERE login = '$username'");
 
                if (db_num_rows($result) == 0) {
                        print "error: could not find user $username.\n";
@@ -39,11 +39,11 @@ class Import_Export extends Plugin implements IHandler {
 
                $owner_uid = db_fetch_result($result, 0, "id");
 
-               $this->perform_data_import( $filename, $owner_uid);
+               $this->perform_data_import($filename, $owner_uid);
        }
 
        function save() {
-               $example_value = db_escape_string( $_POST["example_value"]);
+               $example_value = db_escape_string($_POST["example_value"]);
 
                echo "Value set to $example_value (not really)";
        }
@@ -57,9 +57,9 @@ class Import_Export extends Plugin implements IHandler {
 
                print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
 
-               print "<h3>" . __("Article archive") . "</h3>";
+               print_notice(__("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version."));
 
-               print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
+               print "<p>";
 
                print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
                        __('Export my data')."</button> ";
@@ -82,6 +82,8 @@ class Import_Export extends Plugin implements IHandler {
 
                print "</form>";
 
+               print "</p>";
+
                print "</div>"; # pane
        }
 
@@ -104,11 +106,13 @@ class Import_Export extends Plugin implements IHandler {
                if (file_exists($exportname)) {
                        header("Content-type: text/xml");
 
+                       $timestamp_suffix = date("Y-m-d", filemtime($exportname));
+
                        if (function_exists('gzencode')) {
-                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
+                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz");
                                echo gzencode(file_get_contents($exportname));
                        } else {
-                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
+                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml");
                                echo file_get_contents($exportname);
                        }
                } else {
@@ -117,12 +121,12 @@ class Import_Export extends Plugin implements IHandler {
        }
 
        function exportrun() {
-               $offset = (int) db_escape_string( $_REQUEST['offset']);
+               $offset = (int) db_escape_string($_REQUEST['offset']);
                $exported = 0;
                $limit = 250;
 
                if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
-                       $result = db_query( "SELECT
+                       $result = db_query("SELECT
                                        ttrss_entries.guid,
                                        ttrss_entries.title,
                                        content,
@@ -181,12 +185,14 @@ class Import_Export extends Plugin implements IHandler {
                print json_encode(array("exported" => $exported));
        }
 
-       function perform_data_import( $filename, $owner_uid) {
+       function perform_data_import($filename, $owner_uid) {
 
                $num_imported = 0;
                $num_processed = 0;
                $num_feeds_created = 0;
 
+               libxml_disable_entity_loader(false);
+
                $doc = @DOMDocument::load($filename);
 
                if (!$doc) {
@@ -204,6 +210,8 @@ class Import_Export extends Plugin implements IHandler {
                                $doc = DOMDocument::loadXML($data);
                }
 
+               libxml_disable_entity_loader(true);
+
                if ($doc) {
 
                        $xpath = new DOMXpath($doc);
@@ -233,10 +241,13 @@ class Import_Export extends Plugin implements IHandler {
                                        $article = array();
 
                                        foreach ($article_node->childNodes as $child) {
-                                               if ($child->nodeName != 'label_cache')
-                                                       $article[$child->nodeName] = db_escape_string( $child->nodeValue);
-                                               else
+                                               if ($child->nodeName == 'content') {
+                                                       $article[$child->nodeName] = db_escape_string($child->nodeValue, false);
+                                               } else if ($child->nodeName == 'label_cache') {
                                                        $article[$child->nodeName] = $child->nodeValue;
+                                               } else {
+                                                       $article[$child->nodeName] = db_escape_string($child->nodeValue);
+                                               }
                                        }
 
                                        //print_r($article);
@@ -245,11 +256,11 @@ class Import_Export extends Plugin implements IHandler {
 
                                                ++$num_processed;
 
-                                               //db_query( "BEGIN");
+                                               //db_query("BEGIN");
 
                                                //print 'GUID:' . $article['guid'] . "\n";
 
-                                               $result = db_query( "SELECT id FROM ttrss_entries
+                                               $result = db_query("SELECT id FROM ttrss_entries
                                                        WHERE guid = '".$article['guid']."'");
 
                                                if (db_num_rows($result) == 0) {
@@ -282,7 +293,7 @@ class Import_Export extends Plugin implements IHandler {
                                                                        '0',
                                                                        '')");
 
-                                                       $result = db_query( "SELECT id FROM ttrss_entries
+                                                       $result = db_query("SELECT id FROM ttrss_entries
                                                                WHERE guid = '".$article['guid']."'");
 
                                                        if (db_num_rows($result) != 0) {
@@ -303,7 +314,7 @@ class Import_Export extends Plugin implements IHandler {
                                                        $feed = 'NULL';
 
                                                        if ($feed_url && $feed_title) {
-                                                               $result = db_query( "SELECT id FROM ttrss_feeds
+                                                               $result = db_query("SELECT id FROM ttrss_feeds
                                                                        WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
 
                                                                if (db_num_rows($result) != 0) {
@@ -311,10 +322,10 @@ class Import_Export extends Plugin implements IHandler {
                                                                } else {
                                                                        // try autocreating feed in Uncategorized...
 
-                                                                       $result = db_query( "INSERT INTO ttrss_feeds (owner_uid,
+                                                                       $result = db_query("INSERT INTO ttrss_feeds (owner_uid,
                                                                                feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
 
-                                                                       $result = db_query( "SELECT id FROM ttrss_feeds
+                                                                       $result = db_query("SELECT id FROM ttrss_feeds
                                                                                WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
 
                                                                        if (db_num_rows($result) != 0) {
@@ -332,7 +343,7 @@ class Import_Export extends Plugin implements IHandler {
 
                                                        //print "$ref_id / $feed / " . $article['title'] . "\n";
 
-                                                       $result = db_query( "SELECT int_id FROM ttrss_user_entries
+                                                       $result = db_query("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) {
@@ -342,7 +353,6 @@ class Import_Export extends Plugin implements IHandler {
                                                                $score = (int) $article['score'];
 
                                                                $tag_cache = $article['tag_cache'];
-                                                               $label_cache = db_escape_string( $article['label_cache']);
                                                                $note = $article['note'];
 
                                                                //print "Importing " . $article['title'] . "<br/>";
@@ -355,22 +365,22 @@ class Import_Export extends Plugin implements IHandler {
                                                                                published, score, tag_cache, label_cache, uuid, note)
                                                                        VALUES ($ref_id, $owner_uid, $feed, false,
                                                                                NULL, $marked, $published, $score, '$tag_cache',
-                                                                                       '$label_cache', '', '$note')");
+                                                                                       '', '', '$note')");
 
-                                                               $label_cache = json_decode($label_cache, true);
+                                                               $label_cache = json_decode($article['label_cache'], true);
 
                                                                if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
                                                                        foreach ($label_cache as $label) {
 
-                                                                               label_create( $label[1],
+                                                                               label_create($label[1],
                                                                                        $label[2], $label[3], $owner_uid);
 
-                                                                               label_add_article( $ref_id, $label[1], $owner_uid);
+                                                                               label_add_article($ref_id, $label[1], $owner_uid);
 
                                                                        }
                                                                }
 
-                                                               //db_query( "COMMIT");
+                                                               //db_query("COMMIT");
                                                        }
                                                }
                                        }
@@ -379,9 +389,9 @@ class Import_Export extends Plugin implements IHandler {
 
                        print "<p>" .
                                __("Finished: ").
-                               vsprintf(ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
-                               vsprintf(ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
-                               vsprintf(ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
+                               vsprintf(_ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
+                               vsprintf(_ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
+                               vsprintf(_ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
                                        "</p>";
 
                } else {
@@ -438,7 +448,7 @@ class Import_Export extends Plugin implements IHandler {
                }
 
                if (is_file($tmp_file)) {
-                       $this->perform_data_import( $tmp_file, $_SESSION['uid']);
+                       $this->perform_data_import($tmp_file, $_SESSION['uid']);
                        unlink($tmp_file);
                } else {
                        print_error(__('No file uploaded.'));
@@ -453,6 +463,9 @@ class Import_Export extends Plugin implements IHandler {
 
        }
 
+       function api_version() {
+               return 2;
+       }
 
 }
 ?>