eZ Publish  [4.0]
ezsubtreeremove.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <27-Jul-2006 15:00:00 vd>
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 // Subtree Remove Script
00031 // file  bin/php/ezsubtreeremove.php
00032 
00033 // script initializing
00034 //include_once( 'lib/ezutils/classes/ezcli.php' );
00035 //include_once( 'kernel/classes/ezscript.php' );
00036 //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00037 //include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00038 
00039 require 'autoload.php';
00040 
00041 $cli = eZCLI::instance();
00042 $script = eZScript::instance( array( 'description' => ( "\n" .
00043                                                          "This script will make a remove of a content object subtrees.\n" ),
00044                                       'use-session' => false,
00045                                       'use-modules' => true,
00046                                       'use-extensions' => true ) );
00047 $script->startup();
00048 
00049 $scriptOptions = $script->getOptions( "[nodes-id:][ignore-trash]",
00050                                       "",
00051                                       array( 'nodes-id' => "Subtree nodes ID (separated by comma ',').",
00052                                              'ignore-trash' => "Ignore trash ('move to trash' by default)."
00053                                              ),
00054                                       false );
00055 $script->initialize();
00056 $srcNodesID  = $scriptOptions[ 'nodes-id' ] ? trim( $scriptOptions[ 'nodes-id' ] ) : false;
00057 $moveToTrash = $scriptOptions[ 'ignore-trash' ] ? false : true;
00058 $deleteIDArray = $srcNodesID ? explode( ',', $srcNodesID ) : false;
00059 
00060 if ( !$deleteIDArray )
00061 {
00062     $cli->error( "Subtree remove Error!\nCannot get subtree nodes. Please check nodes-id argument and try again." );
00063     $script->showHelp();
00064     $script->shutdown( 1 );
00065 }
00066 
00067 $ini = eZINI::instance();
00068 // Get user's ID who can remove subtrees. (Admin by default with userID = 14)
00069 $userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
00070 $user = eZUser::fetch( $userCreatorID );
00071 if ( !$user )
00072 {
00073     $cli->error( "Subtree remove Error!\nCannot get user object by userID = '$userCreatorID'.\n(See site.ini[UserSettings].UserCreatorID)" );
00074     $script->shutdown( 1 );
00075 }
00076 eZUser::setCurrentlyLoggedInUser( $user, $userCreatorID );
00077 
00078 $deleteIDArrayResult = array();
00079 foreach ( $deleteIDArray as $nodeID )
00080 {
00081     $node = eZContentObjectTreeNode::fetch( $nodeID );
00082     if ( $node === null )
00083     {
00084         $cli->error( "\nSubtree remove Error!\nCannot find subtree with nodeID: '$nodeID'." );
00085         continue;
00086     }
00087     $deleteIDArrayResult[] = $nodeID;
00088 }
00089 // Get subtree removal information
00090 $info = eZContentObjectTreeNode::subtreeRemovalInformation( $deleteIDArrayResult );
00091 
00092 $deleteResult = $info['delete_list'];
00093 
00094 if ( count( $deleteResult ) == 0 )
00095 {
00096     $cli->output( "\nExit." );
00097     $script->shutdown( 1 );
00098 }
00099 
00100 $totalChildCount = $info['total_child_count'];
00101 $canRemoveAll = $info['can_remove_all'];
00102 $moveToTrashStr = $moveToTrash ? 'true' : 'false';
00103 $reverseRelatedCount = $info['reverse_related_count'];
00104 
00105 $cli->output( "\nTotal child count: $totalChildCount" );
00106 $cli->output( "Move to trash: $moveToTrashStr" );
00107 $cli->output( "Reverse related count: $reverseRelatedCount\n" );
00108 
00109 $cli->output( "Removing subtrees:\n" );
00110 
00111 foreach ( $deleteResult as $deleteItem )
00112 {
00113     $node = $deleteItem['node'];
00114     $nodeName = $deleteItem['node_name'];
00115     if ( $node === null )
00116     {
00117         $cli->error( "\nSubtree remove Error!\nCannot find subtree '$nodeName'." );
00118         continue;
00119     }
00120     $nodeID = $node->attribute( 'node_id' );
00121     $childCount = $deleteItem['child_count'];
00122     $objectNodeCount = $deleteItem['object_node_count'];
00123 
00124     $cli->output( "Node id: $nodeID" );
00125     $cli->output( "Node name: $nodeName" );
00126 
00127     $canRemove = $deleteItem['can_remove'];
00128     if ( !$canRemove )
00129     {
00130         $cli->error( "\nSubtree remove Error!\nInsufficient permissions. You do not have permissions to remove the subtree with nodeID: $nodeID\n" );
00131         continue;
00132     }
00133     $cli->output( "Child count: $childCount" );
00134     $cli->output( "Object node count: $objectNodeCount" );
00135 
00136     // Remove subtrees
00137     eZContentObjectTreeNode::removeSubtrees( array( $nodeID ), $moveToTrash );
00138 
00139     // We should make sure that all subitems have been removed.
00140     $itemInfo = eZContentObjectTreeNode::subtreeRemovalInformation( array( $nodeID ) );
00141     $itemTotalChildCount = $itemInfo['total_child_count'];
00142     $itemDeleteList = $itemInfo['delete_list'];
00143 
00144     if ( count( $itemDeleteList ) != 0 or ( $childCount != 0 and $itemTotalChildCount != 0 ) )
00145         $cli->error( "\nWARNING!\nSome subitems have not been removed.\n" );
00146     else
00147         $cli->output( "Successfuly DONE.\n" );
00148 }
00149 
00150 $cli->output( "Done." );
00151 $script->shutdown();
00152 
00153 ?>