main_listener   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 63
c 6
b 0
f 0
dl 0
loc 192
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 5 1
A search_modify_submit_parameters() 0 11 2
A get_search_forum() 0 35 4
A search_native_index_before() 0 22 4
A get_search_forum_enable() 0 6 4
A get_common_words_ary() 0 3 1
1
<?php
2
/**
3
 *
4
 * Reduce Search Index [RSI]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-forever, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\reducesearchindex\event;
12
13
/**
14
 * @ignore
15
 */
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use dark1\reducesearchindex\core\consts;
18
use phpbb\config\config;
19
use phpbb\db\driver\driver_interface as db_driver;
20
use phpbb\cache\driver\driver_interface as cache_driver;
21
use phpbb\template\template;
22
use phpbb\user;
23
use phpbb\language\language;
24
use phpbb\config\db_text as config_text;
25
26
/**
27
 * Reduce Search Index Event listener.
28
 */
29
class main_listener implements EventSubscriberInterface
30
{
31
	/** @var config */
32
	protected $config;
33
34
	/** @var db_driver */
35
	protected $db;
36
37
	/** @var cache_driver */
38
	protected $cache;
39
40
	/** @var template */
41
	protected $template;
42
43
	/** @var user */
44
	protected $user;
45
46
	/** @var language */
47
	protected $language;
48
49
	/** @var config_text */
50
	protected $config_text;
51
52
	/**
53
	 * Constructor for listener
54
	 *
55
	 * @param config		$config			phpBB config
56
	 * @param db_driver		$db				phpBB DBAL object
57
	 * @param cache_driver	$cache			phpBB Cache object
58
	 * @param template		$template		phpBB template
59
	 * @param user			$user			phpBB user
60
	 * @param language		$language		phpBB language object
61
	 * @param config_text	$config_text	phpBB config text
62
	 * @access public
63
	 */
64
	public function __construct(config $config, db_driver $db, cache_driver $cache, template $template, user $user, language $language, config_text $config_text)
65
	{
66
		$this->config		= $config;
67
		$this->db			= $db;
68
		$this->cache		= $cache;
69
		$this->template		= $template;
70
		$this->user			= $user;
71
		$this->language		= $language;
72
		$this->config_text	= $config_text;
73
	}
74
75
	/**
76
	* Assign functions defined in this class to event listeners in the core
77
	*
78
	* @return array
79
	* @static
80
	* @access public
81
	*/
82
	public static function getSubscribedEvents()
83
	{
84
		return [
85
			'core.search_modify_submit_parameters'		=> 'search_modify_submit_parameters',
86
			'core.search_native_index_before'			=> 'search_native_index_before',
87
		];
88
	}
89
90
91
92
	/**
93
	 * Search Page Notice
94
	 *
95
	 * @return null
96
	 * @access public
97
	 */
98
	public function search_modify_submit_parameters()
99
	{
100
		if ($this->config['dark1_rsi_enable'])
101
		{
102
			$this->language->add_lang('lang_rsi', 'dark1/reducesearchindex');
103
			$common_words_ary = $this->get_common_words_ary();
104
			$this->template->assign_vars([
105
				'RSI_SEARCH_FLAG'			=> $this->config['dark1_rsi_enable'],
106
				'RSI_SEARCH_TIME'			=> $this->user->create_datetime()->setTimestamp((int) $this->config['dark1_rsi_time']),
107
				'RSI_SEARCH_IGN_COM_FLAG'	=> $this->config['dark1_rsi_ign_com_enable'],
108
				'RSI_SEARCH_IGN_COM_WORDS'	=> implode(', ', $common_words_ary),
109
			]);
110
		}
111
	}
112
113
114
115
	/**
116
	 * Search Native Index Before
117
	 *
118
	 * @param object $event	Event object
119
	 * @return null
120
	 * @access public
121
	 */
122
	public function search_native_index_before($event)
123
	{
124
		$words = $event['words'];
125
		$cur_words = $event['cur_words'];
126
127
		if ($this->config['dark1_rsi_enable'])
128
		{
129
			if ($this->get_search_forum_enable((int) $event['post_id']))
130
			{
131
				$words['add']['post'] = $words['add']['title'] = $words['del']['post'] = $words['del']['title'] = [];
132
			}
133
			else if ($this->config['dark1_rsi_ign_com_enable'])
134
			{
135
				$common_words_ary = $this->get_common_words_ary();
136
				$words['add']['post'] = array_diff($words['add']['post'], $common_words_ary);
137
				$words['add']['title'] = array_diff($words['add']['title'], $common_words_ary);
138
				$words['del']['post'] = array_unique(array_merge($words['del']['post'], array_intersect(array_keys($cur_words['post']), $common_words_ary)));
139
				$words['del']['title'] = array_unique(array_merge($words['del']['title'], array_intersect(array_keys($cur_words['title']), $common_words_ary)));
140
			}
141
		}
142
143
		$event['words'] = $words;
144
	}
145
146
147
148
	/**
149
	 * Get Common Words array
150
	 *
151
	 * @return array Common Words
152
	 * @access private
153
	 */
154
	private function get_common_words_ary()
155
	{
156
		return explode("\n", (string) $this->config_text->get('dark1_rsi_ign_com_words'));
157
	}
158
159
160
161
	/**
162
	 * Get Search Forum Enable or Not
163
	 *
164
	 * @param int $post_id	Post ID
165
	 * @return bool Enabled or Not
166
	 * @access private
167
	 */
168
	private function get_search_forum_enable($post_id)
169
	{
170
		$forum = $this->get_search_forum($post_id);
171
		return (bool) (
172
			($forum['dark1_rsi_f_enable'] >= consts::F_ENABLE_TOPIC && $forum['topic_time'] <= $this->config['dark1_rsi_time'])
173
			|| ($forum['dark1_rsi_f_enable'] == consts::F_ENABLE_POST && $forum['post_time'] <= $this->config['dark1_rsi_time'])
174
		);
175
	}
176
177
178
179
	/**
180
	 * Get Search Forum
181
	 *
182
	 * @param int $post_id	Post ID
183
	 * @return array Forum
184
	 * @access private
185
	 */
186
	private function get_search_forum($post_id)
187
	{
188
		// Get search matrix data from the cache
189
		$search_matrix = $this->cache->get(consts::CACHE_KEY);
190
191
		if ($search_matrix === false || !isset($search_matrix[$post_id]))
192
		{
193
			$sql_ary = [
194
				'SELECT'	=> 'f.dark1_rsi_f_enable, t.topic_time, p.post_time',
195
				'FROM'		=> [
196
					POSTS_TABLE		=> 'p',
197
				],
198
				'LEFT_JOIN'	=> [[
199
					'FROM'	=> [TOPICS_TABLE => 't'],
200
					'ON'	=> 't.topic_id = p.topic_id',
201
				], [
202
					'FROM'	=> [FORUMS_TABLE => 'f'],
203
					'ON'	=> 'f.forum_id = p.forum_id',
204
				],],
205
				'WHERE'	=> 'p.post_id = ' . (int) $post_id,
206
			];
207
			$sql = $this->db->sql_build_query('SELECT', $sql_ary);
208
			$result = $this->db->sql_query($sql);
209
			$rows = $this->db->sql_fetchrowset($result);
210
			$this->db->sql_freeresult($result);
211
			foreach ($rows as $row)
212
			{
213
				$search_matrix[$post_id] = $row;
214
			}
215
216
			// Cache post matrix data
217
			$this->cache->put(consts::CACHE_KEY, $search_matrix);
218
		}
219
220
		return $search_matrix[$post_id];
221
	}
222
}
223