From 1b48999c32bc54c39ae10cb3107127678160ee93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= <grosse@cosmocode.de>
Date: Mon, 26 Mar 2018 14:27:30 +0200
Subject: [PATCH] refactor(search): move filtering by time into _ft_page*

This is the more appropriate place for that functionality, because now
it happens inside the default function for the respective pagesearch and
pagelookup events and can be properly handled by plugins.
---
 inc/Ui/Search.php | 38 ++------------------------------------
 inc/fulltext.php  | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/inc/Ui/Search.php b/inc/Ui/Search.php
index a746a6fd6..72b944a26 100644
--- a/inc/Ui/Search.php
+++ b/inc/Ui/Search.php
@@ -31,45 +31,11 @@ class Search extends Ui
      */
     public function execute()
     {
-        $this->pageLookupResults = $this->filterResultsByTime(
-            ft_pageLookup($this->query, true, useHeading('navigation'))
-        );
-        $this->fullTextResults = $this->filterResultsByTime(
-            ft_pageSearch($this->query, $highlight)
-        );
+        $this->pageLookupResults = ft_pageLookup($this->query, true, useHeading('navigation'));
+        $this->fullTextResults = ft_pageSearch($this->query, $highlight);
         $this->highlight = $highlight;
     }
 
-    /**
-     * @param array $results search results in the form pageid => value
-     *
-     * @return array
-     */
-    protected function filterResultsByTime(array $results) {
-        global $INPUT;
-        if ($INPUT->has('after') || $INPUT->has('before')) {
-            $after = $INPUT->str('after');
-            $after = is_int($after) ? $after : strtotime($after);
-
-            $before = $INPUT->str('before');
-            $before = is_int($before) ? $before : strtotime($before);
-
-            // todo: should we filter $this->pageLookupResults as well?
-            foreach ($results as $id => $value) {
-                $mTime = filemtime(wikiFN($id));
-                if ($after && $after > $mTime) {
-                    unset($results[$id]);
-                    continue;
-                }
-                if ($before && $before < $mTime) {
-                    unset($results[$id]);
-                }
-            }
-        }
-
-        return $results;
-    }
-
     /**
      * display the search result
      *
diff --git a/inc/fulltext.php b/inc/fulltext.php
index 9034d229b..987010c93 100644
--- a/inc/fulltext.php
+++ b/inc/fulltext.php
@@ -134,6 +134,7 @@ function _ft_pageSearch(&$data) {
         }
     }
 
+    $docs = _ft_filterResultsByTime($docs);
     // sort docs by count
     arsort($docs);
 
@@ -281,10 +282,42 @@ function _ft_pageLookup(&$data){
         }
     }
 
+    $pages = _ft_filterResultsByTime($pages);
+
     uksort($pages,'ft_pagesorter');
     return $pages;
 }
 
+
+/**
+ * @param array $results search results in the form pageid => value
+ *
+ * @return array
+ */
+function _ft_filterResultsByTime(array $results) {
+    global $INPUT;
+    if ($INPUT->has('after') || $INPUT->has('before')) {
+        $after = $INPUT->str('after');
+        $after = is_int($after) ? $after : strtotime($after);
+
+        $before = $INPUT->str('before');
+        $before = is_int($before) ? $before : strtotime($before);
+
+        foreach ($results as $id => $value) {
+            $mTime = filemtime(wikiFN($id));
+            if ($after && $after > $mTime) {
+                unset($results[$id]);
+                continue;
+            }
+            if ($before && $before < $mTime) {
+                unset($results[$id]);
+            }
+        }
+    }
+
+    return $results;
+}
+
 /**
  * Tiny helper function for comparing the searched title with the title
  * from the search index. This function is a wrapper around stripos with
-- 
GitLab