eZ Publish  [4.0]
fixorphanimages.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <15-Nov-2009 bd>
00005 //
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.2
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2009 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 
00027 require 'autoload.php';
00028 
00029 $cli = eZCLI::instance();
00030 $script = eZScript::instance( array( 'description' => ( "Fixes eZImageAliasHandler bug (http://issues.ez.no/15155)" ),
00031                                      'use-session' => false,
00032                                      'use-modules' => true,
00033                                      'use-extensions' => true ) );
00034 
00035 $script->startup();
00036 
00037 $options = $script->getOptions( "[n]",
00038                                 "",
00039                                 array( 'n' => 'Do not wait the 10 safety seconds before starting' ) );
00040 $script->initialize();
00041 
00042 $output = new ezcConsoleOutput();
00043 $output->formats->error->style = array( 'bold' );
00044 $output->formats->error->color = 'red';
00045 
00046 if ( !isset( $options['n'] ) )
00047 {
00048     $output->outputLine( 'This script will delete images that look orphan' );
00049     $output->outputLine( 'Press ctrl-c in the next 10 seconds to prevent the script from starting...' );
00050     sleep( 10 );
00051 }
00052 
00053 try
00054 {
00055     $output->outputLine( 'Looking for obsolete image files...' );
00056 
00057     // Fetch all image files in ezimagefile table
00058     $aImageFiles = eZPersistentObject::fetchObjectList( eZImageFile::definition() );
00059     $nbImageFiles = count( $aImageFiles );
00060 
00061     if( $nbImageFiles > 0 )
00062     {
00063         // Progress bar initialization
00064         $progressBarOptions = array(
00065             'emptyChar'     => ' ',
00066             'barChar'       => '='
00067         );
00068         $progressBar = new ezcConsoleProgressbar( $output, $nbImageFiles, $progressBarOptions );
00069 
00070         // Loop the image files and check if it is still used by a content object attribute. If not, delete it.
00071         foreach( $aImageFiles as $image )
00072         {
00073             $filePath = $image->attribute( 'filepath' );
00074             $dirpath = dirname( $filePath );
00075             $contentObjectAttributeID = $image->attribute( 'contentobject_attribute_id' );
00076             $dbResult = eZImageFile::fetchImageAttributesByFilepath( $filePath, $contentObjectAttributeID );
00077             if( count( $dbResult ) == 0 )
00078             {
00079                 $file = eZClusterFileHandler::instance( $filePath );
00080                 if ( $file->exists() ) // Delete the file physically
00081                 {
00082                     $file->delete();
00083                     eZImageFile::removeFilepath( $contentObjectAttributeID, $filePath );
00084                     eZDir::cleanupEmptyDirectories( $dirpath );
00085                 }
00086 
00087                 // Delete the obsolete reference in the database
00088                 $image->remove();
00089             }
00090 
00091             $progressBar->advance();
00092         }
00093 
00094         $progressBar->finish();
00095         $output->outputLine();
00096     }
00097     else
00098     {
00099         $output->outputText( 'No image file found !' );
00100         $output->outputLine();
00101     }
00102 
00103     $script->shutdown();
00104 }
00105 catch(Exception $e)
00106 {
00107     $output->outputText( $e->getMessage(), 'error' );
00108     $output->outputLine();
00109     $script->shutdown( $e->getCode() );
00110 }
00111 ?>