aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/LinkDB.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-03-21 21:40:49 +0100
committerArthurHoaro <arthur@hoa.ro>2016-03-25 19:17:59 +0100
commit528a6f8a232c060faf024008e4f8a09b4aa8dabc (patch)
tree86cac78b7f4d3998bedd923da83145c37ec88ff4 /application/LinkDB.php
parentee88a4bcc29da721cf43b750663aebeac4969517 (diff)
downloadShaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.tar.gz
Shaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.tar.zst
Shaarli-528a6f8a232c060faf024008e4f8a09b4aa8dabc.zip
Refactor filter in LinkDB
* search type now carried by LinkDB in order to factorize code between different search sources. * LinkDB->filter split in 3 method: filterSearch, filterHash, filterDay (we know what type of filter is needed). * filterHash now throw a LinkNotFoundException if it doesn't exist: internal implementation choice, still displays a 404. * Smallhash regex has been rewritten. * Unit tests update
Diffstat (limited to 'application/LinkDB.php')
-rw-r--r--application/LinkDB.php64
1 files changed, 59 insertions, 5 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 1b505620..a62341fc 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -341,17 +341,71 @@ You use the community supported version of the original Shaarli project, by Seba
341 } 341 }
342 342
343 /** 343 /**
344 * Filter links. 344 * Returns the shaare corresponding to a smallHash.
345 * 345 *
346 * @param string $type Type of filter. 346 * @param string $request QUERY_STRING server parameter.
347 * @param mixed $request Search request, string or array. 347 *
348 * @return array $filtered array containing permalink data.
349 *
350 * @throws LinkNotFoundException if the smallhash is malformed or doesn't match any link.
351 */
352 public function filterHash($request)
353 {
354 $request = substr($request, 0, 6);
355 $linkFilter = new LinkFilter($this->_links);
356 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
357 }
358
359 /**
360 * Returns the list of articles for a given day.
361 *
362 * @param string $request day to filter. Format: YYYYMMDD.
363 *
364 * @return array list of shaare found.
365 */
366 public function filterDay($request) {
367 $linkFilter = new LinkFilter($this->_links);
368 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
369 }
370
371 /**
372 * Filter links according to search parameters.
373 *
374 * @param array $filterRequest Search request content. Supported keys:
375 * - searchtags: list of tags
376 * - searchterm: term search
348 * @param bool $casesensitive Optional: Perform case sensitive filter 377 * @param bool $casesensitive Optional: Perform case sensitive filter
349 * @param bool $privateonly Optional: Returns private links only if true. 378 * @param bool $privateonly Optional: Returns private links only if true.
350 * 379 *
351 * @return array filtered links 380 * @return array filtered links, all links if no suitable filter was provided.
352 */ 381 */
353 public function filter($type = '', $request = '', $casesensitive = false, $privateonly = false) 382 public function filterSearch($filterRequest = array(), $casesensitive = false, $privateonly = false)
354 { 383 {
384 // Filter link database according to parameters.
385 $searchtags = !empty($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
386 $searchterm = !empty($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
387
388 // Search tags + fullsearch.
389 if (empty($type) && ! empty($searchtags) && ! empty($searchterm)) {
390 $type = LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT;
391 $request = array($searchtags, $searchterm);
392 }
393 // Search by tags.
394 elseif (! empty($searchtags)) {
395 $type = LinkFilter::$FILTER_TAG;
396 $request = $searchtags;
397 }
398 // Fulltext search.
399 elseif (! empty($searchterm)) {
400 $type = LinkFilter::$FILTER_TEXT;
401 $request = $searchterm;
402 }
403 // Otherwise, display without filtering.
404 else {
405 $type = '';
406 $request = '';
407 }
408
355 $linkFilter = new LinkFilter($this->_links); 409 $linkFilter = new LinkFilter($this->_links);
356 return $linkFilter->filter($type, $request, $casesensitive, $privateonly); 410 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
357 } 411 }