eZ Publish  [4.0]
fixclassremoteid.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00005 // SOFTWARE NAME: eZ Publish
00006 // SOFTWARE RELEASE: 4.1.x
00007 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00008 // SOFTWARE LICENSE: GNU General Public License v2.0
00009 // NOTICE: >
00010 //   This program is free software; you can redistribute it and/or
00011 //   modify it under the terms of version 2.0  of the GNU General
00012 //   Public License as published by the Free Software Foundation.
00013 //
00014 //   This program is distributed in the hope that it will be useful,
00015 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //   GNU General Public License for more details.
00018 //
00019 //   You should have received a copy of version 2.0 of the GNU General
00020 //   Public License along with this program; if not, write to the Free
00021 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022 //   MA 02110-1301, USA.
00023 //
00024 //
00025 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00026 //
00027 
00028 if ( !function_exists( 'readline' ) )
00029 {
00030     function readline( $prompt = '' )
00031     {
00032         echo $prompt . ' ';
00033         return trim( fgets( STDIN ) );
00034     }
00035 }
00036 
00037 require 'autoload.php';
00038 
00039 $cli = eZCLI::instance();
00040 
00041 $scriptSettings = array();
00042 $scriptSettings['description'] = 'Fix non-unique usage of content class remote ID\'s';
00043 $scriptSettings['use-session'] = false;
00044 $scriptSettings['use-modules'] = false;
00045 $scriptSettings['use-extensions'] = false;
00046 
00047 $script = eZScript::instance( $scriptSettings );
00048 $script->startup();
00049 
00050 $config = '[mode:]';
00051 $argumentConfig = '';
00052 $optionHelp = array( 'mode' => "the fixing mode to use, either d (detailed) or a (automatic)" );
00053 $arguments = false;
00054 $useStandardOptions = true;
00055 
00056 $options = $script->getOptions( $config, $argumentConfig, $optionHelp, $arguments, $useStandardOptions );
00057 $script->initialize();
00058 
00059 if ( isset( $options['mode'] ) )
00060 {
00061     if ( !in_array( $options['mode'], array( 'a', 'd' ) ) )
00062     {
00063         $script->shutdown( 1, 'Invalid mode. Use either d for detailed or a for automatic.' );
00064     }
00065 
00066     $mode = $options['mode'];
00067 }
00068 else
00069 {
00070     $mode = false;
00071 }
00072 
00073 
00074 $db = eZDB::instance();
00075 
00076 $cli->output( '' );
00077 $cli->output( 'Removing temporary content classes...' );
00078 eZContentClass::removeTemporary();
00079 $cli->output( '' );
00080 
00081 $nonUniqueRemoteIDDataList = $db->arrayQuery( 'SELECT COUNT( id ) AS cnt, remote_id FROM ezcontentclass GROUP BY remote_id HAVING COUNT( id ) > 1' );
00082 
00083 $nonUniqueRemoteIDDataListCount = count( $nonUniqueRemoteIDDataList );
00084 
00085 $cli->output( '' );
00086 $cli->output( "Found $nonUniqueRemoteIDDataListCount non-unique content class remote IDs." );
00087 $cli->output( '' );
00088 
00089 $totalCount = 0;
00090 
00091 foreach ( $nonUniqueRemoteIDDataList as $nonUniqueRemoteIDData )
00092 {
00093     if ( $mode )
00094     {
00095         $cli->output( "Remote ID '$nonUniqueRemoteIDData[remote_id]' is used for $nonUniqueRemoteIDData[cnt] different content classes." );
00096         $action = $mode;
00097     }
00098     else
00099     {
00100         $action = readline( "Remote ID '$nonUniqueRemoteIDData[remote_id]' is used for $nonUniqueRemoteIDData[cnt] different content classes. Do you want to see the details (d) or do you want this inconsistency to be fixed automatically (a) ?" );
00101 
00102         while ( !in_array( $action, array( 'a', 'd' ) ) )
00103         {
00104             $action = readline( 'Invalid option. Type either d for details or a to fix automatically.' );
00105         }
00106     }
00107 
00108     $escapedRemoteID = $db->escapeString( $nonUniqueRemoteIDData['remote_id'] );
00109 
00110     $sql = "SELECT id, identifier, created FROM ezcontentclass WHERE remote_id='$escapedRemoteID' ORDER by created ASC";
00111     $rows = $db->arrayQuery( $sql );
00112 
00113     switch ( $action )
00114     {
00115         case 'd':
00116         {
00117             $cli->output( '' );
00118             $cli->output( 'Select the number of the content class that you want to keep the current remote ID. The other listed content classes will get a new one.' );
00119             $cli->output( '' );
00120 
00121             foreach ( $rows as $i => $row )
00122             {
00123                 $dateTime = new eZDateTime( $row['created'] );
00124                 $formattedDateTime = $dateTime->toString( true );
00125                 $cli->output( "$i) $row[identifier] (class ID: $row[id], created: $formattedDateTime )" );
00126                 $cli->output( '' );
00127             }
00128 
00129             do {
00130                 $skip = readline( 'Number of class that should keep the current remote ID: ' );
00131             } while ( !array_key_exists( $skip, $rows ) );
00132         } break;
00133 
00134         case 'a':
00135         default:
00136         {
00137             $skip = 0;
00138         }
00139     }
00140 
00141     $cli->output( 'Fixing...' );
00142 
00143     foreach ( $rows as $i => $row )
00144     {
00145         if ( $i == $skip )
00146         {
00147             continue;
00148         }
00149 
00150         $newRemoteID = md5( (string)mt_rand() . (string)time() );
00151         $escapedNewRemoteID = $db->escapeString( $newRemoteID );
00152         $db->query( "UPDATE ezcontentclass SET remote_id='$escapedNewRemoteID' WHERE id=$row[id]" );
00153     }
00154 
00155     $totalCount += $nonUniqueRemoteIDData['cnt'] - 1;
00156 
00157     $cli->output( '' );
00158     $cli->output( '' );
00159 }
00160 
00161 $cli->output( "Number of content classes that received a new remote ID : $totalCount" );
00162 
00163 $script->shutdown( 0 );
00164 
00165 ?>