eZ Publish  [4.0]
ezdbpackagehandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDBPackageHandler class
00004 //
00005 // Created on: <23-Jul-2003 16:11:42 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezdbpackagehandler.php
00032 */
00033 
00034 /*!
00035   \class eZDBPackageHandler ezdbpackagehandler.php
00036   \brief Handles SQL files in the package system
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/ezpackagehandler.php' );
00041 
00042 class eZDBPackageHandler extends eZPackageHandler
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZDBPackageHandler()
00048     {
00049         $this->eZPackageHandler( 'ezdb' );
00050     }
00051 
00052     /*!
00053      Installs the package type
00054     */
00055     function install( $package, $installType, $parameters,
00056                       $name, $os, $filename, $subdirectory,
00057                       $content, &$installParameters,
00058                       &$installData )
00059     {
00060         $path = $package->path();
00061         $databaseType = false;
00062         if ( isset( $parameters['database-type'] ) )
00063         {
00064             $databaseType = $parameters['database-type'];
00065         }
00066         $path .= '/' . eZDBPackageHandler::sqlDirectory();
00067         if ( $databaseType )
00068         {
00069             $path .= '/' . $databaseType;
00070         }
00071 
00072         if ( file_exists( $path ) )
00073         {
00074             $db = eZDB::instance();
00075             $canInsert = true;
00076             if ( $databaseType and
00077                  $databaseType != $db->databaseName() )
00078             {
00079                 $canInsert = false;
00080             }
00081 
00082             if ( $canInsert )
00083             {
00084                 eZDebug::writeDebug( "Installing SQL file $path/$filename" );
00085                 $db->insertFile( $path, $filename, false );
00086                 return true;
00087             }
00088             else
00089             {
00090                 eZDebug::writeDebug( "Skipping SQL file $path/$filename" );
00091             }
00092         }
00093         else
00094         {
00095             eZDebug::writeError( "Could not find SQL file $path/$filename" );
00096         }
00097         return false;
00098     }
00099 
00100     /*!
00101      \reimp
00102     */
00103     function add( $packageType, $package, $cli, $parameters )
00104     {
00105         if ( isset( $parameters['sql-file-list'] ) )
00106         {
00107             foreach ( $parameters['sql-file-list'] as $fileItem )
00108             {
00109                 $package->appendInstall( 'sql', false, false, true,
00110                                          $fileItem['file'], false,
00111                                          array( 'path' => $fileItem['path'],
00112                                                 'database-type' => $fileItem['database_type'],
00113                                                 'copy-file' => true ) );
00114                 if ( $fileItem['database_type'] )
00115                     $package->appendDependency( 'requires',
00116                                                 array( 'type' => 'ezdb',
00117                                                        'name' => $fileItem['database_type'],
00118                                                        'value' => false ) );
00119                 $noticeText = "Adding " . $cli->stylize( 'mark', "sql" ) . " file " . $cli->stylize( 'file', $fileItem['path'] );
00120                 if ( $fileItem['database_type'] )
00121                     $noticeText .= " for database " . $cli->stylize( 'emphasize',  $fileItem['database_type']  );
00122                 $cli->notice( $noticeText );
00123             }
00124         }
00125     }
00126 
00127     function handleAddParameters( $packageType, $package, $cli, $arguments )
00128     {
00129         return $this->handleParameters( $packageType, $package, $cli, 'add', $arguments );
00130     }
00131 
00132     function handleParameters( $packageType, $package, $cli, $type, $arguments )
00133     {
00134         $sqlFileList = array();
00135         $currentDatabaseType = false;
00136 
00137         for ( $i = 0; $i < count( $arguments ); ++$i )
00138         {
00139             $argument = $arguments[$i];
00140             if ( $argument[0] == '-' )
00141             {
00142                 if ( strlen( $argument ) > 1 and
00143                      $argument[1] == '-' )
00144                 {
00145                 }
00146                 else
00147                 {
00148                     $flag = substr( $argument, 1, 1 );
00149                     if ( $flag == 'd' )
00150                     {
00151                         if ( strlen( $argument ) > 2 )
00152                         {
00153                             $data = substr( $argument, 2 );
00154                         }
00155                         else
00156                         {
00157                             $data = $arguments[$i+1];
00158                             ++$i;
00159                         }
00160                         if ( $flag == 'd' )
00161                         {
00162                             $currentDatabaseType = $data;
00163                         }
00164                     }
00165                 }
00166             }
00167             else
00168             {
00169                 $sqlFile = $argument;
00170                 $databaseType = $currentDatabaseType;
00171                 $realFilePath = $this->sqlFileExists( $sqlFile, $databaseType,
00172                                                       $triedFiles );
00173                 if ( !$realFilePath )
00174                 {
00175                     $cli->error( "SQL file " . $cli->style( 'file' ) . $sqlFile . $cli->style( 'file-end' ) . " does not exist\n" .
00176                                  "The following files were searched for:\n" .
00177                                  implode( "\n", $triedFiles ) );
00178                     return false;
00179                 }
00180                 $fileList[] = array( 'file' => $sqlFile,
00181                                      'database_type' => $databaseType,
00182                                      'path' => $realFilePath );
00183             }
00184         }
00185         if ( count( $fileList ) == 0 )
00186         {
00187             $cli->error( "No files were added" );
00188             return false;
00189         }
00190         return array( 'sql-file-list' => $fileList );
00191     }
00192 
00193     function sqlFileExists( &$sqlFile, &$databaseType,
00194                             &$triedFiles )
00195     {
00196         $triedFiles = array();
00197         if ( file_exists( $sqlFile ) )
00198         {
00199             $filePath = $sqlFile;
00200             if ( preg_match( '#^.+/([^/]+$)#', $sqlFile, $matches ) )
00201                 $sqlFile = $matches[1];
00202             return $filePath;
00203         }
00204         $filePath = 'kernel/sql/' . $databaseType . '/' . $sqlFile;
00205         if ( file_exists( $filePath ) )
00206             return $filePath;
00207         $triedFiles[] = $filePath;
00208         $filePath = 'kernel/sql/' . $databaseType . '/' . $sqlFile . '.sql';
00209         if ( file_exists( $filePath ) )
00210         {
00211             $sqlFile .= '.sql';
00212             return $filePath;
00213         }
00214         $triedFiles[] = $filePath;
00215         return false;
00216     }
00217 
00218     function sqlDirectory()
00219     {
00220         return 'sql';
00221     }
00222 
00223     /*!
00224      \reimp
00225     */
00226     function createInstallNode( $package, $installNode, $installItem, $installType )
00227     {
00228         $installNode->setAttribute( 'original-path', $installItem['path'] );
00229         $installNode->setAttribute( 'database-type', $installItem['database-type'] );
00230 
00231         $originalPath = $installItem['path'];
00232         $installDirectory = $package->path() . '/' . eZDBPackageHandler::sqlDirectory();
00233         if ( $installItem['database-type'] )
00234             $installDirectory .= '/' . $installItem['database-type'];
00235         if ( !file_exists(  $installDirectory ) )
00236             eZDir::mkdir( $installDirectory, false, true );
00237         eZFileHandler::copy( $originalPath, $installDirectory . '/' . $installItem['filename'] );
00238     }
00239 
00240     /*!
00241      \reimp
00242     */
00243     function parseInstallNode( $package, $installNode, &$installParameters, $isInstall )
00244     {
00245         $installParameters['path'] = $installNode->getAttribute( 'original-path' );
00246         $installParameters['database-type'] = $installNode->getAttribute( 'database-type' );
00247     }
00248 }
00249 
00250 ?>