[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/lib/ezdbschema/classes/ -> ezdbschemachecker.php (source)

   1  <?php
   2  //
   3  // Created on: <28-Jan-2004 16:10:44 dr>
   4  //
   5  // SOFTWARE NAME: eZ publish
   6  // SOFTWARE RELEASE: 3.9.0
   7  // BUILD VERSION: 17785
   8  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
   9  // SOFTWARE LICENSE: GNU General Public License v2.0
  10  // NOTICE: >
  11  //   This program is free software; you can redistribute it and/or
  12  //   modify it under the terms of version 2.0  of the GNU General
  13  //   Public License as published by the Free Software Foundation.
  14  //
  15  //   This program is distributed in the hope that it will be useful,
  16  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18  //   GNU General Public License for more details.
  19  //
  20  //   You should have received a copy of version 2.0 of the GNU General
  21  //   Public License along with this program; if not, write to the Free
  22  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23  //   MA 02110-1301, USA.
  24  //
  25  //
  26  
  27  /*!
  28    \class eZDBSchemaChecker ezdbschemachecker.php
  29    \ingroup eZDBSchema
  30    \brief Checks differences between schemas
  31  
  32  */
  33  
  34  class eZDbSchemaChecker
  35  {
  36      /*!
  37       \static
  38       Finds the difference between the scemas \a $schema1 and \a $schema2.
  39       \return An array containing:
  40               - new_tables - A list of new tables that have been added
  41               - removed_tables - A list of tables that have been removed
  42               - table_changes - Changes in table definition
  43                 - added_fields - A list of new fields in the table
  44                 - removed_fields - A list of removed fields in the table
  45                 - changed_fields - A list of fields that have changed definition
  46                 - added_indexes - A list of new indexes in the table
  47                 - removed_indexes - A list of removed indexes in the table
  48                 - changed_indexes - A list of indexes that have changed definition
  49      */
  50  	function diff( $schema1, $schema2 = array(), $schema1Type = false, $schema2Type = false )
  51      {
  52          if ( !is_array( $schema1 ) )
  53          {
  54              return false;
  55          }
  56          $diff = array();
  57  
  58          foreach ( $schema2 as $name => $def )
  59          {
  60              // Skip the info structure, this is not a table
  61              if ( $name == '_info' )
  62                  continue;
  63  
  64              if ( !isset( $schema1[$name] ) )
  65              {
  66                  $diff['new_tables'][$name] = $def;
  67              }
  68              else
  69              {
  70                  $table_diff = eZDbSchemaChecker::diffTable( $schema1[$name], $def, $schema1Type, $schema2Type );
  71                  if ( count( $table_diff ) )
  72                  {
  73                      $diff['table_changes'][$name] = $table_diff;
  74                  }
  75              }
  76          }
  77  
  78          /* Check if there are tables removed */
  79          foreach ( $schema1 as $name => $def )
  80          {
  81              // Skip the info structure, this is not a table
  82              if ( $name == '_info' )
  83                  continue;
  84  
  85              if ( !isset( $schema2[$name] ) )
  86              {
  87                  $diff['removed_tables'][$name] = $def;
  88              }
  89              else if ( isset( $schema2[$name]['removed'] ) and
  90                        isset( $schema2[$name]['removed'] ) )
  91              {
  92                  $diff['removed_tables'][$name] = $def;
  93              }
  94          }
  95  
  96          return $diff;
  97      }
  98  
  99      /*!
 100       \static
 101       Finds the difference between the tables \a $table1 and \a $table2 by looking
 102       at the fields and indexes.
 103  
 104       \return An array containing:
 105               - added_fields - A list of new fields in the table
 106               - removed_fields - A list of removed fields in the table
 107               - changed_fields - A list of fields that have changed definition
 108               - added_indexes - A list of new indexes in the table
 109               - removed_indexes - A list of removed indexes in the table
 110               - changed_indexes - A list of indexes that have changed definition
 111      */
 112  	function diffTable( $table1, $table2, $schema1Type, $schema2Type )
 113      {
 114          $table_diff = array();
 115  
 116          /* See if all the fields in table 1 exist in table 2 */
 117          foreach ( $table2['fields'] as $name => $def )
 118          {
 119              if ( !isset( $table1['fields'][$name] ) )
 120              {
 121                  $table_diff['added_fields'][$name] = $def;
 122              }
 123          }
 124          /* See if there are any removed fields in table 2 */
 125          foreach ( $table1['fields'] as $name => $def )
 126          {
 127              if ( !isset( $table2['fields'][$name] ) )
 128              {
 129                  $table_diff['removed_fields'][$name] = true;
 130              }
 131              else if ( isset( $table2['fields'][$name]['removed'] ) and
 132                        $table2['fields'][$name]['removed'] )
 133              {
 134                  $table_diff['removed_fields'][$name] = true;
 135              }
 136          }
 137          /* See if there are any changed definitions */
 138          foreach ( $table1['fields'] as $name => $def )
 139          {
 140              if ( isset( $table2['fields'][$name] ) )
 141              {
 142                  if ( is_array( $field_diff = eZDbSchemaChecker::diffField( $def, $table2['fields'][$name], $schema1Type, $schema2Type ) ) )
 143                  {
 144                      $table_diff['changed_fields'][$name] = $field_diff;
 145                  }
 146              }
 147          }
 148  
 149          $table1Indexes = $table1['indexes'];
 150          $table2Indexes = $table2['indexes'];
 151  
 152          /* See if all the indexes in table 1 exist in table 2 */
 153          foreach ( $table2Indexes as $name => $def )
 154          {
 155              if ( !isset( $table1Indexes[$name] ) )
 156              {
 157                  $table_diff['added_indexes'][$name] = $def;
 158              }
 159          }
 160          /* See if there are any removed indexes in table 2 */
 161          foreach ( $table1Indexes as $name => $def )
 162          {
 163              if ( !isset( $table2Indexes[$name] ) )
 164              {
 165                  $table_diff['removed_indexes'][$name] = $def;
 166              }
 167              else if ( isset( $table2Indexes[$name]['removed'] ) and
 168                        $table2Indexes[$name]['removed'] )
 169              {
 170                  if ( isset( $table2Indexes[$name]['comments'] ) )
 171                      $def['comments'] = array_merge( isset( $def['comments'] ) ? $def['comments'] : array(),
 172                                                      $table2Indexes[$name]['comments'] );
 173                  $table_diff['removed_indexes'][$name] = $def;
 174              }
 175          }
 176          /* See if there are any changed definitions */
 177          foreach ( $table1Indexes as $name => $def )
 178          {
 179              if ( isset( $table2Indexes[$name] ) )
 180              {
 181                  if ( is_array( $index_diff = eZDbSchemaChecker::diffIndex( $def, $table2Indexes[$name], $schema1Type, $schema2Type ) ) )
 182                  {
 183                      $table_diff['changed_indexes'][$name] = $index_diff;
 184                  }
 185              }
 186          }
 187  
 188          return $table_diff;
 189      }
 190  
 191      /*!
 192       \static
 193       Finds the difference between the field \a $field1 and \a $field2.
 194  
 195       \return The field definition of the changed field or \c false if there are no changes.
 196      */
 197  	function diffField( $field1, $field2, $schema1Type, $schema2Type )
 198      {
 199          /* Type is always available */
 200          if ( $field1['type'] != $field2['type'] )
 201          {
 202              return array( 'different-options' => array( 'type' ), 'field-def' => $field2 );
 203              return $field2;
 204          }
 205          
 206          $test_fields = array( 'length', 'default', 'not_null' );
 207          $different_options = array();
 208  
 209          foreach ( $test_fields as $test_field )
 210          {
 211              if ( isset( $field1[$test_field] ) )
 212              {
 213                  if ( !isset( $field2[$test_field] ) ||
 214                       ( $field1[$test_field] != $field2[$test_field] ) )
 215                  {
 216                      $different_options[] = $test_field;
 217                  }
 218              }
 219              else
 220              {
 221                  if ( isset( $field2[$test_field] ) )
 222                  {
 223                      $different_options[] = $test_field;
 224                  }
 225              }
 226          }
 227  
 228          if ( $different_options )
 229              return array( 'different-options' => $different_options, 'field-def' => $field2 );
 230          else
 231              return false;
 232      }
 233  
 234      /*!
 235       \static
 236       Finds the difference between the indexes \a $index1 and \a $index2.
 237  
 238       \return The index definition of the changed index or \c false if there are no changes.
 239      */
 240  	function diffIndex( $index1, $index2, $schema1Type, $schema2Type )
 241      {
 242          if ( ( $index1['type'] != $index2['type'] ) ||
 243               count( array_diff( $index1, $index2 ) ) )
 244          {
 245              return $index2;
 246          }
 247  
 248          $test_fields = array( 'link_table' );
 249          foreach ( $test_fields as $test_field )
 250          {
 251              if ( isset($index1[$test_field] ) )
 252              {
 253                  if ( !isset( $index2[$test_field] ) ||
 254                       ( $index1[$test_field] != $index2[$test_field] ) )
 255                  {
 256                      return $index2;
 257                  }
 258              }
 259              else
 260              {
 261                  if ( isset($index2[$test_field] ) )
 262                  {
 263                      return $index2;
 264                  }
 265              }
 266          }
 267  
 268          $test_fields = array( 'fields', 'link_fields' );
 269          foreach ( $test_fields as $test_field )
 270          {
 271              if ( isset( $index1[$test_field] ) )
 272              {
 273                  if ( !isset( $index2[$test_field] ) ||
 274                       !( $index1[$test_field] == $index2[$test_field] ) )
 275                  {
 276                      return $index2;
 277                  }
 278              }
 279              else
 280              {
 281                  if ( isset( $index2[$test_field] ) )
 282                  {
 283                      return $index2;
 284                  }
 285              }
 286          }
 287      }
 288  }
 289  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7