• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

assessment/classes/class.XML2SQL.php

Go to the documentation of this file.
00001 <?php
00002  /*
00003    +----------------------------------------------------------------------------+
00004    | ILIAS open source                                                          |
00005    +----------------------------------------------------------------------------+
00006    | Copyright (c) 1998-2001 ILIAS open source, University of Cologne           |
00007    |                                                                            |
00008    | This program is free software; you can redistribute it and/or              |
00009    | modify it under the terms of the GNU General Public License                |
00010    | as published by the Free Software Foundation; either version 2             |
00011    | of the License, or (at your option) any later version.                     |
00012    |                                                                            |
00013    | This program is distributed in the hope that it will be useful,            |
00014    | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
00015    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              |
00016    | GNU General Public License for more details.                               |
00017    |                                                                            |
00018    | You should have received a copy of the GNU General Public License          |
00019    | along with this program; if not, write to the Free Software                |
00020    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
00021    +----------------------------------------------------------------------------+
00022 */
00023 
00032 class XML2SQL
00033 {
00040         var $obj_id;
00041 
00048         var $mapping;
00049         var $version;
00050         var $encoding;
00051         var $charset;
00058         var $db;
00059 
00066         var $xmltree;   
00067 
00074         function XML2SQL ($a_dbconnection, $a_xmltree, $a_version = "1.0", $a_encoding = "UTF-8", $a_charset = "UTF-8")
00075         {
00076                 $this->xmltree = $a_xmltree;
00077                 $this->db = $a_dbconnection;
00078                 $this->version = $a_version;
00079                 $this->encoding = $a_encoding;
00080                 $this->charset = $a_charset;
00081         }
00082                 
00083         function insertDocument ()
00084         { 
00085                 $this->obj_id = $this->insertXMLObject();
00086                 if (!$this->obj_id) 
00087                 {
00088                         print "There was an error writing the xml file to the database!";
00089                         exit();
00090                 }
00091                 // insert basic structure of document
00092                 foreach ($this->xmltree as $id => $node) {
00093                         $node_id = $this->insertNode($node);
00094                         $this->mapping[$id] = $node_id;
00095                 } 
00096                 // re-map node_ids
00097                 foreach ($this->xmltree as $id => $node) {
00098                         $this->xmltree[$id]["parent"] = $this->mapping[$node["parent"]];
00099                         $this->xmltree[$id]["prev"] = $this->mapping[$node["prev"]];
00100                         $this->xmltree[$id]["next"] = $this->mapping[$node["next"]];
00101                         $this->xmltree[$id]["first"] = $this->mapping[$node["first"]];
00102                         $this->xmltree[$id]["node"] = $this->mapping[$id];
00103                 } 
00104 
00105                 foreach ($this->xmltree as $id => $node) {
00106                         $this->updateNode($node);
00107                         $this->insertNodeData($node);
00108                 } 
00109 
00110                 return $this->xmltree;
00111         } 
00112         
00113         function insertXMLObject() {
00114                 $q = sprintf("INSERT INTO xml_object (ID, version, encoding, charset, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00115                         $this->db->quote($this->version),
00116                         $this->db->quote($this->encoding),
00117                         $this->db->quote($this->charset)
00118                 );
00119                 $result = $this->db->query($q);
00120                 return $this->getLastInsertId();
00121         }
00122 
00123         function insertNode ($a_node)
00124         {
00125                 $q = "INSERT INTO xml_tree ".
00126                          "(xml_id,lft,rgt,node_type_id,depth,struct) ".
00127                          "VALUES ".
00128                          "('".$this->obj_id."','".$a_node["left"].
00129                          "','".$a_node["right"]."','".$a_node["type"].
00130                          "','".$a_node["depth"]."','".$a_node["struct"]."') ";
00131                 $this->db->query($q);
00132 
00133                 return $this->getLastInsertId();
00134         } 
00135 
00136         function updateNode ($a_node)
00137         {
00138                 $q = "UPDATE xml_tree SET ".
00139                          "parent_node_id = '".$a_node["parent"]."',".
00140                          "prev_sibling_node_id = '".$a_node["prev"]."',".
00141                          "next_sibling_node_id = '".$a_node["next"]."',".
00142                          "first_child_node_id = '".$a_node["first"]."' ".
00143                          "WHERE node_id = '".$a_node["node"]."' ".
00144                          "AND xml_id = '".$this->obj_id."'";
00145                 $this->db->query($q);
00146         } 
00147 
00148         function insertNodeData ($a_node)
00149         {
00150                 //echo "<PRE>";echo var_dump($a_node);echo "</PRE>";
00151                 $a_node = $this->prepareData($a_node);
00152                 
00153                 //echo "<PRE>";echo var_dump($a_node);echo "</PRE>";
00154 
00155                 switch ($a_node["type"]) {
00156                         case 1:
00157                                 $this->insertElement($a_node);
00158                                 $this->insertAttributes($a_node);
00159                                 break;
00160 
00161                         case 3:
00162                                 $this->insertText($a_node);
00163                                 break;
00164 
00165                         case 4: 
00166                                 // $this->insertCData($a_node);
00167                                 break;
00168 
00169                         case 5: 
00170                                 // $this->insertEntityRef($a_node);
00171                                 break;
00172 
00173                         case 6: 
00174                                 // $this->insertEntity($a_node);
00175                                 break;
00176 
00177                         case 7: 
00178                                 // $this->insertPI($a_node);
00179                                 break;
00180 
00181                         case 8:
00182                                 $this->insertComment($a_node);
00183                                 break;
00184 
00185                         default: 
00186                                 // nix
00187                                 break;
00188                 } // switch
00189         } 
00190 
00196         function insertElement ($a_node)
00197         {
00198                 $element_id = $this->getEntryId("xml_element_name","element","element_id",$a_node["name"]);
00199                 
00200                 // insert element first if it does not exist
00201                 if ($element_id == false)
00202                 {
00203                         $q = "INSERT INTO xml_element_name (element) ".
00204                                  "VALUES ('".$a_node["name"]."')";
00205                         $this->db->query($q);
00206                         
00207                         $element_id = $this->getLastInsertId();
00208                 }
00209                 
00210                 // create reference entry
00211                 $q = "INSERT INTO xml_element_idx (node_id,element_id) ".
00212                          "VALUES ('".$a_node["node"]."','".$element_id."')";
00213                 $this->db->query($q);
00214         } 
00215 
00221         function insertText ($a_node)
00222         {
00223                 // klappt nicht, weil die spaces maskiert sind :-(
00224                 $content = $a_node["content"];
00225         
00226                 $q = "INSERT INTO xml_text ".
00227                          "(node_id,textnode) ".
00228                          "VALUES ".
00229                          "('".$a_node["node"]."','".$content."')";
00230                 $this->db->query($q);
00231         } 
00232 
00238         function insertComment ($a_node)
00239         {
00240                 $q = "INSERT INTO xml_comment ".
00241                          "(node_id,comment) ".
00242                          "VALUES ".
00243                          "('".$a_node["node"]."','".$a_node["content"]."')";
00244                 $this->db->query($q);
00245         } 
00246 
00253         function insertAttributes ($a_node)
00254         {
00255                 if (is_array($a_node["attr_list"]))
00256                 {
00257                         foreach ($a_node["attr_list"] as $attr => $value)
00258                         {
00259                                 $attribute_id = $this->getEntryId("xml_attribute_name","attribute","attribute_id",$attr);
00260 
00261                                 // insert attribute first if it does not exist
00262                                 if ($attribute_id == false)
00263                                 {
00264                                         $q = "INSERT INTO xml_attribute_name (attribute) ".
00265                                                  "VALUES ('".$attr."')";
00266                                         $this->db->query($q);
00267                         
00268                                         $attribute_id = $this->getLastInsertId();
00269                                 }
00270 
00271                                 //$value_id = $this->getEntryId("xml_attribute_value","value","value_id",$value);
00272 
00273                                 // insert attribute value first if it does not exist
00274                                 //if ($value_id == false)
00275                                 //{
00276                                         $q = "INSERT INTO xml_attribute_value (value) ".
00277                                                  "VALUES ('".$value."')";
00278                                         $this->db->query($q);
00279                         
00280                                         $value_id = $this->getLastInsertId();
00281                                 //}
00282 
00283                                 // create reference entry
00284                                 $q = "INSERT INTO xml_attribute_idx (node_id,attribute_id,value_id) ".
00285                                          "VALUES ".
00286                                          "('".$a_node["node"]."','".$attribute_id."','".$value_id."')";
00287                                 $this->db->query($q);
00288                         }
00289                         
00290                         return true;
00291                 }
00292                 
00293                 return false;
00294         }
00295         
00305         function getEntryId ($a_table,$a_column,$a_return_value,$a_value)
00306         {
00307                 $q = "SELECT DISTINCT ".$a_return_value." FROM ".$a_table." ".
00308                          "WHERE ".$a_column."='".$a_value."'";
00309                 $res = $this->db->query($q, DB_FETCHMODE_ASSOC);
00310                 if ($res->numRows() == 0)
00311                 {
00312                         return false;
00313                 }
00314 
00315                 $row = $res->fetchRow();
00316                 return $row[0];
00317         }
00318 
00324         function getLastInsertId ()
00325         {
00326                 $q = "SELECT LAST_INSERT_ID()";
00327                 $res = $this->db->query($q);
00328                 $row = $res->fetchRow();
00329                 return $row[0];
00330         }       
00331 
00338         function prepareData ($a_data)
00339         {
00340                 foreach ($a_data as $key => $value)
00341                 {
00342                         if (is_string($value))
00343                                 $data[$key] = addslashes($value);
00344                         else
00345                                 $data[$key] = $value;                   
00346                 }
00347                 
00348                 return $data;
00349         }
00350 
00351         // information saved in $mapping how the LOs are connected in the this Module is
00352         // written to tree
00353         function insertStructureIntoTree($a_nodes,$a_id)
00354         {
00355                 // init tree
00356                 $lm_tree = new Tree($a_id,$a_id);
00357                 
00358                 //prepare array and kick all nodes with no children
00359                 foreach ($a_nodes as $key => $nodes)
00360                 {
00361                         if (!is_array($nodes[key($nodes)]))
00362                         {
00363                                 array_splice($a_nodes,$key);
00364                                 break;
00365                         }
00366                 }
00367 
00368                 // insert first_node
00369                 $parent_id = $a_id;
00370                 $lm_tree->insertNode(key($a_nodes[0]),$parent_id,0);
00371                 
00372                 // traverse array to build tree structure by inserting nodes to db-table tree
00373                 foreach ($a_nodes as $key => $nodes)
00374                 {
00375                         $parent_parent_id = $parent_id;
00376                         $parent_id = key($nodes);
00377 
00378                         foreach (array_reverse($nodes[$parent_id]) as $child_id)
00379                         {
00380                                 $lm_tree->insertNode($child_id,$parent_id,$parent_parent_id);
00381                         }
00382                 }
00383         }
00384 } // END class xml2sql
00385 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1