Browse Source

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 '.
Alexis Degrugillier 11 years ago
parent
commit
1156129593
3 changed files with 40 additions and 48 deletions
  1. 1 1
      app/Controllers/indexController.php
  2. 1 1
      app/Models/Context.php
  3. 38 46
      app/Models/EntryDAO.php

+ 1 - 1
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
 		);

+ 1 - 1
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 = '';

+ 38 - 46
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,