aboutsummaryrefslogtreecommitdiff
path: root/app/Models/Search.php
blob: 575a9a2cb95382507b31536ab42bbe47085125e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php

require_once(LIB_PATH . '/lib_date.php');

/**
 * Contains a search from the search form.
 *
 * It allows to extract meaningful bits of the search and store them in a
 * convenient object
 */
class FreshRSS_Search {

	// This contains the user input string
	private $raw_input = '';
	// The following properties are extracted from the raw input
	private $intitle;
	private $min_date;
	private $max_date;
	private $min_pubdate;
	private $max_pubdate;
	private $inurl;
	private $author;
	private $tags;
	private $search;

	public function __construct($input) {
		if (strcmp($input, '') == 0) {
			return;
		}
		$this->raw_input = $input;
		$input = $this->parseIntitleSearch($input);
		$input = $this->parseAuthorSearch($input);
		$input = $this->parseInurlSearch($input);
		$input = $this->parsePubdateSearch($input);
		$input = $this->parseDateSearch($input);
		$input = $this->parseTagsSeach($input);
		$this->parseSearch($input);
	}

	public function __toString() {
		return $this->getRawInput();
	}

	public function getRawInput() {
		return $this->raw_input;
	}

	public function getIntitle() {
		return $this->intitle;
	}

	public function getMinDate() {
		return $this->min_date;
	}

	public function getMaxDate() {
		return $this->max_date;
	}

	public function getMinPubdate() {
		return $this->min_pubdate;
	}

	public function getMaxPubdate() {
		return $this->max_pubdate;
	}

	public function getInurl() {
		return $this->inurl;
	}

	public function getAuthor() {
		return $this->author;
	}

	public function getTags() {
		return $this->tags;
	}

	public function getSearch() {
		return $this->search;
	}

	/**
	 * Parse the search string to find intitle keyword and the search related
	 * to it.
	 * The search is the first word following the keyword.
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseIntitleSearch($input) {
		if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
			$this->intitle = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
			$this->intitle = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find author keyword and the search related
	 * to it.
	 * The search is the first word following the keyword except when using
	 * a delimiter. Supported delimiters are single quote (') and double
	 * quotes (").
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseAuthorSearch($input) {
		if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
			$this->author = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
			$this->author = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find inurl keyword and the search related
	 * to it.
	 * The search is the first word following the keyword except.
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseInurlSearch($input) {
		if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
			$this->inurl = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find date keyword and the search related
	 * to it.
	 * The search is the first word following the keyword.
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseDateSearch($input) {
		if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
			list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find pubdate keyword and the search related
	 * to it.
	 * The search is the first word following the keyword.
	 *
	 * @param string $input
	 * @return string
	 */
	private function parsePubdateSearch($input) {
		if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
			list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find tags keyword (# followed by a word)
	 * and the search related to it.
	 * The search is the first word following the #.
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseTagsSeach($input) {
		if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
			$this->tags = $matches['search'];
			return str_replace($matches[0], '', $input);
		}
		return $input;
	}

	/**
	 * Parse the search string to find search values.
	 * Every word is a distinct search value, except when using a delimiter.
	 * Supported delimiters are single quote (') and double quotes (").
	 *
	 * @param string $input
	 * @return string
	 */
	private function parseSearch($input) {
		$input = $this->cleanSearch($input);
		if (strcmp($input, '') == 0) {
			return;
		}
		if (preg_match_all('/(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
			$this->search = $matches['search'];
			$input = str_replace($matches[0], '', $input);
		}
		$input = $this->cleanSearch($input);
		if (strcmp($input, '') == 0) {
			return;
		}
		if (is_array($this->search)) {
			$this->search = array_merge($this->search, explode(' ', $input));
		} else {
			$this->search = explode(' ', $input);
		}
	}

	/**
	 * Remove all unnecessary spaces in the search
	 *
	 * @param string $input
	 * @return string
	 */
	private function cleanSearch($input) {
		$input = preg_replace('/\s+/', ' ', $input);
		return trim($input);
	}

}