Newer
Older
<?php
/**
* This File contains QUI\Cron\Manager
*/
namespace QUI\Cron;
/**
* Cron Manager
*
* @author www.pcsg.de (Henning Leutz)
*/
class Manager
{
* @param String $cron - Name of the Cron
* @param String $min - On which minute should it start
* @param String $hour - On which hour should it start
* @param String $day - On which day should it start
* @param String $month - On which month should it start
* @param Array $params - Cron Parameter
public function add($cron, $min, $hour, $day, $month, $params=array())
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.add' );
if ( !$this->_cronExists( $cron ) ) {
throw new \QUI\Exception( 'Cannot add Cron. Cron not exists', 404 );
}
$cronData = $this->getCronData( $cron );
if ( !is_array( $params ) ) {
$params = array();
}
\QUI::getDataBase()->insert($this->Table(), array(
'exec' => $cronData['exec'],
'title' => $cronData['title'],
'min' => $min,
'hour' => $hour,
'day' => $day,
'month' => $month,
'params' => json_encode( $params )
\QUI::getMessagesHandler()->addSuccess(
* @param Integer $cronId
* @param String $min
* @param String $hour
* @param String $day
* @param String $month
* @param Array $params
public function edit($cronId, $cron, $min, $hour, $day, $month, $params=array())
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.edit' );
if ( !$this->_cronExists( $cron ) ) {
throw new \QUI\Exception( 'Cannot edit Cron. Cron command not exists', 404 );
}
$cronData = $this->getCronData( $cron );
\QUI::getDataBase()->update($this->Table(), array(
'exec' => $cronData['exec'],
'title' => $cronData['title'],
'min' => $min,
'hour' => $hour,
'day' => $day,
'month' => $month,
'params' => json_encode( $params )
), array(
'id' => $cronId
));
\QUI::getMessagesHandler()->addSuccess(
'Cron erfolgreich editiert'
);
}
/**
* activate a cron in the cron list
* @param Integer $cronId - ID of the cron
*/
public function activateCron($cronId)
{
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.deactivate' );
\QUI::getDataBase()->update(
$this->Table(),
array('active' => 1),
array('id' => (int)$cronId)
);
}
/**
* deactivate a cron in the cron list
* @param Integer $cronId - ID of the cron
*/
public function deactivateCron($cronId)
{
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.activate' );
\QUI::getDataBase()->update(
$this->Table(),
array('active' => 0),
array('id' => (int)$cronId)
);
}
/**
* Delete the crons
* @param Array $ids - Array of the Cron-Ids
*/
public function deleteCronIds($ids)
{
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.delete' );
$DataBase = \QUI::getDataBase();
foreach ( $ids as $id )
{
$id = (int)$id;
if ( $this->getCronById( $id ) === false ) {
return;
}
$DataBase->delete($this->Table(), array(
'id' => $id
));
}
}
/**
* Execute all upcoming crons
*/
public function execute()
{
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.execute' );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
$list = $this->getList();
$time = time();
foreach ( $list as $entry )
{
if ( $entry['active'] != 1 ) {
continue;
}
$lastexec = $entry['lastexec'];
$min = $entry['min'];
$hour = $entry['hour'];
$day = $entry['day'];
$month = $entry['month'];
$year = '*';
$Cron = \Cron\CronExpression::factory(
"$min $hour $day $month $year"
);
$next = $Cron->getNextRunDate( $lastexec )->getTimestamp();
// no execute
if ( $next > $time ) {
continue;
}
// execute cron
$this->executeCron( $entry['id'] );
}
}
/**
* Execute a cron
*
* @param Integer $cronId - ID of the cron
* @return \QUI\Cron\Manager
*/
public function executeCron($cronId)
{
\QUI\Rights\Permission::checkPermission( 'quiqqer.cron.execute' );
$cronData = $this->getCronById( $cronId );
if ( !$cronData ) {
throw new \QUI\Exception( 'Cron ID not exist' );
}
if ( isset( $cronData['params'] ) )
{
$cronDataParams = json_decode( $cronData['params'], true );
if ( is_array( $cronDataParams ) )
{
foreach ( $cronDataParams as $entry ) {
$params[ $entry['name'] ] = $entry['value'];
}
}
if ( !is_array( $params ) ) {
$params = array();
}
}
call_user_func_array( $cronData['exec'], array($params, $this) );
\QUI::getMessagesHandler()->addSuccess(
\QUI::getLocale()->get(
'quiqqer/cron',
'message.cron.succesful.executed'
)
);
\QUI::getDataBase()->insert(self::TableHistory(), array(
'cronid' => $cronId,
'lastexec' => date( 'Y-m-d H:i:s' ),
'uid' => \QUI::getUserBySession()->getId()
));
\QUI::getDataBase()->update(
self::Table(),
array('lastexec' => date( 'Y-m-d H:i:s' )),
array('id' => $cronId)
);
return $this;
}
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Return the Crons which are available and from other Plugins provided
*
* @return Array
*/
public function getAvailableCrons()
{
$PackageManager = \QUI::getPackageManager();
$packageList = $PackageManager->getInstalled();
$result = array();
foreach ( $packageList as $entry )
{
$dir = OPT_DIR . $entry['name'] .'/';
$cronFile = $dir . 'cron.xml';
if ( !file_exists( $cronFile ) ) {
continue;
}
$result = array_merge(
$result,
$this->getCronsFromFile( $cronFile )
);
}
return $result;
}
/**
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
* Return the data of a inserted cron
*
* @param Integer $cronId - ID of the Cron
* @return Array|false - Cron Data
*/
public function getCronById($cronId)
{
$result = \QUI::getDataBase()->fetch(array(
'from' => $this->Table(),
'where' => array(
'id' => (int)$cronId
),
'limit' => 1
));
if ( !isset( $result[ 0 ] ) ) {
return false;
}
return $result[ 0 ];
}
/**
* Return the data of a specific cron from the available cron list
* This cron is not in the cron list
*
* @param String $cron - Name of the Cron
* @return Array|false - Cron Data
*/
public function getCronData($cron)
{
$availableCrons = $this->getAvailableCrons();
// check if cron is available
foreach ( $availableCrons as $entry )
{
return $entry;
}
}
return false;
}
/**
* Return the history list
*/
public function getHistoryList()
{
return \QUI::getDataBase()->fetch(array(
'from' => self::TableHistory()
));
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Return the cron list
*
* @return Array
*/
public function getList()
{
return \QUI::getDataBase()->fetch(array(
'from' => self::Table()
));
}
/**
* Exist the cron?
*
* @return Bool
*/
protected function _cronExists($cron)
{
return $this->getCronData( $cron ) === false ? false : true;
}
/**
* static
*/
/**
* Return the cron tabe
*
* @return String
*/
static function Table()
{
return QUI_DB_PRFX .'cron';
}
/**
* Return the cron tabe
*
* @return String
*/
static function TableHistory()
{
return QUI_DB_PRFX .'cron_history';
}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Return the Crons from a XML File
*
* @param String $file
* @return Array
*/
static function getCronsFromFile($file)
{
if ( !file_exists( $file ) ) {
return array();
}
$Dom = \QUI\Utils\XML::getDomFromXml( $file );
$crons = $Dom->getElementsByTagName( 'crons' );
if ( !$crons || !$crons->length ) {
return array();
}
$Crons = $crons->item( 0 );
$list = $Crons->getElementsByTagName( 'cron' );
if ( !$list || !$list->length ) {
return array();
}
$result = array();
for ( $i = 0; $i < $list->length; $i++ )
{
$Cron = $list->item( $i );
$title = '';
$desc = '';
$Title = $Cron->getElementsByTagName( 'title' );
$Desc = $Cron->getElementsByTagName( 'description' );
if ( $Title->length ) {
$title = trim( $Title->item( 0 )->nodeValue );
}
if ( $Desc->length ) {
$desc = trim( $Desc->item( 0 )->nodeValue );
}
$result[] = array(
'title' => $title,
'description' => $desc,
'exec' => $Cron->getAttribute( 'exec' )
);
}
return $result;
}
/**
* Print a message to the log cron.log
*
* @param String $message - Message
*/
static function log($message)
{
$User = \QUI::getUsers()->getUserBySession();
$dir = VAR_DIR . 'log/';
$file = $dir . 'cron_'. date('Y-m-d') .'.log';
$str = '['. date('Y-m-d H:i:s') .' :: '. $User->getName() .'] '. $message;
QUI\System\Log::write( $str, 'cron' );
}