1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | * Reduce Search Index [RSI]. An extension for the phpBB Forum Software package. |
||
5 | * |
||
6 | * @copyright (c) 2020-2021, 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 | |||
25 | /** |
||
26 | * Reduce Search Index Event listener. |
||
27 | */ |
||
28 | class main_listener implements EventSubscriberInterface |
||
29 | { |
||
30 | /** @var \phpbb\config\config */ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
31 | protected $config; |
||
32 | |||
33 | /** @var \phpbb\db\driver\driver_interface */ |
||
0 ignored issues
–
show
|
|||
34 | protected $db; |
||
35 | |||
36 | /** @var \phpbb\cache\driver\driver_interface */ |
||
0 ignored issues
–
show
|
|||
37 | protected $cache; |
||
38 | |||
39 | /** @var \phpbb\template\template */ |
||
0 ignored issues
–
show
|
|||
40 | protected $template; |
||
41 | |||
42 | /** @var \phpbb\user */ |
||
0 ignored issues
–
show
|
|||
43 | protected $user; |
||
44 | |||
45 | /** @var \phpbb\language\language */ |
||
0 ignored issues
–
show
|
|||
46 | protected $language; |
||
47 | |||
48 | /** |
||
49 | * Constructor for listener |
||
50 | * |
||
51 | * @param \phpbb\config\config $config phpBB config |
||
0 ignored issues
–
show
|
|||
52 | * @param \phpbb\db\driver\driver_interface $db phpBB DBAL object |
||
0 ignored issues
–
show
|
|||
53 | * @param \phpbb\cache\driver\driver_interface $cache phpBB Cache object |
||
0 ignored issues
–
show
|
|||
54 | * @param \phpbb\template\template $template phpBB template |
||
0 ignored issues
–
show
|
|||
55 | * @param \phpbb\user $user phpBB user |
||
0 ignored issues
–
show
|
|||
56 | * @param \phpbb\language\language $language phpBB language object |
||
0 ignored issues
–
show
|
|||
57 | * @access public |
||
58 | */ |
||
59 | public function __construct(config $config, db_driver $db, cache_driver $cache, template $template, user $user, language $language) |
||
60 | { |
||
61 | $this->config = $config; |
||
62 | $this->db = $db; |
||
63 | $this->cache = $cache; |
||
64 | $this->template = $template; |
||
65 | $this->user = $user; |
||
66 | $this->language = $language; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Assign functions defined in this class to event listeners in the core |
||
71 | * |
||
72 | * @return array |
||
73 | * @static |
||
74 | * @access public |
||
75 | */ |
||
76 | public static function getSubscribedEvents() |
||
77 | { |
||
78 | return [ |
||
79 | 'core.search_modify_submit_parameters' => 'search_modify_submit_parameters', |
||
80 | 'core.search_native_index_before' => 'search_native_index_before', |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Search Page Notice |
||
88 | * |
||
89 | * @return null |
||
90 | * @access public |
||
91 | */ |
||
92 | public function search_modify_submit_parameters() |
||
93 | { |
||
94 | if ($this->config['dark1_rsi_enable']) |
||
95 | { |
||
96 | $this->language->add_lang('lang_rsi', 'dark1/reducesearchindex'); |
||
97 | $this->template->assign_vars([ |
||
98 | 'RSI_SEARCH_FLAG' => $this->config['dark1_rsi_enable'], |
||
99 | 'RSI_SEARCH_TIME' => $this->user->create_datetime()->setTimestamp((int) $this->config['dark1_rsi_time']), |
||
100 | ]); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Search Native Index Before |
||
108 | * |
||
109 | * @param \phpbb\event\data $event Event object |
||
110 | * @return null |
||
111 | * @access public |
||
112 | */ |
||
113 | public function search_native_index_before($event) |
||
114 | { |
||
115 | $post_id = $event['post_id']; |
||
116 | $words = $event['words']; |
||
117 | |||
118 | if ($this->config['dark1_rsi_enable']) |
||
119 | { |
||
120 | $forum = $this->get_search_forum($post_id); |
||
121 | |||
122 | if ($forum['dark1_rsi_f_enable'] >= consts::F_ENABLE_TOPIC && $forum['topic_time'] <= $this->config['dark1_rsi_time']) |
||
123 | { |
||
124 | $words['add']['post'] = $words['add']['title'] = $words['del']['post'] = $words['del']['title'] = []; |
||
125 | } |
||
126 | else if ($forum['dark1_rsi_f_enable'] == consts::F_ENABLE_POST && $forum['post_time'] <= $this->config['dark1_rsi_time']) |
||
127 | { |
||
128 | $words['add']['post'] = $words['add']['title'] = $words['del']['post'] = $words['del']['title'] = []; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | $event['words'] = $words; |
||
133 | } |
||
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * Get Search Forum |
||
139 | * |
||
140 | * @param int $post_id Post ID |
||
141 | * @return array Forum |
||
142 | * @access private |
||
143 | */ |
||
144 | private function get_search_forum($post_id) |
||
145 | { |
||
146 | $cache_key = '_dark1_rsi_search_matrix'; |
||
147 | |||
148 | // Get search matrix data from the cache |
||
149 | $search_matrix = $this->cache->get($cache_key); |
||
150 | |||
151 | if ($search_matrix === false || !isset($search_matrix[$post_id])) |
||
152 | { |
||
153 | $sql_ary = [ |
||
154 | 'SELECT' => 'f.dark1_rsi_f_enable, t.topic_time, p.post_time', |
||
155 | 'FROM' => [ |
||
156 | POSTS_TABLE => 'p', |
||
157 | ], |
||
158 | 'LEFT_JOIN' => [ |
||
159 | [ |
||
160 | 'FROM' => [TOPICS_TABLE => 't'], |
||
161 | 'ON' => 't.topic_id = p.topic_id', |
||
162 | ], |
||
163 | [ |
||
164 | 'FROM' => [FORUMS_TABLE => 'f'], |
||
165 | 'ON' => 'f.forum_id = p.forum_id', |
||
166 | ], |
||
167 | ], |
||
168 | 'WHERE' => 'p.post_id = ' . (int) $post_id, |
||
169 | ]; |
||
170 | $sql = $this->db->sql_build_query('SELECT', $sql_ary); |
||
171 | $result = $this->db->sql_query($sql); |
||
172 | while ($row = $this->db->sql_fetchrow($result)) |
||
173 | { |
||
174 | $search_matrix[$post_id] = $row; |
||
175 | } |
||
176 | $this->db->sql_freeresult($result); |
||
177 | |||
178 | // Cache post matrix data |
||
179 | $this->cache->put($cache_key, $search_matrix); |
||
180 | } |
||
181 | |||
182 | return $search_matrix[$post_id]; |
||
183 | } |
||
184 | } |
||
185 |