Source for file LVU_DOMDOCUMENT.phpclass

Documentation is available at LVU_DOMDOCUMENT.phpclass

  1. <?php
  2. /**
  3.   * Class file LVU_DOMDOCUMENT.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    xml
  7.   *
  8.   * @author     Christian Gaisberger (cg) <c.gaisberger@lvus.at>
  9.   * @author     Peter Krebs (pk)<p.krebs@lvus.at>
  10.   *
  11.   * @copyright  (c) 2002-2003 by Landesverlag Unternehmensservice
  12.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  13.   *
  14.   ***/
  15.  
  16. require_once $GLOBALS['DEFAULTCONFPATH']."xml.conf.phpinc";
  17. /**
  18.   * Class LVU_DOMDOCUMENT
  19.   *
  20.   * @project    Open CSP-Management
  21.   * @package    xml
  22.   *
  23.   * @author     Christian Gaisberger <c.gaisberger@lvus.at>
  24.   * @copyright  (c) 2004 by Landesverlag Unternehmensservice
  25.   *
  26.   * @since cg-04-07-26
  27.   *
  28.   ***/
  29. class LVU_DOMDOCUMENT {
  30.     /**
  31.       * @var   string  $xmlFile   dateiname zum öffnen
  32.       ***/
  33.     var $xmlFile = "";
  34.     /**
  35.       * @var   string  $xmlWorkdir     datei pfad
  36.       ***/
  37.     var $xmlWorkdir = "";
  38.     /**
  39.       * @var   object  $myDom     zum öffnen des xml files
  40.       ***/
  41.     var $myDom = NULL;
  42.     /**
  43.       * @var   object  $myDtd     ausgabe des dtd namen
  44.       ***/
  45.     var $myDtd = NULL;
  46.     /**
  47.       * @var   object  $myTag     ausgabe des tagnamen
  48.       ***/
  49.     var $myTag = NULL;   
  50.     /**
  51.       * @var   string  $gDBIDX     key of $GLOBALS[]   to the db object
  52.       ***/
  53.     var $gDBIDX = "USRDB";
  54.  
  55.     /**
  56.       * @var bool $debugMode 
  57.       ***/
  58.     var $debugMode=FALSE;
  59.            
  60.     function LVU_DOMDOCUMENT($xmlFile,$debug=FALSE{
  61.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::LVU_DOMDOCUMENT($xmlFile)</b> (".get_class($this).")</p>\n";
  62.  
  63.         if ((file_exists($xmlFile)) && ($this->myDom = new domDocument())) {
  64.             $this->myDom->load($xmlFile);
  65.             $this->xmlFile    = basename($xmlFile);
  66.             $this->xmlWorkdir = dirname($xmlFile);
  67.             if ($debug{
  68.                 echo "\n\t<blockquote><!-- -------------------------------------- -->\n<pre>\n".print_r($this->myDom,TRUE)."\n</pre>\n</blockquote><!-- -------------------------------------- -->\n";
  69.                 $this->print_node();
  70.                 echo "<hr />\n";
  71.             }
  72.  
  73.         }
  74.     }
  75.  
  76.  
  77.     /**
  78.       * returns the root node of myDom
  79.       *
  80.       * @returns DOMElement NULL in case of error
  81.       *
  82.       ***/
  83.     function getRoot({
  84.         if (is_object($this->myDom)) {
  85.             return $this->myDom->documentElement;
  86.         else {
  87.             return NULL;
  88.         }
  89.     }
  90.  
  91.     /**
  92.       * gibt einen array mit dem ersten wurzel elemt zurück
  93.       * @returns array
  94.       ***/
  95.     function getRootTags($debug=FALSE{
  96.         $debug=($debug || $this->debugMode);
  97.         if ($debugecho "\n\n<p><b>LVU_DOMDOCUMENT::getRoottag()</b> (".get_class($this).")</p>\n\n\t<blockquote><!-- -------------------------------------- -->\n";
  98.  
  99.         if ($debug$this->print_node($this->myDom->documentElement);
  100.  
  101.         if ($this->myDom->documentElement->childNodes->length{
  102.             foreach($this->myDom->documentElement->childNodes as $n{
  103.                 $this->print_node($n);
  104.             }
  105.         }
  106.         if ($debugecho "\t\n</blockquote><!-- -------------------------------------- -->\n";
  107.     }
  108.  
  109.     /**
  110.       * returns an array of tag descriptions
  111.       *
  112.       * use $reqNOC  = 2 (require number of children) to get only nodes which are
  113.       * parent of a complex element
  114.       *
  115.       * @param array $arr 
  116.       * @param DOMnode $node 
  117.       * @param int $maxLevels (999 = unlimited)
  118.       * @param int $reqNOC 
  119.       * @param string $parentName 
  120.       * @param string $pPath path to root (exmpl TBL>ROW>COL)
  121.       * @param bool $debug 
  122.       *
  123.       * @todo $reqNOC may fail for complex nodes which consists of only on complex parent
  124.       *
  125.       *
  126.       ***/
  127.     function populateAllTagsArr(&$tagArr,$maxLevels=999,$reqNOC=0,$node=NULL,$parentName="ROOT",$pPath="",$debug=FALSE{
  128.         $debug=($debug || $this->debugMode);
  129.  
  130.         if (!$node$node=$this->getRoot();
  131.         if ($debug{
  132.             echo "\n\n<p><b>LVU_DOMDOCUMENT::populateAllTagsArr(\$tagArr,$maxLevels,$reqNOC,\$node,$parentName,$pPath ...)</b> (".get_class($this).")</p>\n";
  133.             $this->print_node($node);
  134.         }
  135.  
  136.         if ($node{
  137.             if (!$maxLevelsreturn TRUE;
  138.             if (($node->nodeType != XML_TEXT_NODE&& ($node->childNodes->length >= $reqNOC)) {
  139.                 $nodeDesc ="NAME:".$node->nodeName;
  140.                 $nodeDesc.="/TYPE:".$node->nodeType;
  141.                 $nodeDesc.="/PARENT:".$parentName;
  142.                 $nodeDesc.="/PATH:".$pPath;
  143.  
  144.                 if (!in_array($nodeDesc,$tagArr)) {
  145.                     $tagArr[]=$nodeDesc;
  146.                 }
  147.  
  148.                 if (!empty($pPath)) $pPath .= ">";
  149.                 $pPath .= $node->nodeName;
  150.  
  151.                 if ($maxLevels 999$maxLevels--;
  152.                 if ($node->childNodes->length{
  153.                     foreach($node->childNodes as $child{
  154.                         $this->populateAllTagsArr($tagArr,$maxLevels,$reqNOC,$child,$node->nodeName,$pPath,$debug);
  155.                     }
  156.                 }
  157.             }
  158.             return TRUE;
  159.         else {
  160.             return FALSE;
  161.         }
  162.     }
  163.  
  164.     /**
  165.       * returns an array with xml tag desc array
  166.       *
  167.       * @param int $maxLevels (999 = unlimited)
  168.       * @param int $reqNOC    see THIS::populateAllTagsArr
  169.       * @param bool $debug 
  170.       *
  171.       *  @ returns array
  172.       *
  173.       ***/
  174.     function getTagDescArr($maxLevels=999,$reqNOC=0,$debug=FALSE{
  175.         $debug=($debug || $this->debugMode);
  176.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::getTagDescArr($maxLevels,...)</b> (".get_class($this).")</p>\n";
  177.         $tagArr=array();
  178.         $this->populateAllTagsArr($tagArr,$maxLevels,$reqNOC,NULL,"ROOT","",$debug);
  179.  
  180.         $retArr=array();$idx=0;
  181.         $keyArr=array()// tmporary used to store keys
  182.         foreach($tagArr as $tagDescStr{
  183.             if ($tmpArr1 explode("/",$tagDescStr)) {
  184.                 $tagDesc=array();
  185.                 foreach($tmpArr1 as $tmpStr{
  186.                     $tmpArr2=explode(":",$tmpStr);
  187.                     $tagDesc[$tmpArr2[0]]=$tmpArr2[1];
  188.                 }
  189.                 $retArr[$idx++]=$tagDesc;
  190.             }
  191.         }
  192.         return $retArr;
  193.     }
  194.  
  195.  
  196.     /**********************************************************************************/
  197.     /******************** DATA FUNCTIONS   ********************************************/
  198.     /**********************************************************************************/
  199.  
  200.     /**
  201.       * populates a tmpImpArr element
  202.       *
  203.       * @param string $tblTag 
  204.       * @param string $rowTag 
  205.       * @param boolean $debug 
  206.       *
  207.       * @returns boolean
  208.       ***/
  209.     function populate_tmpImpArr($tblTag,$rowTag,$debug=FALSE{
  210.         $debug=($debug || $this->debugMode);
  211.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::populate_tmpImpArr($tblTag,$rowTag..)</b> (".get_class($this).")</p>\n";
  212.         $key=$tblTag.">".$rowTag;
  213.  
  214.         $query ="SELECT * FROM T_SYS_XMLROOTS ";
  215.         $query.=" WHERE XRS_TBLTG='".$tblTag."'";
  216.         $query.="   AND XRS_ROWTG='".$rowTag."'";
  217.         //if ($debug) echo "\n\t<blockquote><!-- -------------------------------------- -->\n<pre>$query</pre>\n</blockquote><!-- -------------------------------------- -->\n";
  218.         if ($this->tmpImpArr[$key]=$GLOBALS[$this->gDBIDX]->quickQuery($query)) {
  219.             $query ="SELECT XMD_COLTG AS ARRKEY, d.* FROM T_SYS_XMLDESC d WHERE XRS_ID=".$this->tmpImpArr[$key]['XRS_ID'];
  220.             //if ($debug) echo "\n\t<blockquote><!-- -------------------------------------- -->\n<pre>$query</pre>\n</blockquote><!-- -------------------------------------- -->\n";
  221.             $this->tmpImpArr[$key]['COLS']=$GLOBALS[$this->gDBIDX]->queryArray($query,0,-1);
  222.             return TRUE;
  223.         else {
  224.             $this->tmpImpArr[$key]=FALSE;
  225.         }
  226.         return FALSE;
  227.     }
  228.  
  229.     /**
  230.       * inserts single multivalue tag
  231.       *
  232.       * for each child a db replace statement is executed
  233.       * returns the number of rows processed
  234.       *
  235.       * @param string $smvkey 
  236.       * @param DOMNode $node 
  237.       * @param array $row 
  238.       * @param boolean $debug 
  239.       *
  240.       * @returns int
  241.       *
  242.       * @since pk-04-10-20
  243.       *
  244.       ***/
  245.     function insert_smvRow($smvkey,$node,$row,$debug{
  246.         $debug=($debug || $this->debugMode);
  247.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::getDataArray($smvkey,\$node...)</b> (".get_class($this).")</p>\n";
  248.  
  249.         $count=0;
  250.         foreach($node->childNodes as $child{
  251.             if (is_array($this->tmpImpArr[$smvkey]['COLS'][$child->nodeName])) {
  252.                 $tblKey=$this->tmpImpArr[$smvkey]['COLS'][$child->nodeName]['XMD_TABLE'];
  253.                 $colKey=$this->tmpImpArr[$smvkey]['COLS'][$child->nodeName]['XMD_COL'];
  254.                 $row[$tblKey][$colKey]=utf8_decode($child->nodeValue);
  255.                 $GLOBALS['USRDB']->replaceArray($tblKey,$row[$tblKey],$debug);
  256.                 $count++;
  257.             }
  258.         }
  259.         return $count;
  260.     }
  261.  
  262.     /**
  263.       * gets a data array out of a node child list
  264.       *
  265.       * each child not found in $this->tmpImpArr[$key]['COLS']
  266.       * is added to $notUsed
  267.       *
  268.       * @param string $key 
  269.       * @param DOMnode $node 
  270.       * @param array &$notUsed 
  271.       * @param boolean $debug 
  272.       *
  273.       * @returns array
  274.       *
  275.       ***/
  276.     function getDataArray($key,&$node,&$notUsed,$debug=FALSE{
  277.         $debug=($debug || $this->debugMode);
  278.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::getDataArray($key,\$node...)</b> (".get_class($this).")</p>\n";
  279.  
  280.         if (!is_array($this->tmpImpArr[$key]['COLS'])) return FALSE;
  281.  
  282.         if ($debugecho "\n\t<blockquote><!-- -------------------------------------- -->\n<ul>";
  283.         $row=Array();
  284.         $singleMultiValChildren=array();
  285.         foreach($node->childNodes as $child{
  286.             if (is_array($this->tmpImpArr[$key]['COLS'][$child->nodeName])) {
  287.                 $tblKey=$this->tmpImpArr[$key]['COLS'][$child->nodeName]['XMD_TABLE'];
  288.                 $colKey=$this->tmpImpArr[$key]['COLS'][$child->nodeName]['XMD_COL'];
  289.                 $row[$tblKey][$colKey]=str_replace("\\","\\\\",utf8_decode($child->nodeValue));
  290.                 if ($debugecho "<li>\$row[".$tblKey."][".$colKey."]:\n\t<blockquote><!-- -------------------------------------- -->\n<pre>".htmlspecialchars(utf8_decode($child->nodeValue))."</pre>\n</blockquote><!-- -------------------------------------- -->\n</li>";
  291.             else if (is_array($this->tmpImpArr[$key]['COLS']["<".$child->nodeName.">"])) {
  292.                 if ($debugecho "\t\t<li>SubNode ".$child->nodeName." </li>\n";
  293.                 $smvkey=$node->nodeName.">".$child->nodeName;
  294.                 if (!isset($this->tmpImpArr[$smvkey])) {
  295.                     $this->populate_tmpImpArr($node->nodeName,$child->nodeName,$debug);
  296.                 }
  297.                 if ($this->tmpImpArr[$smvkey]['XRS_SINGLEVALMULTI']{
  298.                     $singleMultiValChildren[$smvkey]=$child;
  299.                 }
  300.             else {
  301.                 if ($child->nodeType != XML_TEXT_NODE{
  302.                     $notUsed[]=$child;
  303.                     if ($debugecho "\t\t<li>".$child->nodeName." NOT USED </li>\n";
  304.                 }
  305.             }
  306.         }
  307.  
  308.         foreach($singleMultiValChildren as $smvkey => $child{
  309.             $this->insert_smvRow($smvkey,$child,$row,$debug);
  310.         }
  311.  
  312.         if ($debugecho "</ul>\n\n<p>Done LVU_DOMDOCUMENT::getDataArray()</p>\n<hr></blockquote><!-- -------------------------------------- -->\n";
  313.         return $row;
  314.     }
  315.  
  316.     /**
  317.       * imports a subtree
  318.       *
  319.       * @param array $parData 
  320.       * @param string $tblTag 
  321.       * @param string $rowTag 
  322.       * @param DOMnode $node 
  323.       * @param boolean $debug 
  324.       *
  325.       * @since pk-04-08-10
  326.       *
  327.       ***/
  328.     function importSubTreeData($parData,$tblTag,$rowTag,&$node,$debug=FALSE{
  329.         $debug=($debug || $this->debugMode);
  330.         if ($debugecho "\n<p><b>LVU_DOMDOCUMENT::importSubTreeData(\$parData (".sizeof($parData).",$tblTag,$rowTag,\$node...)</b> (".get_class($this).")</p>\n\n\t<blockquote><!-- -------------------------------------- -->\n";
  331.         $key=$tblTag.">".$rowTag;
  332.  
  333.         if (!isset($this->tmpImpArr[$key])) {
  334.             $this->populate_tmpImpArr($tblTag,$rowTag,$debug);
  335.         }
  336.  
  337.         if (is_array($this->tmpImpArr[$key]['COLS'])) {
  338.             $childNodes=array();
  339.             $row=$this->getDataArray($key,$node,$childNodes,$debug);
  340.             // echo "<pre>ROW: ".print_r($row,TRUE)."</pre>\n";
  341.             foreach($row as $impTbl => $impRow{
  342.                 foreach($parData as $parTbl => $parRow{
  343.                     foreach($parRow as $col => $val{
  344.                         if (!isset($impRow[$col])) {
  345.                             $impRow[$col]=$val;
  346.                         }
  347.                     }
  348.                 }
  349.                 $GLOBALS[$this->gDBIDX]->replaceArray($impTbl,$impRow,$debug);
  350.             }
  351.  
  352.             foreach($childNodes as $cchild{
  353.                 if ($cchild->nodeType != XML_TEXT_NODE{
  354.                     $this->print_node($cchild);
  355.                     $this->importSubTreeData($row,$node->nodeName,$cchild->nodeName,$cchild,$debug);
  356.                 }
  357.             }
  358.         }
  359.         if ($debugecho "</blockquote><!-- -------------------------------------- -->\n";
  360.     }
  361.  
  362.     /**
  363.       * imports the data of the xml file
  364.       *
  365.       * @param DOMnode $node 
  366.       * @param boolean $delTab 
  367.       * @param boolean $debug 
  368.       *
  369.       ***/
  370.     function importData($node=NULL,$delTab=NULL,$debug=FALSE{
  371.         $debug=($debug || $this->debugMode);
  372.         if ($debug){
  373.             echo "\n<p><b>LVU_DOMDOCUMENT::importData(\$node,$delTab,...)</b> (".get_class($this).")</p>\n";
  374.         }
  375.  
  376.         if (!$node{
  377.             $node=$this->getRoot();
  378.             $this->tmpImpArr=array();
  379.         }
  380.  
  381.         if ($node{
  382.             if ($node->childNodes->length{
  383.                 $childNodes=array();
  384.                 foreach($node->childNodes as $child{
  385.                     if (($node->nodeType != XML_TEXT_NODE&& ($child->nodeType != XML_TEXT_NODE)) {    // we have a subtree
  386.                         $key=$node->nodeName.">".$child->nodeName;                                      // generate a node key
  387.                         if ($debugecho "<hr><h2>Checking: ".htmlspecialchars($key)."</h2>";
  388.                         if (!isset($this->tmpImpArr[$key])) {                                           // not yet checked
  389.                             if ($this->populate_tmpImpArr($node->nodeName,$child->nodeName,$debug)) {   // check if we have a definition for the subtree
  390.                                 if ($delTab == 1{                                                     // truncate table 
  391.                                     $cmd "TRUNCATE ".$this->tmpImpArr[$key]['XRS_TABLE'];                                                                                     
  392.                                     if ($debugecho "\n<p>$cmd</p>";
  393.                                     $GLOBALS['USRDB']->executeCmd($cmd,$GLOBALS['DEBUGMODE']);
  394.                                 }
  395.                             }
  396.                         }
  397.  
  398.                         if (is_array($this->tmpImpArr[$key]['COLS'])) {                                 // 
  399.                             $chChildNodes=array();
  400.                             $row=$this->getDataArray($key,$child,$chChildNodes,$debug);
  401.                             // echo "<pre>ROW: ".print_r($row,TRUE)."</pre>\n";
  402.                             if ($this->tmpImpArr[$key]['XRS_DOINSERT']{
  403.                                 foreach($row as $impTbl => $impRow{
  404.                                     $GLOBALS[$this->gDBIDX]->replaceArray($impTbl,$impRow,$debug);
  405.                                 }
  406.                             }
  407.                             foreach($chChildNodes as $cchild{
  408.                                 if ($cchild->nodeType != XML_TEXT_NODE{
  409.                                     $this->print_node($cchild);
  410.                                     $this->importSubTreeData($row,$child->nodeName,$cchild->nodeName,$cchild,$debug);
  411.                                 }
  412.                             }
  413.                         else {
  414.                             $childNodes[]=$child;
  415.                         }
  416.                     }
  417.                 }
  418.  
  419.                 foreach($childNodes as $child{
  420.                     if ($child->nodeType != XML_TEXT_NODE{
  421.                         if ($debug$this->print_node($child);
  422.                         $this->importData($child,$delTab,$debug);
  423.                     }
  424.                 }
  425.             }
  426.         }
  427.     }
  428.  
  429.     /**
  430.       * echos a node used for debuging
  431.       *
  432.       * @param DOMNode $node 
  433.       *
  434.       ***/
  435.     function print_node($node=NULL,$fullDebug=FALSE{
  436.         if (!$node$node=$this->myDom;
  437.         echo "\n<p>Node Info: (file: ".$this->xmlFile.")</p>\n";
  438.  
  439.         print "\n\t<blockquote><!-- -------------------------------------- -->\n<pre>Node Name: " $node->nodeName;
  440.         print "\nNode Type: " $node->nodeType;
  441.         $child_count $node->childNodes->length;
  442.         print "\nNum Children: " $child_count;
  443.         if ($child_count <= 1{
  444.             print "\nNode Content: " $node->nodeValue;
  445.         }
  446.  
  447.         echo "\n-----------------------------\n\tNODETYPE: ";
  448.         switch($node->nodeType{
  449.             case XML_ELEMENT_NODE:
  450.                 echo "ELEMENT_NODE\n";
  451.                 break;
  452.             case XML_ATTRIBUTE_NODE:
  453.                 echo "ATTRIBUTE NODE\n";
  454.                 break;
  455.             case XML_TEXT_NODE:
  456.                 echo "TEXT NODE\n";
  457.                 break;
  458.             default:
  459.                 echo "UNKNOWN ".$node->nodeType."\n";
  460.                 break;
  461.  
  462.         }
  463.  
  464.         if ($fullDebugecho "<pre>".pcf_object_info($node)."</pre>\n";
  465.         print "</pre>\n</blockquote><!-- -------------------------------------- -->\n";
  466.     }
  467.  
  468. }
  469.  
  470.  
  471. ?>

Documentation generated on Thu, 08 Jan 2009 17:44:35 +0100 by phpDocumentor 1.4.0a2