Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezscripttrashpurge.php
Go to the documentation of this file.
1 <?php
12 class eZScriptTrashPurge
13 {
20 protected $cli;
21
27 protected $quiet;
28
34 protected $memoryMonitoring;
35
41 protected $script;
42
48 protected $logFile;
49
59 public function __construct( eZCLI $cli, $quiet = true, $memoryMonitoring = false, eZScript $script = null, $logFile = "trashpurge.log" )
60 {
61 $this->cli = $cli;
62 $this->quiet = $quiet;
63 $this->memoryMonitoring = $memoryMonitoring;
64 $this->script = $script;
65 $this->logFile = $logFile;
66 }
67
77 public function run( $iterationLimit = 100, $sleep = 1, $trashedDays = null )
78 {
79 if ( $iterationLimit === null )
80 {
81 $iterationLimit = 100;
82 }
83
84 if ( $sleep === null )
85 {
86 $sleep = 1;
87 }
88
89 $trashed = $trashedDays ? strtotime( "-{$trashedDays} days" ) : null;
90
91 if ( $this->memoryMonitoring )
92 {
93 eZLog::rotateLog( $this->logFile );
94 $this->cli->output( "Logging memory usage to {$this->logFile}" );
95 }
96
97 $this->cli->output( "Purging trash items:" );
98
99 $this->monitor( "start" );
100
101 $db = eZDB::instance();
102
103 // Get user's ID who can remove subtrees. (Admin by default with userID = 14)
104 $userCreatorID = eZINI::instance()->variable( "UserSettings", "UserCreatorID" );
105 $user = eZUser::fetch( $userCreatorID );
106 if ( !$user )
107 {
108 $this->cli->error( "Cannot get user object with userID = '$userCreatorID'.\n(See site.ini[UserSettings].UserCreatorID)" );
109 return false;
110 }
111 eZUser::setCurrentlyLoggedInUser( $user, $userCreatorID );
112
113 $trashCount = eZContentObjectTrashNode::trashListCount( array( 'Trashed' => $trashed ) );
114 if ( !$this->quiet && !$trashed )
115 {
116 $this->cli->output( "Found $trashCount object(s) in trash." );
117 }
118 else if ( !$this->quiet && $trashed )
119 {
120 $this->cli->output( "Found $trashCount object(s) in trash for at least $trashedDays days." );
121 }
122 if ( $trashCount == 0 )
123 {
124 return true;
125 }
126 if ( $this->script !== null )
127 {
128 $this->script->resetIteration( $trashCount );
129 }
130
131 while ( $trashCount > 0 )
132 {
133 $this->monitor( "iteration start" );
134 $trashList = eZContentObjectTrashNode::trashList( array( 'Limit' => $iterationLimit, 'Trashed' => $trashed ), false );
135
136 $db->begin();
137
138 foreach ( $trashList as $trashNode )
139 {
140 $object = $trashNode->attribute( 'object' );
141 $this->monitor( "purge" );
142 $object->purge();
143 if ( $this->script !== null )
144 $this->script->iterate( $this->cli, true );
145 }
146
147 if ( !$db->commit() )
148 {
149 $this->cli->output();
150 $this->cli->error( 'Trash has not been emptied, impossible to commit the whole transaction' );
151 return false;
152 }
153
154 $trashCount = eZContentObjectTrashNode::trashListCount( array( 'Trashed' => $trashed ) );
155 if ( $trashCount > 0 )
156 {
157 eZContentObject::clearCache();
158 if ( $sleep > 0 )
159 {
160 sleep( $sleep );
161 }
162 }
163 $this->monitor( "iteration end" );
164 }
165
166 if ( !$this->quiet )
167 {
168 $this->cli->output( 'Trash successfully emptied' );
169 }
170
171 $this->monitor( "end" );
172
173 return true;
174 }
175
181 private function monitor( $text )
182 {
183 if ( $this->memoryMonitoring )
184 {
185 eZLog::write( "mem [$text]: " . memory_get_usage(), $this->logFile );
186 }
187 }
188 }
189
190 ?>
eZLog\write
static write( $message, $logName='common.log', $dir='var/log')
Definition: ezlog.php:30
eZLog\rotateLog
static rotateLog( $fileName)
Definition: ezlog.php:178
eZScriptTrashPurge
Definition: ezscripttrashpurge.php:13
eZScriptTrashPurge\$memoryMonitoring
$memoryMonitoring
Definition: ezscripttrashpurge.php:34
eZScriptTrashPurge\monitor
monitor( $text)
Definition: ezscripttrashpurge.php:181
eZScriptTrashPurge\__construct
__construct(eZCLI $cli, $quiet=true, $memoryMonitoring=false, eZScript $script=null, $logFile="trashpurge.log")
Definition: ezscripttrashpurge.php:59
eZScriptTrashPurge\$cli
$cli
Definition: ezscripttrashpurge.php:20
eZScriptTrashPurge\run
run( $iterationLimit=100, $sleep=1, $trashedDays=null)
Definition: ezscripttrashpurge.php:77
eZScriptTrashPurge\$script
$script
Definition: ezscripttrashpurge.php:41
eZScriptTrashPurge\$logFile
$logFile
Definition: ezscripttrashpurge.php:48
eZScriptTrashPurge\$quiet
$quiet
Definition: ezscripttrashpurge.php:27
$db
$db
Definition: ezarticle/user/authorlist.php:53
$user
$user
Definition: user/adlist.php:39
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.