1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Precise Similar Topics |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2013 Matt Friedman |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace vse\similartopics\acp; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package acp |
15
|
|
|
*/ |
16
|
|
|
class similar_topics_module |
17
|
|
|
{ |
18
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
19
|
|
|
protected $cache; |
20
|
|
|
|
21
|
|
|
/** @var \phpbb\config\config */ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
25
|
|
|
protected $db; |
26
|
|
|
|
27
|
|
|
/** @var \phpbb\log\log */ |
28
|
|
|
protected $log; |
29
|
|
|
|
30
|
|
|
/** @var \phpbb\request\request */ |
31
|
|
|
protected $request; |
32
|
|
|
|
33
|
|
|
/** @var \vse\similartopics\driver\driver_interface */ |
34
|
|
|
protected $similartopics; |
35
|
|
|
|
36
|
|
|
/** @var \phpbb\template\template */ |
37
|
|
|
protected $template; |
38
|
|
|
|
39
|
|
|
/** @var \phpbb\user */ |
40
|
|
|
protected $user; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
protected $root_path; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
protected $php_ext; |
47
|
|
|
|
48
|
|
|
/** @var array */ |
49
|
|
|
protected $times; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
public $page_title; |
53
|
|
|
|
54
|
|
|
/** @var string */ |
55
|
|
|
public $tpl_name; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
public $u_action; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* ACP module constructor |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
*/ |
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
global $phpbb_container; |
68
|
|
|
|
69
|
|
|
$this->cache = $phpbb_container->get('cache'); |
70
|
|
|
$this->config = $phpbb_container->get('config'); |
71
|
|
|
$this->db = $phpbb_container->get('dbal.conn'); |
72
|
|
|
$this->similartopics = $phpbb_container->get('vse.similartopics.driver.manager')->get_driver($this->db->get_sql_layer()); |
73
|
|
|
$this->log = $phpbb_container->get('log'); |
74
|
|
|
$this->request = $phpbb_container->get('request'); |
75
|
|
|
$this->template = $phpbb_container->get('template'); |
76
|
|
|
$this->user = $phpbb_container->get('user'); |
77
|
|
|
$this->root_path = $phpbb_container->getParameter('core.root_path'); |
78
|
|
|
$this->php_ext = $phpbb_container->getParameter('core.php_ext'); |
79
|
|
|
$this->times = array( |
80
|
|
|
'd' => 86400, // one day |
81
|
|
|
'w' => 604800, // one week |
82
|
|
|
'm' => 2626560, // one month |
83
|
|
|
'y' => 31536000, // one year |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Main ACP module |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
*/ |
92
|
|
|
public function main() |
93
|
|
|
{ |
94
|
|
|
$this->user->add_lang_ext('vse/similartopics', 'acp_similar_topics'); |
95
|
|
|
|
96
|
|
|
$this->tpl_name = 'acp_similar_topics'; |
97
|
|
|
$this->page_title = $this->user->lang('PST_TITLE_ACP'); |
98
|
|
|
|
99
|
|
|
$form_key = 'acp_similar_topics'; |
100
|
|
|
add_form_key($form_key); |
101
|
|
|
|
102
|
|
|
$action = $this->request->variable('action', ''); |
103
|
|
|
|
104
|
|
|
switch ($action) |
105
|
|
|
{ |
106
|
|
|
case 'advanced': |
107
|
|
|
$forum_id = $this->request->variable('f', 0); |
108
|
|
|
|
109
|
|
|
if ($this->request->is_set_post('submit')) |
110
|
|
|
{ |
111
|
|
|
$this->check_form_key($form_key); |
112
|
|
|
|
113
|
|
|
$similar_topic_forums = $this->request->variable('similar_forums_id', array(0)); |
114
|
|
|
$similar_topic_forums = !empty($similar_topic_forums) ? json_encode($similar_topic_forums) : ''; |
115
|
|
|
|
116
|
|
|
$sql = 'UPDATE ' . FORUMS_TABLE . " |
117
|
|
|
SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "' |
118
|
|
|
WHERE forum_id = $forum_id"; |
119
|
|
|
$this->db->sql_query($sql); |
120
|
|
|
|
121
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
122
|
|
|
|
123
|
|
|
$this->end('PST_SAVED'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$forum_name = ''; |
127
|
|
|
$selected = array(); |
128
|
|
|
if ($forum_id > 0) |
129
|
|
|
{ |
130
|
|
|
$sql = 'SELECT forum_name, similar_topic_forums |
131
|
|
|
FROM ' . FORUMS_TABLE . " |
132
|
|
|
WHERE forum_id = $forum_id"; |
133
|
|
|
$result = $this->db->sql_query($sql); |
134
|
|
|
while ($fid = $this->db->sql_fetchrow($result)) |
135
|
|
|
{ |
136
|
|
|
$selected = json_decode($fid['similar_topic_forums'], true); |
137
|
|
|
$forum_name = $fid['forum_name']; |
138
|
|
|
} |
139
|
|
|
$this->db->sql_freeresult($result); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->template->assign_vars(array( |
143
|
|
|
'S_ADVANCED_SETTINGS' => true, |
144
|
|
|
'SIMILAR_FORUMS_OPTIONS' => make_forum_select($selected, false, false, true), |
145
|
|
|
'PST_FORUM_NAME' => $forum_name, |
146
|
|
|
'PST_ADVANCED_EXP' => $this->user->lang('PST_ADVANCED_EXP', $forum_name), |
147
|
|
|
'U_ACTION' => $this->u_action . '&action=advanced&f=' . $forum_id, |
148
|
|
|
'U_BACK' => $this->u_action, |
149
|
|
|
)); |
150
|
|
|
break; |
151
|
|
|
|
152
|
|
|
default: |
153
|
|
|
if ($this->request->is_set_post('submit')) |
154
|
|
|
{ |
155
|
|
|
$this->check_form_key($form_key); |
156
|
|
|
|
157
|
|
|
// Set basic config settings |
158
|
|
|
$this->config->set('similar_topics', $this->request->variable('pst_enable', 0)); |
159
|
|
|
$this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only |
160
|
|
|
$this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only |
161
|
|
|
$this->config->set('similar_topics_words', $this->request->variable('pst_words', '', true)); |
162
|
|
|
|
163
|
|
|
// Set sensitivity |
164
|
|
|
$pst_sense = min(abs($this->request->variable('pst_sense', 5)), 10); // use abs for positive values only |
165
|
|
|
$this->config->set('similar_topics_sense', $pst_sense); |
166
|
|
|
|
167
|
|
|
// Set date/time config settings |
168
|
|
|
$pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only |
169
|
|
|
$pst_time_type = $this->request->variable('pst_time_type', ''); |
170
|
|
|
$this->config->set('similar_topics_type', $pst_time_type); |
171
|
|
|
$this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type)); |
172
|
|
|
|
173
|
|
|
// Set checkbox array form data |
174
|
|
|
$this->update_forum('similar_topics_hide', $this->request->variable('mark_noshow_forum', array(0), true)); |
175
|
|
|
$this->update_forum('similar_topics_ignore', $this->request->variable('mark_ignore_forum', array(0), true)); |
176
|
|
|
|
177
|
|
|
// Set PostgreSQL TS Name |
178
|
|
|
if ($this->similartopics && $this->similartopics->get_type() === 'postgres') |
179
|
|
|
{ |
180
|
|
|
$ts_name = $this->request->variable('pst_postgres_ts_name', ($this->config['pst_postgres_ts_name'] ?: 'simple')); |
181
|
|
|
$this->config->set('pst_postgres_ts_name', $ts_name); |
182
|
|
|
$this->similartopics->create_fulltext_index('topic_title'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
186
|
|
|
|
187
|
|
|
$this->cache->destroy('sql', TOPICS_TABLE); |
188
|
|
|
|
189
|
|
|
$this->end('PST_SAVED'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Build the time options select menu |
193
|
|
|
$time_options = array( |
194
|
|
|
'd' => $this->user->lang('PST_DAYS'), |
195
|
|
|
'w' => $this->user->lang('PST_WEEKS'), |
196
|
|
|
'm' => $this->user->lang('PST_MONTHS'), |
197
|
|
|
'y' => $this->user->lang('PST_YEARS') |
198
|
|
|
); |
199
|
|
|
foreach ($time_options as $value => $label) |
200
|
|
|
{ |
201
|
|
|
$this->template->assign_block_vars('similar_time_options', array( |
202
|
|
|
'VALUE' => $value, |
203
|
|
|
'LABEL' => $label, |
204
|
|
|
'S_SELECTED' => $value === $this->config['similar_topics_type'], |
205
|
|
|
)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->template->assign_vars(array( |
209
|
|
|
'S_PST_ENABLE' => $this->isset_or_default($this->config['similar_topics'], false), |
210
|
|
|
'PST_LIMIT' => $this->isset_or_default($this->config['similar_topics_limit'], ''), |
211
|
|
|
'PST_CACHE' => $this->isset_or_default($this->config['similar_topics_cache'], ''), |
212
|
|
|
'PST_SENSE' => $this->isset_or_default($this->config['similar_topics_sense'], ''), |
213
|
|
|
'PST_WORDS' => $this->isset_or_default($this->config['similar_topics_words'], ''), |
214
|
|
|
'PST_TIME' => $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']), |
215
|
|
|
'S_PST_NO_COMPAT' => $this->similartopics === null || !$this->similartopics->is_fulltext('topic_title'), |
216
|
|
|
'U_ACTION' => $this->u_action, |
217
|
|
|
)); |
218
|
|
|
|
219
|
|
|
// If postgresql, we need to make an options list of text search names |
220
|
|
|
if ($this->similartopics && $this->similartopics->get_type() === 'postgres') |
221
|
|
|
{ |
222
|
|
|
$this->user->add_lang('acp/search'); |
223
|
|
|
foreach ($this->get_cfgname_list() as $row) |
224
|
|
|
{ |
225
|
|
|
$this->template->assign_block_vars('postgres_ts_names', array( |
226
|
|
|
'NAME' => $row['ts_name'], |
227
|
|
|
'S_SELECTED' => $row['ts_name'] === $this->config['pst_postgres_ts_name'], |
228
|
|
|
)); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$forum_list = $this->get_forum_list(); |
233
|
|
|
foreach ($forum_list as $row) |
234
|
|
|
{ |
235
|
|
|
$this->template->assign_block_vars('forums', array( |
236
|
|
|
'FORUM_NAME' => $row['forum_name'], |
237
|
|
|
'FORUM_ID' => $row['forum_id'], |
238
|
|
|
'CHECKED_IGNORE_FORUM' => $row['similar_topics_ignore'] ? 'checked="checked"' : '', |
239
|
|
|
'CHECKED_NOSHOW_FORUM' => $row['similar_topics_hide'] ? 'checked="checked"' : '', |
240
|
|
|
'S_IS_ADVANCED' => (bool) $row['similar_topic_forums'], |
241
|
|
|
'U_ADVANCED' => "{$this->u_action}&action=advanced&f=" . $row['forum_id'], |
242
|
|
|
'U_FORUM' => append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']), |
243
|
|
|
)); |
244
|
|
|
} |
245
|
|
|
break; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Check form key, trigger error if invalid |
251
|
|
|
* |
252
|
|
|
* @access protected |
253
|
|
|
* @param string $form_key The form key value |
254
|
|
|
*/ |
255
|
|
|
protected function check_form_key($form_key) |
256
|
|
|
{ |
257
|
|
|
if (!check_form_key($form_key)) |
258
|
|
|
{ |
259
|
|
|
$this->end('FORM_INVALID', E_USER_WARNING); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get list of PostgreSQL text search names |
265
|
|
|
* |
266
|
|
|
* @return array array of text search names |
267
|
|
|
*/ |
268
|
|
|
protected function get_cfgname_list() |
269
|
|
|
{ |
270
|
|
|
$sql = 'SELECT cfgname AS ts_name FROM pg_ts_config'; |
271
|
|
|
$result = $this->db->sql_query($sql); |
272
|
|
|
$ts_options = $this->db->sql_fetchrowset($result); |
273
|
|
|
$this->db->sql_freeresult($result); |
274
|
|
|
|
275
|
|
|
return $ts_options; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get forums list |
280
|
|
|
* |
281
|
|
|
* @access protected |
282
|
|
|
* @return array forum data rows |
283
|
|
|
*/ |
284
|
|
|
protected function get_forum_list() |
285
|
|
|
{ |
286
|
|
|
$sql = 'SELECT forum_id, forum_name, similar_topic_forums, similar_topics_hide, similar_topics_ignore |
287
|
|
|
FROM ' . FORUMS_TABLE . ' |
288
|
|
|
WHERE forum_type = ' . FORUM_POST . ' |
289
|
|
|
ORDER BY left_id ASC'; |
290
|
|
|
$result = $this->db->sql_query($sql); |
291
|
|
|
$forum_list = $this->db->sql_fetchrowset($result); |
292
|
|
|
$this->db->sql_freeresult($result); |
293
|
|
|
|
294
|
|
|
return $forum_list; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Update the similar topics columns in the forums table |
299
|
|
|
* |
300
|
|
|
* @param string $column The name of the column to update |
301
|
|
|
* @param array $forum_ids An array of forum_ids |
302
|
|
|
*/ |
303
|
|
|
protected function update_forum($column, $forum_ids) |
304
|
|
|
{ |
305
|
|
|
$this->db->sql_transaction('begin'); |
306
|
|
|
|
307
|
|
|
// Set marked forums (in set) to 1 |
308
|
|
|
$sql = 'UPDATE ' . FORUMS_TABLE . " |
309
|
|
|
SET $column = 1 |
310
|
|
|
WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, false, true); |
311
|
|
|
$this->db->sql_query($sql); |
312
|
|
|
|
313
|
|
|
// Set unmarked forums (not in set) to 0 |
314
|
|
|
$sql = 'UPDATE ' . FORUMS_TABLE . " |
315
|
|
|
SET $column = 0 |
316
|
|
|
WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, true, true); |
317
|
|
|
$this->db->sql_query($sql); |
318
|
|
|
|
319
|
|
|
$this->db->sql_transaction('commit'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Calculate the time in seconds based on requested time period length |
324
|
|
|
* |
325
|
|
|
* @access protected |
326
|
|
|
* @param int $length user entered value |
327
|
|
|
* @param string $type years, months, weeks, days (y|m|w|d) |
328
|
|
|
* @return int time in seconds |
329
|
|
|
*/ |
330
|
|
|
protected function set_pst_time($length, $type = 'y') |
331
|
|
|
{ |
332
|
|
|
$type = isset($this->times[$type]) ? $type : 'y'; |
333
|
|
|
|
334
|
|
|
return (int) ($length * $this->times[$type]); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Get the correct time period length value for the form |
339
|
|
|
* |
340
|
|
|
* @access protected |
341
|
|
|
* @param int $time as a timestamp |
342
|
|
|
* @param string $type years, months, weeks, days (y|m|w|d) |
343
|
|
|
* @return int time converted to the given $type |
344
|
|
|
*/ |
345
|
|
|
protected function get_pst_time($time, $type = '') |
346
|
|
|
{ |
347
|
|
|
return isset($this->times[$type]) ? (int) round($time / $this->times[$type]) : 0; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Return a variable if it is set, otherwise default |
352
|
|
|
* |
353
|
|
|
* @access protected |
354
|
|
|
* @param mixed $var The variable to test |
355
|
|
|
* @param mixed $default The default value to use |
356
|
|
|
* @return mixed The value of the variable if set, otherwise default value |
357
|
|
|
*/ |
358
|
|
|
protected function isset_or_default($var, $default) |
359
|
|
|
{ |
360
|
|
|
return null !== $var ? $var : $default; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* End script execution with a trigger_error message |
365
|
|
|
* |
366
|
|
|
* @access protected |
367
|
|
|
* @param string $message Language key string |
368
|
|
|
* @param int $code E_USER_NOTICE|E_USER_WARNING |
369
|
|
|
* @return void |
370
|
|
|
*/ |
371
|
|
|
protected function end($message, $code = E_USER_NOTICE) |
372
|
|
|
{ |
373
|
|
|
trigger_error($this->user->lang($message) . adm_back_link($this->u_action), $code); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|