namespacing: \Shaarli\Bookmark\LinkFilter
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
parent
f24896b237
commit
6696729b88
8 changed files with 66 additions and 50 deletions
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Shaarli\Bookmark\LinkDB;
|
||||
use Shaarli\Bookmark\LinkFilter;
|
||||
use Shaarli\Config\ConfigJson;
|
||||
use Shaarli\Config\ConfigPhp;
|
||||
use Shaarli\Config\ConfigManager;
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
use Countable;
|
||||
use DateTime;
|
||||
use Iterator;
|
||||
use LinkFilter;
|
||||
use LinkNotFoundException;
|
||||
use Shaarli\Bookmark\LinkFilter;
|
||||
use Shaarli\Bookmark\Exception\LinkNotFoundException;
|
||||
use Shaarli\Exceptions\IOException;
|
||||
use Shaarli\FileUtils;
|
||||
|
||||
|
@ -431,7 +431,7 @@ public function filterDay($request)
|
|||
* - searchterm: term search
|
||||
* @param bool $casesensitive Optional: Perform case sensitive filter
|
||||
* @param string $visibility return only all/private/public links
|
||||
* @param string $untaggedonly return only untagged links
|
||||
* @param bool $untaggedonly return only untagged links
|
||||
*
|
||||
* @return array filtered links, all links if no suitable filter was provided.
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
<?php
|
||||
|
||||
use Shaarli\Bookmark\LinkDB;
|
||||
namespace Shaarli\Bookmark;
|
||||
|
||||
use Exception;
|
||||
use Shaarli\Bookmark\Exception\LinkNotFoundException;
|
||||
|
||||
/**
|
||||
* Class LinkFilter.
|
||||
|
@ -60,7 +63,7 @@ public function __construct($links)
|
|||
*/
|
||||
public function filter($type, $request, $casesensitive = false, $visibility = 'all', $untaggedonly = false)
|
||||
{
|
||||
if (! in_array($visibility, ['all', 'public', 'private'])) {
|
||||
if (!in_array($visibility, ['all', 'public', 'private'])) {
|
||||
$visibility = 'all';
|
||||
}
|
||||
|
||||
|
@ -119,7 +122,7 @@ private function noFilter($visibility = 'all')
|
|||
foreach ($this->links as $key => $value) {
|
||||
if ($value['private'] && $visibility === 'private') {
|
||||
$out[$key] = $value;
|
||||
} elseif (! $value['private'] && $visibility === 'public') {
|
||||
} elseif (!$value['private'] && $visibility === 'public') {
|
||||
$out[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +137,7 @@ private function noFilter($visibility = 'all')
|
|||
*
|
||||
* @return array $filtered array containing permalink data.
|
||||
*
|
||||
* @throws LinkNotFoundException if the smallhash doesn't match any link.
|
||||
* @throws \Shaarli\Bookmark\Exception\LinkNotFoundException if the smallhash doesn't match any link.
|
||||
*/
|
||||
private function filterSmallHash($smallHash)
|
||||
{
|
||||
|
@ -209,7 +212,7 @@ private function filterFulltext($searchterms, $visibility = 'all')
|
|||
foreach ($this->links as $id => $link) {
|
||||
// ignore non private links when 'privatonly' is on.
|
||||
if ($visibility !== 'all') {
|
||||
if (! $link['private'] && $visibility === 'private') {
|
||||
if (!$link['private'] && $visibility === 'private') {
|
||||
continue;
|
||||
} elseif ($link['private'] && $visibility === 'public') {
|
||||
continue;
|
||||
|
@ -252,7 +255,9 @@ private function filterFulltext($searchterms, $visibility = 'all')
|
|||
|
||||
/**
|
||||
* generate a regex fragment out of a tag
|
||||
*
|
||||
* @param string $tag to to generate regexs from. may start with '-' to negate, contain '*' as wildcard
|
||||
*
|
||||
* @return string generated regex fragment
|
||||
*/
|
||||
private static function tag2regex($tag)
|
||||
|
@ -336,7 +341,7 @@ public function filterTags($tags, $casesensitive = false, $visibility = 'all')
|
|||
// check level of visibility
|
||||
// ignore non private links when 'privateonly' is on.
|
||||
if ($visibility !== 'all') {
|
||||
if (! $link['private'] && $visibility === 'private') {
|
||||
if (!$link['private'] && $visibility === 'private') {
|
||||
continue;
|
||||
} elseif ($link['private'] && $visibility === 'public') {
|
||||
continue;
|
||||
|
@ -379,7 +384,7 @@ public function filterUntagged($visibility)
|
|||
$filtered = [];
|
||||
foreach ($this->links as $key => $link) {
|
||||
if ($visibility !== 'all') {
|
||||
if (! $link['private'] && $visibility === 'private') {
|
||||
if (!$link['private'] && $visibility === 'private') {
|
||||
continue;
|
||||
} elseif ($link['private'] && $visibility === 'public') {
|
||||
continue;
|
||||
|
@ -408,7 +413,7 @@ public function filterUntagged($visibility)
|
|||
*/
|
||||
public function filterDay($day)
|
||||
{
|
||||
if (! checkDateFormat('Ymd', $day)) {
|
||||
if (!checkDateFormat('Ymd', $day)) {
|
||||
throw new Exception('Invalid date format');
|
||||
}
|
||||
|
||||
|
@ -442,14 +447,3 @@ public static function tagsStrToArray($tags, $casesensitive)
|
|||
return preg_split('/\s+/', $tagsOut, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
class LinkNotFoundException extends Exception
|
||||
{
|
||||
/**
|
||||
* LinkNotFoundException constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->message = t('The link you are trying to reach does not exist or has been deleted.');
|
||||
}
|
||||
}
|
15
application/bookmark/exception/LinkNotFoundException.php
Normal file
15
application/bookmark/exception/LinkNotFoundException.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
namespace Shaarli\Bookmark\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class LinkNotFoundException extends Exception
|
||||
{
|
||||
/**
|
||||
* LinkNotFoundException constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->message = t('The link you are trying to reach does not exist or has been deleted.');
|
||||
}
|
||||
}
|
|
@ -36,6 +36,7 @@
|
|||
"Shaarli\\Api\\Controllers\\": "application/api/controllers",
|
||||
"Shaarli\\Api\\Exceptions\\": "application/api/exceptions",
|
||||
"Shaarli\\Bookmark\\": "application/bookmark",
|
||||
"Shaarli\\Bookmark\\Exception\\": "application/bookmark/exception",
|
||||
"Shaarli\\Config\\": "application/config/",
|
||||
"Shaarli\\Config\\Exception\\": "application/config/exception",
|
||||
"Shaarli\\Exceptions\\": "application/exceptions",
|
||||
|
|
|
@ -63,7 +63,6 @@
|
|||
require_once 'application/http/UrlUtils.php';
|
||||
require_once 'application/FileUtils.php';
|
||||
require_once 'application/History.php';
|
||||
require_once 'application/LinkFilter.php';
|
||||
require_once 'application/LinkUtils.php';
|
||||
require_once 'application/NetscapeBookmarkUtils.php';
|
||||
require_once 'application/TimeZone.php';
|
||||
|
@ -72,6 +71,7 @@
|
|||
require_once 'application/Router.php';
|
||||
require_once 'application/Updater.php';
|
||||
|
||||
use \Shaarli\Bookmark\Exception\LinkNotFoundException;
|
||||
use \Shaarli\Bookmark\LinkDB;
|
||||
use \Shaarli\Config\ConfigManager;
|
||||
use \Shaarli\Feed\CachedPage;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
namespace Shaarli\Bookmark;
|
||||
|
||||
use DateTime;
|
||||
use LinkNotFoundException;
|
||||
use Shaarli\Bookmark\Exception\LinkNotFoundException;
|
||||
use ReferenceLinkDB;
|
||||
use ReflectionClass;
|
||||
use Shaarli;
|
||||
|
@ -457,7 +457,7 @@ public function testFilterHashValid()
|
|||
/**
|
||||
* Test filterHash() with an invalid smallhash.
|
||||
*
|
||||
* @expectedException LinkNotFoundException
|
||||
* @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
|
||||
*/
|
||||
public function testFilterHashInValid1()
|
||||
{
|
||||
|
@ -468,7 +468,7 @@ public function testFilterHashInValid1()
|
|||
/**
|
||||
* Test filterHash() with an empty smallhash.
|
||||
*
|
||||
* @expectedException LinkNotFoundException
|
||||
* @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
|
||||
*/
|
||||
public function testFilterHashInValid()
|
||||
{
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
<?php
|
||||
|
||||
use Shaarli\Bookmark\LinkDB;
|
||||
namespace Shaarli\Bookmark;
|
||||
|
||||
require_once 'application/LinkFilter.php';
|
||||
use Exception;
|
||||
use ReferenceLinkDB;
|
||||
|
||||
/**
|
||||
* Class LinkFilterTest.
|
||||
*/
|
||||
class LinkFilterTest extends PHPUnit_Framework_TestCase
|
||||
class LinkFilterTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var string Test datastore path.
|
||||
|
@ -29,7 +30,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
|
|||
protected static $linkDB;
|
||||
|
||||
/**
|
||||
* Instanciate linkFilter with ReferenceLinkDB data.
|
||||
* Instantiate linkFilter with ReferenceLinkDB data.
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
|
@ -81,10 +82,14 @@ public function testFilter()
|
|||
count(
|
||||
self::$linkFilter->filter(
|
||||
LinkFilter::$FILTER_TAG,
|
||||
/*$request=*/'',
|
||||
/*$casesensitive=*/false,
|
||||
/*$visibility=*/'all',
|
||||
/*$untaggedonly=*/true
|
||||
/*$request=*/
|
||||
'',
|
||||
/*$casesensitive=*/
|
||||
false,
|
||||
/*$visibility=*/
|
||||
'all',
|
||||
/*$untaggedonly=*/
|
||||
true
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -229,7 +234,7 @@ public function testFilterSmallHash()
|
|||
/**
|
||||
* No link for this hash
|
||||
*
|
||||
* @expectedException LinkNotFoundException
|
||||
* @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
|
||||
*/
|
||||
public function testFilterUnknownSmallHash()
|
||||
{
|
Loading…
Reference in a new issue