2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Analyzes a query and gives user feedback.
8 if (! defined('PHPMYADMIN')) {
21 * Gets the starting position of each line.
23 * @param string $str String to be analyzed.
27 public static function getLines($str)
29 if ((!($str instanceof SqlParser\UtfString
))
30 && (defined('USE_UTF_STRINGS')) && (USE_UTF_STRINGS
)
32 // If the lexer uses UtfString for processing then the position will
33 // represent the position of the character and not the position of
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
50 $len = ($str instanceof SqlParser\UtfString
) ?
51 $str->length() : mb_strlen($len, '8bit');
54 for ($i = 0; $i < $len; ++
$i) {
55 if ($str[$i] === "\n") {
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
70 public static function findLineNumberAndColumn($lines, $pos)
73 foreach ($lines as $lineNo => $lineStart) {
74 if ($lineStart > $pos) {
79 return array($line, $pos - $lines[$line]);
83 * Runs the linting process.
85 * @param string $query The query to be checked.
89 public static function lint($query)
91 // Disabling lint for huge queries to save some resources.
92 if (/*overload*/mb_strlen($query) > 10000) {
96 'Linting is disabled for this query because it exceeds the '
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.
127 $errors = SqlParser\Utils\Error
::get(array($lexer, $parser));
130 * The response containing of all errors.
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.
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(
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.
161 'message' => sprintf(
162 __('%1$s (near <code>%2$s</code>)'),
165 'fromLine' => $fromLine,
166 'fromColumn' => $fromColumn,
168 'toColumn' => $toColumn,
169 'severity' => 'error',
173 // Sending back the answer.