2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Analyzes a query and gives user feedback.
8 namespace PMA\libraries
;
10 use PhpMyAdmin\SqlParser\Lexer
;
11 use PhpMyAdmin\SqlParser\Parser
;
12 use PhpMyAdmin\SqlParser\UtfString
;
13 use PhpMyAdmin\SqlParser\Utils\Error
as ParserError
;
24 * Gets the starting position of each line.
26 * @param string $str String to be analyzed.
30 public static function getLines($str)
32 if ((!($str instanceof UtfString
))
33 && (defined('USE_UTF_STRINGS')) && (USE_UTF_STRINGS
)
35 // If the lexer uses UtfString for processing then the position will
36 // represent the position of the character and not the position of
38 $str = new UtfString($str);
41 // The reason for using the strlen is that the length
42 // required is the length in bytes, not characters.
44 // Given the following string: `????+`, where `?` represents a
45 // multi-byte character (lets assume that every `?` is a 2-byte
46 // character) and `+` is a newline, the first value of `$i` is `0`
47 // and the last one is `4` (because there are 5 characters). Bytes
48 // `$str[0]` and `$str[1]` are the first character, `$str[2]` and
49 // `$str[3]` are the second one and `$str[4]` is going to be the
50 // first byte of the third character. The fourth and the last one
51 // (which is actually a new line) aren't going to be processed at
53 $len = ($str instanceof UtfString
) ?
54 $str->length() : strlen($str);
57 for ($i = 0; $i < $len; ++
$i) {
58 if ($str[$i] === "\n") {
66 * Computes the number of the line and column given an absolute position.
68 * @param array $lines The starting position of each line.
69 * @param int $pos The absolute position
73 public static function findLineNumberAndColumn($lines, $pos)
76 foreach ($lines as $lineNo => $lineStart) {
77 if ($lineStart > $pos) {
82 return array($line, $pos - $lines[$line]);
86 * Runs the linting process.
88 * @param string $query The query to be checked.
92 public static function lint($query)
94 // Disabling lint for huge queries to save some resources.
95 if (mb_strlen($query) > 10000) {
99 'Linting is disabled for this query because it exceeds the '
106 'severity' => 'warning',
112 * Lexer used for tokenizing the query.
116 $lexer = new Lexer($query);
119 * Parsed used for analysing the query.
123 $parser = new Parser($lexer->list);
126 * Array containing all errors.
130 $errors = ParserError
::get(array($lexer, $parser));
133 * The response containing of all errors.
140 * The starting position for each line.
142 * CodeMirror requires relative position to line, but the parser stores
143 * only the absolute position of the character in string.
147 $lines = static::getLines($query);
149 // Building the response.
150 foreach ($errors as $idx => $error) {
152 // Starting position of the string that caused the error.
153 list($fromLine, $fromColumn) = static::findLineNumberAndColumn(
157 // Ending position of the string that caused the error.
158 list($toLine, $toColumn) = static::findLineNumberAndColumn(
159 $lines, $error[3] +
mb_strlen($error[2])
162 // Building the response.
164 'message' => sprintf(
165 __('%1$s (near <code>%2$s</code>)'),
166 htmlspecialchars($error[0]), htmlspecialchars($error[2])
168 'fromLine' => $fromLine,
169 'fromColumn' => $fromColumn,
171 'toColumn' => $toColumn,
172 'severity' => 'error',
176 // Sending back the answer.