eZ Publish  [4.0]
cleanup.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <18-Dec-2003 17:44:15 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 require 'autoload.php';
00031 
00032 set_time_limit( 0 );
00033 
00034 //include_once( 'lib/ezutils/classes/ezcli.php' );
00035 //include_once( 'kernel/classes/ezscript.php' );
00036 
00037 $cli = eZCLI::instance();
00038 $endl = $cli->endlineString();
00039 
00040 $script = eZScript::instance( array( 'description' => ( "eZ Publish database cleanup.\n\n" .
00041                                                         "Will cleanup various data from the currently used database in eZ Publish\n" .
00042                                                         "\n" .
00043                                                         "Possible values for NAME is:\n" .
00044                                                         "session, expired_session, preferences, browse, tipafriend, shop, forgotpassword, workflow,\n" .
00045                                                         "collaboration, collectedinformation, notification, searchstats or all (for all items)\n" .
00046                                                         "cleanup.php -s admin session"),
00047                                      'use-session' => false,
00048                                      'use-modules' => true,
00049                                      'use-extensions' => true ) );
00050 
00051 $script->startup();
00052 
00053 $options = $script->getOptions( "[db-host:][db-user:][db-password:][db-database:][db-type:|db-driver:][sql]",
00054                                 "[name]",
00055                                 array( 'db-host' => "Database host",
00056                                        'db-user' => "Database user",
00057                                        'db-password' => "Database password",
00058                                        'db-database' => "Database name",
00059                                        'db-driver' => "Database driver",
00060                                        'db-type' => "Database driver, alias for --db-driver",
00061                                        'sql' => "Display sql queries"
00062                                        ) );
00063 $script->initialize();
00064 
00065 if ( count( $options['arguments'] ) < 1 )
00066 {
00067     $cli->error( "Missing NAME value ( could be session, expired_session, preferences, browse, tipafriend, shop, forgotpassword, workflow,\n" .
00068                  "collaboration, collectedinformation, notification, searchstats or all )" );
00069     $script->shutdown( 1 );
00070 }
00071 
00072 $dbUser = $options['db-user'] ? $options['db-user'] : false;
00073 $dbPassword = $options['db-password'] ? $options['db-password'] : false;
00074 $dbHost = $options['db-host'] ? $options['db-host'] : false;
00075 $dbName = $options['db-database'] ? $options['db-database'] : false;
00076 $dbImpl = $options['db-driver'] ? $options['db-driver'] : false;
00077 $showSQL = $options['sql'] ? true : false;
00078 $siteAccess = $options['siteaccess'] ? $options['siteaccess'] : false;
00079 
00080 if ( $siteAccess )
00081 {
00082     changeSiteAccessSetting( $siteaccess, $siteAccess );
00083 }
00084 
00085 $cleanAllItems = false;
00086 $clean = array( 'session' => false,
00087                 'expired_session' => false,
00088                 'preferences' => false,
00089                 'browse' => false,
00090                 'tipafriend' => false,
00091                 'shop' => false,
00092                 'forgotpassword' => false,
00093                 'workflow' => false,
00094                 'collaboration' => false,
00095                 'collectedinformation' => false,
00096                 'notification' => false,
00097                 'searchstats' => false );
00098 
00099 foreach ( $options['arguments'] as $arg )
00100 {
00101 
00102     $item = strtolower( $arg );
00103     if ( $item == 'all' )
00104         $cleanAllItems = true;
00105     else
00106         $cleanItems[] = $item;
00107 }
00108 
00109 if ( $cleanAllItems )
00110 {
00111     $names = array_keys( $clean );
00112     foreach ( $names as $name )
00113     {
00114         $clean[$name] = true;
00115     }
00116 }
00117 else
00118 {
00119     if ( count( $cleanItems ) == 0 )
00120     {
00121         help();
00122         $script->shutdown( 0 );
00123     }
00124     foreach ( $cleanItems as $name )
00125     {
00126         $clean[$name] = true;
00127     }
00128 }
00129 
00130 function changeSiteAccessSetting( &$siteaccess, $optionData )
00131 {
00132     global $isQuiet;
00133     $cli = eZCLI::instance();
00134     if ( file_exists( 'settings/siteaccess/' . $optionData ) )
00135     {
00136         $siteaccess = $optionData;
00137         if ( !$isQuiet )
00138             $cli->notice( "Using siteaccess $siteaccess for database cleanup" );
00139     }
00140     else
00141     {
00142         if ( !$isQuiet )
00143             $cli->notice( "Siteaccess $optionData does not exist, using default siteaccess" );
00144     }
00145 }
00146 
00147 $db = eZDB::instance();
00148 if ( $dbHost or $dbName or $dbUser or $dbImpl )
00149 {
00150     $params = array();
00151     if ( $dbHost !== false )
00152         $params['server'] = $dbHost;
00153     if ( $dbUser !== false )
00154     {
00155         $params['user'] = $dbUser;
00156         $params['password'] = '';
00157     }
00158     if ( $dbPassword !== false )
00159         $params['password'] = $dbPassword;
00160     if ( $dbName !== false )
00161         $params['database'] = $dbName;
00162     $db = eZDB::instance( $dbImpl, $params, true );
00163     eZDB::setInstance( $db );
00164 }
00165 
00166 $db->setIsSQLOutputEnabled( $showSQL );
00167 
00168 //include_once( 'kernel/classes/ezpersistentobject.php' );
00169 
00170 require_once( 'lib/ezutils/classes/ezsession.php' );
00171 if ( $clean['session'] )
00172 {
00173     $cli->output( "Removing all sessions" );
00174     eZSessionEmpty();
00175 }
00176 
00177 if ( $clean['expired_session'] )
00178 {
00179     $cli->output( "Removing expired sessions,", false );
00180     eZSessionGarbageCollector();
00181     $activeCount = eZSessionCountActive();
00182     $cli->output( " " . $cli->stylize( 'emphasize', $activeCount ) . " left" );
00183 }
00184 
00185 if ( $clean['preferences'] )
00186 {
00187     //include_once( 'kernel/classes/ezpreferences.php' );
00188     $cli->output( "Removing all preferences" );
00189     eZPreferences::cleanup();
00190 }
00191 
00192 if ( $clean['browse'] )
00193 {
00194     //include_once( 'kernel/classes/ezcontentbrowserecent.php' );
00195     //include_once( 'kernel/classes/ezcontentbrowsebookmark.php' );
00196     $cli->output( "Removing all recent items and bookmarks for browse page" );
00197     eZContentBrowseRecent::cleanup();
00198     eZContentBrowseBookmark::cleanup();
00199 }
00200 
00201 if ( $clean['tipafriend'] )
00202 {
00203     //include_once( 'kernel/classes/eztipafriendcounter.php' );
00204     $cli->output( "Removing all counters for tip-a-friend" );
00205     eZTipafriendCounter::cleanup();
00206 }
00207 
00208 if ( $clean['shop'] )
00209 {
00210     //include_once( 'kernel/classes/ezbasket.php' );
00211     $cli->output( "Removing all baskets" );
00212     eZBasket::cleanup();
00213     //include_once( 'kernel/classes/ezwishlist.php' );
00214     $cli->output( "Removing all wishlists" );
00215     eZWishList::cleanup();
00216     //include_once( 'kernel/classes/ezorder.php' );
00217     $cli->output( "Removing all orders" );
00218     eZOrder::cleanup();
00219     $productCount = eZPersistentObject::count( eZProductCollection::definition() );
00220     if ( $productCount > 0 )
00221     {
00222         $cli->warning( "$productCount product collections still exists, must be a leak" );
00223     }
00224 }
00225 
00226 if ( $clean['forgotpassword'] )
00227 {
00228     //include_once( 'kernel/classes/datatypes/ezuser/ezforgotpassword.php' );
00229     $cli->output( "Removing all forgot password requests" );
00230     eZForgotPassword::cleanup();
00231 }
00232 
00233 if ( $clean['workflow'] )
00234 {
00235     //include_once( 'lib/ezutils/classes/ezoperationmemento.php' );
00236     //include_once( 'kernel/classes/ezworkflowprocess.php' );
00237     $cli->output( "Removing all workflow processes and operation mementos" );
00238     eZOperationMemento::cleanup();
00239     eZWorkflowProcess::cleanup();
00240 }
00241 
00242 if ( $clean['collaboration'] )
00243 {
00244     //include_once( 'kernel/classes/ezcollaborationitem.php' );
00245     $cli->output( "Removing all collaboration elements" );
00246     eZCollaborationItem::cleanup();
00247 }
00248 
00249 if ( $clean['collectedinformation'] )
00250 {
00251     //include_once( 'kernel/classes/ezinformationcollection.php' );
00252     $cli->output( "Removing all collected information" );
00253     eZInformationCollection::cleanup();
00254 }
00255 
00256 if ( $clean['notification'] )
00257 {
00258     //include_once( 'kernel/classes/notification/eznotificationevent.php' );
00259     //include_once( 'kernel/classes/notification/eznotificationcollection.php' );
00260     //include_once( 'kernel/classes/notification/eznotificationeventfilter.php' );
00261     $cli->output( "Removing all notifications events" );
00262     eZNotificationEvent::cleanup();
00263     eZNotificationCollection::cleanup();
00264     eZNotificationEventFilter::cleanup();
00265 }
00266 
00267 if ( $clean['searchstats'] )
00268 {
00269     //include_once( 'kernel/classes/ezsearchlog.php' );
00270     $cli->output( "Removing all search statistics" );
00271     eZSearchLog::removeStatistics();
00272 }
00273 
00274 
00275 $script->shutdown();
00276 
00277 ?>