]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkFilter.php
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.
126 * print_r($mydb->filterFulltext('hollandais'));
128 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
129 * - allows to perform searches on Unicode text
130 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
132 * @param string $searchterms search query.
133 * @param bool $privateonly return only private links if true.
135 * @return array search results.
137 private function filterFulltext($searchterms, $privateonly = false)
139 // FIXME: explode(' ',$searchterms) and perform a AND search.
140 // FIXME: accept double-quotes to search for a string "as is"?
142 $search = mb_convert_case($searchterms, MB_CASE_LOWER
, 'UTF-8');
143 $explodedSearch = explode(' ', trim($search));
144 $keys = array('title', 'description', 'url', 'tags');
146 // Iterate over every stored link.
147 foreach ($this->links
as $link) {
150 // ignore non private links when 'privatonly' is on.
151 if (! $link['private'] && $privateonly === true) {
155 // Iterate over searchable link fields.
156 foreach ($keys as $key) {
157 // Search full expression.
159 mb_convert_case($link[$key], MB_CASE_LOWER
, 'UTF-8'),
171 $filtered[$link['linkdate']] = $link;
180 * Returns the list of links associated with a given list of tags
182 * You can specify one or more tags, separated by space or a comma, e.g.
183 * print_r($mydb->filterTags('linux programming'));
185 * @param string $tags list of tags separated by commas or blank spaces.
186 * @param bool $casesensitive ignore case if false.
187 * @param bool $privateonly returns private links only.
189 * @return array filtered links.
191 public function filterTags($tags, $casesensitive = false, $privateonly = false)
193 $searchtags = $this->tagsStrToArray($tags, $casesensitive);
196 foreach ($this->links
as $l) {
197 // ignore non private links when 'privatonly' is on.
198 if (! $l['private'] && $privateonly === true) {
202 $linktags = $this->tagsStrToArray($l['tags'], $casesensitive);
204 if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) {
205 $filtered[$l['linkdate']] = $l;
213 * Returns the list of articles for a given day, chronologically sorted
215 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
216 * print_r($mydb->filterDay('20120125'));
218 * @param string $day day to filter.
220 * @return array all link matching given day.
222 * @throws Exception if date format is invalid.
224 public function filterDay($day)
226 if (! checkDateFormat('Ymd', $day)) {
227 throw new Exception('Invalid date format');
231 foreach ($this->links
as $l) {
232 if (startsWith($l['linkdate'], $day)) {
233 $filtered[$l['linkdate']] = $l;
241 * Convert a list of tags (str) to an array. Also
242 * - handle case sensitivity.
243 * - accepts spaces commas as separator.
244 * - remove private tags for loggedout users.
246 * @param string $tags string containing a list of tags.
247 * @param bool $casesensitive will convert everything to lowercase if false.
249 * @return array filtered tags string.
251 public function tagsStrToArray($tags, $casesensitive)
253 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
254 $tagsOut = $casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER
, 'UTF-8');
255 $tagsOut = str_replace(',', ' ', $tagsOut);
257 return explode(' ', trim($tagsOut));