aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <github@ainw.org> 2015-02-11 20:41:00 -0500
committerGravatar Alexis Degrugillier <github@ainw.org> 2015-02-11 21:49:21 -0500
commitf30ded2f863ed4a33ae8194d6cf00eaa877c9b6a (patch)
tree8c6e4ea14dad4f024c15e00300ed2b3e9cd4dd48 /app/Models
parent4bb8548eccf22b40c25352fe27c66f1f8039ebcd (diff)
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.
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/Context.php148
-rw-r--r--app/Models/Search.php189
2 files changed, 189 insertions, 148 deletions
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<delim>[\'"])(?P<search>.*)(?P=delim)/U', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('intitle' => $matches['search']),
- );
- }
- if (preg_match('/intitle:(?P<search>\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<delim>[\'"])(?P<search>.*)(?P=delim)/U', $search, $matches)) {
- return array(
- 'string' => str_replace($matches[0], '', $search),
- 'search' => array('author' => $matches['search']),
- );
- }
- if (preg_match('/author:(?P<search>\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<search>[^\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<search>[^\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<search>[^\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 @@
+<?php
+
+/**
+ * Contains a search from the search form.
+ *
+ * It allows to extract meaningful bits of the search and store them in a
+ * convenient object
+ *
+ * @author alexis
+ */
+class FreshRSS_Search {
+
+ // This contains the user input string
+ private $raw_input;
+ // The following properties are extracted from the raw input
+ private $intitle;
+ private $min_date;
+ private $max_date;
+ private $min_pubdate;
+ private $max_pubdate;
+ private $inurl;
+ private $author;
+ private $tags;
+ private $search;
+
+ public function __construct($input) {
+ if (strcmp($input, '') == 0) {
+ return;
+ }
+ $this->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<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+ $this->intitle = $matches['search'];
+ $input = str_replace($matches[0], '', $input);
+ } else if (preg_match('/intitle:(?P<search>\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<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
+ $this->author = $matches['search'];
+ $input = str_replace($matches[0], '', $input);
+ } else if (preg_match('/author:(?P<search>\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<search>[^\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<search>[^\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<search>[^\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<search>[^\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);
+ }
+
+}