1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / Linter.class.php
blob700fbe1bec8614d222e24e9613d6ac28be8ae6a7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Analyzes a query and gives user feedback.
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * The linter itself.
15 * @package PhpMyAdmin
17 class PMA_Linter
20 /**
21 * Gets the starting position of each line.
23 * @param string $str String to be analyzed.
25 * @return array
27 public static function getLines($str)
29 if ((!($str instanceof SqlParser\UtfString))
30 && (defined('USE_UTF_STRINGS')) && (USE_UTF_STRINGS)
31 ) {
32 // If the lexer uses UtfString for processing then the position will
33 // represent the position of the character and not the position of
34 // the byte.
35 $str = new SqlParser\UtfString($str);
38 // The reason for using the '8bit' parameter is that the length
39 // required is the length in bytes, not characters.
41 // Given the following string: `????+`, where `?` represents a
42 // multi-byte character (lets assume that every `?` is a 2-byte
43 // character) and `+` is a newline, the first value of `$i` is `0`
44 // and the last one is `4` (because there are 5 characters). Bytes
45 // `$str[0]` and `$str[1]` are the first character, `$str[2]` and
46 // `$str[3]` are the second one and `$str[4]` is going to be the
47 // first byte of the third character. The fourth and the last one
48 // (which is actually a new line) aren't going to be processed at
49 // all.
50 $len = ($str instanceof SqlParser\UtfString) ?
51 $str->length() : mb_strlen($len, '8bit');
53 $lines = array(0);
54 for ($i = 0; $i < $len; ++$i) {
55 if ($str[$i] === "\n") {
56 $lines[] = $i + 1;
59 return $lines;
62 /**
63 * Computes the number of the line and column given an absolute position.
65 * @param array $lines The starting position of each line.
66 * @param int $pos The absolute position
68 * @return array
70 public static function findLineNumberAndColumn($lines, $pos)
72 $line = 0;
73 foreach ($lines as $lineNo => $lineStart) {
74 if ($lineStart > $pos) {
75 break;
77 $line = $lineNo;
79 return array($line, $pos - $lines[$line]);
82 /**
83 * Runs the linting process.
85 * @param string $query The query to be checked.
87 * @return array
89 public static function lint($query)
91 // Disabling lint for huge queries to save some resources.
92 if (/*overload*/mb_strlen($query) > 10000) {
93 return array(
94 array(
95 'message' => __(
96 'Linting is disabled for this query because it exceeds the '
97 . 'maximum length.'
99 'fromLine' => 0,
100 'fromColumn' => 0,
101 'toLine' => 0,
102 'toColumn' => 0,
103 'severity' => 'warning',
109 * Lexer used for tokenizing the query.
111 * @var SqlParser\Lexer
113 $lexer = new SqlParser\Lexer($query);
116 * Parsed used for analysing the query.
118 * @var SqlParser\Parser
120 $parser = new SqlParser\Parser($lexer->list);
123 * Array containing all errors.
125 * @var array
127 $errors = SqlParser\Utils\Error::get(array($lexer, $parser));
130 * The response containing of all errors.
132 * @var array
134 $response = array();
137 * The starting position for each line.
139 * CodeMirror requires relative position to line, but the parser stores
140 * only the absolute position of the character in string.
142 * @var array
144 $lines = static::getLines($query);
146 // Building the response.
147 foreach ($errors as $idx => $error) {
149 // Starting position of the string that caused the error.
150 list($fromLine, $fromColumn) = static::findLineNumberAndColumn(
151 $lines, $error[3]
154 // Ending position of the string that caused the error.
155 list($toLine, $toColumn) = static::findLineNumberAndColumn(
156 $lines, $error[3] + /*overload*/mb_strlen($error[2])
159 // Building the response.
160 $response[] = array(
161 'message' => sprintf(
162 __('%1$s (near <code>%2$s</code>)'),
163 $error[0], $error[2]
165 'fromLine' => $fromLine,
166 'fromColumn' => $fromColumn,
167 'toLine' => $toLine,
168 'toColumn' => $toColumn,
169 'severity' => 'error',
173 // Sending back the answer.
174 return $response;