eZ Publish  [trunk]
ezdbschema.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZDbSchema class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZDbSchema ezdbschema.php
00013   \ingroup eZDbSchema
00014   \brief A factory for schema handlers
00015 
00016 */
00017 
00018 class eZDbSchema
00019 {
00020     /**
00021      * Returns a shared instance of the eZDBSchemaInterface class.
00022      *
00023      * @param array|eZDBInterface|false $params If array, following key is needed:
00024      *        - instance: the eZDB instance (optional), if none provided, eZDB::instance() will be used.
00025      * @return eZDBSchemaInterface|false
00026      */
00027     static function instance( $params = false )
00028     {
00029         if ( is_object( $params ) )
00030         {
00031             $db = $params;
00032             $params = array( 'instance' => $db );
00033         }
00034 
00035         if ( !isset( $params['instance'] ) )
00036         {
00037             $db = eZDB::instance();
00038             $params['instance'] = $db;
00039         }
00040 
00041         $db = $params['instance'];
00042 
00043         if ( !isset( $params['type'] ) )
00044             $params['type'] = $db->databaseName();
00045         if ( !isset( $params['schema'] ) )
00046             $params['schema'] = false;
00047 
00048         $dbname = $params['type'];
00049 
00050         /* Load the database schema handler INI stuff */
00051         $ini = eZINI::instance( 'dbschema.ini' );
00052         $schemaPaths = $ini->variable( 'SchemaSettings', 'SchemaPaths' );
00053         $schemaHandlerClasses = $ini->variable( 'SchemaSettings', 'SchemaHandlerClasses' );
00054 
00055         /* Check if we have a handler */
00056         if ( !isset( $schemaPaths[$dbname] ) or !isset( $schemaHandlerClasses[$dbname] ) )
00057         {
00058             eZDebug::writeError( "No schema handler for database type: $dbname", __METHOD__ );
00059             return false;
00060         }
00061 
00062         /* Include the schema file and instantiate it */
00063         require_once( $schemaPaths[$dbname] );
00064         return new $schemaHandlerClasses[$dbname]( $params );
00065     }
00066 
00067     /*!
00068      \static
00069     */
00070     static function read( $filename, $returnArray = false )
00071     {
00072         $fd = @fopen( $filename, 'rb' );
00073         if ( $fd )
00074         {
00075             $buf = fread( $fd, 100 );
00076             fclose( $fd );
00077             if ( preg_match( '#^<\?' . "php#", $buf ) )
00078             {
00079                 include( $filename );
00080                 if ( $returnArray )
00081                 {
00082                     $params = array();
00083                     if ( isset( $schema ) )
00084                         $params['schema'] = $schema;
00085                     if ( isset( $data ) )
00086                         $params['data'] = $data;
00087                     return $params;
00088                 }
00089                 else
00090                 {
00091                     return $schema;
00092                 }
00093             }
00094             else if ( preg_match( '#a:[0-9]+:{#', $buf ) )
00095             {
00096                 return unserialize( file_get_contents( $filename ) );
00097             }
00098             else
00099             {
00100                 eZDebug::writeError( "Unknown format for file $filename" );
00101                 return false;
00102             }
00103         }
00104         return false;
00105     }
00106 
00107     /*!
00108      \static
00109     */
00110     static function readArray( $filename )
00111     {
00112         $schema = false;
00113         include( $filename );
00114         return $schema;
00115     }
00116 
00117     /*!
00118      \static
00119     */
00120     static function generateUpgradeFile( $differences )
00121     {
00122         $diff = var_export( $differences, true );
00123         return ( "<?php \n\$diff = \n" . $diff . ";\nreturn \$diff;\n?>\n" );
00124     }
00125 
00126     /*!
00127      \static
00128     */
00129     static function writeUpgradeFile( $differences, $filename )
00130     {
00131         $fp = @fopen( $filename, 'w' );
00132         if ( $fp )
00133         {
00134             fputs( $fp, eZDbSchema::generateUpgradeFile( $differences ) );
00135             fclose( $fp );
00136             return true;
00137         }
00138         else
00139         {
00140             return false;
00141         }
00142     }
00143 
00144     /**
00145      * Merges 2 db schemas, basically appending 2nd on top of 1st
00146      * @return array the merged schema
00147      */
00148     static function merge( $schema1, $schema2 )
00149     {
00150         $merged = $schema1;
00151         foreach( $schema2 as $tablename => $tabledef )
00152         {
00153             if ( $tablename != '_info' )
00154             {
00155                 $merged[$tablename] = $tabledef;
00156             }
00157         }
00158        return $merged;
00159     }
00160 }
00161 ?>