Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_check.php
1 <?php
29 error_reporting(E_ALL);
30 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
31 if (php_sapi_name() != 'cli') {
32  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
33  exit(1);
34 }//end if
35 
36 $help = "";
37 $help .= "Syntax: system_check.php [ARGS]\n";
38 $help .= "Where [ARGS] can be:\n";
39 $help .= "\t--system=[MATRIX_ROOT]\tThe path to the matrix system\n";
40 $help .= "\t--verbose\t\tShow more detailed errors\n";
41 $help .= "\t--colours\t\tUse colours\n";
42 $help .= "\t--stats\t\t\tShow statistics for this process\n";
43 $help .= "\t--help\t\t\tShow this help screen\n";
44 $help .= "\t--execute\t\tMake a script execute it's action if it has one\n";
45 $help .= "\t[test_to_run]\n";
46 
47 // Defaults
48 $SYSTEM_ROOT = '';
49 $VERBOSE = FALSE;
50 $COLOURS = FALSE;
51 $STATS = FALSE;
52 $tests_to_run = Array();
53 $EXECUTE = FALSE;
54 
55 // Process arguments
56 foreach ($_SERVER['argv'] as $placement => $argument) {
57  if (!empty($placement)) {
58  $argument = ltrim($argument, '-');
59  if (strpos($argument, '=') != FALSE) {
60  list($command, $parameter) = explode('=', $argument);
61  } else {
62  $command = $argument;
63  $parameter = '';
64  }//end if
65  switch ($command) {
66  case 'system':
67  $SYSTEM_ROOT = $parameter;
68  break;
69  case 'verbose':
70  $VERBOSE = TRUE;
71  break;
72  case 'colours':
73  $COLOURS = TRUE;
74  break;
75  case 'stats':
76  $STATS = TRUE;
77  break;
78  case 'execute':
79  $EXECUTE = TRUE;
80  break;
81  case 'help':
82  echo $help;
83  exit();
84  break;
85  default:
86  $tests_to_run[] = $command;
87  }//end switch
88  }//end if
89 }//end foreach
90 
91 if (empty($SYSTEM_ROOT)) {
92  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
93  echo $help;
94  exit();
95 }
96 
97 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
98  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
99  echo $help;
100  exit();
101 }
102 
103 define('SQ_SYSTEM_ROOT', $SYSTEM_ROOT);
104 require_once SQ_SYSTEM_ROOT.'/core/include/init.inc';
105 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
106 
107 // Starting stats reporting
108 mem_check(NULL, TRUE);
109 speed_check('', FALSE, FALSE);
110 
111 // Deep system checking requires full access
112 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
113 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
114 $GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user);
115 
116 // Check the system here
117 $test_dir = dirname(__FILE__).'/system_tests';
118 $tests = list_files($test_dir.'/*.inc');
119 foreach ($tests as $test) {
120  // Only use files starting with test and ending with .inc
121  if (!preg_match('/^test(.*)\.inc$/i', $test)) continue;
122 
123  $test_basename = basename($test, '.inc');
124  if (!empty($tests_to_run)) {
125  if (!in_array($test_basename, $tests_to_run)) {
126  continue;
127  }
128  }
129 
130  include_once $test_dir.'/'.$test;
131  $class_name = str_replace('_', ' ', $test_basename);
132  $class_name = ucwords($class_name);
133  $class_name = str_replace(' ', '_', $class_name);
134  $messages = Array();
135  $errors = Array();
136  $runTestMethod = TRUE;
137  if ($EXECUTE) {
138  if (method_exists($class_name, 'execute')) {
139  $status = call_user_func_array(Array($class_name, 'execute'), Array(&$messages, &$errors));
140  $runTestMethod = FALSE;
141  } else {
142  echo "Test " . $test . " doesn't have an 'execute' option.\n";
143  }
144  }
145 
146  if ($runTestMethod) {
147  $status = call_user_func_array(Array($class_name, 'test'), Array(&$messages, &$errors));
148  }
149 
150  $name = call_user_func_array(Array($class_name, 'getName'), Array());
151 
152  showStatus($name, $status, $messages, $errors, $VERBOSE, $COLOURS);
153 }//end foreach
154 
155 // All done, cleaning up
156 $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
157 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
158 
159 // Show stats
160 if ($STATS) {
161  echo "Memory Usage: ".mem_check(NULL, TRUE)."\n";
162  echo "Time taken: ";
163  speed_check('', FALSE, FALSE);
164  echo "seconds\n";
165 }//end if
166 exit();
167 
181 function showStatus($name, $status, $messages=Array(), $errors=Array(), $verbose=FALSE, $colours=TRUE)
182 {
183  // Local variables
184  if ($colours) {
185  $statusOk = "\033[50G[ \033[1;32mOK\033[0m ]";
186  $statusErr = "\033[50G[ \033[1;31m!!\033[0m ]";
187  $statusWarn = "\033[50G[ \033[1;33m??\033[0m ]";
188  $statusInfo = "\033[50G[ \033[1;34minfo\033[0m ]";
189  } else {
190  $statusOk = "\033[50G[ OK ]";
191  $statusErr = "\033[50G[ !! ]";
192  $statusWarn = "\033[50G[ ?? ]";
193  $statusInfo = "\033[50G[ info ]";
194  }//end if
195 
196  // Current step
197  echo $name;
198  if (is_bool($status)) {
199  echo ($status) ? $statusOk : $statusErr;
200  } else {
201  switch ($status) {
202  case '1':
203  echo $statusOk;
204  break;
205  case '2':
206  echo $statusInfo;
207  break;
208  case '3':
209  echo $statusWarn;
210  break;
211  default:
212  echo $statusErr;
213  }//end switch
214  }//end if
215  echo "\n";
216 
217  // Messages
218  if (($status !== TRUE || $status !== '1') && !empty($messages)) {
219  // Show messages
220  foreach ($messages as $message) {
221  echo "\t".$message."\n";
222  }//end foreach
223  }//end if
224 
225  // Errors
226  if (($status !== TRUE || $status !== '1') && $verbose && !empty($errors)) {
227  // Show errors
228  foreach ($errors as $error) {
229  echo "\t".$error."\n";
230  }//end foreach
231  }//end if
232 
233 }//end showStatus()
234 
235 
236 ?>