[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/xmlsql/ -> class.xmlsql.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class xmlsql
  24  {
  25      protected $con;
  26      protected $xml;
  27      
  28      protected $tables;
  29      
  30      protected $stack;
  31      
  32      public $test_version = 0;
  33      
  34  	public function __construct(&$con,$xml)
  35      {
  36          $this->con =& $con;
  37          $this->xml = $xml;
  38          
  39          $this->tables = $this->con->getTables();
  40      }
  41      
  42  	public function replace($str,$rep)
  43      {
  44          $this->xml = str_replace($str,$rep,$this->xml);
  45      }
  46      
  47  	public function execute($version=0)
  48      {
  49          $this->test_version = $version;
  50          
  51          $x = @simplexml_load_string($this->xml);
  52          if (!$x) {
  53              throw new Exception('Unable to load XML file.');
  54          }
  55          
  56          $this->parseNode($x);
  57      }
  58      
  59  	protected function parseNode($node)
  60      {
  61          foreach ($node->children() as $n)
  62          {
  63              switch (dom_import_simplexml($n)->nodeName)
  64              {
  65                  case 'test':
  66                      $this->performTest($n);
  67                      break;
  68                  case 'action':
  69                      $this->performAction($n);
  70                      break;
  71              }
  72          }
  73      }
  74      
  75  	protected function performTest($node)
  76      {
  77          /* Test like:
  78          <test type="table" name="table name" [eq="neq"]>...</test>
  79          */
  80          if (isset($node['type']) && (string) $node['type'] == 'table')
  81          {
  82              $test['result'] = in_array($node['name'],$this->tables);
  83              $test['label'] = 'Table %s does not exists';
  84              $test['string'] = (string) $node['name'];
  85              
  86              $xtest = $node;
  87          }
  88          /* Test syntax:
  89          <test type="column" name"table.column" [eq="neq"]>...</test>
  90          */
  91          elseif (isset($node['type']) && (string) $node['type'] == 'column')
  92          {
  93              $c = explode('.',$node['name']);
  94              
  95              if (count($c) != 2) {
  96                  return false;
  97                  
  98              }
  99              
 100              list($table,$col) = $c;
 101              
 102              $rs = $this->con->getColumns($table);
 103              
 104              $test['result'] = isset($rs[$col]);
 105              $test['label'] = 'Column %s does not exists';
 106              $test['string'] = (string) $node['name'];
 107              
 108              $xtest = $node;
 109          }
 110          /* Test syntax:
 111          <test type="version" name="version number" [comp=">"]>...</test>
 112          */
 113          elseif (isset($node['type']) && (string) $node['type'] == 'version')
 114          {
 115              $comp = isset($node['comp']) ? $node['comp'] : '>';
 116              $test['result'] = version_compare($node['name'],$this->test_version,$comp);
 117              $test['label'] = 'Version %s is too low';
 118              $test['string'] = (string) $node['name'];
 119              
 120              $xtest = $node;
 121          }
 122          
 123          # End tests
 124          if (isset($xtest))
 125          {
 126              if ($xtest['eq'] == 'neq') {
 127                  $test['result'] = !$test['result'];
 128              }
 129              
 130              if (isset($xtest['alert'])) {
 131                  $test['alert'] = (boolean) (integer) $xtest['alert'];
 132              } else {
 133                  $test['alert'] = false;
 134              }
 135              
 136              if (isset($xtest['label'])) {
 137                  $test['label'] = (string) $xtest['label'];
 138              }
 139              if (isset($xtest['string'])) {
 140                  $test['string'] = (string) $xtest['string'];
 141              }
 142              unset($xtest);
 143              
 144              # Test false
 145              if (!$test['result'])
 146              {
 147                  if ($test['alert']) {
 148                      throw new Exception(sprintf($test['label'],$test['string']));
 149                  }
 150              }
 151              # Test true
 152              else
 153              {
 154                  $this->parseNode($node);
 155              }
 156          }
 157          else
 158          {
 159              return false;
 160          }
 161      }
 162      
 163  	protected function performAction($node)
 164      {
 165          $req = trim((string) $node);
 166          if ($req)
 167          {
 168              try {
 169                  $this->con->execute($req);
 170              } catch (Exception $e) {
 171                  if ($node['silent'] != 1) {
 172                      throw $e;
 173                  }
 174              }
 175          }
 176      }
 177  }
 178  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7