From 4bb8548eccf22b40c25352fe27c66f1f8039ebcd Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Sun, 8 Feb 2015 20:51:41 -0500 Subject: Add a way to parse search string to extract keywords This feature is not in use at the moment, but it will be handy to reorganize the query building process. It allows to have more than one keyword in the search box. Full tests are available as well. It probably needs a refactoring later, but I think this is the first step to make the application full object oriented and testable. --- app/Models/Context.php | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) (limited to 'app/Models/Context.php') diff --git a/app/Models/Context.php b/app/Models/Context.php index 1c770c756..645639907 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -301,4 +301,153 @@ class FreshRSS_Context { } return false; } + + /** + * Parse search string to extract the different keywords. + * + * @return array + */ + public function parseSearch() { + $search = self::$search; + $intitle = $this->parseIntitleSearch($search); + $author = $this->parseAuthorSearch($intitle['string']); + $inurl = $this->parseInurlSearch($author['string']); + $pubdate = $this->parsePubdateSearch($inurl['string']); + $date = $this->parseDateSearch($pubdate['string']); + + $remaining = array(); + $remaining_search = trim($date['string']); + if (strcmp($remaining_search, '') != 0) { + $remaining['search'] = $remaining_search; + } + + return array_merge($intitle['search'], $author['search'], $inurl['search'], $date['search'], $pubdate['search'], $remaining); + } + + /** + * Parse the search string to find intitle keyword and the search related + * to it. + * The search is the first word following the keyword. + * It returns an array containing the matched string and the search. + * + * @param string $search + * @return array + */ + private function parseIntitleSearch($search) { + if (preg_match('/intitle:(?P[\'"])(?P.*)(?P=delim)/U', $search, $matches)) { + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('intitle' => $matches['search']), + ); + } + if (preg_match('/intitle:(?P\w*)/', $search, $matches)) { + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('intitle' => $matches['search']), + ); + } + return array( + 'string' => $search, + 'search' => array(), + ); + } + + /** + * Parse the search string to find author keyword and the search related + * to it. + * The search is the first word following the keyword except when using + * a delimiter. Supported delimiters are single quote (') and double + * quotes ("). + * It returns an array containing the matched string and the search. + * + * @param string $search + * @return array + */ + private function parseAuthorSearch($search) { + if (preg_match('/author:(?P[\'"])(?P.*)(?P=delim)/U', $search, $matches)) { + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('author' => $matches['search']), + ); + } + if (preg_match('/author:(?P\w*)/', $search, $matches)) { + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('author' => $matches['search']), + ); + } + return array( + 'string' => $search, + 'search' => array(), + ); + } + + /** + * Parse the search string to find inurl keyword and the search related + * to it. + * The search is the first word following the keyword except. + * It returns an array containing the matched string and the search. + * + * @param string $search + * @return array + */ + private function parseInurlSearch($search) { + if (preg_match('/inurl:(?P[^\s]*)/', $search, $matches)) { + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('inurl' => $matches['search']), + ); + } + return array( + 'string' => $search, + 'search' => array(), + ); + } + + /** + * Parse the search string to find date keyword and the search related + * to it. + * The search is the first word following the keyword. + * It returns an array containing the matched string and the search. + * + * @param string $search + * @return array + */ + private function parseDateSearch($search) { + if (preg_match('/date:(?P[^\s]*)/', $search, $matches)) { + list($min_date, $max_date) = parseDateInterval($matches['search']); + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('min_date' => $min_date, 'max_date' => $max_date), + ); + } + return array( + 'string' => $search, + 'search' => array(), + ); + } + + /** + * Parse the search string to find pubdate keyword and the search related + * to it. + * The search is the first word following the keyword. + * It returns an array containing the matched string and the search. + * + * @param string $search + * @return array + */ + private function parsePubdateSearch($search) { + if (preg_match('/pubdate:(?P[^\s]*)/', $search, $matches)) { + list($min_date, $max_date) = parseDateInterval($matches['search']); + return array( + 'string' => str_replace($matches[0], '', $search), + 'search' => array('min_pubdate' => $min_date, 'max_pubdate' => $max_date), + ); + } + return array( + 'string' => $search, + 'search' => array(), + ); + } + } -- cgit v1.2.3 From f30ded2f863ed4a33ae8194d6cf00eaa877c9b6a Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Wed, 11 Feb 2015 20:41:00 -0500 Subject: Extract the search code from the context I figured that the code for the search could be extracted from the context to have separation of concern. It supports multiple keywords. It suports also multiple tag keywords. --- app/Models/Context.php | 148 --------------------- app/Models/Search.php | 189 +++++++++++++++++++++++++++ tests/app/Models/ContextTest.php | 236 --------------------------------- tests/app/Models/SearchTest.php | 276 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 465 insertions(+), 384 deletions(-) create mode 100644 app/Models/Search.php create mode 100644 tests/app/Models/SearchTest.php (limited to 'app/Models/Context.php') diff --git a/app/Models/Context.php b/app/Models/Context.php index 645639907..f00bb1e97 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -302,152 +302,4 @@ class FreshRSS_Context { return false; } - /** - * Parse search string to extract the different keywords. - * - * @return array - */ - public function parseSearch() { - $search = self::$search; - $intitle = $this->parseIntitleSearch($search); - $author = $this->parseAuthorSearch($intitle['string']); - $inurl = $this->parseInurlSearch($author['string']); - $pubdate = $this->parsePubdateSearch($inurl['string']); - $date = $this->parseDateSearch($pubdate['string']); - - $remaining = array(); - $remaining_search = trim($date['string']); - if (strcmp($remaining_search, '') != 0) { - $remaining['search'] = $remaining_search; - } - - return array_merge($intitle['search'], $author['search'], $inurl['search'], $date['search'], $pubdate['search'], $remaining); - } - - /** - * Parse the search string to find intitle keyword and the search related - * to it. - * The search is the first word following the keyword. - * It returns an array containing the matched string and the search. - * - * @param string $search - * @return array - */ - private function parseIntitleSearch($search) { - if (preg_match('/intitle:(?P[\'"])(?P.*)(?P=delim)/U', $search, $matches)) { - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('intitle' => $matches['search']), - ); - } - if (preg_match('/intitle:(?P\w*)/', $search, $matches)) { - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('intitle' => $matches['search']), - ); - } - return array( - 'string' => $search, - 'search' => array(), - ); - } - - /** - * Parse the search string to find author keyword and the search related - * to it. - * The search is the first word following the keyword except when using - * a delimiter. Supported delimiters are single quote (') and double - * quotes ("). - * It returns an array containing the matched string and the search. - * - * @param string $search - * @return array - */ - private function parseAuthorSearch($search) { - if (preg_match('/author:(?P[\'"])(?P.*)(?P=delim)/U', $search, $matches)) { - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('author' => $matches['search']), - ); - } - if (preg_match('/author:(?P\w*)/', $search, $matches)) { - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('author' => $matches['search']), - ); - } - return array( - 'string' => $search, - 'search' => array(), - ); - } - - /** - * Parse the search string to find inurl keyword and the search related - * to it. - * The search is the first word following the keyword except. - * It returns an array containing the matched string and the search. - * - * @param string $search - * @return array - */ - private function parseInurlSearch($search) { - if (preg_match('/inurl:(?P[^\s]*)/', $search, $matches)) { - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('inurl' => $matches['search']), - ); - } - return array( - 'string' => $search, - 'search' => array(), - ); - } - - /** - * Parse the search string to find date keyword and the search related - * to it. - * The search is the first word following the keyword. - * It returns an array containing the matched string and the search. - * - * @param string $search - * @return array - */ - private function parseDateSearch($search) { - if (preg_match('/date:(?P[^\s]*)/', $search, $matches)) { - list($min_date, $max_date) = parseDateInterval($matches['search']); - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('min_date' => $min_date, 'max_date' => $max_date), - ); - } - return array( - 'string' => $search, - 'search' => array(), - ); - } - - /** - * Parse the search string to find pubdate keyword and the search related - * to it. - * The search is the first word following the keyword. - * It returns an array containing the matched string and the search. - * - * @param string $search - * @return array - */ - private function parsePubdateSearch($search) { - if (preg_match('/pubdate:(?P[^\s]*)/', $search, $matches)) { - list($min_date, $max_date) = parseDateInterval($matches['search']); - return array( - 'string' => str_replace($matches[0], '', $search), - 'search' => array('min_pubdate' => $min_date, 'max_pubdate' => $max_date), - ); - } - return array( - 'string' => $search, - 'search' => array(), - ); - } - } diff --git a/app/Models/Search.php b/app/Models/Search.php new file mode 100644 index 000000000..0475bc685 --- /dev/null +++ b/app/Models/Search.php @@ -0,0 +1,189 @@ +raw_input = $input; + $input = $this->parseIntitleSearch($input); + $input = $this->parseAuthorSearch($input); + $input = $this->parseInurlSearch($input); + $input = $this->parsePubdateSearch($input); + $input = $this->parseDateSearch($input); + $input = $this->parseTagsSeach($input); + $this->search = $input; + } + + public function getRawInput() { + return $this->raw_input; + } + + public function getIntitle() { + return $this->intitle; + } + + public function getMinDate() { + return $this->min_date; + } + + public function getMaxDate() { + return $this->max_date; + } + + public function getMinPubdate() { + return $this->min_pubdate; + } + + public function getMaxPubdate() { + return $this->max_pubdate; + } + + public function getInurl() { + return $this->inurl; + } + + public function getAuthor() { + return $this->author; + } + + public function getTags() { + return $this->tags; + } + + public function getSearch() { + return $this->search; + } + + /** + * Parse the search string to find intitle keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parseIntitleSearch($input) { + if (preg_match('/intitle:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { + $this->intitle = $matches['search']; + $input = str_replace($matches[0], '', $input); + } else if (preg_match('/intitle:(?P\w*)/', $input, $matches)) { + $this->intitle = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find author keyword and the search related + * to it. + * The search is the first word following the keyword except when using + * a delimiter. Supported delimiters are single quote (') and double + * quotes ("). + * + * @param string $input + * @return string + */ + private function parseAuthorSearch($input) { + if (preg_match('/author:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { + $this->author = $matches['search']; + $input = str_replace($matches[0], '', $input); + } else if (preg_match('/author:(?P\w*)/', $input, $matches)) { + $this->author = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find inurl keyword and the search related + * to it. + * The search is the first word following the keyword except. + * + * @param string $input + * @return string + */ + private function parseInurlSearch($input) { + if (preg_match('/inurl:(?P[^\s]*)/', $input, $matches)) { + $this->inurl = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find date keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parseDateSearch($input) { + if (preg_match('/date:(?P[^\s]*)/', $input, $matches)) { + list($this->min_date, $this->max_date) = parseDateInterval($matches['search']); + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find pubdate keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parsePubdateSearch($input) { + if (preg_match('/pubdate:(?P[^\s]*)/', $input, $matches)) { + list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']); + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find tags keyword (# followed by a word) + * and the search related to it. + * The search is the first word following the #. + * + * @param string $input + * @return string + */ + private function parseTagsSeach($input) { + if (preg_match_all('/#(?P[^\s]+)/', $input, $matches)) { + $this->tags = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + private function cleanSearch($input) { + $input = preg_replace('/\s+/', ' ', $input); + return trim($input); + } + +} diff --git a/tests/app/Models/ContextTest.php b/tests/app/Models/ContextTest.php index c5da6f667..4dc8b7757 100644 --- a/tests/app/Models/ContextTest.php +++ b/tests/app/Models/ContextTest.php @@ -1,241 +1,5 @@ context = new FreshRSS_Context(); - } - - public function testParseSearch_whenEmpty_returnsEmptyArray() { - $this->assertCount(0, $this->context->parseSearch()); - } - - /** - * @dataProvider provideMultipleKeywordSearch - * @param string $search - * @param string $expected_values - */ - public function testParseSearch_whenMultipleKeywords_returnArrayWithMultipleValues($search, $expected_values) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_values, $parsed_search); - } - - /** - * @return array - */ - public function provideMultipleKeywordSearch() { - return array( - array( - 'intitle:word1 author:word2', - array( - 'intitle' => 'word1', - 'author' => 'word2', - ), - ), - array( - 'author:word2 intitle:word1', - array( - 'intitle' => 'word1', - 'author' => 'word2', - ), - ), - array( - 'author:word1 inurl:word2', - array( - 'author' => 'word1', - 'inurl' => 'word2', - ), - ), - array( - 'inurl:word2 author:word1', - array( - 'author' => 'word1', - 'inurl' => 'word2', - ), - ), - array( - 'date:2008-01-01/2008-02-01 pubdate:2007-01-01/2007-02-01', - array( - 'min_date' => '1199163600', - 'max_date' => '1201928399', - 'min_pubdate' => '1167627600', - 'max_pubdate' => '1170392399', - ), - ), - array( - 'pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01', - array( - 'min_date' => '1199163600', - 'max_date' => '1201928399', - 'min_pubdate' => '1167627600', - 'max_pubdate' => '1170392399', - ), - ), - array( - 'inurl:word1 author:word2 intitle:word3 pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01 hello world', - array( - 'inurl' => 'word1', - 'author' => 'word2', - 'intitle' => 'word3', - 'min_date' => '1199163600', - 'max_date' => '1201928399', - 'min_pubdate' => '1167627600', - 'max_pubdate' => '1170392399', - 'search' => 'hello world', - ), - ), - ); - } - - /** - * @dataProvider provideIntitleSearch - * @param string $search - * @param string $expected_value - */ - public function testParseSearch_whenIntitleKeyword_returnArrayWithIntitleValue($search, $expected_value) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_value, $parsed_search['intitle']); - } - - /** - * @return array - */ - public function provideIntitleSearch() { - return array( - array('intitle:word1', 'word1'), - array('intitle:word1 word2', 'word1'), - array('intitle:"word1 word2"', 'word1 word2'), - array("intitle:'word1 word2'", 'word1 word2'), - array('word1 intitle:word2', 'word2'), - array('word1 intitle:word2 word3', 'word2'), - array('word1 intitle:"word2 word3"', 'word2 word3'), - array("word1 intitle:'word2 word3'", 'word2 word3'), - array('intitle:word1 intitle:word2', 'word1'), - array('intitle: word1 word2', ''), - array('intitle:123', '123'), - array('intitle:"word1 word2" word3"', 'word1 word2'), - array("intitle:'word1 word2' word3'", 'word1 word2'), - array('intitle:"word1 word2\' word3"', "word1 word2' word3"), - array("intitle:'word1 word2\" word3'", 'word1 word2" word3'), - ); - } - - /** - * @dataProvider provideAuthorSearch - * @param string $search - * @param string $expected_value - */ - public function testParseSearch_whenAuthorKeyword_returnArrayWithAuthorValue($search, $expected_value) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_value, $parsed_search['author']); - } - - /** - * @return array - */ - public function provideAuthorSearch() { - return array( - array('author:word1', 'word1'), - array('author:word1 word2', 'word1'), - array('author:"word1 word2"', 'word1 word2'), - array("author:'word1 word2'", 'word1 word2'), - array('word1 author:word2', 'word2'), - array('word1 author:word2 word3', 'word2'), - array('word1 author:"word2 word3"', 'word2 word3'), - array("word1 author:'word2 word3'", 'word2 word3'), - array('author:word1 author:word2', 'word1'), - array('author: word1 word2', ''), - array('author:123', '123'), - array('author:"word1 word2" word3"', 'word1 word2'), - array("author:'word1 word2' word3'", 'word1 word2'), - array('author:"word1 word2\' word3"', "word1 word2' word3"), - array("author:'word1 word2\" word3'", 'word1 word2" word3'), - ); - } - - /** - * @dataProvider provideInurlSearch - * @param string $search - * @param string $expected_value - */ - public function testParseSearch_whenInurlKeyword_returnArrayWithInurlValue($search, $expected_value) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_value, $parsed_search['inurl']); - } - - /** - * @return array - */ - public function provideInurlSearch() { - return array( - array('inurl:word1', 'word1'), - array('inurl: word1', ''), - array('inurl:123', '123'), - array('inurl:word1 word2', 'word1'), - array('inurl:"word1 word2"', '"word1'), - ); - } - - /** - * @dataProvider provideDateSearch - * @param string $search - * @param string $expected_min_value - * @param string $expected_max_value - */ - public function testParseSearch_whenDateKeyword_returnArrayWithDateValues($search, $expected_min_value, $expected_max_value) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_min_value, $parsed_search['min_date']); - $this->assertEquals($expected_max_value, $parsed_search['max_date']); - } - - /** - * @return array - */ - public function provideDateSearch() { - return array( - array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), - array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'), - array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'), - array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'), - array('date:2007-03-01/', '1172725200', ''), - array('date:/2008-05-11', '', '1210564799'), - ); - } - - /** - * @dataProvider providePubdateSearch - * @param string $search - * @param string $expected_min_value - * @param string $expected_max_value - */ - public function testParseSearch_whenPubdateKeyword_returnArrayWithPubdateValues($search, $expected_min_value, $expected_max_value) { - FreshRSS_Context::$search = $search; - $parsed_search = $this->context->parseSearch(); - $this->assertEquals($expected_min_value, $parsed_search['min_pubdate']); - $this->assertEquals($expected_max_value, $parsed_search['max_pubdate']); - } - - /** - * @return array - */ - public function providePubdateSearch() { - return array( - array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), - array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'), - array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'), - array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'), - array('pubdate:2007-03-01/', '1172725200', ''), - array('pubdate:/2008-05-11', '', '1210564799'), - ); - } - } diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php new file mode 100644 index 000000000..6ddfc0370 --- /dev/null +++ b/tests/app/Models/SearchTest.php @@ -0,0 +1,276 @@ +assertNull($search->getRawInput()); + $this->assertNull($search->getIntitle()); + $this->assertNull($search->getMinDate()); + $this->assertNull($search->getMaxDate()); + $this->assertNull($search->getMinPubdate()); + $this->assertNull($search->getMaxPubdate()); + $this->assertNull($search->getAuthor()); + $this->assertNull($search->getTags()); + $this->assertNull($search->getSearch()); + } + + /** + * Return an array of values for the search object. + * Here is the description of the values + * @return array + */ + public function provideEmptyInput() { + return array( + array(''), + array(null), + ); + } + + /** + * @dataProvider provideIntitleSearch + * @param string $input + * @param string $intitle_value + * @param string|null $search_value + */ + public function test__construct_whenInputContainsIntitle_setsIntitlePropery($input, $intitle_value, $search_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($intitle_value, $search->getIntitle()); + $this->assertEquals($search_value, $search->getSearch()); + } + + /** + * @return array + */ + public function provideIntitleSearch() { + return array( + array('intitle:word1', 'word1', null), + array('intitle:word1 word2', 'word1', 'word2'), + array('intitle:"word1 word2"', 'word1 word2', null), + array("intitle:'word1 word2'", 'word1 word2', null), + array('word1 intitle:word2', 'word2', 'word1'), + array('word1 intitle:word2 word3', 'word2', 'word1 word3'), + array('word1 intitle:"word2 word3"', 'word2 word3', 'word1'), + array("word1 intitle:'word2 word3'", 'word2 word3', 'word1'), + array('intitle:word1 intitle:word2', 'word1', 'intitle:word2'), + array('intitle: word1 word2', null, 'word1 word2'), + array('intitle:123', '123', null), + array('intitle:"word1 word2" word3"', 'word1 word2', 'word3"'), + array("intitle:'word1 word2' word3'", 'word1 word2', "word3'"), + array('intitle:"word1 word2\' word3"', "word1 word2' word3", null), + array("intitle:'word1 word2\" word3'", 'word1 word2" word3', null), + ); + } + + /** + * @dataProvider provideAuthorSearch + * @param string $input + * @param string $author_value + * @param string|null $search_value + */ + public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($author_value, $search->getAuthor()); + $this->assertEquals($search_value, $search->getSearch()); + } + + /** + * @return array + */ + public function provideAuthorSearch() { + return array( + array('author:word1', 'word1', null), + array('author:word1 word2', 'word1', 'word2'), + array('author:"word1 word2"', 'word1 word2', null), + array("author:'word1 word2'", 'word1 word2', null), + array('word1 author:word2', 'word2', 'word1'), + array('word1 author:word2 word3', 'word2', 'word1 word3'), + array('word1 author:"word2 word3"', 'word2 word3', 'word1'), + array("word1 author:'word2 word3'", 'word2 word3', 'word1'), + array('author:word1 author:word2', 'word1', 'author:word2'), + array('author: word1 word2', null, 'word1 word2'), + array('author:123', '123', null), + array('author:"word1 word2" word3"', 'word1 word2', 'word3"'), + array("author:'word1 word2' word3'", 'word1 word2', "word3'"), + array('author:"word1 word2\' word3"', "word1 word2' word3", null), + array("author:'word1 word2\" word3'", 'word1 word2" word3', null), + ); + } + + /** + * @dataProvider provideInurlSearch + * @param string $input + * @param string $inurl_value + * @param string|null $search_value + */ + public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($inurl_value, $search->getInurl()); + $this->assertEquals($search_value, $search->getSearch()); + } + + /** + * @return array + */ + public function provideInurlSearch() { + return array( + array('inurl:word1', 'word1', null), + array('inurl: word1', null, 'word1'), + array('inurl:123', '123', null), + array('inurl:word1 word2', 'word1', 'word2'), + array('inurl:"word1 word2"', '"word1', 'word2"'), + ); + } + + /** + * @dataProvider provideDateSearch + * @param string $input + * @param string $min_date_value + * @param string $max_date_value + */ + public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($min_date_value, $search->getMinDate()); + $this->assertEquals($max_date_value, $search->getMaxDate()); + } + + /** + * @return array + */ + public function provideDateSearch() { + return array( + array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), + array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'), + array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'), + array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'), + array('date:2007-03-01/', '1172725200', ''), + array('date:/2008-05-11', '', '1210564799'), + ); + } + + /** + * @dataProvider providePubdateSearch + * @param string $input + * @param string $min_pubdate_value + * @param string $max_pubdate_value + */ + public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($min_pubdate_value, $search->getMinPubdate()); + $this->assertEquals($max_pubdate_value, $search->getMaxPubdate()); + } + + /** + * @return array + */ + public function providePubdateSearch() { + return array( + array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'), + array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'), + array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'), + array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'), + array('pubdate:2007-03-01/', '1172725200', ''), + array('pubdate:/2008-05-11', '', '1210564799'), + ); + } + + /** + * @dataProvider provideTagsSearch + * @param string $input + * @param string $tags_value + * @param string|null $search_value + */ + public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($tags_value, $search->getTags()); + $this->assertEquals($search_value, $search->getSearch()); + } + + /** + * @return array + */ + public function provideTagsSearch() { + return array( + array('#word1', array('word1'), null), + array('# word1', null, '# word1'), + array('#123', array('123'), null), + array('#word1 word2', array('word1'), 'word2'), + array('#"word1 word2"', array('"word1'), 'word2"'), + array('#word1 #word2', array('word1', 'word2'), null), + ); + } + + /** + * @dataProvider provideMultipleSearch + * @param string $input + * @param string $author_value + * @param string $min_date_value + * @param string $max_date_value + * @param string $intitle_value + * @param string $inurl_value + * @param string $min_pubdate_value + * @param string $max_pubdate_value + * @param array $tags_value + * @param string|null $search_value + */ + public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value, $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) { + $search = new FreshRSS_Search($input); + $this->assertEquals($author_value, $search->getAuthor()); + $this->assertEquals($min_date_value, $search->getMinDate()); + $this->assertEquals($max_date_value, $search->getMaxDate()); + $this->assertEquals($intitle_value, $search->getIntitle()); + $this->assertEquals($inurl_value, $search->getInurl()); + $this->assertEquals($min_pubdate_value, $search->getMinPubdate()); + $this->assertEquals($max_pubdate_value, $search->getMaxPubdate()); + $this->assertEquals($tags_value, $search->getTags()); + $this->assertEquals($search_value, $search->getSearch()); + } + + public function provideMultipleSearch() { + return array( + array( + 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5', + 'word1', + '1172725200', + '1210564799', + 'word2', + 'word3', + '1172725200', + '1210564799', + array('word4', 'word5'), + null, + ), + array( + 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11', + 'word1', + '1172725200', + '1210564799', + 'word2', + 'word3', + '1172725200', + '1210564799', + array('word4', 'word5'), + 'word6', + ), + array( + 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11', + 'word1', + '1172725200', + '1210564799', + 'word2', + 'word3', + '1172725200', + '1210564799', + array('word4', 'word5'), + 'word6 word7', + ), + ); + } + +} -- cgit v1.2.3 From 115612959302fc51c4720b7a954bf8cbe1b14f3b Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Sat, 21 Feb 2015 09:08:06 -0500 Subject: Use the search object to get values in the search It is now possible to combine multiple keywords to do a search. The separation of concern is better now since the search extraction is not done in the DAO anymore. At the moment, a multiple keyword search is stored as this. It could be nice to have it rendered differently in the search page to make it more readable. At the moment, there is a problem with search enclosed by ". Same search works well when enclosed by '. --- app/Controllers/indexController.php | 2 +- app/Models/Context.php | 2 +- app/Models/EntryDAO.php | 84 +++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 48 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index c53d3223e..c1aaca53f 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -173,7 +173,7 @@ class FreshRSS_index_Controller extends Minz_ActionController { FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ; } - FreshRSS_Context::$search = Minz_Request::param('search', ''); + FreshRSS_Context::$search = new FreshRSS_Search(Minz_Request::param('search', '')); FreshRSS_Context::$order = Minz_Request::param( 'order', FreshRSS_Context::$user_conf->sort_order ); diff --git a/app/Models/Context.php b/app/Models/Context.php index f00bb1e97..dbdbfaa69 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -30,7 +30,7 @@ class FreshRSS_Context { public static $state = 0; public static $order = 'DESC'; public static $number = 0; - public static $search = ''; + public static $search; public static $first_id = ''; public static $next_id = ''; public static $id_max = ''; diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index 61beeea13..0cf4e1367 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -441,54 +441,46 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo { $where .= 'AND e1.id >= ' . $date_min . '000000 '; } $search = ''; - if ($filter !== '') { - require_once(LIB_PATH . '/lib_date.php'); - $filter = trim($filter); - $filter = addcslashes($filter, '\\%_'); - $terms = array_unique(explode(' ', $filter)); - //sort($terms); //Put #tags first //TODO: Put the cheapest filters first - foreach ($terms as $word) { - $word = trim($word); - if (stripos($word, 'intitle:') === 0) { - $word = substr($word, strlen('intitle:')); - $search .= 'AND e1.title LIKE ? '; - $values[] = '%' . $word .'%'; - } elseif (stripos($word, 'inurl:') === 0) { - $word = substr($word, strlen('inurl:')); - $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? '; - $values[] = '%' . $word .'%'; - } elseif (stripos($word, 'author:') === 0) { - $word = substr($word, strlen('author:')); - $search .= 'AND e1.author LIKE ? '; - $values[] = '%' . $word .'%'; - } elseif (stripos($word, 'date:') === 0) { - $word = substr($word, strlen('date:')); - list($minDate, $maxDate) = parseDateInterval($word); - if ($minDate) { - $search .= 'AND e1.id >= ' . $minDate . '000000 '; - } - if ($maxDate) { - $search .= 'AND e1.id <= ' . $maxDate . '000000 '; - } - } elseif (stripos($word, 'pubdate:') === 0) { - $word = substr($word, strlen('pubdate:')); - list($minDate, $maxDate) = parseDateInterval($word); - if ($minDate) { - $search .= 'AND e1.date >= ' . $minDate . ' '; - } - if ($maxDate) { - $search .= 'AND e1.date <= ' . $maxDate . ' '; - } - } else { - if ($word[0] === '#' && isset($word[1])) { - $search .= 'AND e1.tags LIKE ? '; - $values[] = '%' . $word .'%'; - } else { - $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? '; - $values[] = '%' . $word .'%'; - } + if ($filter instanceof FreshRSS_Search) { + if ($filter->getIntitle()) { + $search .= 'AND e1.title LIKE ? '; + $values[] = "%{$filter->getIntitle()}%"; + } + if ($filter->getInurl()) { + $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? '; + $values[] = "%{$filter->getInurl()}%"; + } + if ($filter->getAuthor()) { + $search .= 'AND e1.author LIKE ? '; + $values[] = "%{$filter->getAuthor()}%"; + } + if ($filter->getMinDate()) { + $search .= 'AND e1.id >= ? '; + $values[] = "{$filter->getMinDate()}000000"; + } + if ($filter->getMaxDate()) { + $search .= 'AND e1.id <= ?'; + $values[] = "{$filter->getMaxDate()}000000"; + } + if ($filter->getMinPubdate()) { + $search .= 'AND e1.date >= ? '; + $values[] = $filter->getMinPubdate(); + } + if ($filter->getMaxPubdate()) { + $search .= 'AND e1.date <= ? '; + $values[] = $filter->getMaxPubdate(); + } + if ($filter->getTags()) { + $tags = $filter->getTags(); + foreach ($tags as $tag) { + $search .= 'AND e1.tags LIKE ? '; + $values[] = "%{$tag}%"; } } + if ($filter->getSearch()) { + $search .= 'AND ' . $this->sqlconcat('e1.title', $this->isCompressed() ? 'UNCOMPRESS(content_bin)' : 'content') . ' LIKE ? '; + $values[] = "%{$filter->getSearch()}%"; + } } return array($values, -- cgit v1.2.3 From 02a3cb4652d4c87ec3202d39f6cd8a240a1a7373 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 24 Oct 2015 23:47:13 +0200 Subject: Config allow robots https://github.com/FreshRSS/FreshRSS/issues/938 --- app/Models/Context.php | 13 +++++++++++-- app/layout/layout.phtml | 4 ++++ data/config.default.php | 3 +++ index.html | 2 +- p/index.html | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) (limited to 'app/Models/Context.php') diff --git a/app/Models/Context.php b/app/Models/Context.php index dbdbfaa69..d8dd81e88 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -10,6 +10,7 @@ class FreshRSS_Context { public static $categories = array(); public static $name = ''; + public static $description = ''; public static $total_unread = 0; public static $total_starred = array( @@ -93,6 +94,13 @@ class FreshRSS_Context { } } + /** + * Return true iif the current requests target a feed and not a category or all articles. + */ + public static function isFeed() { + return self::$current_get['feed'] != false; + } + /** * Return true if $get parameter correspond to the $current_get attribute. */ @@ -146,8 +154,8 @@ class FreshRSS_Context { self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE; break; case 'f': - // We try to find the corresponding feed. - $feed = FreshRSS_CategoryDAO::findFeed(self::$categories, $id); + // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description + $feed = FreshRSS_Context::$system_conf->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id); if ($feed === null) { $feedDAO = FreshRSS_Factory::createFeedDao(); $feed = $feedDAO->searchById($id); @@ -160,6 +168,7 @@ class FreshRSS_Context { self::$current_get['feed'] = $id; self::$current_get['category'] = $feed->category(); self::$name = $feed->name(); + self::$description = $feed->description(); self::$get_unread = $feed->nbNotRead(); break; case 'c': diff --git a/app/layout/layout.phtml b/app/layout/layout.phtml index 083ffd4b3..d7e9d115b 100644 --- a/app/layout/layout.phtml +++ b/app/layout/layout.phtml @@ -36,7 +36,11 @@ +allow_robots) { ?> + + + partial('header'); ?> diff --git a/data/config.default.php b/data/config.default.php index a7a29b12c..8eccee8a5 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -61,6 +61,9 @@ return array( # /!\ It should NOT be enabled if base_url is not reachable by an external server. 'pubsubhubbub_enabled' => false, + # Allow or not Web robots (e.g. search engines) in HTML headers. + 'allow_robots' => false, + 'limits' => array( # Duration in seconds of the SimplePie cache, diff --git a/index.html b/index.html index 6ac025960..5414211a1 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ Redirection - + diff --git a/p/index.html b/p/index.html index 260f437bd..ef5cb87ce 100644 --- a/p/index.html +++ b/p/index.html @@ -8,7 +8,7 @@ - +