2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * A simple rules engine, that parses and executes the rules in advisory_rules.txt.
5 * Adjusted to phpMyAdmin.
9 if (! defined('PHPMYADMIN')) {
25 * Parses and executes advisor rules
27 * @return array with run and parse results
31 // HowTo: A simple Advisory system in 3 easy steps.
33 // Step 1: Get some variables to evaluate on
34 $this->variables
= array_merge(
35 $GLOBALS['dbi']->fetchResult('SHOW GLOBAL STATUS', 0, 1),
36 $GLOBALS['dbi']->fetchResult('SHOW GLOBAL VARIABLES', 0, 1)
39 $this->variables
= array_merge(
41 $GLOBALS['dbi']->fetchResult(
42 "SELECT concat('Com_', variable_name), variable_value "
43 . "FROM data_dictionary.GLOBAL_STATEMENTS", 0, 1
47 // Add total memory to variables as well
48 include_once 'libraries/sysinfo.lib.php';
49 $sysinfo = PMA_getSysInfo();
50 $memory = $sysinfo->memory();
51 $this->variables
['system_memory'] = $memory['MemTotal'];
53 // Step 2: Read and parse the list of rules
54 $this->parseResult
= $this->parseRulesFile();
55 // Step 3: Feed the variables to the rules and let them fire. Sets
60 'parse' => array('errors' => $this->parseResult
['errors']),
61 'run' => $this->runResult
66 * Stores current error in run results.
68 * @param string $description description of an error.
69 * @param object $exception exception raised
73 function storeError($description, $exception)
75 $this->runResult
['errors'][] = $description
78 __('PHP threw following error: %s'),
79 $exception->getMessage()
84 * Executes advisor rules
90 $this->runResult
= array(
92 'notfired' => array(),
93 'unchecked'=> array(),
97 foreach ($this->parseResult
['rules'] as $rule) {
98 $this->variables
['value'] = 0;
101 if (isset($rule['precondition'])) {
103 $precond = $this->ruleExprEvaluate($rule['precondition']);
104 } catch (Exception
$e) {
107 __('Failed evaluating precondition for rule \'%s\'.'),
117 $this->addRule('unchecked', $rule);
120 $value = $this->ruleExprEvaluate($rule['formula']);
121 } catch(Exception
$e) {
124 __('Failed calculating value for rule \'%s\'.'),
132 $this->variables
['value'] = $value;
135 if ($this->ruleExprEvaluate($rule['test'])) {
136 $this->addRule('fired', $rule);
138 $this->addRule('notfired', $rule);
140 } catch(Exception
$e) {
143 __('Failed running test for rule \'%s\'.'),
156 * Escapes percent string to be used in format string.
158 * @param string $str string to escape
162 static function escapePercent($str)
164 return preg_replace('/%( |,|\.|$|\(|\)|<|>)/', '%%\1', $str);
168 * Wrapper function for translating.
170 * @param string $str the string
171 * @param mixed $param the parameters
175 function translate($str, $param = null)
177 if (is_null($param)) {
178 return sprintf(_gettext(Advisor
::escapePercent($str)));
180 $printf = 'sprintf("' . _gettext(Advisor
::escapePercent($str)) . '",';
181 return $this->ruleExprEvaluate(
182 $printf . $param . ')',
189 * Splits justification to text and formula.
191 * @param string $rule the rule
195 static function splitJustification($rule)
197 $jst = preg_split('/\s*\|\s*/', $rule['justification'], 2);
198 if (count($jst) > 1) {
199 return array($jst[0], $jst[1]);
201 return array($rule['justification']);
205 * Adds a rule to the result list
207 * @param string $type type of rule
208 * @param array $rule rule itslef
212 function addRule($type, $rule)
217 $jst = Advisor
::splitJustification($rule);
218 if (count($jst) > 1) {
221 $str = $this->translate($jst[0], $jst[1]);
222 } catch (Exception
$e) {
225 __('Failed formatting string for rule \'%s\'.'),
233 $rule['justification'] = $str;
235 $rule['justification'] = $this->translate($rule['justification']);
237 $rule['id'] = $rule['name'];
238 $rule['name'] = $this->translate($rule['name']);
239 $rule['issue'] = $this->translate($rule['issue']);
241 // Replaces {server_variable} with 'server_variable'
242 // linking to server_variables.php
243 $rule['recommendation'] = preg_replace(
244 '/\{([a-z_0-9]+)\}/Ui',
245 '<a href="server_variables.php?' . PMA_URL_getCommon()
246 . '&filter=\1">\1</a>',
247 $this->translate($rule['recommendation'])
250 // Replaces external Links with PMA_linkURL() generated links
251 $rule['recommendation'] = preg_replace_callback(
252 '#href=("|\')(https?://[^\1]+)\1#i',
253 array($this, '_replaceLinkURL'),
254 $rule['recommendation']
259 $this->runResult
[$type][] = $rule;
263 * Callback for wrapping links with PMA_linkURL
265 * @param array $matches List of matched elements form preg_replace_callback
267 * @return Replacement value
269 private function _replaceLinkURL($matches)
271 return 'href="' . PMA_linkURL($matches[2]) . '"';
275 * Callback for evaluating fired() condition.
277 * @param array $matches List of matched elements form preg_replace_callback
279 * @return Replacement value
281 private function _ruleExprEvaluateFired($matches)
283 // No list of fired rules
284 if (!isset($this->runResult
['fired'])) {
288 // Did matching rule fire?
289 foreach ($this->runResult
['fired'] as $rule) {
290 if ($rule['id'] == $matches[2]) {
299 * Callback for evaluating variables in expression.
301 * @param array $matches List of matched elements form preg_replace_callback
303 * @return Replacement value
305 private function _ruleExprEvaluateVariable($matches)
307 if (! isset($this->variables
[$matches[1]])) {
310 if (is_numeric($this->variables
[$matches[1]])) {
311 return $this->variables
[$matches[1]];
313 return '\'' . addslashes($this->variables
[$matches[1]]) . '\'';
318 * Runs a code expression, replacing variable names with their respective
321 * @param string $expr expression to evaluate
322 * @param int $ignoreUntil if > 0, it doesn't replace any variables until
323 * that string position, but still evaluates the
326 * @return result of evaluated expression
328 function ruleExprEvaluate($expr, $ignoreUntil = 0)
330 if ($ignoreUntil > 0) {
331 $exprIgnore = substr($expr, 0, $ignoreUntil);
332 $expr = substr($expr, $ignoreUntil);
334 // Evaluate fired() conditions
335 $expr = preg_replace_callback(
336 '/fired\s*\(\s*(\'|")(.*)\1\s*\)/Ui',
337 array($this, '_ruleExprEvaluateFired'),
340 // Evaluate variables
341 $expr = preg_replace_callback(
343 array($this, '_ruleExprEvaluateVariable'),
346 if ($ignoreUntil > 0) {
347 $expr = $exprIgnore . $expr;
352 // Actually evaluate the code
354 eval('$value = ' . $expr . ';');
355 $err = ob_get_contents();
362 . '<br />Executed code: $value = ' . htmlspecialchars($expr) . ';'
369 * Reads the rule file into an array, throwing errors messages on syntax
372 * @return array with parsed data
374 static function parseRulesFile()
376 $file = file('libraries/advisory_rules.txt', FILE_IGNORE_NEW_LINES
);
381 'name', 'formula', 'test', 'issue', 'recommendation', 'justification'
383 $numRules = count($ruleSyntax);
384 $numLines = count($file);
388 for ($i = 0; $i < $numLines; $i++
) {
390 if ($line == "" ||
$line[0] == '#') {
395 if (substr($line, 0, 4) == 'rule') {
398 __('Invalid rule declaration on line %1$s, expected line %2$s of previous rule.'),
400 $ruleSyntax[$ruleLine++
]
404 if (preg_match("/rule\s'(.*)'( \[(.*)\])?$/", $line, $match)) {
407 $rules[$ruleNo] = array('name' => $match[1]);
408 $lines[$ruleNo] = array('name' => $i +
1);
409 if (isset($match[3])) {
410 $rules[$ruleNo]['precondition'] = $match[3];
411 $lines[$ruleNo]['precondition'] = $i +
1;
415 __('Invalid rule declaration on line %s.'),
421 if ($ruleLine == -1) {
423 __('Unexpected characters on line %s.'),
429 // Reading rule lines
431 if (!isset($line[0])) {
432 continue; // Empty lines are ok
434 // Non tabbed lines are not
435 if ($line[0] != "\t") {
437 __('Unexpected character on line %1$s. Expected tab, but found "%2$s".'),
443 $rules[$ruleNo][$ruleSyntax[$ruleLine]] = chop(substr($line, 1));
444 $lines[$ruleNo][$ruleSyntax[$ruleLine]] = $i +
1;
449 if ($ruleLine == $numRules) {
454 return array('rules' => $rules, 'lines' => $lines, 'errors' => $errors);
459 * Formats interval like 10 per hour
461 * @param integer $num number to format
462 * @param intefer $precision required precision
464 * @return formatted string
466 function ADVISOR_bytime($num, $precision)
469 if ($num >= 1) { // per second
470 $per = __('per second');
471 } elseif ($num * 60 >= 1) { // per minute
473 $per = __('per minute');
474 } elseif ($num * 60 * 60 >= 1 ) { // per hour
475 $num = $num * 60 * 60;
476 $per = __('per hour');
478 $num = $num * 60 * 60 * 24;
479 $per = __('per day');
482 $num = round($num, $precision);
485 $num = '<' . PMA_Util
::pow(10, -$precision);
492 * Wrapper for PMA_Util::timespanFormat
494 * @param int $seconds the timespan
496 * @return string the formatted value
498 function ADVISOR_timespanFormat($seconds)
500 return PMA_Util
::timespanFormat($seconds);
504 * Wrapper around PMA_Util::formatByteDown
506 * @param double $value the value to format
507 * @param int $limes the sensitiveness
508 * @param int $comma the number of decimals to retain
510 * @return array the formatted value and its unit
512 function ADVISOR_formatByteDown($value, $limes = 6, $comma = 0)
514 return PMA_Util
::formatByteDown($value, $limes, $comma);