]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
e2ef94ea78ff5476cca3d058ff72538060358539
6 * Perform search and filter operation on link data list.
11 * @var string permalinks.
13 public static $FILTER_HASH = 'permalink';
16 * @var string text search.
18 public static $FILTER_TEXT = 'fulltext';
21 * @var string tag filter.
23 public static $FILTER_TAG = 'tags';
26 * @var string filter by day.
28 public static $FILTER_DAY = 'FILTER_DAY';
31 * @var array all available links.
36 * @param array $links initialization.
38 public function __construct($links)
40 $this->links
= $links;
44 * Filter links according to parameters.
46 * @param string $type Type of filter (eg. tags, permalink, etc.).
47 * @param string $request Filter content.
48 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
49 * @param bool $privateonly Optional: Only returns private links if true.
51 * @return array filtered link list.
53 public function filter($type, $request, $casesensitive = false, $privateonly = false)
56 case self
::$FILTER_HASH:
57 return $this->filterSmallHash($request);
59 case self
::$FILTER_TEXT:
60 return $this->filterFulltext($request, $privateonly);
62 case self
::$FILTER_TAG:
63 return $this->filterTags($request, $casesensitive, $privateonly);
65 case self
::$FILTER_DAY:
66 return $this->filterDay($request);
69 return $this->noFilter($privateonly);
74 * Unknown filter, but handle private only.
76 * @param bool $privateonly returns private link only if true.
78 * @return array filtered links.
80 private function noFilter($privateonly = false)
88 foreach ($this->links
as $value) {
89 if ($value['private']) {
90 $out[$value['linkdate']] = $value;
99 * Returns the shaare corresponding to a smallHash.
101 * @param string $smallHash permalink hash.
103 * @return array $filtered array containing permalink data.
105 private function filterSmallHash($smallHash)
108 foreach ($this->links
as $l) {
109 if ($smallHash == smallHash($l['linkdate'])) {
110 // Yes, this is ugly and slow
111 $filtered[$l['linkdate']] = $l;
119 * Returns the list of links corresponding to a full-text search
122 * - in the URLs, title and description;
123 * - are case-insensitive;
124 * - terms surrounded by quotes " are exact terms search.
125 * - terms starting with a dash - are excluded (except exact terms).
128 * print_r($mydb->filterFulltext('hollandais'));
130 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
131 * - allows to perform searches on Unicode text
132 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
134 * @param string $searchterms search query.
135 * @param bool $privateonly return only private links if true.
137 * @return array search results.
139 private function filterFulltext($searchterms, $privateonly = false)
141 $search = mb_convert_case(html_entity_decode($searchterms), MB_CASE_LOWER
, 'UTF-8');
142 $exactRegex = '/"([^"]+)"/';
143 // Retrieve exact search terms.
144 preg_match_all($exactRegex, $search, $exactSearch);
145 $exactSearch = array_values(array_filter($exactSearch[1]));
147 // Remove exact search terms to get AND terms search.
148 $explodedSearchAnd = explode(' ', trim(preg_replace($exactRegex, '', $search)));
149 $explodedSearchAnd = array_values(array_filter($explodedSearchAnd));
151 // Filter excluding terms and update andSearch.
152 $excludeSearch = array();
153 $andSearch = array();
154 foreach ($explodedSearchAnd as $needle) {
155 if ($needle[0] == '-' && strlen($needle) > 1) {
156 $excludeSearch[] = substr($needle, 1);
158 $andSearch[] = $needle;
162 $keys = array('title', 'description', 'url', 'tags');
164 // Iterate over every stored link.
165 foreach ($this->links
as $link) {
167 // ignore non private links when 'privatonly' is on.
168 if (! $link['private'] && $privateonly === true) {
172 // Iterate over searchable link fields.
173 foreach ($keys as $key) {
177 $haystack = mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8');
179 // First, we look for exact term search
180 for ($i = 0; $i < count($exactSearch) && $found; $i++
) {
181 $found = strpos($haystack, $exactSearch[$i]) !== false;
184 // Iterate over keywords, if keyword is not found,
185 // no need to check for the others. We want all or nothing.
186 for ($i = 0; $i < count($andSearch) && $found; $i++
) {
187 $found = strpos($haystack, $andSearch[$i]) !== false;
191 for ($i = 0; $i < count($excludeSearch) && $found; $i++
) {
192 $found = strpos($haystack, $excludeSearch[$i]) === false;
195 // One of the fields of the link matches, no need to check the other.
202 $filtered[$link['linkdate']] = $link;
211 * Returns the list of links associated with a given list of tags
213 * You can specify one or more tags, separated by space or a comma, e.g.
214 * print_r($mydb->filterTags('linux programming'));
216 * @param string $tags list of tags separated by commas or blank spaces.
217 * @param bool $casesensitive ignore case if false.
218 * @param bool $privateonly returns private links only.
220 * @return array filtered links.
222 public function filterTags($tags, $casesensitive = false, $privateonly = false)
224 $searchtags = self
::tagsStrToArray($tags, $casesensitive);
226 if (empty($searchtags)) {
230 foreach ($this->links
as $link) {
231 // ignore non private links when 'privatonly' is on.
232 if (! $link['private'] && $privateonly === true) {
236 $linktags = self
::tagsStrToArray($link['tags'], $casesensitive);
239 for ($i = 0 ; $i < count($searchtags) && $found; $i++
) {
240 // Exclusive search, quit if tag found.
241 // Or, tag not found in the link, quit.
242 if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags))
243 || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags)
250 $filtered[$link['linkdate']] = $link;
258 * Returns the list of articles for a given day, chronologically sorted
260 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
261 * print_r($mydb->filterDay('20120125'));
263 * @param string $day day to filter.
265 * @return array all link matching given day.
267 * @throws Exception if date format is invalid.
269 public function filterDay($day)
271 if (! checkDateFormat('Ymd', $day)) {
272 throw new Exception('Invalid date format');
276 foreach ($this->links
as $l) {
277 if (startsWith($l['linkdate'], $day)) {
278 $filtered[$l['linkdate']] = $l;
286 * Convert a list of tags (str) to an array. Also
287 * - handle case sensitivity.
288 * - accepts spaces commas as separator.
290 * @param string $tags string containing a list of tags.
291 * @param bool $casesensitive will convert everything to lowercase if false.
293 * @return array filtered tags string.
295 public static function tagsStrToArray($tags, $casesensitive)
297 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
298 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
299 $tagsOut = str_replace(',', ' ', $tagsOut);
301 return array_filter(explode(' ', trim($tagsOut)), 'strlen');