eZ Publish  [4.0]
updatecontentobjectname.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <28-Nov-2002 12:45:40 bf>
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 require 'autoload.php';
00031 
00032 set_time_limit( 0 );
00033 
00034 //include_once( "lib/ezutils/classes/ezextension.php" );
00035 //include_once( "lib/ezutils/classes/ezmodule.php" );
00036 //include_once( 'lib/ezutils/classes/ezcli.php' );
00037 //include_once( 'kernel/classes/ezscript.php' );
00038 
00039 $cli = eZCLI::instance();
00040 $script = eZScript::instance( array( 'debug-message' => '',
00041                                       'use-session' => true,
00042                                       'use-modules' => true,
00043                                       'use-extensions' => true ) );
00044 
00045 $script->startup();
00046 
00047 $endl = $cli->endlineString();
00048 $webOutput = $cli->isWebOutput();
00049 
00050 function help()
00051 {
00052     $argv = $_SERVER['argv'];
00053     $cli = eZCLI::instance();
00054     $cli->output( "Usage: " . $argv[0] . " [OPTION]...\n" .
00055                   "eZ Publish content object name update.\n" .
00056                   "Goes trough all objects and updates all content object names\n" .
00057                   "\n" .
00058                   "General options:\n" .
00059                   "  -h,--help          display this help and exit \n" .
00060                   "  -q,--quiet         do not give any output except when errors occur\n" .
00061                   "  -s,--siteaccess    selected siteaccess for operations, if not specified default siteaccess is used\n" .
00062                   "  -d,--debug         display debug output at end of execution\n" .
00063                   "  --db-host=HOST     Use database host HOST\n" .
00064                   "  --db-user=USER     Use database user USER\n" .
00065                   "  --db-password=PWD  Use database password PWD\n" .
00066                   "  --db-database=DB   Use database named DB\n" .
00067                   "  --db-driver=DRIVER Use database driver DRIVER\n" .
00068                   "  -c,--colors        display output using ANSI colors\n" .
00069                   "  --sql              display sql queries\n" .
00070                   "  --logfiles         create log files\n" .
00071                   "  --no-logfiles      do not create log files (default)\n" .
00072                   "  --no-colors        do not use ANSI coloring (default)\n" );
00073 }
00074 
00075 function changeSiteAccessSetting( &$siteaccess, $optionData )
00076 {
00077     global $isQuiet;
00078     $cli = eZCLI::instance();
00079     if ( file_exists( 'settings/siteaccess/' . $optionData ) )
00080     {
00081         $siteaccess = $optionData;
00082         if ( !$isQuiet )
00083             $cli->notice( "Using siteaccess $siteaccess for content object name update" );
00084     }
00085     else
00086     {
00087         if ( !$isQuiet )
00088             $cli->notice( "Siteaccess $optionData does not exist, using default siteaccess" );
00089     }
00090 }
00091 
00092 $siteaccess = false;
00093 $debugOutput = false;
00094 $allowedDebugLevels = false;
00095 $useDebugAccumulators = false;
00096 $useDebugTimingpoints = false;
00097 $useIncludeFiles = false;
00098 $useColors = false;
00099 $isQuiet = false;
00100 $useLogFiles = false;
00101 $showSQL = false;
00102 
00103 $dbUser = false;
00104 $dbPassword = false;
00105 $dbHost = false;
00106 $dbName = false;
00107 $dbImpl = false;
00108 
00109 $optionsWithData = array( 's' );
00110 $longOptionsWithData = array( 'siteaccess' );
00111 
00112 $readOptions = true;
00113 
00114 for ( $i = 1; $i < count( $argv ); ++$i )
00115 {
00116     $arg = $argv[$i];
00117     if ( $readOptions and
00118          strlen( $arg ) > 0 and
00119          $arg[0] == '-' )
00120     {
00121         if ( strlen( $arg ) > 1 and
00122              $arg[1] == '-' )
00123         {
00124             $flag = substr( $arg, 2 );
00125             if ( in_array( $flag, $longOptionsWithData ) )
00126             {
00127                 $optionData = $argv[$i+1];
00128                 ++$i;
00129             }
00130             if ( $flag == 'help' )
00131             {
00132                 help();
00133                 $script->shutdown( 0 );
00134             }
00135             else if ( $flag == 'siteaccess' )
00136             {
00137                 changeSiteAccessSetting( $siteaccess, $optionData );
00138             }
00139             else if ( preg_match( "/^db-host=(.*)$/", $flag, $matches ) )
00140             {
00141                 $dbHost = $matches[1];
00142             }
00143             else if ( preg_match( "/^db-user=(.*)$/", $flag, $matches ) )
00144             {
00145                 $dbUser = $matches[1];
00146             }
00147             else if ( preg_match( "/^db-password=(.*)$/", $flag, $matches ) )
00148             {
00149                 $dbPassword = $matches[1];
00150             }
00151             else if ( preg_match( "/^db-database=(.*)$/", $flag, $matches ) )
00152             {
00153                 $dbName = $matches[1];
00154             }
00155             else if ( preg_match( "/^db-driver=(.*)$/", $flag, $matches ) )
00156             {
00157                 $dbImpl = $matches[1];
00158             }
00159             else if ( $flag == 'debug' )
00160             {
00161                 $debugOutput = true;
00162             }
00163             else if ( $flag == 'quiet' )
00164             {
00165                 $isQuiet = true;
00166             }
00167             else if ( $flag == 'colors' )
00168             {
00169                 $useColors = true;
00170             }
00171             else if ( $flag == 'no-colors' )
00172             {
00173                 $useColors = false;
00174             }
00175             else if ( $flag == 'no-logfiles' )
00176             {
00177                 $useLogFiles = false;
00178             }
00179             else if ( $flag == 'logfiles' )
00180             {
00181                 $useLogFiles = true;
00182             }
00183             else if ( $flag == 'sql' )
00184             {
00185                 $showSQL = true;
00186             }
00187         }
00188         else
00189         {
00190             $flag = substr( $arg, 1, 1 );
00191             $optionData = false;
00192             if ( in_array( $flag, $optionsWithData ) )
00193             {
00194                 if ( strlen( $arg ) > 2 )
00195                 {
00196                     $optionData = substr( $arg, 2 );
00197                 }
00198                 else
00199                 {
00200                     $optionData = $argv[$i+1];
00201                     ++$i;
00202                 }
00203             }
00204             if ( $flag == 'h' )
00205             {
00206                 help();
00207                 $script->shutdown( 0 );
00208             }
00209             else if ( $flag == 'q' )
00210             {
00211                 $isQuiet = true;
00212             }
00213             else if ( $flag == 'c' )
00214             {
00215                 $useColors = true;
00216             }
00217             else if ( $flag == 'd' )
00218             {
00219                 $debugOutput = true;
00220                 if ( strlen( $arg ) > 2 )
00221                 {
00222                     $levels = explode( ',', substr( $arg, 2 ) );
00223                     $allowedDebugLevels = array();
00224                     foreach ( $levels as $level )
00225                     {
00226                         if ( $level == 'all' )
00227                         {
00228                             $useDebugAccumulators = true;
00229                             $allowedDebugLevels = false;
00230                             $useDebugTimingpoints = true;
00231                             break;
00232                         }
00233                         if ( $level == 'accumulator' )
00234                         {
00235                             $useDebugAccumulators = true;
00236                             continue;
00237                         }
00238                         if ( $level == 'timing' )
00239                         {
00240                             $useDebugTimingpoints = true;
00241                             continue;
00242                         }
00243                         if ( $level == 'include' )
00244                         {
00245                             $useIncludeFiles = true;
00246                         }
00247                         if ( $level == 'error' )
00248                             $level = eZDebug::LEVEL_ERROR;
00249                         else if ( $level == 'warning' )
00250                             $level = eZDebug::LEVEL_WARNING;
00251                         else if ( $level == 'debug' )
00252                             $level = eZDebug::LEVEL_DEBUG;
00253                         else if ( $level == 'notice' )
00254                             $level = eZDebug::LEVEL_NOTICE;
00255                         else if ( $level == 'timing' )
00256                             $level = eZDebug::EZ_LEVEL_TIMING;
00257                         $allowedDebugLevels[] = $level;
00258                     }
00259                 }
00260             }
00261             else if ( $flag == 's' )
00262             {
00263                 changeSiteAccessSetting( $siteaccess, $optionData );
00264             }
00265         }
00266     }
00267 }
00268 $script->setUseDebugOutput( $debugOutput );
00269 $script->setAllowedDebugLevels( $allowedDebugLevels );
00270 $script->setUseDebugAccumulators( $useDebugAccumulators );
00271 $script->setUseDebugTimingPoints( $useDebugTimingpoints );
00272 $script->setUseIncludeFiles( $useIncludeFiles );
00273 
00274 if ( $webOutput )
00275     $useColors = true;
00276 
00277 $cli->setUseStyles( $useColors );
00278 $script->setDebugMessage( "\n\n" . str_repeat( '#', 36 ) . $cli->style( 'emphasize' ) . " DEBUG " . $cli->style( 'emphasize-end' )  . str_repeat( '#', 36 ) . "\n" );
00279 
00280 $script->setUseSiteAccess( $siteaccess );
00281 
00282 $script->initialize();
00283 
00284 print( "Updating content object names\n" );
00285 
00286 //eZDebug::setHandleType( eZDebug::HANDLE_FROM_PHP );
00287 
00288 //include_once( "lib/ezutils/classes/ezmodule.php" );
00289 // eZModule::setGlobalPathList( array( "kernel" ) );
00290 require_once( 'lib/ezutils/classes/ezexecution.php' );
00291 require_once( "lib/ezutils/classes/ezdebug.php" );
00292 
00293 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00294 
00295 $db = eZDB::instance();
00296 
00297 if ( $dbHost or $dbName or $dbUser or $dbImpl )
00298 {
00299     $params = array();
00300     if ( $dbHost !== false )
00301         $params['server'] = $dbHost;
00302     if ( $dbUser !== false )
00303     {
00304         $params['user'] = $dbUser;
00305         $params['password'] = '';
00306     }
00307     if ( $dbPassword !== false )
00308         $params['password'] = $dbPassword;
00309     if ( $dbName !== false )
00310         $params['database'] = $dbName;
00311     $db = eZDB::instance( $dbImpl, $params, true );
00312     eZDB::setInstance( $db );
00313 }
00314 
00315 $db->setIsSQLOutputEnabled( $showSQL );
00316 
00317 // Get top node
00318 $topNodeArray = eZPersistentObject::fetchObjectList( eZContentObjectTreeNode::definition(),
00319                                                      null,
00320                                                      array( 'parent_node_id' => 1,
00321                                                             'depth' => 1 ) );
00322 $subTreeCount = 0;
00323 foreach ( array_keys ( $topNodeArray ) as $key  )
00324 {
00325     $subTreeCount += $topNodeArray[$key]->subTreeCount( array( 'Limitation' => false ) );
00326 }
00327 
00328 print( "Number of objects to update: $subTreeCount $endl" );
00329 
00330 $i = 0;
00331 $dotMax = 70;
00332 $dotCount = 0;
00333 $limit = 50;
00334 
00335 foreach ( array_keys ( $topNodeArray ) as $key  )
00336 {
00337     $node =& $topNodeArray[$key];
00338     $offset = 0;
00339     $subTree =& $node->subTree( array( 'Offset' => $offset, 'Limit' => $limit,
00340                                        'Limitation' => array() ) );
00341     while ( $subTree != null )
00342     {
00343         foreach ( $subTree as $innerNode )
00344         {
00345             $object = $innerNode->attribute( 'object' );
00346             $class = $object->contentClass();
00347             $object->setName( $class->contentObjectName( $object ) );
00348             $object->store();
00349             unset( $object );
00350             unset( $class );
00351 
00352             // show progress bar
00353             ++$i;
00354             ++$dotCount;
00355             print( "." );
00356             if ( $dotCount >= $dotMax or $i >= $subTreeCount )
00357             {
00358                 $dotCount = 0;
00359                 $percent = (float)( ($i*100.0) / $subTreeCount );
00360                 print( " " . $percent . "%" . $endl );
00361             }
00362         }
00363         $offset += $limit;
00364         unset( $subTree );
00365         $subTree =& $node->subTree( array( 'Offset' => $offset, 'Limit' => $limit,
00366                                            'Limitation' => array() ) );
00367     }
00368 }
00369 
00370 print( $endl . "done" . $endl );
00371 
00372 $script->shutdown();
00373 
00374 ?>