4
0

Search.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. require_once(LIB_PATH . '/lib_date.php');
  3. /**
  4. * Contains a search from the search form.
  5. *
  6. * It allows to extract meaningful bits of the search and store them in a
  7. * convenient object
  8. */
  9. class FreshRSS_Search {
  10. // This contains the user input string
  11. private $raw_input = '';
  12. // The following properties are extracted from the raw input
  13. private $intitle;
  14. private $min_date;
  15. private $max_date;
  16. private $min_pubdate;
  17. private $max_pubdate;
  18. private $inurl;
  19. private $author;
  20. private $tags;
  21. private $search;
  22. private $not_intitle;
  23. private $not_inurl;
  24. private $not_author;
  25. private $not_tags;
  26. private $not_search;
  27. public function __construct($input) {
  28. if ($input == '') {
  29. return;
  30. }
  31. $this->raw_input = $input;
  32. $input = $this->parseNotIntitleSearch($input);
  33. $input = $this->parseNotAuthorSearch($input);
  34. $input = $this->parseNotInurlSearch($input);
  35. $input = $this->parseNotTagsSeach($input);
  36. $input = $this->parsePubdateSearch($input);
  37. $input = $this->parseDateSearch($input);
  38. $input = $this->parseIntitleSearch($input);
  39. $input = $this->parseAuthorSearch($input);
  40. $input = $this->parseInurlSearch($input);
  41. $input = $this->parseTagsSeach($input);
  42. $input = $this->parseNotSearch($input);
  43. $input = $this->parseSearch($input);
  44. }
  45. public function __toString() {
  46. return $this->getRawInput();
  47. }
  48. public function getRawInput() {
  49. return $this->raw_input;
  50. }
  51. public function getIntitle() {
  52. return $this->intitle;
  53. }
  54. public function getNotIntitle() {
  55. return $this->not_intitle;
  56. }
  57. public function getMinDate() {
  58. return $this->min_date;
  59. }
  60. public function getMaxDate() {
  61. return $this->max_date;
  62. }
  63. public function getMinPubdate() {
  64. return $this->min_pubdate;
  65. }
  66. public function getMaxPubdate() {
  67. return $this->max_pubdate;
  68. }
  69. public function getInurl() {
  70. return $this->inurl;
  71. }
  72. public function getNotInurl() {
  73. return $this->not_inurl;
  74. }
  75. public function getAuthor() {
  76. return $this->author;
  77. }
  78. public function getNotAuthor() {
  79. return $this->not_author;
  80. }
  81. public function getTags() {
  82. return $this->tags;
  83. }
  84. public function getNotTags() {
  85. return $this->not_tags;
  86. }
  87. public function getSearch() {
  88. return $this->search;
  89. }
  90. public function getNotSearch() {
  91. return $this->not_search;
  92. }
  93. private static function removeEmptyValues($anArray) {
  94. return is_array($anArray) ? array_filter($anArray, function($value) { return $value !== ''; }) : array();
  95. }
  96. /**
  97. * Parse the search string to find intitle keyword and the search related
  98. * to it.
  99. * The search is the first word following the keyword.
  100. *
  101. * @param string $input
  102. * @return string
  103. */
  104. private function parseIntitleSearch($input) {
  105. if (preg_match_all('/\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  106. $this->intitle = $matches['search'];
  107. $input = str_replace($matches[0], '', $input);
  108. }
  109. if (preg_match_all('/\bintitle:(?P<search>\w*)/', $input, $matches)) {
  110. $this->intitle = array_merge($this->intitle ? $this->intitle : array(), $matches['search']);
  111. $input = str_replace($matches[0], '', $input);
  112. }
  113. $this->intitle = self::removeEmptyValues($this->intitle);
  114. return $input;
  115. }
  116. private function parseNotIntitleSearch($input) {
  117. if (preg_match_all('/[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  118. $this->not_intitle = $matches['search'];
  119. $input = str_replace($matches[0], '', $input);
  120. }
  121. if (preg_match_all('/[!-]intitle:(?P<search>\w*)/', $input, $matches)) {
  122. $this->not_intitle = array_merge($this->not_intitle ? $this->not_intitle : array(), $matches['search']);
  123. $input = str_replace($matches[0], '', $input);
  124. }
  125. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  126. return $input;
  127. }
  128. /**
  129. * Parse the search string to find author keyword and the search related
  130. * to it.
  131. * The search is the first word following the keyword except when using
  132. * a delimiter. Supported delimiters are single quote (') and double
  133. * quotes (").
  134. *
  135. * @param string $input
  136. * @return string
  137. */
  138. private function parseAuthorSearch($input) {
  139. if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  140. $this->author = $matches['search'];
  141. $input = str_replace($matches[0], '', $input);
  142. }
  143. if (preg_match_all('/\bauthor:(?P<search>\w*)/', $input, $matches)) {
  144. $this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
  145. $input = str_replace($matches[0], '', $input);
  146. }
  147. $this->author = self::removeEmptyValues($this->author);
  148. return $input;
  149. }
  150. private function parseNotAuthorSearch($input) {
  151. if (preg_match_all('/[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  152. $this->not_author = $matches['search'];
  153. $input = str_replace($matches[0], '', $input);
  154. }
  155. if (preg_match_all('/[!-]author:(?P<search>\w*)/', $input, $matches)) {
  156. $this->not_author = array_merge($this->not_author ? $this->not_author : array(), $matches['search']);
  157. $input = str_replace($matches[0], '', $input);
  158. }
  159. $this->not_author = self::removeEmptyValues($this->not_author);
  160. return $input;
  161. }
  162. /**
  163. * Parse the search string to find inurl keyword and the search related
  164. * to it.
  165. * The search is the first word following the keyword.
  166. *
  167. * @param string $input
  168. * @return string
  169. */
  170. private function parseInurlSearch($input) {
  171. if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
  172. $this->inurl = $matches['search'];
  173. $input = str_replace($matches[0], '', $input);
  174. }
  175. $this->inurl = self::removeEmptyValues($this->inurl);
  176. return $input;
  177. }
  178. private function parseNotInurlSearch($input) {
  179. if (preg_match_all('/[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  180. $this->not_inurl = $matches['search'];
  181. $input = str_replace($matches[0], '', $input);
  182. }
  183. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  184. return $input;
  185. }
  186. /**
  187. * Parse the search string to find date keyword and the search related
  188. * to it.
  189. * The search is the first word following the keyword.
  190. *
  191. * @param string $input
  192. * @return string
  193. */
  194. private function parseDateSearch($input) {
  195. if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
  196. $input = str_replace($matches[0], '', $input);
  197. $dates = self::removeEmptyValues($matches['search']);
  198. if (!empty($dates[0])) {
  199. list($this->min_date, $this->max_date) = parseDateInterval($dates[0]);
  200. }
  201. }
  202. return $input;
  203. }
  204. /**
  205. * Parse the search string to find pubdate keyword and the search related
  206. * to it.
  207. * The search is the first word following the keyword.
  208. *
  209. * @param string $input
  210. * @return string
  211. */
  212. private function parsePubdateSearch($input) {
  213. if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  214. $input = str_replace($matches[0], '', $input);
  215. $dates = self::removeEmptyValues($matches['search']);
  216. if (!empty($dates[0])) {
  217. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($dates[0]);
  218. }
  219. }
  220. return $input;
  221. }
  222. /**
  223. * Parse the search string to find tags keyword (# followed by a word)
  224. * and the search related to it.
  225. * The search is the first word following the #.
  226. *
  227. * @param string $input
  228. * @return string
  229. */
  230. private function parseTagsSeach($input) {
  231. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  232. $this->tags = $matches['search'];
  233. $input = str_replace($matches[0], '', $input);
  234. }
  235. $this->tags = self::removeEmptyValues($this->tags);
  236. return $input;
  237. }
  238. private function parseNotTagsSeach($input) {
  239. if (preg_match_all('/[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
  240. $this->not_tags = $matches['search'];
  241. $input = str_replace($matches[0], '', $input);
  242. }
  243. $this->not_tags = self::removeEmptyValues($this->not_tags);
  244. return $input;
  245. }
  246. /**
  247. * Parse the search string to find search values.
  248. * Every word is a distinct search value, except when using a delimiter.
  249. * Supported delimiters are single quote (') and double quotes (").
  250. *
  251. * @param string $input
  252. * @return string
  253. */
  254. private function parseSearch($input) {
  255. $input = self::cleanSearch($input);
  256. if ($input == '') {
  257. return;
  258. }
  259. if (preg_match_all('/(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  260. $this->search = $matches['search'];
  261. $input = str_replace($matches[0], '', $input);
  262. }
  263. $input = self::cleanSearch($input);
  264. if ($input == '') {
  265. return;
  266. }
  267. if (is_array($this->search)) {
  268. $this->search = array_merge($this->search, explode(' ', $input));
  269. } else {
  270. $this->search = explode(' ', $input);
  271. }
  272. }
  273. private function parseNotSearch($input) {
  274. $input = self::cleanSearch($input);
  275. if ($input == '') {
  276. return;
  277. }
  278. if (preg_match_all('/[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  279. $this->not_search = $matches['search'];
  280. $input = str_replace($matches[0], '', $input);
  281. }
  282. if ($input == '') {
  283. return;
  284. }
  285. if (preg_match_all('/[!-](?P<search>[^\s]+)/', $input, $matches)) {
  286. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : array(), $matches['search']);
  287. $input = str_replace($matches[0], '', $input);
  288. }
  289. $this->not_search = self::removeEmptyValues($this->not_search);
  290. return $input;
  291. }
  292. /**
  293. * Remove all unnecessary spaces in the search
  294. *
  295. * @param string $input
  296. * @return string
  297. */
  298. private static function cleanSearch($input) {
  299. $input = preg_replace('/\s+/', ' ', $input);
  300. return trim($input);
  301. }
  302. }