eZ Publish  [4.0]
checkdbfiles.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <26-Mar-2004 10:31:14 amos>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //include_once( 'lib/ezutils/classes/ezcli.php' );
00031 //include_once( 'kernel/classes/ezscript.php' );
00032 
00033 require 'autoload.php';
00034 
00035 $cli = eZCLI::instance();
00036 $script = eZScript::instance( array( 'description' => ( "eZ Publish DB file verifier\n\n" .
00037                                                         "Checks the database update files and gives a report on them.\n" .
00038                                                         "It will show which files are missing and which should not be present.\n" .
00039                                                         "\n" .
00040                                                         "For each file with problems it will output a status and the filepath\n" .
00041                                                         "The status will be one of these:\n" .
00042                                                         " '?' file is not defined in upgrade path\n" .
00043                                                         " '!' file defined in upgrade path but missing on filesystem\n" .
00044                                                         " 'A' file is present in working copy but not in the original stable branch\n" .
00045                                                         " 'C' file data conflicts with the original stable branch\n" .
00046                                                         "\n" .
00047                                                         "Example output:\n" .
00048                                                         "  checkdbfiles.php\n" .
00049                                                         "  ? update/database/mysql/3.5/dbupdate-3.5.0-to-3.5.1.sql" ),
00050                                      'use-session' => false,
00051                                      'use-modules' => false,
00052                                      'use-extensions' => true ) );
00053 
00054 $script->startup();
00055 
00056 $options = $script->getOptions( "[no-verify-branches][export-path:]",
00057                                 "",
00058                                 array( 'no-verify-branches' => "Do not verify the content of the files with previous branches (To avoid SVN usage)",
00059                                        'export-path' => "Directory to use for doing SVN exports."
00060                                        ) );
00061 $script->initialize();
00062 
00063 $dbTypes = array();
00064 $dbTypes[] = 'mysql';
00065 $dbTypes[] = 'postgresql';
00066 
00067 $branches = array();
00068 $branches[] = '4.0';
00069 
00070 // Controls the lowest version which will be exported and verified against current data
00071 $lowestExportVersion = '4.0';
00072 
00073 /********************************************************
00074 *** NOTE: The following arrays do not follow the
00075 ***       coding standard, the reason for this is
00076 ***       to make it easy to merge any changes between
00077 ***       the various eZ Publish branches.
00078 *********************************************************/
00079 
00080 $versions = array();
00081 $versions40 = array( 'unstable' => array( array( '3.10.0', '4.0.0alpha1' )
00082                                           , array( '4.0.0alpha1', '4.0.0alpha2' )
00083                                           , array( '4.0.0alpha2', '4.0.0beta1' )
00084                                           , array( '4.0.0beta1', '4.0.0rc1' )
00085                                           , array( '4.0.0rc1', '4.0.0' )
00086                       , array( '4.0.0', '4.0.1rc1' )
00087                       , array( '4.0.1rc1', '4.0.1rc2' )
00088                       , array( '4.0.1rc2', '4.0.1' )
00089                                          ),
00090                      'unstable_subdir' => 'unstable',
00091                      'stable' => array( array( '3.10.0', '4.0.0' )
00092                                         , array( '4.0.0', '4.0.1' )
00093                                         , array( '4.0.1', '4.0.2' )
00094                                         , array( '4.0.2', '4.0.3' )
00095                                         , array( '4.0.3', '4.0.4' )
00096                                         , array( '4.0.4', '4.0.5' )
00097                                         , array( '4.0.5', '4.0.6' )
00098                                         , array( '4.0.6', '4.0.7' )
00099                                         , array( '4.0.7', '4.0.8' )
00100                 )
00101            );
00102 
00103 $versions['4.0'] = $versions40;
00104 
00105 $fileList = array();
00106 $missingFileList = array();
00107 $conflictFileList = array();
00108 $exportMissingFileList = array();
00109 $scannedDirs = array();
00110 
00111 function handleVersionList( $basePath, $subdir,
00112                             &$fileList, &$missingFileList, &$conflictFileList, &$scannedDirs,
00113                             $useInfoFiles, $versionList )
00114 {
00115     $updatePath = $basePath . $subdir;
00116     if ( is_dir( $updatePath ) )
00117     {
00118         if ( !in_array( $updatePath, $scannedDirs ) )
00119         {
00120             $dh = opendir( $updatePath );
00121             if ( $dh )
00122             {
00123                 while ( ( $file = readdir( $dh ) ) !== false )
00124                 {
00125                     if ( $file == '.' or
00126                          $file == '..' or
00127                          substr( $file, strlen( $file ) - 1, 1 ) == '~' or
00128                          is_dir( $updatePath . '/' . $file ) )
00129                         continue;
00130                     $fileList[] = $updatePath . '/' . $file;
00131                 }
00132                 closedir( $dh );
00133             }
00134             $fileList = array_unique( $fileList );
00135             $scannedDirs[] = $updatePath;
00136         }
00137     }
00138 
00139     foreach ( $versionList as $versionEntry )
00140     {
00141         $from = $versionEntry[0];
00142         $to = $versionEntry[1];
00143         $dir = $updatePath;
00144         $file = $dir . '/dbupdate-' . $from . '-to-' . $to . '.sql';
00145         $fileList = array_diff( $fileList, array( $file ) );
00146         if ( !file_exists( $file ) )
00147         {
00148             $missingFileList[] = $file;
00149         }
00150         if ( $useInfoFiles )
00151         {
00152             $infoFile = $dir . '/dbupdate-' . $from . '-to-' . $to . '.info';
00153             $fileList = array_diff( $fileList, array( $infoFile ) );
00154             if ( !file_exists( $infoFile ) )
00155             {
00156                 $missingFileList[] = $infoFile;
00157             }
00158         }
00159     }
00160 }
00161 
00162 function handleExportVersionList( $basePath, $exportBasePath, $subdir,
00163                                   &$fileList, &$missingFileList, &$exportMissingFileList, &$conflictFileList, &$scannedDirs,
00164                                   $useInfoFiles, $versionList )
00165 {
00166     $updatePath = $basePath . $subdir;
00167     $exportUpdatePath = $exportBasePath . $subdir;
00168     foreach ( $versionList as $versionEntry )
00169     {
00170         $from = $versionEntry[0];
00171         $to = $versionEntry[1];
00172         $file = $updatePath . '/dbupdate-' . $from . '-to-' . $to . '.sql';
00173         $exportFile = $exportUpdatePath . '/dbupdate-' . $from . '-to-' . $to . '.sql';
00174         if ( file_exists( $file ) and file_exists( $exportFile ) )
00175         {
00176             $srcMD5 = md5_file( $file );
00177             $dstMD5 = md5_file( $exportFile );
00178             // If the MD5s differ we flag it as a conflict
00179             if ( strcmp( $srcMD5, $dstMD5 ) != 0 )
00180             {
00181                 $conflictFileList[] = $file;
00182             }
00183         }
00184         else
00185         {
00186             $exportMissingFileList[] = $file;
00187         }
00188 
00189         if ( $useInfoFiles )
00190         {
00191             $infoFile = $updatePath . '/dbupdate-' . $from . '-to-' . $to . '.info';
00192             $exportInfoFile = $exportUpdatePath . '/dbupdate-' . $from . '-to-' . $to . '.sql';
00193             if ( file_exists( $infoFile ) and file_exists( $exportInfoFile ) )
00194             {
00195                 $srcMD5 = md5_file( $infoFile );
00196                 $dstMD5 = md5_file( $exportInfoFile );
00197                 // If the MD5s differ we flag it as a conflict
00198                 if ( strcmp( $srcMD5, $dstMD5 ) != 0 )
00199                 {
00200                     $conflictFileList[] = $file;
00201                 }
00202             }
00203             else
00204             {
00205                 $exportMissingFileList[] = $infoFile;
00206             }
00207         }
00208     }
00209 }
00210 
00211 function exportSVNVersion( $version, $exportPath )
00212 {
00213     $versionPath = $exportPath . '/' . $version;
00214     if ( file_exists( $versionPath ) )
00215         return true;
00216 
00217     $svn = "svn export http://svn.ez.no/svn/ezpublish/stable/$version/update/database \"$versionPath\"";
00218     exec( $svn, $output, $code );
00219     if ( $code != 0 )
00220     {
00221         print( "Failed to export using:\n$svn\n" );
00222         return false;
00223     }
00224     return file_exists( $versionPath );
00225 }
00226 
00227 // Check for required md5_file function
00228 if ( !function_exists( 'md5_file' ) )
00229 {
00230     $cli->error( "The function 'md5_file' does not exist in your PHP version" );
00231     $cli->error( "You must upgrade PHP to a version (4.2.0) that has this function" );
00232     $script->shutdown( 1 );
00233 }
00234 
00235 if ( !$options['no-verify-branches'] )
00236 {
00237     // Clean up the export path and/or recreate the directory path
00238     if ( $options['export-path'] )
00239     {
00240         $exportPath = $options['export-path'];
00241     }
00242     else
00243     {
00244         $exportPath = '/tmp/';
00245         if ( isset( $_SERVER['TMPDIR'] ) )
00246             $exportPath = $_SERVER['TMPDIR'] . '/';
00247         if ( isset( $_SERVER['USER'] ) )
00248             $exportPath .= "ez-" . $_SERVER['USER'];
00249         $exportPath .= "/dbupdate-check/";
00250     }
00251 
00252     if ( file_exists( $exportPath ) )
00253     {
00254         //include_once( 'lib/ezfile/classes/ezdir.php' );
00255         eZDir::recursiveDelete( $exportPath );
00256     }
00257     //include_once( 'lib/ezfile/classes/ezdir.php' );
00258     eZDir::mkdir( $exportPath, false, true );
00259 }
00260 
00261 // Figure out the current branch, we do not want to export it
00262 //include_once( 'lib/version.php' );
00263 $currentBranch = eZPublishSDK::VERSION_MAJOR . '.' . eZPublishSDK::VERSION_MINOR;
00264 
00265 foreach ( $dbTypes as $dbType )
00266 {
00267     foreach ( $branches as $branch )
00268     {
00269         $basePath = 'update/database/' . $dbType . '/' . $branch;
00270         $versionList = $versions[$branch];
00271         $useInfoFiles = false;
00272         if ( isset( $versionList['info_files'] ) )
00273         {
00274             $useInfoFiles = $versionList['info_files'];
00275         }
00276         if ( isset( $versionList['unstable'] ) )
00277         {
00278             $subdir = false;
00279             if ( isset( $versionList['unstable_subdir'] ) )
00280             {
00281                 $subdir = '/' . $versionList['unstable_subdir'];
00282             }
00283             handleVersionList( $basePath, $subdir,
00284                                $fileList, $missingFileList, $conflictFileList, $scannedDirs,
00285                                $useInfoFiles, $versionList['unstable'] );
00286         }
00287         if ( isset( $versionList['stable'] ) )
00288         {
00289             $subdir = false;
00290             if ( isset( $versionList['stable_subdir'] ) )
00291             {
00292                 $subdir = '/' . $versionList['stable_subdir'];
00293             }
00294             handleVersionList( $basePath, $subdir,
00295                                $fileList, $missingFileList, $conflictFileList, $scannedDirs,
00296                                $useInfoFiles, $versionList['stable'] );
00297         }
00298 
00299         if ( !$options['no-verify-branches'] and
00300              version_compare( $branch, $lowestExportVersion ) >= 0 and
00301              version_compare( $branch, $currentBranch ) < 0 )
00302         {
00303             if ( !exportSVNVersion( $branch, $exportPath ) )
00304             {
00305                 $conflictFileList[] = $dir . '/' . $branch;
00306                 continue;
00307             }
00308             if ( isset( $versionList['unstable'] ) )
00309             {
00310                 $exportBasePath = $exportPath . '/' . $branch . '/' . $dbType . '/' . $branch;
00311                 $subdir = false;
00312                 if ( isset( $versionList['unstable_subdir'] ) )
00313                 {
00314                     $subdir = '/' . $versionList['unstable_subdir'];
00315                 }
00316                 handleExportVersionList( $basePath, $exportBasePath, $subdir,
00317                                          $fileList, $missingFileList, $exportMissingFileList, $conflictFileList, $scannedDirs,
00318                                          $useInfoFiles, $versionList['unstable'] );
00319             }
00320             if ( isset( $versionList['stable'] ) )
00321             {
00322                 $exportBasePath = $exportPath . '/' . $branch . '/' . $dbType . '/' . $branch;
00323                 $subdir = false;
00324                 if ( isset( $versionList['stable_subdir'] ) )
00325                 {
00326                     $subdir = '/' . $versionList['stable_subdir'];
00327                 }
00328                 handleExportVersionList( $basePath, $exportBasePath, $subdir,
00329                                          $fileList, $missingFileList, $exportMissingFileList, $conflictFileList, $scannedDirs,
00330                                          $useInfoFiles, $versionList['stable'] );
00331             }
00332         }
00333     }
00334 }
00335 
00336 if ( count( $missingFileList ) > 0 or
00337      count( $exportMissingFileList ) > 0 or
00338      count( $fileList ) > 0 or
00339      count( $conflictFileList ) > 0 )
00340 {
00341     if ( count( $fileList ) > 0 )
00342     {
00343         foreach ( $fileList as $file )
00344         {
00345             print( '? ' . $file . "\n" );
00346         }
00347     }
00348 
00349     if ( count( $missingFileList ) > 0 )
00350     {
00351         foreach ( $missingFileList as $file )
00352         {
00353             print( '! ' . $file . "\n" );
00354         }
00355     }
00356 
00357     if ( count( $exportMissingFileList ) > 0 )
00358     {
00359         foreach ( $exportMissingFileList as $file )
00360         {
00361             print( 'A ' . $file . "\n" );
00362         }
00363     }
00364 
00365     if ( count( $conflictFileList ) > 0 )
00366     {
00367         foreach ( $conflictFileList as $file )
00368         {
00369             print( 'C ' . $file . "\n" );
00370         }
00371     }
00372     $script->setExitCode( 1 );
00373 }
00374 
00375 if ( !$options['no-verify-branches'] )
00376 {
00377     // Cleanup any exports
00378     if ( file_exists( $exportPath ) )
00379     {
00380         //include_once( 'lib/ezfile/classes/ezdir.php' );
00381         eZDir::recursiveDelete( $exportPath );
00382     }
00383 }
00384 
00385 $script->shutdown();
00386 
00387 ?>