From 9b8ddc269872f6bc42f95c3b3957df5028338ddb Mon Sep 17 00:00:00 2001
From: rubencm <rubencm@gmail.com>
Date: Sun, 30 Aug 2020 22:31:17 +0000
Subject: [PATCH] [ticket/15540] Move create_index and add delete_index to base

PHPBB3-15540
---
 phpBB/phpbb/search/backend/base.php           | 120 ++++++++++++++++++
 .../phpbb/search/backend/fulltext_native.php  |  74 -----------
 2 files changed, 120 insertions(+), 74 deletions(-)

diff --git a/phpBB/phpbb/search/backend/base.php b/phpBB/phpbb/search/backend/base.php
index 281e4da3a8..019226a8cc 100644
--- a/phpBB/phpbb/search/backend/base.php
+++ b/phpBB/phpbb/search/backend/base.php
@@ -283,4 +283,124 @@ abstract class base
 			WHERE search_time < ' . (time() - (int) $config['search_store_results']);
 		$db->sql_query($sql);
 	}
+
+	/**
+	 * {@inheritdoc}
+	 */
+	public function create_index($acp_module, $u_action)
+	{
+		$sql = 'SELECT forum_id, enable_indexing
+			FROM ' . FORUMS_TABLE;
+		$result = $this->db->sql_query($sql, 3600);
+
+		while ($row = $this->db->sql_fetchrow($result))
+		{
+			$forums[$row['forum_id']] = (bool) $row['enable_indexing'];
+		}
+		$this->db->sql_freeresult($result);
+
+		$starttime = microtime(true);
+		$row_count = 0;
+
+		$post_counter = &$acp_module->state[2];
+		while (still_on_time() && $post_counter <= $acp_module->max_post_id)
+		{
+			$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
+				FROM ' . POSTS_TABLE . '
+				WHERE post_id >= ' . (int) ($post_counter + 1) . '
+					AND post_id <= ' . (int) ($post_counter + $acp_module->batch_size);
+			$result = $this->db->sql_query($sql);
+
+			$buffer = $this->db->sql_buffer_nested_transactions();
+
+			if ($buffer)
+			{
+				$rows = $this->db->sql_fetchrowset($result);
+				$rows[] = false; // indicate end of array for while loop below
+
+				$this->db->sql_freeresult($result);
+			}
+
+			$i = 0;
+			while ($row = ($buffer ? $rows[$i++] : $this->db->sql_fetchrow($result)))
+			{
+				// Indexing enabled for this forum
+				if (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']])
+				{
+					$this->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
+				}
+				$row_count++;
+			}
+			if (!$buffer)
+			{
+				$this->db->sql_freeresult($result);
+			}
+
+			$post_counter += $acp_module->batch_size;
+		}
+
+		// save the current state
+		$acp_module->save_state();
+
+		// pretend the number of posts was as big as the number of ids we indexed so far
+		// just an estimation as it includes deleted posts
+		$num_posts = $this->config['num_posts'];
+		$this->config['num_posts'] = min($this->config['num_posts'], $post_counter);
+		$this->tidy();
+		$this->config['num_posts'] = $num_posts;
+
+		if ($post_counter <= $acp_module->max_post_id)
+		{
+			$totaltime = microtime(true) - $starttime;
+			$rows_per_second = $row_count / $totaltime;
+			meta_refresh(1, $u_action);
+			trigger_error($this->user->lang('SEARCH_INDEX_CREATE_REDIRECT', (int) $row_count, $post_counter) . $this->user->lang('SEARCH_INDEX_CREATE_REDIRECT_RATE', $rows_per_second));
+		}
+	}
+
+	/**
+	 * {@inheritdoc}
+	 */
+	public function delete_index($acp_module, $u_action)
+	{
+		$starttime = microtime(true);
+		$row_count = 0;
+		$post_counter = &$acp_module->state[2];
+		while (still_on_time() && $post_counter <= $acp_module->max_post_id)
+		{
+			$sql = 'SELECT post_id, poster_id, forum_id
+								FROM ' . POSTS_TABLE . '
+								WHERE post_id >= ' . (int) ($post_counter + 1) . '
+									AND post_id <= ' . (int) ($post_counter + $acp_module->batch_size);
+			$result = $this->db->sql_query($sql);
+
+			$ids = $posters = $forum_ids = array();
+			while ($row = $this->db->sql_fetchrow($result))
+			{
+				$ids[] = $row['post_id'];
+				$posters[] = $row['poster_id'];
+				$forum_ids[] = $row['forum_id'];
+			}
+			$db->sql_freeresult($result);
+			$row_count += count($ids);
+
+			if (count($ids))
+			{
+				$this->index_remove($ids, $posters, $forum_ids);
+			}
+
+			$post_counter += $acp_module->batch_size;
+		}
+
+		// save the current state
+		$acp_module->save_state();
+
+		if ($post_counter <= $acp_module->max_post_id)
+		{
+			$totaltime = microtime(true) - $starttime;
+			$rows_per_second = $row_count / $totaltime;
+			meta_refresh(1, append_sid($u_action));
+			trigger_error($this->user->lang('SEARCH_INDEX_DELETE_REDIRECT', (int) $row_count, $post_counter) . $this->user->lang('SEARCH_INDEX_DELETE_REDIRECT_RATE', $rows_per_second));
+		}
+	}
 }
diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php
index 538498f3b6..6bf3224fed 100644
--- a/phpBB/phpbb/search/backend/fulltext_native.php
+++ b/phpBB/phpbb/search/backend/fulltext_native.php
@@ -1670,80 +1670,6 @@ class fulltext_native extends base implements search_backend_interface
 		$this->config->set('search_last_gc', time(), false);
 	}
 
-	/**
-	 * {@inheritdoc}
-	 */
-	public function create_index($acp_module, $u_action)
-	{
-		$sql = 'SELECT forum_id, enable_indexing
-			FROM ' . FORUMS_TABLE;
-		$result = $this->db->sql_query($sql, 3600);
-
-		while ($row = $this->db->sql_fetchrow($result))
-		{
-			$forums[$row['forum_id']] = (bool) $row['enable_indexing'];
-		}
-		$this->db->sql_freeresult($result);
-
-		$starttime = microtime(true);
-		$row_count = 0;
-
-		$post_counter = &$acp_module->state[2];
-		while (still_on_time() && $post_counter <= $acp_module->max_post_id)
-		{
-			$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
-				FROM ' . POSTS_TABLE . '
-				WHERE post_id >= ' . (int) ($post_counter + 1) . '
-					AND post_id <= ' . (int) ($post_counter + $acp_module->batch_size);
-			$result = $this->db->sql_query($sql);
-
-			$buffer = $this->db->sql_buffer_nested_transactions();
-
-			if ($buffer)
-			{
-				$rows = $this->db->sql_fetchrowset($result);
-				$rows[] = false; // indicate end of array for while loop below
-
-				$this->db->sql_freeresult($result);
-			}
-
-			$i = 0;
-			while ($row = ($buffer ? $rows[$i++] : $this->db->sql_fetchrow($result)))
-			{
-				// Indexing enabled for this forum
-				if (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']])
-				{
-					$this->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
-				}
-				$row_count++;
-			}
-			if (!$buffer)
-			{
-				$this->db->sql_freeresult($result);
-			}
-
-			$post_counter += $acp_module->batch_size;
-		}
-
-		// save the current state
-		$acp_module->save_state();
-
-		// pretend the number of posts was as big as the number of ids we indexed so far
-		// just an estimation as it includes deleted posts
-		$num_posts = $this->config['num_posts'];
-		$this->config['num_posts'] = min($this->config['num_posts'], $post_counter);
-		$this->tidy();
-		$this->config['num_posts'] = $num_posts;
-
-		if ($post_counter <= $acp_module->max_post_id)
-		{
-			$totaltime = microtime(true) - $starttime;
-			$rows_per_second = $row_count / $totaltime;
-			meta_refresh(1, $u_action);
-			trigger_error($this->user->lang('SEARCH_INDEX_CREATE_REDIRECT', (int) $row_count, $post_counter) . $this->user->lang('SEARCH_INDEX_CREATE_REDIRECT_RATE', $rows_per_second));
-		}
-	}
-
 	/**
 	 * {@inheritdoc}
 	 */