add import of XML files using PHP's xml functions, if they are installed (which they...
authorchriskl <chriskl>
Mon, 12 Apr 2004 07:50:32 +0000 (07:50 +0000)
committerchriskl <chriskl>
Mon, 12 Apr 2004 07:50:32 +0000 (07:50 +0000)
HISTORY
dataexport.php
dataimport.php
lang/english.php
lang/recoded/english.php
tblproperties.php

diff --git a/HISTORY b/HISTORY
index 3ce4a4c6abc60757209135e27d0154753762c86d..7f6c2bfcf1a3d5fa5fdedd03d1a9f65a4d4b6ded 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -11,7 +11,8 @@ Features
 * Allow adding array columns to tables
 * Allow creating domains with type length and arrays
 * Show domain base type and comment in domains list
-* Allow import of CSV and Tabbed data
+* Allow import of CSV, Tabbed and XML data.  "Auto" mode chooses
+  import mode based on the file extension.
 * Allow upload and execution of _basic_ SQL scripts
 
 Bugs
index 2d1a74f64cce58bd02ed0c1eaa519414cdaa7edb..bd3c78f42741b565fcfc6bac09c2cff67bd199a5 100644 (file)
@@ -4,7 +4,7 @@
         * Does an export to the screen or as a download.  This checks to
         * see if they have pg_dump set up, and will use it if possible.
         *
-        * $Id: dataexport.php,v 1.13 2004/04/12 06:30:55 chriskl Exp $
+        * $Id: dataexport.php,v 1.14 2004/04/12 07:50:32 chriskl Exp $
         */
 
        $extensions = array(
                        echo "<option value=\"sql\">SQL</option>\n";
                }
                echo "<option value=\"csv\">CSV</option>\n";
-               echo "<option value=\"tab\">Tabbed</option>\n";
+               echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
                echo "<option value=\"html\">XHTML</option>\n";
                echo "<option value=\"xml\">XML</option>\n";
                echo "</select></td></tr>";
index 84999a683eda21992fcf3f96409cb3dba6089873..b9243f466e9b122905114c8d78806352aab6e836 100644 (file)
        /**
         * Does an import to a particular table from a text file
         *
-        * $Id: dataimport.php,v 1.1 2004/04/12 06:43:15 chriskl Exp $
+        * $Id: dataimport.php,v 1.2 2004/04/12 07:50:33 chriskl Exp $
         */
 
        // Include application functions
        include_once('./libraries/lib.inc.php');
 
