Linter.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Analyzes a query and gives user feedback.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use PhpMyAdmin\SqlParser\Lexer;
  8. use PhpMyAdmin\SqlParser\Parser;
  9. use PhpMyAdmin\SqlParser\UtfString;
  10. use PhpMyAdmin\SqlParser\Utils\Error as ParserError;
  11. use function __;
  12. use function defined;
  13. use function htmlspecialchars;
  14. use function mb_strlen;
  15. use function sprintf;
  16. use function strlen;
  17. /**
  18. * The linter itself.
  19. */
  20. class Linter
  21. {
  22. /**
  23. * Gets the starting position of each line.
  24. *
  25. * @param string|UtfString $str String to be analyzed.
  26. *
  27. * @return array<int,int>
  28. * @psalm-return list<int>
  29. */
  30. public static function getLines($str)
  31. {
  32. if ((! ($str instanceof UtfString)) && defined('USE_UTF_STRINGS') && USE_UTF_STRINGS) {
  33. // If the lexer uses UtfString for processing then the position will
  34. // represent the position of the character and not the position of
  35. // the byte.
  36. $str = new UtfString($str);
  37. }
  38. // The reason for using the strlen is that the length
  39. // required is the length in bytes, not characters.
  40. //
  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 UtfString ?
  51. $str->length() : strlen($str);
  52. $lines = [0];
  53. for ($i = 0; $i < $len; ++$i) {
  54. if ($str[$i] !== "\n") {
  55. continue;
  56. }
  57. $lines[] = $i + 1;
  58. }
  59. return $lines;
  60. }
  61. /**
  62. * Computes the number of the line and column given an absolute position.
  63. *
  64. * @param array $lines The starting position of each line.
  65. * @param int $pos The absolute position
  66. * @psalm-param list<int> $lines
  67. *
  68. * @return array
  69. * @psalm-return array{int, int}
  70. */
  71. public static function findLineNumberAndColumn(array $lines, $pos)
  72. {
  73. $line = 0;
  74. foreach ($lines as $lineNo => $lineStart) {
  75. if ($lineStart > $pos) {
  76. break;
  77. }
  78. $line = $lineNo;
  79. }
  80. return [
  81. $line,
  82. $pos - $lines[$line],
  83. ];
  84. }
  85. /**
  86. * Runs the linting process.
  87. *
  88. * @param string $query The query to be checked.
  89. *
  90. * @return array
  91. * @psalm-return list<array{
  92. * message: string,
  93. * fromLine: int,
  94. * fromColumn: int,
  95. * toLine: int,
  96. * toColumn: int,
  97. * severity: string,
  98. * }>
  99. */
  100. public static function lint($query)
  101. {
  102. // Disabling lint for huge queries to save some resources.
  103. if (mb_strlen($query) > 10000) {
  104. return [
  105. [
  106. 'message' => __('Linting is disabled for this query because it exceeds the maximum length.'),
  107. 'fromLine' => 0,
  108. 'fromColumn' => 0,
  109. 'toLine' => 0,
  110. 'toColumn' => 0,
  111. 'severity' => 'warning',
  112. ],
  113. ];
  114. }
  115. /**
  116. * Lexer used for tokenizing the query.
  117. */
  118. $lexer = new Lexer($query);
  119. /**
  120. * Parsed used for analysing the query.
  121. */
  122. $parser = new Parser($lexer->list);
  123. /**
  124. * Array containing all errors.
  125. */
  126. $errors = ParserError::get([$lexer, $parser]);
  127. /**
  128. * The response containing of all errors.
  129. */
  130. $response = [];
  131. /**
  132. * The starting position for each line.
  133. *
  134. * CodeMirror requires relative position to line, but the parser stores
  135. * only the absolute position of the character in string.
  136. */
  137. $lines = static::getLines($query);
  138. // Building the response.
  139. foreach ($errors as $error) {
  140. // Starting position of the string that caused the error.
  141. [$fromLine, $fromColumn] = static::findLineNumberAndColumn($lines, $error[3]);
  142. // Ending position of the string that caused the error.
  143. [$toLine, $toColumn] = static::findLineNumberAndColumn(
  144. $lines,
  145. $error[3] + mb_strlen((string) $error[2])
  146. );
  147. // Building the response.
  148. $response[] = [
  149. 'message' => sprintf(
  150. __('%1$s (near <code>%2$s</code>)'),
  151. htmlspecialchars((string) $error[0]),
  152. htmlspecialchars((string) $error[2])
  153. ),
  154. 'fromLine' => $fromLine,
  155. 'fromColumn' => $fromColumn,
  156. 'toLine' => $toLine,
  157. 'toColumn' => $toColumn,
  158. 'severity' => 'error',
  159. ];
  160. }
  161. // Sending back the answer.
  162. return $response;
  163. }
  164. }