5
* @version $Id: acp_search.php,v 1.38 2007/10/05 14:36:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
28
var $batch_size = 100;
30
function main($id, $mode)
34
$user->add_lang('acp/search');
36
// For some this may be of help...
37
@ini_set('memory_limit', '128M');
42
$this->settings($id, $mode);
46
$this->index($id, $mode);
51
function settings($id, $mode)
53
global $db, $user, $auth, $template, $cache;
54
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
56
$submit = (isset($_POST['submit'])) ? true : false;
58
$search_types = $this->get_search_types();
61
'search_interval' => 'float',
62
'search_anonymous_interval' => 'float',
63
'load_search' => 'bool',
64
'limit_search_load' => 'float',
65
'min_search_author_chars' => 'integer',
66
'search_store_results' => 'integer',
72
foreach ($search_types as $type)
74
if ($this->init_search($type, $search, $error))
79
$name = ucfirst(strtolower(str_replace('_', ' ', $type)));
80
$selected = ($config['search_type'] == $type) ? ' selected="selected"' : '';
81
$search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
83
if (method_exists($search, 'acp'))
85
$vars = $search->acp();
89
$template->assign_block_vars('backend', array(
91
'SETTINGS' => $vars['tpl'])
94
else if (is_array($vars['config']))
96
$settings = array_merge($settings, $vars['config']);
103
$cfg_array = (isset($_REQUEST['config'])) ? request_var('config', array('' => ''), true) : array();
104
$updated = request_var('updated', false);
106
foreach ($settings as $config_name => $var_type)
108
if (!isset($cfg_array[$config_name]))
113
// e.g. integer:4:12 (min 4, max 12)
114
$var_type = explode(':', $var_type);
116
$config_value = $cfg_array[$config_name];
117
settype($config_value, $var_type[0]);
119
if (isset($var_type[1]))
121
$config_value = max($var_type[1], $config_value);
124
if (isset($var_type[2]))
126
$config_value = min($var_type[2], $config_value);
129
// only change config if anything was actually changed
130
if ($submit && ($config[$config_name] != $config_value))
132
set_config($config_name, $config_value);
142
add_log('admin', 'LOG_CONFIG_SEARCH');
145
if (isset($cfg_array['search_type']) && in_array($cfg_array['search_type'], $search_types, true) && ($cfg_array['search_type'] != $config['search_type']))
150
if (!$this->init_search($cfg_array['search_type'], $search, $error))
152
if (confirm_box(true))
154
if (!method_exists($search, 'init') || !($error = $search->init()))
156
set_config('search_type', $cfg_array['search_type']);
160
add_log('admin', 'LOG_CONFIG_SEARCH');
162
$extra_message = '<br />' . $user->lang['SWITCHED_SEARCH_BACKEND'] . '<br /><a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=search&mode=index') . '">» ' . $user->lang['GO_TO_SEARCH_INDEX'] . '</a>';
166
trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
171
confirm_box(false, $user->lang['CONFIRM_SEARCH_BACKEND'], build_hidden_fields(array(
175
'updated' => $updated,
176
'config' => array('search_type' => $cfg_array['search_type']),
182
trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
186
trigger_error($user->lang['CONFIG_UPDATED'] . $extra_message . adm_back_link($this->u_action));
190
$this->tpl_name = 'acp_search';
191
$this->page_title = 'ACP_SEARCH_SETTINGS';
193
$template->assign_vars(array(
194
'LIMIT_SEARCH_LOAD' => (float) $config['limit_search_load'],
195
'MIN_SEARCH_AUTHOR_CHARS' => (int) $config['min_search_author_chars'],
196
'SEARCH_INTERVAL' => (float) $config['search_interval'],
197
'SEARCH_GUEST_INTERVAL' => (float) $config['search_anonymous_interval'],
198
'SEARCH_STORE_RESULTS' => (int) $config['search_store_results'],
200
'S_SEARCH_TYPES' => $search_options,
201
'S_YES_SEARCH' => (bool) $config['load_search'],
202
'S_SETTINGS' => true,
204
'U_ACTION' => $this->u_action)
208
function index($id, $mode)
210
global $db, $user, $auth, $template, $cache;
211
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
213
if (isset($_REQUEST['action']) && is_array($_REQUEST['action']))
215
$action = request_var('action', array('' => false));
216
$action = key($action);
220
$action = request_var('action', '');
222
$this->state = explode(',', $config['search_indexing_state']);
224
if (isset($_POST['cancel']))
227
$this->state = array();
236
$type = request_var('type', '');
237
$this->display_progress_bar($type);
241
$this->state[1] = 'delete';
245
$this->state[1] = 'create';
249
trigger_error('NO_ACTION', E_USER_ERROR);
253
if (empty($this->state[0]))
255
$this->state[0] = request_var('search_type', '');
258
$this->search = null;
260
if ($this->init_search($this->state[0], $this->search, $error))
262
trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
264
$name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0])));
266
$action = &$this->state[1];
268
$this->max_post_id = $this->get_max_post_id();
270
$post_counter = (isset($this->state[2])) ? $this->state[2] : 0;
271
$this->state[2] = &$post_counter;
277
if (method_exists($this->search, 'delete_index'))
279
// pass a reference to myself so the $search object can make use of save_state() and attributes
280
if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
282
$this->state = array('');
284
trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
289
$starttime = explode(' ', microtime());
290
$starttime = $starttime[1] + $starttime[0];
292
while (still_on_time() && $post_counter <= $this->max_post_id)
294
$sql = 'SELECT post_id, poster_id, forum_id
295
FROM ' . POSTS_TABLE . '
296
WHERE post_id >= ' . (int) ($post_counter + 1) . '
297
AND post_id <= ' . (int) ($post_counter + $this->batch_size);
298
$result = $db->sql_query($sql);
300
$ids = $posters = $forum_ids = array();
301
while ($row = $db->sql_fetchrow($result))
303
$ids[] = $row['post_id'];
304
$posters[] = $row['poster_id'];
305
$forum_ids[] = $row['forum_id'];
307
$db->sql_freeresult($result);
308
$row_count += sizeof($ids);
312
$this->search->index_remove($ids, $posters, $forum_ids);
315
$post_counter += $this->batch_size;
317
// save the current state
320
if ($post_counter <= $this->max_post_id)
322
$mtime = explode(' ', microtime());
323
$totaltime = $mtime[0] + $mtime[1] - $starttime;
324
$rows_per_second = $row_count / $totaltime;
325
meta_refresh(1, append_sid($this->u_action . '&action=delete&skip_rows=' . $post_counter));
326
trigger_error(sprintf($user->lang['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
330
$this->search->tidy();
332
$this->state = array('');
335
add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
336
trigger_error($user->lang['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action) . $this->close_popup_js());
340
if (method_exists($this->search, 'create_index'))
342
// pass a reference to acp_search so the $search object can make use of save_state() and attributes
343
if ($error = $this->search->create_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=create", false)))
345
$this->state = array('');
347
trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
352
$sql = 'SELECT forum_id, enable_indexing
353
FROM ' . FORUMS_TABLE;
354
$result = $db->sql_query($sql, 3600);
356
while ($row = $db->sql_fetchrow($result))
358
$forums[$row['forum_id']] = (bool) $row['enable_indexing'];
360
$db->sql_freeresult($result);
362
$starttime = explode(' ', microtime());
363
$starttime = $starttime[1] + $starttime[0];
365
while (still_on_time() && $post_counter <= $this->max_post_id)
367
$sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
368
FROM ' . POSTS_TABLE . '
369
WHERE post_id >= ' . (int) ($post_counter + 1) . '
370
AND post_id <= ' . (int) ($post_counter + $this->batch_size);
371
$result = $db->sql_query($sql);
373
while ($row = $db->sql_fetchrow($result))
375
// Indexing enabled for this forum or global announcement?
376
// Global announcements get indexed by default.
377
if (!$row['forum_id'] || (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]))
379
$this->search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
383
$db->sql_freeresult($result);
385
$post_counter += $this->batch_size;
387
// save the current state
390
// pretend the number of posts was as big as the number of ids we indexed so far
391
// just an estimation as it includes deleted posts
392
$num_posts = $config['num_posts'];
393
$config['num_posts'] = min($config['num_posts'], $post_counter);
394
$this->search->tidy();
395
$config['num_posts'] = $num_posts;
397
if ($post_counter <= $this->max_post_id)
399
$mtime = explode(' ', microtime());
400
$totaltime = $mtime[0] + $mtime[1] - $starttime;
401
$rows_per_second = $row_count / $totaltime;
402
meta_refresh(1, append_sid($this->u_action . '&action=create&skip_rows=' . $post_counter));
403
trigger_error(sprintf($user->lang['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
407
$this->search->tidy();
409
$this->state = array('');
412
add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
413
trigger_error($user->lang['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action) . $this->close_popup_js());
418
$search_types = $this->get_search_types();
422
$search_options = '';
423
foreach ($search_types as $type)
425
if ($this->init_search($type, $search, $error) || !method_exists($search, 'index_created'))
430
$name = ucfirst(strtolower(str_replace('_', ' ', $type)));
433
if (method_exists($search, 'index_stats'))
435
$data = $search->index_stats();
438
$statistics = array();
439
foreach ($data as $statistic => $value)
441
$n = sizeof($statistics);
442
if ($n && sizeof($statistics[$n - 1]) < 3)
444
$statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
448
$statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
452
$template->assign_block_vars('backend', array(
456
'S_ACTIVE' => ($type == $config['search_type']) ? true : false,
457
'S_HIDDEN_FIELDS' => build_hidden_fields(array('search_type' => $type)),
458
'S_INDEXED' => (bool) $search->index_created(),
459
'S_STATS' => (bool) sizeof($statistics))
462
foreach ($statistics as $statistic)
464
$template->assign_block_vars('backend.data', array(
465
'STATISTIC_1' => $statistic['statistic_1'],
466
'VALUE_1' => $statistic['value_1'],
467
'STATISTIC_2' => (isset($statistic['statistic_2'])) ? $statistic['statistic_2'] : '',
468
'VALUE_2' => (isset($statistic['value_2'])) ? $statistic['value_2'] : '')
477
$this->tpl_name = 'acp_search';
478
$this->page_title = 'ACP_SEARCH_INDEX';
480
$template->assign_vars(array(
482
'U_ACTION' => $this->u_action,
483
'U_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar"),
484
'UA_PROGRESS_BAR' => addslashes(append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar")),
487
if (isset($this->state[1]))
489
$template->assign_vars(array(
490
'S_CONTINUE_INDEXING' => $this->state[1],
491
'U_CONTINUE_INDEXING' => $this->u_action . '&action=' . $this->state[1],
492
'L_CONTINUE' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'],
493
'L_CONTINUE_EXPLAIN' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN'])
498
function display_progress_bar($type)
500
global $template, $user;
502
$l_type = ($type == 'create') ? 'INDEXING_IN_PROGRESS' : 'DELETING_INDEX_IN_PROGRESS';
504
adm_page_header($user->lang[$l_type]);
506
$template->set_filenames(array(
507
'body' => 'progress_bar.html')
510
$template->assign_vars(array(
511
'L_PROGRESS' => $user->lang[$l_type],
512
'L_PROGRESS_EXPLAIN' => $user->lang[$l_type . '_EXPLAIN'])
518
function close_popup_js()
520
return "<script type=\"text/javascript\">\n" .
522
" close_waitscreen = 1;\n" .
527
function get_search_types()
529
global $phpbb_root_path, $phpEx;
531
$search_types = array();
533
$dp = @opendir($phpbb_root_path . 'includes/search');
537
while (($file = readdir($dp)) !== false)
539
if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx"))
541
$search_types[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file);
549
return $search_types;
552
function get_max_post_id()
556
$sql = 'SELECT MAX(post_id) as max_post_id
558
$result = $db->sql_query($sql);
559
$max_post_id = (int) $db->sql_fetchfield('max_post_id');
560
$db->sql_freeresult($result);
565
function save_state($state = false)
569
$this->state = $state;
574
set_config('search_indexing_state', implode(',', $this->state));
578
* Initialises a search backend object
580
* @return false if no error occurred else an error message
582
function init_search($type, &$search, &$error)
584
global $phpbb_root_path, $phpEx, $user;
586
if (!preg_match('#^\w+$#', $type) || !file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
588
$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
592
include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
594
if (!class_exists($type))
596
$error = $user->lang['NO_SUCH_SEARCH_MODULE'];
601
$search = new $type($error);
b'\\ No newline at end of file'