Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
remove_old_cron_jobs.php
1 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 if (empty($SYSTEM_ROOT)) {
30  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
31  exit();
32 }
33 
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
35  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
36  exit();
37 }
38 
39 require_once $SYSTEM_ROOT.'/core/include/init.inc';
40 
41 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
42 
43 // log in as root
44 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
45  echo "ERROR: Failed login in as root user\n";
46  exit();
47 }
48 
49 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
50 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
51 
52 $old_date = date('Y-m-d', strtotime('-1 week'));
53 $sql = "SELECT assetid
54  FROM sq_ast_attr_val
55  WHERE
56  attrid IN
57  (select attrid from sq_ast_attr where (type_code = 'cron_job' or owning_type_code = 'cron_job') and name='when')
58  AND CAST(custom_val AS varchar2(255)) < :old_date";
59 $query = MatrixDAL::preparePdoQuery($sql);
60 MatrixDAL::bindValueToPdo($query, 'old_date', 'OO='.$old_date);
61 $assetids = MatrixDAL::executePdoAssoc($query, 0);
62 if (empty($assetids)) {
63  echo "No old cron jobs found\n";
64 } else {
65  echo 'Found '.count($assetids).' old crons'."\n";
66  $assetids_list = '('.implode(', ', $assetids).')';
67  $res = MatrixDAL::executeSql('DELETE FROM sq_ast WHERE assetid IN '.$assetids_list);
68  $res = MatrixDAL::executeSql('DELETE FROM sq_ast_lnk WHERE minorid IN '.$assetids_list);
69  $res = MatrixDAL::executeSql('DELETE FROM sq_ast_lnk_tree WHERE linkid NOT IN (SELECT linkid FROM sq_ast_lnk)');
70  $res = MatrixDAL::executeSql('DELETE FROM sq_ast_attr_val WHERE assetid IN '.$assetids_list);
71 }
72 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
73 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
74 echo "Done\n";