aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/LinkFilter.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-01-16 13:57:11 +0100
committerArthurHoaro <arthur@hoa.ro>2017-01-16 13:57:11 +0100
commit7f96d9ec21a95cb85d0292b46e18235b20efbcb2 (patch)
tree7fbedb066b3eef2d3951935806069b2236effb83 /application/LinkFilter.php
parentae7f6b9d09c0f3e31d64e6c8a9804d5ab0c62eae (diff)
downloadShaarli-7f96d9ec21a95cb85d0292b46e18235b20efbcb2.tar.gz
Shaarli-7f96d9ec21a95cb85d0292b46e18235b20efbcb2.tar.zst
Shaarli-7f96d9ec21a95cb85d0292b46e18235b20efbcb2.zip
Update LinkFilter to be able to filter only public links
No update regarding the UI or the API for now Fixes #758
Diffstat (limited to 'application/LinkFilter.php')
-rw-r--r--application/LinkFilter.php60
1 files changed, 37 insertions, 23 deletions
diff --git a/application/LinkFilter.php b/application/LinkFilter.php
index 57ebfd5c..81832a4b 100644
--- a/application/LinkFilter.php
+++ b/application/LinkFilter.php
@@ -51,12 +51,16 @@ class LinkFilter
51 * @param string $type Type of filter (eg. tags, permalink, etc.). 51 * @param string $type Type of filter (eg. tags, permalink, etc.).
52 * @param mixed $request Filter content. 52 * @param mixed $request Filter content.
53 * @param bool $casesensitive Optional: Perform case sensitive filter if true. 53 * @param bool $casesensitive Optional: Perform case sensitive filter if true.
54 * @param bool $privateonly Optional: Only returns private links if true. 54 * @param string $visibility Optional: return only all/private/public links
55 * 55 *
56 * @return array filtered link list. 56 * @return array filtered link list.
57 */ 57 */
58 public function filter($type, $request, $casesensitive = false, $privateonly = false) 58 public function filter($type, $request, $casesensitive = false, $visibility = 'all')
59 { 59 {
60 if (! in_array($visibility, ['all', 'public', 'private'])) {
61 $visibility = 'all';
62 }
63
60 switch($type) { 64 switch($type) {
61 case self::$FILTER_HASH: 65 case self::$FILTER_HASH:
62 return $this->filterSmallHash($request); 66 return $this->filterSmallHash($request);
@@ -64,42 +68,44 @@ class LinkFilter
64 if (!empty($request)) { 68 if (!empty($request)) {
65 $filtered = $this->links; 69 $filtered = $this->links;
66 if (isset($request[0])) { 70 if (isset($request[0])) {
67 $filtered = $this->filterTags($request[0], $casesensitive, $privateonly); 71 $filtered = $this->filterTags($request[0], $casesensitive, $visibility);
68 } 72 }
69 if (isset($request[1])) { 73 if (isset($request[1])) {
70 $lf = new LinkFilter($filtered); 74 $lf = new LinkFilter($filtered);
71 $filtered = $lf->filterFulltext($request[1], $privateonly); 75 $filtered = $lf->filterFulltext($request[1], $visibility);
72 } 76 }
73 return $filtered; 77 return $filtered;
74 } 78 }
75 return $this->noFilter($privateonly); 79 return $this->noFilter($visibility);
76 case self::$FILTER_TEXT: 80 case self::$FILTER_TEXT:
77 return $this->filterFulltext($request, $privateonly); 81 return $this->filterFulltext($request, $visibility);
78 case self::$FILTER_TAG: 82 case self::$FILTER_TAG:
79 return $this->filterTags($request, $casesensitive, $privateonly); 83 return $this->filterTags($request, $casesensitive, $visibility);
80 case self::$FILTER_DAY: 84 case self::$FILTER_DAY:
81 return $this->filterDay($request); 85 return $this->filterDay($request);
82 default: 86 default:
83 return $this->noFilter($privateonly); 87 return $this->noFilter($visibility);
84 } 88 }
85 } 89 }
86 90
87 /** 91 /**
88 * Unknown filter, but handle private only. 92 * Unknown filter, but handle private only.
89 * 93 *
90 * @param bool $privateonly returns private link only if true. 94 * @param string $visibility Optional: return only all/private/public links
91 * 95 *
92 * @return array filtered links. 96 * @return array filtered links.
93 */ 97 */
94 private function noFilter($privateonly = false) 98 private function noFilter($visibility = 'all')
95 { 99 {
96 if (! $privateonly) { 100 if ($visibility === 'all') {
97 return $this->links; 101 return $this->links;
98 } 102 }
99 103
100 $out = array(); 104 $out = array();
101 foreach ($this->links as $key => $value) { 105 foreach ($this->links as $key => $value) {
102 if ($value['private']) { 106 if ($value['private'] && $visibility === 'private') {
107 $out[$key] = $value;
108 } else if (! $value['private'] && $visibility === 'public') {
103 $out[$key] = $value; 109 $out[$key] = $value;
104 } 110 }
105 } 111 }
@@ -151,14 +157,14 @@ class LinkFilter
151 * - see https://github.com/shaarli/Shaarli/issues/75 for examples 157 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
152 * 158 *
153 * @param string $searchterms search query. 159 * @param string $searchterms search query.
154 * @param bool $privateonly return only private links if true. 160 * @param string $visibility Optional: return only all/private/public links.
155 * 161 *
156 * @return array search results. 162 * @return array search results.
157 */ 163 */
158 private function filterFulltext($searchterms, $privateonly = false) 164 private function filterFulltext($searchterms, $visibility = 'all')
159 { 165 {
160 if (empty($searchterms)) { 166 if (empty($searchterms)) {
161 return $this->links; 167 return $this->noFilter($visibility);
162 } 168 }
163 169
164 $filtered = array(); 170 $filtered = array();
@@ -189,8 +195,12 @@ class LinkFilter
189 foreach ($this->links as $id => $link) { 195 foreach ($this->links as $id => $link) {
190 196
191 // ignore non private links when 'privatonly' is on. 197 // ignore non private links when 'privatonly' is on.
192 if (! $link['private'] && $privateonly === true) { 198 if ($visibility !== 'all') {
193 continue; 199 if (! $link['private'] && $visibility === 'private') {
200 continue;
201 } else if ($link['private'] && $visibility === 'public') {
202 continue;
203 }
194 } 204 }
195 205
196 // Concatenate link fields to search across fields. 206 // Concatenate link fields to search across fields.
@@ -235,16 +245,16 @@ class LinkFilter
235 * 245 *
236 * @param string $tags list of tags separated by commas or blank spaces. 246 * @param string $tags list of tags separated by commas or blank spaces.
237 * @param bool $casesensitive ignore case if false. 247 * @param bool $casesensitive ignore case if false.
238 * @param bool $privateonly returns private links only. 248 * @param string $visibility Optional: return only all/private/public links.
239 * 249 *
240 * @return array filtered links. 250 * @return array filtered links.
241 */ 251 */
242 public function filterTags($tags, $casesensitive = false, $privateonly = false) 252 public function filterTags($tags, $casesensitive = false, $visibility = 'all')
243 { 253 {
244 // Implode if array for clean up. 254 // Implode if array for clean up.
245 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags; 255 $tags = is_array($tags) ? trim(implode(' ', $tags)) : $tags;
246 if (empty($tags)) { 256 if (empty($tags)) {
247 return $this->links; 257 return $this->noFilter($visibility);
248 } 258 }
249 259
250 $searchtags = self::tagsStrToArray($tags, $casesensitive); 260 $searchtags = self::tagsStrToArray($tags, $casesensitive);
@@ -255,8 +265,12 @@ class LinkFilter
255 265
256 foreach ($this->links as $key => $link) { 266 foreach ($this->links as $key => $link) {
257 // ignore non private links when 'privatonly' is on. 267 // ignore non private links when 'privatonly' is on.
258 if (! $link['private'] && $privateonly === true) { 268 if ($visibility !== 'all') {
259 continue; 269 if (! $link['private'] && $visibility === 'private') {
270 continue;
271 } else if ($link['private'] && $visibility === 'public') {
272 continue;
273 }
260 } 274 }
261 275
262 $linktags = self::tagsStrToArray($link['tags'], $casesensitive); 276 $linktags = self::tagsStrToArray($link['tags'], $casesensitive);
@@ -341,7 +355,7 @@ class LinkFilter
341 * @param bool $casesensitive will convert everything to lowercase if false. 355 * @param bool $casesensitive will convert everything to lowercase if false.
342 * 356 *
343 * @return array filtered tags string. 357 * @return array filtered tags string.
344 */ 358 */
345 public static function tagsStrToArray($tags, $casesensitive) 359 public static function tagsStrToArray($tags, $casesensitive)
346 { 360 {
347 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek) 361 // We use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)