+       // Default state for XML parser
+       $state = 'XML';
+       $curr_col_name = null;
+       $curr_col_val = null;
+       $curr_col_null = false;
+       $curr_row = array();
+
+       /**
+        * Open tag handler for XML import feature
+        */
+       function _startElement($parser, $name, $attrs) {
+               global $data, $misc, $lang;
+               global $state, $curr_row, $curr_col_name, $curr_col_val, $curr_col_null;
+
+               switch ($name) {
+                       case 'DATA':
+                               if ($state != 'XML') {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               $state = 'DATA';
+                               break;
+                       case 'HEADER':
+                               if ($state != 'DATA') {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               $state = 'HEADER';
+                               break;
+                       case 'RECORDS':
+                               if ($state != 'READ_HEADER') {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               $state = 'RECORDS';
+                               break;
+                       case 'ROW':
+                               if ($state != 'RECORDS') {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               $state = 'ROW';
+                               $curr_row = array();
+                               break;
+                       case 'COLUMN':
+                               // We handle columns in rows
+                               if ($state == 'ROW') {
+                                       $state = 'COLUMN';
+                                       $curr_col_name = $attrs['NAME'];
+                                       $curr_col_null = isset($attrs['NULL']);
+                               }
+                               // And we ignore columns in headers and fail in any other context                               
+                               elseif ($state != 'HEADER') {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               break;
+                       default:
+                               // An unrecognised tag means failure
+                               $data->rollbackTransaction();
+                               $misc->printMsg($lang['strimporterror']);
+                               exit;                   
+               }
+       }
+       
+       /**
+        * Close tag handler for XML import feature
+        */
+       function _endElement($parser, $name) {
+               global $data, $misc, $lang;
+               global $state, $curr_row, $curr_col_name, $curr_col_val, $curr_col_null;
+
+               switch ($name) {
+                       case 'DATA':
+                               $state = 'READ_DATA';
+                               break;
+                       case 'HEADER':
+                               $state = 'READ_HEADER';
+                               break;
+                       case 'RECORDS':
+                               $state = 'READ_RECORDS';
+                               break;
+                       case 'ROW':
+                               // Build value map in order to insert row into table
+                               $vars = array();
+                               $nulls = array();
+                               $format = array();              
+                               $types = array();                               
+                               foreach ($curr_row as $k => $v) {
+                                       // Check for nulls
+                                       if ($v === null) $nulls[$k] = 'on';
+                                       // Add to value array
+                                       $vars[$k] = $v;
+                                       // Format is always VALUE
+                                       $format[$k] = 'VALUE';
+                                       // Type is always text
+                                       $types[$k] = 'text';
+                               }
+                               $status = $data->insertRow($_REQUEST['table'], $vars, $nulls, $format, $types);
+                               if ($status != 0) {
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;
+                               }
+                               $curr_row = array();
+                               $state = 'RECORDS';
+                               break;
+                       case 'COLUMN':
+                               $curr_row[$curr_col_name] = ($curr_col_null ? null : $curr_col_val);
+                               $curr_col_name = null;
+                               $curr_col_val = null;
+                               $curr_col_null = false;
+                               $state = 'ROW';
+                               break;
+                       default:
+                               // An unrecognised tag means failure
+                               $data->rollbackTransaction();
+                               $misc->printMsg($lang['strimporterror']);
+                               exit;
+               }
+       }
+
+       /**
+        * Character data handler for XML import feature
+        */
+       function _charHandler($parser, $cdata) {
+               global $data, $misc, $lang;
+               global $state, $curr_col_val;
+
+               if ($state == 'COLUMN') {
+                       $curr_col_val = $cdata;
+               } 
+       }
+
+
        $misc->printHeader($lang['strimport']);         
        $misc->printTableNav();
        echo "<h2>", $misc->printVal($_REQUEST['database']), ": ", $misc->printVal($_REQUEST['table']), ": {$lang['strimport']}</h2>\n";
                                $misc->printMsg($lang['strimporterror']);
                                exit;
                        }
-                       
+
+                       // If format is set to 'auto', then determine format automatically from file name
+                       $extension = substr(strrchr($_FILES['source']['name'], '.'), 1);
+                       switch ($extension) {
+                               case 'csv':
+                                       $_REQUEST['format'] = 'csv';
+                                       break;
+                               case 'txt':
+                                       $_REQUEST['format'] = 'tab';
+                                       break;
+                               case 'xml':
+                                       $_REQUEST['format'] = 'xml';
+                                       break;
+                               default:
+                                       $data->rollbackTransaction();
+                                       $misc->printMsg($lang['strimporterror']);
+                                       exit;                   
+                       }               
+
+                       // Do different import technique depending on file format
                        switch ($_REQUEST['format']) {
                                case 'csv':
                                case 'tab':
                                                // Build value map
                                                $vars = array();
                                                $nulls = array();
-                                               $format = array();                                              
+                                               $format = array();
+                                               $types = array();
                                                $i = 0;
                                                foreach ($fields as $f) {
                                                        // Check that there is a column
                                                $row++;
                                        }
                                        break;
+                               case 'xml':
+                                       $parser = xml_parser_create();\r
+                                       xml_set_element_handler($parser, '_startElement', '_endElement');\r
+                                       xml_set_character_data_handler($parser, '_charHandler');\r
+                                       \r
+                                       while (!feof($fd)) {\r
+                                               $line = fgets($fd, 4096);\r
+                                               xml_parse($parser, $line);\r
+                                       }\r
+                                       \r
+                                       xml_parser_free($parser);
+                                       break;
                                default:
                                        // Unknown type
                                        $data->rollbackTransaction();
index 0cbccb4bc1eefced0e7dcbe04fea88fe002a70a8..d70c0a8d9dd24f964d0a52d7225181d2f2c938ae 100755 (executable)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.137 2004/04/12 06:30:55 chriskl Exp $
+        * $Id: english.php,v 1.138 2004/04/12 07:50:33 chriskl Exp $
         */
 
        // Language and character set
        $lang['strdataonly'] = 'Data only';
        $lang['strstructureonly'] = 'Structure only';
        $lang['strstructureanddata'] = 'Structure and data';
+       $lang['strtabbed'] = 'Tabbed';
+       $lang['strauto'] = 'Auto';
 
        // Users
        $lang['struser'] = 'User';
index a869fb89216ba94c747c812d7e8a74664aa8d28c..907e96d7e09bc29ead80a9f6c891b1b65be60f31 100644 (file)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.90 2004/04/12 06:30:56 chriskl Exp $
+        * $Id: english.php,v 1.91 2004/04/12 07:50:43 chriskl Exp $
         */
 
        // Language and character set
        $lang['strdataonly'] = 'Data only';
        $lang['strstructureonly'] = 'Structure only';
        $lang['strstructureanddata'] = 'Structure and data';
+       $lang['strtabbed'] = 'Tabbed';
+       $lang['strauto'] = 'Auto';
 
        // Users
        $lang['struser'] = 'User';
index b718a713fa8f9fefc9ca261777734cbc82e230da..dc7c7be9c5f22bc213e2c6412e4632cc85f9699a 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * List tables in a database
         *
-        * $Id: tblproperties.php,v 1.41 2004/04/12 06:43:15 chriskl Exp $
+        * $Id: tblproperties.php,v 1.42 2004/04/12 07:50:33 chriskl Exp $
         */
 
        // Include application functions
                echo "<option value=\"copy\">COPY</option>\n";
                echo "<option value=\"sql\">SQL</option>\n";
                echo "<option value=\"csv\">CSV</option>\n";
-               echo "<option value=\"tab\">Tabbed</option>\n";
+               echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
                echo "<option value=\"html\">XHTML</option>\n";
                echo "<option value=\"xml\">XML</option>\n";
                echo "</select>\n</td>\n</tr>\n";
                                echo "<table>\n";
                                echo "<tr><th class=\"data left required\">{$lang['strformat']}</th>";
                                echo "<td><select name=\"format\">\n";
-                               //echo "<option value=\"copy\">COPY</option>\n";
-                               //echo "<option value=\"sql\">SQL</option>\n";
+                               echo "<option value=\"auto\">{$lang['strauto']}</option>\n";
                                echo "<option value=\"csv\">CSV</option>\n";
-                               echo "<option value=\"tab\">Tabbed</option>\n";
-                               //echo "<option value=\"html\">XHTML</option>\n";
-                               //echo "<option value=\"xml\">XML</option>\n";
+                               echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
+                               if (function_exists('xml_parser_create')) {
+                                       echo "<option value=\"xml\">XML</option>\n";
+                               }
                                echo "</select>\n</td>\n</tr>\n";
                                echo "<tr><th class=\"data left required\">{$lang['strfile']}</th>";
                                echo "<td><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$max_size}\" />\n";