瀏覽代碼

Improve search: intitle, author, inurl

Allow multiple values of intitle: , author:, inurl:

Note: Tests for UserQueryTest are broken due to
https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-4.0.0#backwards-compatibility-issues
Alexandre Alapetite 9 年之前
父節點
當前提交
d9c0d25b85

+ 13 - 8
app/Models/EntryDAO.php

@@ -631,16 +631,22 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 		}
 		if ($filter) {
 			if ($filter->getIntitle()) {
-				$search .= 'AND ' . $alias . 'title LIKE ? ';
-				$values[] = "%{$filter->getIntitle()}%";
+				foreach ($filter->getIntitle() as $title) {
+					$search .= 'AND ' . $alias . 'title LIKE ? ';
+					$values[] = "%{$title}%";
+				}
 			}
 			if ($filter->getInurl()) {
-				$search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
-				$values[] = "%{$filter->getInurl()}%";
+				foreach ($filter->getInurl() as $url) {
+					$search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
+					$values[] = "%{$url}%";
+				}
 			}
 			if ($filter->getAuthor()) {
-				$search .= 'AND ' . $alias . 'author LIKE ? ';
-				$values[] = "%{$filter->getAuthor()}%";
+				foreach ($filter->getAuthor() as $author) {
+					$search .= 'AND ' . $alias . 'author LIKE ? ';
+					$values[] = "%{$author}%";
+				}
 			}
 			if ($filter->getMinDate()) {
 				$search .= 'AND ' . $alias . 'id >= ? ';
@@ -659,8 +665,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
 				$values[] = $filter->getMaxPubdate();
 			}
 			if ($filter->getTags()) {
-				$tags = $filter->getTags();
-				foreach ($tags as $tag) {
+				foreach ($filter->getTags() as $tag) {
 					$search .= 'AND ' . $alias . 'tags LIKE ? ';
 					$values[] = "%{$tag}%";
 				}

+ 37 - 23
app/Models/Search.php

@@ -81,6 +81,10 @@ class FreshRSS_Search {
 		return $this->search;
 	}
 
+	private static function removeEmptyValues($anArray) {
+		return is_array($anArray) ? array_filter($anArray, function($value) { return $value !== ''; }) : array();
+	}
+
 	/**
 	 * Parse the search string to find intitle keyword and the search related
 	 * to it.
@@ -90,14 +94,15 @@ class FreshRSS_Search {
 	 * @return string
 	 */
 	private function parseIntitleSearch($input) {
-		if (preg_match('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+		if (preg_match_all('/intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
 			$this->intitle = $matches['search'];
-			return str_replace($matches[0], '', $input);
+			$input = str_replace($matches[0], '', $input);
 		}
-		if (preg_match('/intitle:(?P<search>\w*)/', $input, $matches)) {
-			$this->intitle = $matches['search'];
-			return str_replace($matches[0], '', $input);
+		if (preg_match_all('/intitle:(?P<search>\w*)/', $input, $matches)) {
+			$this->intitle = array_merge($this->intitle ? $this->intitle : array(), $matches['search']);
+			$input = str_replace($matches[0], '', $input);
 		}
+		$this->intitle = self::removeEmptyValues($this->intitle);
 		return $input;
 	}
 
@@ -112,30 +117,32 @@ class FreshRSS_Search {
 	 * @return string
 	 */
 	private function parseAuthorSearch($input) {
-		if (preg_match('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+		if (preg_match_all('/author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
 			$this->author = $matches['search'];
-			return str_replace($matches[0], '', $input);
+			$input = str_replace($matches[0], '', $input);
 		}
-		if (preg_match('/author:(?P<search>\w*)/', $input, $matches)) {
-			$this->author = $matches['search'];
-			return str_replace($matches[0], '', $input);
+		if (preg_match_all('/author:(?P<search>\w*)/', $input, $matches)) {
+			$this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
+			$input = str_replace($matches[0], '', $input);
 		}
+		$this->author = self::removeEmptyValues($this->author);
 		return $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.
+	 * The search is the first word following the keyword.
 	 *
 	 * @param string $input
 	 * @return string
 	 */
 	private function parseInurlSearch($input) {
-		if (preg_match('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
+		if (preg_match_all('/inurl:(?P<search>[^\s]*)/', $input, $matches)) {
 			$this->inurl = $matches['search'];
-			return str_replace($matches[0], '', $input);
+			$input = str_replace($matches[0], '', $input);
 		}
+		$this->inurl = self::removeEmptyValues($this->inurl);
 		return $input;
 	}
 
@@ -148,9 +155,12 @@ class FreshRSS_Search {
 	 * @return string
 	 */
 	private function parseDateSearch($input) {
-		if (preg_match('/date:(?P<search>[^\s]*)/', $input, $matches)) {
-			list($this->min_date, $this->max_date) = parseDateInterval($matches['search']);
-			return str_replace($matches[0], '', $input);
+		if (preg_match_all('/date:(?P<search>[^\s]*)/', $input, $matches)) {
+			$input = str_replace($matches[0], '', $input);
+			$dates = self::removeEmptyValues($matches['search']);
+			if (!empty($dates[0])) {
+				list($this->min_date, $this->max_date) = parseDateInterval($dates[0]);
+			}
 		}
 		return $input;
 	}
@@ -164,9 +174,12 @@ class FreshRSS_Search {
 	 * @return string
 	 */
 	private function parsePubdateSearch($input) {
-		if (preg_match('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
-			list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']);
-			return str_replace($matches[0], '', $input);
+		if (preg_match_all('/pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
+			$input = str_replace($matches[0], '', $input);
+			$dates = self::removeEmptyValues($matches['search']);
+			if (!empty($dates[0])) {
+				list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($dates[0]);
+			}
 		}
 		return $input;
 	}
@@ -182,8 +195,9 @@ class FreshRSS_Search {
 	private function parseTagsSeach($input) {
 		if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
 			$this->tags = $matches['search'];
-			return str_replace($matches[0], '', $input);
+			$input = str_replace($matches[0], '', $input);
 		}
+		$this->tags = self::removeEmptyValues($this->tags);
 		return $input;
 	}
 
@@ -196,7 +210,7 @@ class FreshRSS_Search {
 	 * @return string
 	 */
 	private function parseSearch($input) {
-		$input = $this->cleanSearch($input);
+		$input = self::cleanSearch($input);
 		if (strcmp($input, '') == 0) {
 			return;
 		}
@@ -204,7 +218,7 @@ class FreshRSS_Search {
 			$this->search = $matches['search'];
 			$input = str_replace($matches[0], '', $input);
 		}
-		$input = $this->cleanSearch($input);
+		$input = self::cleanSearch($input);
 		if (strcmp($input, '') == 0) {
 			return;
 		}
@@ -221,7 +235,7 @@ class FreshRSS_Search {
 	 * @param string $input
 	 * @return string
 	 */
-	private function cleanSearch($input) {
+	private static function cleanSearch($input) {
 		$input = preg_replace('/\s+/', ' ', $input);
 		return trim($input);
 	}

+ 7 - 0
tests/README.md

@@ -0,0 +1,7 @@
+# FreshRSS tests
+
+```sh
+cd ./tests/
+wget https://phar.phpunit.de/phpunit.phar
+php phpunit.phar --bootstrap bootstrap.php
+```

+ 1 - 1
tests/app/Models/CategoryTest.php

@@ -1,6 +1,6 @@
 <?php
 
-class FreshRSS_CategoryTest extends \PHPUnit_Framework_TestCase {
+class FreshRSS_CategoryTest extends PHPUnit\Framework\TestCase {
 
 	public function test__construct_whenNoParameters_createsObjectWithDefaultValues() {
 		$category = new FreshRSS_Category();

+ 0 - 5
tests/app/Models/ContextTest.php

@@ -1,5 +0,0 @@
-<?php
-
-class ContextTest extends \PHPUnit_Framework_TestCase {
-
-}

+ 75 - 74
tests/app/Models/SearchTest.php

@@ -2,7 +2,7 @@
 
 require_once(LIB_PATH . '/lib_date.php');
 
-class SearchTest extends \PHPUnit_Framework_TestCase {
+class SearchTest extends PHPUnit\Framework\TestCase {
 
 	/**
 	 * @dataProvider provideEmptyInput
@@ -50,22 +50,22 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 	 */
 	public function provideIntitleSearch() {
 		return array(
-		    array('intitle:word1', 'word1', null),
-		    array('intitle:word1 word2', 'word1', array('word2')),
-		    array('intitle:"word1 word2"', 'word1 word2', null),
-		    array("intitle:'word1 word2'", 'word1 word2', null),
-		    array('word1 intitle:word2', 'word2', array('word1')),
-		    array('word1 intitle:word2 word3', 'word2', array('word1', 'word3')),
-		    array('word1 intitle:"word2 word3"', 'word2 word3', array('word1')),
-		    array("word1 intitle:'word2 word3'", 'word2 word3', array('word1')),
-		    array('intitle:word1 intitle:word2', 'word1', array('intitle:word2')),
-		    array('intitle: word1 word2', null, array('word1', 'word2')),
-		    array('intitle:123', '123', null),
-		    array('intitle:"word1 word2" word3"', 'word1 word2', array('word3"')),
-		    array("intitle:'word1 word2' word3'", 'word1 word2', array("word3'")),
-		    array('intitle:"word1 word2\' word3"', "word1 word2' word3", null),
-		    array("intitle:'word1 word2\" word3'", 'word1 word2" word3', null),
-		    array("intitle:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
+		    array('intitle:word1', array('word1'), null),
+		    array('intitle:word1 word2', array('word1'), array('word2')),
+		    array('intitle:"word1 word2"', array('word1 word2'), null),
+		    array("intitle:'word1 word2'", array('word1 word2'), null),
+		    array('word1 intitle:word2', array('word2'), array('word1')),
+		    array('word1 intitle:word2 word3', array('word2'), array('word1', 'word3')),
+		    array('word1 intitle:"word2 word3"', array('word2 word3'), array('word1')),
+		    array("word1 intitle:'word2 word3'", array('word2 word3'), array('word1')),
+		    array('intitle:word1 intitle:word2', array('word1', 'word2'), null),
+		    array('intitle: word1 word2', array(), array('word1', 'word2')),
+		    array('intitle:123', array('123'), null),
+		    array('intitle:"word1 word2" word3"', array('word1 word2'), array('word3"')),
+		    array("intitle:'word1 word2' word3'", array('word1 word2'), array("word3'")),
+		    array('intitle:"word1 word2\' word3"', array("word1 word2' word3"), null),
+		    array("intitle:'word1 word2\" word3'", array('word1 word2" word3'), null),
+		    array("intitle:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
 		);
 	}
 
@@ -86,22 +86,22 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 	 */
 	public function provideAuthorSearch() {
 		return array(
-		    array('author:word1', 'word1', null),
-		    array('author:word1 word2', 'word1', array('word2')),
-		    array('author:"word1 word2"', 'word1 word2', null),
-		    array("author:'word1 word2'", 'word1 word2', null),
-		    array('word1 author:word2', 'word2', array('word1')),
-		    array('word1 author:word2 word3', 'word2', array('word1', 'word3')),
-		    array('word1 author:"word2 word3"', 'word2 word3', array('word1')),
-		    array("word1 author:'word2 word3'", 'word2 word3', array('word1')),
-		    array('author:word1 author:word2', 'word1', array('author:word2')),
-		    array('author: word1 word2', null, array('word1', 'word2')),
-		    array('author:123', '123', null),
-		    array('author:"word1 word2" word3"', 'word1 word2', array('word3"')),
-		    array("author:'word1 word2' word3'", 'word1 word2', array("word3'")),
-		    array('author:"word1 word2\' word3"', "word1 word2' word3", null),
-		    array("author:'word1 word2\" word3'", 'word1 word2" word3', null),
-		    array("author:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
+		    array('author:word1', array('word1'), null),
+		    array('author:word1 word2', array('word1'), array('word2')),
+		    array('author:"word1 word2"', array('word1 word2'), null),
+		    array("author:'word1 word2'", array('word1 word2'), null),
+		    array('word1 author:word2', array('word2'), array('word1')),
+		    array('word1 author:word2 word3', array('word2'), array('word1', 'word3')),
+		    array('word1 author:"word2 word3"', array('word2 word3'), array('word1')),
+		    array("word1 author:'word2 word3'", array('word2 word3'), array('word1')),
+		    array('author:word1 author:word2', array('word1', 'word2'), null),
+		    array('author: word1 word2', array(), array('word1', 'word2')),
+		    array('author:123', array('123'), null),
+		    array('author:"word1 word2" word3"', array('word1 word2'), array('word3"')),
+		    array("author:'word1 word2' word3'", array('word1 word2'), array("word3'")),
+		    array('author:"word1 word2\' word3"', array("word1 word2' word3"), null),
+		    array("author:'word1 word2\" word3'", array('word1 word2" word3'), null),
+		    array("author:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
 		);
 	}
 
@@ -122,12 +122,13 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 	 */
 	public function provideInurlSearch() {
 		return array(
-		    array('inurl:word1', 'word1', null),
-		    array('inurl: word1', null, array('word1')),
-		    array('inurl:123', '123', null),
-		    array('inurl:word1 word2', 'word1', array('word2')),
-		    array('inurl:"word1 word2"', '"word1', array('word2"')),
-		    array("inurl:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
+		    array('inurl:word1', array('word1'), null),
+		    array('inurl: word1', array(), array('word1')),
+		    array('inurl:123', array('123'), null),
+		    array('inurl:word1 word2', array('word1'), array('word2')),
+		    array('inurl:"word1 word2"', array('"word1'), array('word2"')),
+		    array('inurl:word1 word2 inurl:word3', array('word1', 'word3'), array('word2')),
+		    array("inurl:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
 		);
 	}
 
@@ -151,9 +152,9 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 		    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'),
+		    array('date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
+		    array('date:2007-03-01/', strtotime('2007-03-01'), ''),
+		    array('date:/2008-05-11', '', strtotime('2008-05-12') - 1),
 		);
 	}
 
@@ -177,9 +178,9 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 		    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'),
+		    array('pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
+		    array('pubdate:2007-03-01/', strtotime('2007-03-01'), ''),
+		    array('pubdate:/2008-05-11', '', strtotime('2008-05-12') - 1),
 		);
 	}
 
@@ -201,7 +202,7 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 	public function provideTagsSearch() {
 		return array(
 		    array('#word1', array('word1'), null),
-		    array('# word1', null, array('#', 'word1')),
+		    array('# word1', array(), array('#', 'word1')),
 		    array('#123', array('123'), null),
 		    array('#word1 word2', array('word1'), array('word2')),
 		    array('#"word1 word2"', array('"word1'), array('word2"')),
@@ -241,49 +242,49 @@ class SearchTest extends \PHPUnit_Framework_TestCase {
 		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('word1'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
+			array('word2'),
+			array('word3'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
 			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('word1'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
+			array('word2'),
+			array('word3'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
 			array('word4', 'word5'),
 			array('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('word1'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
+			array('word2'),
+			array('word3'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
 			array('word4', 'word5'),
 			array('word6', 'word7'),
 		    ),
 		    array(
 			'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 "word7 word8" date:2007-03-01/2008-05-11',
-			'word1',
-			'1172725200',
-			'1210564799',
-			'word2',
-			'word3',
-			'1172725200',
-			'1210564799',
+			array('word1'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
+			array('word2'),
+			array('word3'),
+			strtotime('2007-03-01'),
+			strtotime('2008-05-12') - 1,
 			array('word4', 'word5'),
 			array('word7 word8', 'word6'),
 		    ),

+ 1 - 1
tests/app/Models/UserQueryTest.php

@@ -3,7 +3,7 @@
 /**
  * Description of UserQueryTest
  */
-class UserQueryTest extends \PHPUnit_Framework_TestCase {
+class UserQueryTest extends PHPUnit\Framework\TestCase {
 
 	public function test__construct_whenAllQuery_storesAllParameters() {
 		$query = array('get' => 'a');