aboutsummaryrefslogtreecommitdiff
path: root/app/Models/BooleanSearch.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2018-03-14 17:20:41 +0100
committerGravatar GitHub <noreply@github.com> 2018-03-14 17:20:41 +0100
commit84d891f8cf43e4bb097a8b05a85dfeb4c48bd215 (patch)
treeea2ebffb204c3c31a3cb77bd93351d16fcb5a473 /app/Models/BooleanSearch.php
parent881ed44005ec6212f21c0973b772a1652ef1b42a (diff)
Light Boolean search implementation (#1828)
* Light Boolean search implementation "Hello intitle:World OR date:P1D example" https://github.com/FreshRSS/FreshRSS/issues/879 * Doc Boolean search * Doc typos
Diffstat (limited to 'app/Models/BooleanSearch.php')
-rw-r--r--app/Models/BooleanSearch.php55
1 files changed, 55 insertions, 0 deletions
diff --git a/app/Models/BooleanSearch.php b/app/Models/BooleanSearch.php
new file mode 100644
index 000000000..6e016f7e9
--- /dev/null
+++ b/app/Models/BooleanSearch.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * Contains Boolean search from the search form.
+ */
+class FreshRSS_BooleanSearch {
+
+ private $raw_input = '';
+ private $searches = array();
+
+ public function __construct($input) {
+ $input = trim($input);
+ if ($input == '') {
+ return;
+ }
+ $this->raw_input = $input;
+
+ $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
+ $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
+
+ $segment = '';
+ $ns = count($splits);
+ for ($i = 0; $i < $ns; $i++) {
+ $segment = $segment . $splits[$i];
+ if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
+ $segment = '';
+ } else {
+ $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
+ if ($quotes % 2 === 0) {
+ $segment = trim($segment);
+ if ($segment != '') {
+ $this->searches[] = new FreshRSS_Search($segment);
+ }
+ $segment = '';
+ }
+ }
+ }
+ $segment = trim($segment);
+ if ($segment != '') {
+ $this->searches[] = new FreshRSS_Search($segment);
+ }
+ }
+
+ public function searches() {
+ return $this->searches;
+ }
+
+ public function __toString() {
+ return $this->getRawInput();
+ }
+
+ public function getRawInput() {
+ return $this->raw_input;
+ }
+}