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

assessment/classes/class.SQL2XML.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 
00031 class SQL2XML
00032 {
00039         var $domxml;
00040 
00047         var $obj_id;
00048 
00055         var $mapping;
00056 
00057         var $doc;
00064         var $hash;
00065 
00072         var $db;
00073         
00074         var $nestedset;
00081         function SQL2XML ($database_connection, $a_obj_id)
00082         {
00083                 $this->db = $database_connection;
00084                 $this->obj_id = $a_obj_id;
00085                 $this->getXMLDocument();
00086                 register_shutdown_function(array(&$this, '_SQL2XML'));
00087         }
00088 
00089         function _SQL2XML() {
00090                 if ($this->doc) {
00091                         $this->doc->free();
00092                 }
00093         }
00094         
00095         function retrieveHeader() {
00096                 $q = sprintf("SELECT * FROM xml_object WHERE ID=%s",
00097                         $this->db->quote($this->obj_id)
00098                 );
00099                 $result = $this->db->query($q);
00100                 if ($row = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00101                         return "<?xml version=\"$row->version\" encoding=\"$row->encoding\"?>";
00102                 } else {
00103                         return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
00104                 }
00105         }
00106 
00114         function getXMLDocument()
00115         {
00116                 $this->nestedset = $this->getStructure($this->obj_id);
00117                 // create the xml string (workaround for domxml_new_doc) ***
00118                 $xmlHeader = $this->retrieveHeader() . "<root />";
00119                 // create a domxml document object
00120                 $this->doc = domxml_open_mem($xmlHeader);
00121                 // delete dummy node 
00122                 $root = $this->doc->document_element();
00123                 $root->unlink_node();
00124                 // build XML document
00125                 $this->buildXML();
00126                 return $this->doc->dump_mem(true);
00127         }
00128         
00129         function buildXML ()    
00130         {
00131                 foreach ($this->nestedset as $key => $node_data)
00132                 {
00133                         $insert = false;
00134                         switch ($node_data["node_type_id"])
00135                         {
00136                                 case XML_ELEMENT_NODE:
00137                                         $node = $this->doc->create_element($node_data["element"]);
00138                                         // set attributes
00139                                         if (is_array($node_data["attr_list"]))
00140                                         {
00141                                                 foreach ($node_data["attr_list"] as $attr => $value)
00142                                                 {
00143                                                         $node->set_attribute($attr, $value);
00144                                                 }
00145                                         }
00146                                         $insert = true;
00147                                         break;
00148                                 case XML_TEXT_NODE:
00149                                         $node = $this->doc->create_text_node($node_data["textnode"]);
00150                                         $insert = true;
00151                                         break;
00152                                 case XML_COMMENT_NODE:
00153                                         $node = $this->doc->create_comment($node_data["comment"]);
00154                                         $insert = true;
00155                                         break;
00156                         }
00157 
00158                         if ($insert)
00159                         {
00160                                 //get parent node
00161                                 $parent = $this->hash[$node_data["parent_node_id"]];
00162                                 //build node
00163                                 if (!$parent) {
00164                                         $node = $this->doc->append_child($node);
00165                                 } else {
00166                                         $node = $parent->append_child($node);
00167                                 }
00168                                 $this->hash[$this->nestedset[$key]["node_id"]] = $node;
00169                         }
00170                 }
00171         }
00172 
00173         function getStructure($a_xml_id)
00174         {
00175                 $q = "SELECT lo.node_id, lo.node_type_id, lo.xml_id, lo.parent_node_id, lo.struct, tx.textnode, comm.comment, el.element, a_name.attribute, a_value.value ".
00176                          "FROM xml_tree AS lo ".
00177                          "LEFT OUTER JOIN xml_element_idx AS e_idx ON lo.node_id = e_idx.node_id ".
00178                          "LEFT OUTER JOIN xml_element_name AS el ON e_idx.element_id = el.element_id ".
00179                          "LEFT OUTER JOIN xml_text AS tx ON lo.node_id = tx.node_id ".
00180                          "LEFT OUTER JOIN xml_comment AS comm ON lo.node_id = comm.node_id " .
00181                          "LEFT OUTER JOIN xml_attribute_idx AS a_idx ON lo.node_id = a_idx.node_id ".
00182                          "LEFT JOIN xml_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00183                          "LEFT JOIN xml_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00184                          "WHERE xml_id='".$a_xml_id."' ".
00185                          "ORDER BY lft ASC";
00186                 // 2. variant: I think this is the fastest but you need mysql 4.x in order to use UNION statement
00187 /*
00188                 $q = "SELECT rgt FROM lo_tree ".
00189                          "WHERE lo_id = '".$a_lo_id."' ".
00190                          "AND lft = 1";
00191                 
00192                 $res = $this->db->query($q);
00193                 
00194                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00195                 {
00196                         $rgt_max = $row["rgt"];
00197                 }
00198 
00199                 $q = "(SELECT n.lft AS seq_no_1, 4 AS seq_no_2, 0 AS seq_no_3, 0 AS seq_no_4, x.node_id AS seq_no_5, x.textnode AS parsed_text ".
00200                          "FROM lo_tree n, lo_text x ".
00201                          "WHERE n.lo_id = '".$a_lo_id."' ".
00202                          "AND n.lft >= 1 ".
00203                          "AND n.lft < '".$rgt_max."' ".
00204                          "AND n.node_id = x.node_id) ".
00205                          "UNION ".
00206                          "(SELECT n.lft, 1, 0, 0, 0, t.lft_delimiter ".
00207                          "FROM lo_tree n, lo_node_type t ".
00208                          "WHERE n.lo_id = '".$a_lo_id."' ".
00209                          "AND n.node_type_id = t.node_type_id ".
00210                          "AND n.lft >= 1 ".
00211                          "AND n.lft < '".$rgt_max."' ".
00212                          "AND t.lft_delimiter > '') ".
00213                          "UNION ".
00214                          "(SELECT n.lft, 2, 0, 0, 0, e.element ".
00215                          "FROM lo_element_name e, lo_element_idx e_idx, lo_tree n ".
00216                          "WHERE n.lo_id = '".$a_lo_id."' ".
00217                          "AND n.node_id = e_idx.node_id AND ".
00218                          "e.element_id = e_idx.element_id) ".
00219                          "UNION ".
00220                          "(SELECT n.lft, 3, a.attribute_id, 1, 0, CONCAT(' ', a.attribute, '=\"' ) ".
00221                          "FROM lo_attribute_name a, lo_attribute_idx a_idx, lo_tree n ".
00222                          "WHERE n.lo_id = '".$a_lo_id."' ".
00223                          "AND n.lft >= 1 ".
00224                          "AND n.lft < '".$rgt_max."' ".
00225                          "AND n.node_id = a_idx.node_id ".
00226                          "AND a.attribute_id = a_idx.attribute_id) ".
00227                          "UNION ".
00228                          "(SELECT n.lft, 3, a_idx.attribute_id, 2, n.node_id, CONCAT( a.value, '\"' ) ".
00229                          "FROM lo_attribute_value a, lo_attribute_idx a_idx, lo_tree n ".
00230                          "WHERE n.lo_id = '".$a_lo_id."' ".
00231                          "AND n.lft >= 1 ".
00232                          "AND n.lft < '".$rgt_max."' ".
00233                          "AND n.node_id = a_idx.node_id ".
00234                          "AND a.value_id = a_idx.value_id) ".
00235                          "UNION ".
00236                          "(SELECT n.lft, 9, 0, 0, 0, t.rgt_delimiter ".
00237                          "FROM lo_tree n, lo_node_type t ".
00238                          "WHERE n.lo_id = '".$a_lo_id."' ".
00239                          "AND n.node_type_id = t.node_type_id ".
00240                          "AND t.rgt_delimiter > '' ".
00241                          "AND n.lft >= 1 ".
00242                          "AND n.lft < '".$rgt_max."') ".
00243                          "UNION ".
00244                          "(SELECT n.rgt, 10, 0, 0, 0, CONCAT( t.lft_delimiter, '/' ) ".
00245                          "FROM lo_tree n, lo_node_type t ".
00246                          "WHERE n.lo_id = '".$a_lo_id."' ".
00247                          "AND n.node_type_id = t.node_type_id ".
00248                          "AND n.node_type_id = 1 ".
00249                          "AND t.lft_delimiter > '' ".
00250                          "AND n.lft >= 1 ".
00251                          "AND n.lft < '".$rgt_max."') ".
00252                          "UNION ".
00253                          "(SELECT n.rgt, 10, 0, 3, 0, e.element ".
00254                          "FROM lo_element_name e, lo_element_idx e_idx, lo_tree n ".
00255                          "WHERE n.lo_id = '".$a_lo_id."' ".
00256                          "AND n.node_id = e_idx.node_id ".
00257                          "AND e.element_id = e_idx.element_id) ".
00258                          "UNION ".
00259                          "(SELECT n.rgt, 10, 0, 4, 0, t.rgt_delimiter FROM lo_tree n, lo_node_type t ".
00260                          "WHERE n.lo_id = '".$a_lo_id."' ".
00261                          "AND n.node_type_id = t.node_type_id ".
00262                          "AND t.rgt_delimiter > '' ".
00263                          "AND n.lft >= 1 ".
00264                          "AND n.lft < '".$rgt_max."') ".
00265                          "ORDER BY seq_no_1, seq_no_2, seq_no_3, seq_no_4, seq_no_5";
00266                 
00267                 
00268                 //echo $q;exit;
00269 
00270                 $res = $this->db->query($q);
00271                 
00272                 if ($res->numRows() == 0)
00273                 {
00274                         print("no LearningObject ID given");
00275                 }
00276 
00277                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00278                 {
00279                         $data[] = $row["parsed_text"];
00280                 }
00281                 
00282                 echo TUtil::StopWatch($T1)." get_structure<br/>";
00283                 
00284                 return implode($data);
00285 */
00286 
00287                 $res = $this->db->query($q);
00288                 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00289                 {
00290                         $data[$row["node_id"]] = $row;
00291 
00292                         if ($row["struct"] & 1)
00293                         {
00294                                 $tmp[$row["node_id"]][] = array ($row["attribute"] => $row["value"]);
00295                         }
00296                 }
00297 
00298                 foreach ($tmp as $node_id => $node)
00299                 {
00300                         $attr_list = array();
00301                         
00302                         foreach ($node as $attr)
00303                         {
00304                                 //var_dump($attr);
00305                                 $attr_list[key($attr)] = $attr[key($attr)];
00306                         }
00307 
00308                         $data[$node_id]["attr_list"] = $attr_list;
00309                 }
00310                 return $data;
00311         }
00312                         
00313 
00323         function getTree ($a_lo_id)
00324         {
00325                 return $tree;
00326         }
00327 
00336         function getNode ($a_node_id)
00337         {
00338                 return $node;
00339         }
00340         
00348         function getElementName ($a_node_id)
00349         {
00350                 $q = "SELECT leaf_text FROM xml_element_name_leaf ".
00351                          "WHERE node_id='".$a_node_id."' LIMIT 1";
00352                 //$res = $this->db->query($q);
00353                 $res = $this->db->getRow($q, DB_FETCHMODE_ASSOC);
00354 
00355                 return $res["leaf_text"];
00356         }
00357         
00358         function getElementText ($a_node_id)
00359         {
00360                 $q = "SELECT leaf_text FROM xml_text_leaf ".
00361                          "WHERE node_id='".$a_node_id."' LIMIT 1";
00362                 //$res = $this->db->query($q);
00363                 $res = $this->db->getRow($q, DB_FETCHMODE_ASSOC);
00364 
00365                 return $res["leaf_text"];
00366         }
00375         function getAttributes ($a_node_id)
00376         {
00377                 $q = "SELECT a_name.attribute,a_value.value ".
00378                          "FROM xml_attribute_idx AS a_idx ".
00379                          "LEFT JOIN xml_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00380                          "LEFT JOIN xml_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00381                          "WHERE a_idx.node_id = '".$a_node_id."'";
00382                          
00383                  //echo $q;exit;
00384 
00385                 $res = $this->db->query($q);
00386                 
00387                 if ($res->numRows())
00388                 {
00389                         while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00390                         {
00391                                 $data[$row["attribute"]] = $row["value"];
00392                         }
00393 
00394                         return $data;
00395                 }
00396 
00397                 return false;
00398         }
00399 
00400         
00409         function getAttributeValue ($a_lo_id,$a_element,$a_attribute)
00410         {
00411 
00412                 $q = "SELECT a_value.value ".
00413                          "FROM xml_tree AS lo ".
00414                          "LEFT JOIN lo_element_idx AS e_idx ON lo.node_id = e_idx.node_id ".
00415                          "LEFT JOIN lo_element_name AS el ON e_idx.element_id = el.element_id ".
00416                          "LEFT JOIN lo_attribute_idx AS a_idx ON lo.node_id = a_idx.node_id ".
00417                          "LEFT JOIN lo_attribute_name AS a_name ON a_idx.attribute_id=a_name.attribute_id ".
00418                          "LEFT JOIN lo_attribute_value AS a_value ON a_idx.value_id=a_value.value_id ".
00419                          "WHERE xml_id = '".$a_lo_id."' ".
00420                          "AND lo.struct > 0 ". // <-- need a bitwise AND against 1
00421                          "AND el.element = '".$a_element."' ".
00422                          "AND a_name.attribute = '".$a_attribute."'";
00423                          
00424                 //echo $q;exit;
00425 
00426                 $res = $this->db->query($q);
00427                 $row = $res->fetchRow();
00428         
00429                 return $row[0];
00430         }
00431 
00432 } // END class.SQL2XML
00433 
00434 ?>

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