Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Linter.php
blob34f6003f5a97a84a70e7175f15f4ef54a7111daf
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 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;
15 /**
16 * The linter itself.
18 * @package PhpMyAdmin
20 class Linter
23 /**
24 * Gets the starting position of each line.
26 * @param string $str String to be analyzed.
28 * @return array
30 public static function getLines($str)
32 if ((!($str instanceof UtfString))
33 && (defined('USE_UTF_STRINGS')) && (USE_UTF_STRINGS)
34 ) {
35 // If the lexer uses UtfString for processing then the position will
36 // represent the position of the character and not the position of
37 // the byte.
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
52 // all.
53 $len = ($str instanceof UtfString) ?
54 $str->length() : strlen($str);
56 $lines = array(0);
57 for ($i = 0; $i < $len; ++$i) {
58 if ($str[$i] === "\n") {
59 $lines[] = $i + 1;
62 return $lines;
65 /**
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
71 * @return array
73 public static function findLineNumberAndColumn($lines, $pos)
75 $line = 0;
76 foreach ($lines as $lineNo => $lineStart) {
77 if ($lineStart > $pos) {
78 break;
80 $line = $lineNo;
82 return array($line, $pos - $lines[$line]);
85 /**
86 * Runs the linting process.
88 * @param string $query The query to be checked.
90 * @return array
92 public static function lint($query)
94 // Disabling lint for huge queries to save some resources.
95 if (mb_strlen($query) > 10000) {
96 return array(
97 array(
98 'message' => __(
99 'Linting is disabled for this query because it exceeds the '
100 . 'maximum length.'
102 'fromLine' => 0,
103 'fromColumn' => 0,
104 'toLine' => 0,
105 'toColumn' => 0,
106 'severity' => 'warning',
112 * Lexer used for tokenizing the query.
114 * @var Lexer
116 $lexer = new Lexer($query);
119 * Parsed used for analysing the query.
121 * @var Parser
123 $parser = new Parser($lexer->list);
126 * Array containing all errors.
128 * @var array
130 $errors = ParserError::get(array($lexer, $parser));
133 * The response containing of all errors.
135 * @var array
137 $response = array();
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.
145 * @var array
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(
154 $lines, $error[3]
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.
163 $response[] = array(
164 'message' => sprintf(
165 __('%1$s (near <code>%2$s</code>)'),
166 $error[0], $error[2]
168 'fromLine' => $fromLine,
169 'fromColumn' => $fromColumn,
170 'toLine' => $toLine,
171 'toColumn' => $toColumn,
172 'severity' => 'error',
176 // Sending back the answer.
177 return $response;