eZ Publish  [4.0]
clusterize.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <30-Mar-2006 06:30:00 vs>
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 /*
00031 
00032 NOTE:
00033 
00034  Please read doc/features/3.8/clustering.txt and set up clustering
00035  before runnning this script.
00036 
00037 */
00038 
00039 error_reporting( E_ALL | E_NOTICE );
00040 
00041 // require_once( 'lib/ezdb/classes/ezdb.php' );
00042 // require_once( 'lib/ezutils/classes/ezcli.php' );
00043 // require_once( 'lib/ezutils/classes/ezsys.php' );
00044 // require_once( 'kernel/classes/ezscript.php' );
00045 // require_once( 'kernel/classes/ezclusterfilehandler.php' );
00046 
00047 require 'autoload.php';
00048 
00049 // This code is taken from eZBinaryFile::storedFileInfo()
00050 function filePathForBinaryFile($fileName, $mimeType )
00051 {
00052     $storageDir = eZSys::storageDirectory();
00053     list( $group, $type ) = explode( '/', $mimeType );
00054     $filePath = $storageDir . '/original/' . $group . '/' . $fileName;
00055     return $filePath;
00056 }
00057 
00058 function copyBinaryfilesToDB( $remove )
00059 {
00060     global $cli, $dbFileHandler;
00061 
00062     $db = eZDB::instance();
00063 
00064     $cli->output( "Importing binary files to database:");
00065     $rows = $db->arrayQuery('select filename, mime_type from ezbinaryfile' );
00066 
00067     foreach( $rows as $row )
00068     {
00069         $filePath = filePathForBinaryFile( $row['filename'] , $row['mime_type'] );
00070         $cli->output( "- " . $filePath);
00071         $dbFileHandler->fileStore( $filePath, 'binaryfile', $remove );
00072     }
00073 
00074     $cli->output();
00075 }
00076 
00077 function copyMediafilesToDB( $remove )
00078 {
00079     global $cli, $dbFileHandler;
00080 
00081     $db = eZDB::instance();
00082 
00083     $cli->output( "Importing media files to database:");
00084     $rows = $db->arrayQuery('select filename, mime_type from ezmedia' );
00085     foreach( $rows as $row )
00086     {
00087         $filePath = filePathForBinaryFile( $row['filename'] , $row['mime_type'] );
00088         $cli->output( "- " . $filePath);
00089         $dbFileHandler->fileStore( $filePath, 'mediafile', $remove );
00090     }
00091 
00092     $cli->output();
00093 }
00094 
00095 function copyImagesToDB( $remove )
00096 {
00097     global $cli, $dbFileHandler;
00098 
00099     $db = eZDB::instance();
00100 
00101     $cli->output( "Importing images and imagealiases files to database:");
00102     $rows = $db->arrayQuery('select filepath from ezimagefile' );
00103     //include_once( 'lib/ezutils/classes/ezmimetype.php' );
00104 
00105     foreach( $rows as $row )
00106     {
00107         $filePath = $row['filepath'];
00108         $cli->output( "- " . $filePath);
00109 
00110         $mimeData = eZMimeType::findByFileContents( $filePath );
00111         $dbFileHandler->fileStore( $filePath, 'image', $remove, $mimeData['name'] );
00112     }
00113 }
00114 
00115 function copyFilesFromDB( $copyFiles, $copyImages, $remove )
00116 {
00117     global $cli, $dbFileHandler;
00118 
00119     $cli->output( "Exporting files from database:");
00120     $filePathList = $dbFileHandler->getFileList( !$copyFiles, !$copyImages );
00121 
00122     foreach ( $filePathList as $filePath )
00123     {
00124         $cli->output( "- " . $filePath );
00125         eZDir::mkdir( dirname( $filePath ), false, true );
00126         $dbFileHandler->fileFetch( $filePath );
00127 
00128         if ( $remove )
00129             $dbFileHandler->fileDelete( $filePath );
00130     }
00131 
00132     $cli->output();
00133 }
00134 
00135 $cli = eZCLI::instance();
00136 $script = eZScript::instance( array( 'description' => ( "eZ Publish (un)clusterize\n" .
00137                                                         "Script for moving var_dir files from " .
00138                                                         "filesystem to database and vice versa\n" .
00139                                                         "\n" .
00140                                                         "./bin/php/clusterize.php" ),
00141                                      'use-session'    => false,
00142                                      'use-modules'    => false,
00143                                      'use-extensions' => true ) );
00144 
00145 $script->startup();
00146 
00147 $options = $script->getOptions( "[u][skip-binary-files][skip-media-files][skip-images][r][n]",
00148                                 "",
00149                                 array( 'u'                 => 'Unclusterize',
00150                                        'skip-binary-files' => 'Skip copying binary files',
00151                                        'skip-media-files'  => 'Skip copying media files',
00152                                        'skip-images'       => 'Skip copying images',
00153                                        'r'                 => 'Remove files after copying',
00154                                        'n'                 => 'Do not wait' ) );
00155 
00156 $script->initialize();
00157 
00158 $clusterize = !isset( $options['u'] );
00159 $remove     =  isset( $options['r'] );
00160 $copyFiles  = !isset( $options['skip-binary-files'] );
00161 $copyMedia  = !isset( $options['skip-media-files'] );
00162 $copyImages = !isset( $options['skip-images'] );
00163 $wait       = !isset( $options['n'] );
00164 
00165 if ( $wait )
00166 {
00167     $warningMsg = sprintf( "This script will now %s your files and/or images %s database.",
00168                            ( $remove ? "move" : "copy" ),
00169                            ( $clusterize ? 'to' : 'from' ) );
00170     $cli->warning( $warningMsg );
00171     $cli->warning( "You have 10 seconds to break the script (press Ctrl-C)." );
00172     sleep( 10 );
00173 }
00174 
00175 $dbFileHandler = eZClusterFileHandler::instance();
00176 if ( !is_object( $dbFileHandler ) || !( $dbFileHandler instanceof eZDBFileHandler ) )
00177 {
00178     $cli->error( "Clustering settings specified incorrectly or the chosen file handler is ezfs." );
00179     $script->shutdown( 1 );
00180 }
00181 
00182 if ( $clusterize )
00183 {
00184     if ( $copyFiles )
00185         copyBinaryfilesToDB( $remove );
00186 
00187     if ( $copyImages )
00188         copyImagesToDB( $remove );
00189     if ( $copyMedia )
00190         copyMediafilesToDB( $remove );
00191 }
00192 else
00193 {
00194     copyFilesFromDB( $copyFiles, $copyImages, $remove );
00195 }
00196 
00197 $script->shutdown();
00198 ?>