Util.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace Elastica;
  3. /**
  4. * Elastica tools.
  5. *
  6. * @author Nicolas Ruflin <spam@ruflin.com>
  7. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  8. * @author Oleg Zinchenko <olegz@default-value.com>
  9. * @author Roberto Nygaard <roberto@nygaard.es>
  10. */
  11. class Util
  12. {
  13. /** @var array */
  14. protected static $dateMathSymbols = ['<', '>', '/', '{', '}', '|', '+', ':', ','];
  15. /** @var array */
  16. protected static $escapedDateMathSymbols = ['%3C', '%3E', '%2F', '%7B', '%7D', '%7C', '%2B', '%3A', '%2C'];
  17. /**
  18. * Checks if date math is already escaped within request URI.
  19. *
  20. * @param string $requestUri
  21. *
  22. * @return bool
  23. */
  24. public static function isDateMathEscaped($requestUri)
  25. {
  26. // In practice, the only symbol that really needs to be escaped in URI is '/' => '%2F'
  27. return false !== strpos(strtoupper($requestUri), '%2F');
  28. }
  29. /**
  30. * Escapes date math symbols within request URI.
  31. *
  32. * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.x/date-math-index-names.html
  33. *
  34. * @param string $requestUri
  35. *
  36. * @return string
  37. */
  38. public static function escapeDateMath($requestUri)
  39. {
  40. if (empty($requestUri)) {
  41. return $requestUri;
  42. }
  43. // Check if date math if used at all. Find last '>'. E.g. /<log-{now/d}>,log-2011.12.01/log/_refresh
  44. $pos1 = strrpos($requestUri, '>');
  45. if (false === $pos1) {
  46. return $requestUri;
  47. }
  48. // Find the position up to which we should escape.
  49. // Should be next slash '/' after last '>' E.g. /<log-{now/d}>,log-2011.12.01/log/_refresh
  50. $pos2 = strpos($requestUri, '/', $pos1);
  51. $pos2 = false !== $pos2 ? $pos2 : strlen($requestUri);
  52. // Cut out the bit we need to escape: /<log-{now/d}>,log-2011.12.01
  53. $uriSegment = substr($requestUri, 0, $pos2);
  54. // Escape using character map
  55. $escapedUriSegment = str_replace(static::$dateMathSymbols, static::$escapedDateMathSymbols, $uriSegment);
  56. // '\\{' and '\\}' should not be escaped
  57. if (false !== strpos($uriSegment, '\\\\')) {
  58. $escapedUriSegment = str_replace(['\\\\%7B', '\\\\%7D'], ['\\\\{', '\\\\}'], $escapedUriSegment);
  59. }
  60. // Replace part of the string. E.g. /%3Clog-%7Bnow%2Fd%7D%3E%2Clog-2011.12.01/log/_refresh
  61. return substr_replace($requestUri, $escapedUriSegment, 0, $pos2);
  62. }
  63. /**
  64. * Replace known reserved words (e.g. AND OR NOT)
  65. * and
  66. * escape known special characters (e.g. + - && || ! ( ) { } [ ] ^ " ~ * ? : etc.).
  67. *
  68. * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_boolean_operators
  69. * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters
  70. *
  71. * @param string $term Query term to replace and escape
  72. *
  73. * @return string Replaced and escaped query term
  74. */
  75. public static function replaceBooleanWordsAndEscapeTerm($term)
  76. {
  77. $result = $term;
  78. $result = self::replaceBooleanWords($result);
  79. $result = self::escapeTerm($result);
  80. return $result;
  81. }
  82. /**
  83. * Escapes the following terms (because part of the query language)
  84. * + - && || ! ( ) { } [ ] ^ " ~ * ? : \ < >.
  85. *
  86. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
  87. *
  88. * @param string $term Query term to escape
  89. *
  90. * @return string Escaped query term
  91. */
  92. public static function escapeTerm($term)
  93. {
  94. $result = $term;
  95. // \ escaping has to be first, otherwise escaped later once again
  96. $escapableChars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/'];
  97. foreach ($escapableChars as $char) {
  98. $result = str_replace($char, '\\'.$char, $result);
  99. }
  100. // < and > cannot be escaped, so they should be removed
  101. // @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
  102. $nonEscapableChars = ['<', '>'];
  103. foreach ($nonEscapableChars as $char) {
  104. $result = str_replace($char, '', $result);
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Replace the following reserved words (because part of the query language)
  110. * AND OR NOT.
  111. *
  112. * @see http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Boolean%20operators
  113. *
  114. * @param string $term Query term to replace
  115. *
  116. * @return string Replaced query term
  117. */
  118. public static function replaceBooleanWords($term)
  119. {
  120. $replacementMap = [' AND ' => ' && ', ' OR ' => ' || ', ' NOT ' => ' !'];
  121. $result = strtr($term, $replacementMap);
  122. return $result;
  123. }
  124. /**
  125. * Converts a snake_case string to CamelCase.
  126. *
  127. * For example: hello_world to HelloWorld
  128. *
  129. * @param string $string snake_case string
  130. *
  131. * @return string CamelCase string
  132. */
  133. public static function toCamelCase($string)
  134. {
  135. return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
  136. }
  137. /**
  138. * Converts a CamelCase string to snake_case.
  139. *
  140. * For Example HelloWorld to hello_world
  141. *
  142. * @param string $string CamelCase String to Convert
  143. *
  144. * @return string SnakeCase string
  145. */
  146. public static function toSnakeCase($string)
  147. {
  148. $string = preg_replace('/([A-Z])/', '_$1', $string);
  149. return strtolower(substr($string, 1));
  150. }
  151. /**
  152. * Converts given time to format: 1995-12-31T23:59:59Z.
  153. *
  154. * This is the lucene date format
  155. *
  156. * @param int|string $date Date input (could be string etc.) -> must be supported by strtotime
  157. *
  158. * @return string Converted date string
  159. */
  160. public static function convertDate($date)
  161. {
  162. if (is_int($date)) {
  163. $timestamp = $date;
  164. } else {
  165. $timestamp = strtotime($date);
  166. }
  167. $string = date('Y-m-d\TH:i:s\Z', $timestamp);
  168. return $string;
  169. }
  170. /**
  171. * Convert a \DateTime object to format: 1995-12-31T23:59:59Z+02:00.
  172. *
  173. * Converts it to the lucene format, including the appropriate TimeZone
  174. *
  175. * @param \DateTime $dateTime
  176. * @param bool $includeTimezone
  177. *
  178. * @return string
  179. */
  180. public static function convertDateTimeObject(\DateTime $dateTime, $includeTimezone = true)
  181. {
  182. $formatString = 'Y-m-d\TH:i:s'.(true === $includeTimezone ? 'P' : '\Z');
  183. $string = $dateTime->format($formatString);
  184. return $string;
  185. }
  186. /**
  187. * Tries to guess the name of the param, based on its class
  188. * Example: \Elastica\Query\MatchAll => match_all.
  189. *
  190. * @param string|object Object or class name
  191. *
  192. * @return string parameter name
  193. */
  194. public static function getParamName($class)
  195. {
  196. if (is_object($class)) {
  197. $class = get_class($class);
  198. }
  199. $parts = explode('\\', $class);
  200. $last = array_pop($parts);
  201. $last = preg_replace('/Query$/', '', $last); // for BoolQuery
  202. return self::toSnakeCase($last);
  203. }
  204. /**
  205. * Converts Request to Curl console command.
  206. *
  207. * @param Request $request
  208. *
  209. * @return string
  210. */
  211. public static function convertRequestToCurlCommand(Request $request)
  212. {
  213. $message = 'curl -X'.strtoupper($request->getMethod()).' ';
  214. $message .= '\'http://'.$request->getConnection()->getHost().':'.$request->getConnection()->getPort().'/';
  215. $message .= $request->getPath();
  216. $query = $request->getQuery();
  217. if (!empty($query)) {
  218. $message .= '?'.http_build_query($query);
  219. }
  220. $message .= '\'';
  221. $data = $request->getData();
  222. if (!empty($data)) {
  223. $message .= ' -d \''.JSON::stringify($data).'\'';
  224. }
  225. return $message;
  226. }
  227. }