6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
20 var $batch_size = 100;
22 function main($id, $mode)
26 $user->add_lang('acp/search');
28 // For some this may be of help...
29 @ini_set
('memory_limit', '128M');
34 $this->settings($id, $mode);
38 $this->index($id, $mode);
43 function settings($id, $mode)
45 global $db, $user, $auth, $template, $cache;
46 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
48 $submit = (isset($_POST['submit'])) ?
true : false;
50 $search_types = $this->get_search_types();
53 'search_interval' => 'float',
54 'search_anonymous_interval' => 'float',
55 'load_search' => 'bool',
56 'limit_search_load' => 'float',
57 'min_search_author_chars' => 'integer',
58 'search_store_results' => 'integer',
64 foreach ($search_types as $type)
66 if ($this->init_search($type, $search, $error))
71 $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
72 $selected = ($config['search_type'] == $type) ?
' selected="selected"' : '';
73 $search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
75 if (method_exists($search, 'acp'))
77 $vars = $search->acp();
81 $template->assign_block_vars('backend', array(
83 'SETTINGS' => $vars['tpl'])
86 else if (is_array($vars['config']))
88 $settings = array_merge($settings, $vars['config']);
95 $cfg_array = (isset($_REQUEST['config'])) ?
request_var('config', array('' => '')) : array();
96 $updated = request_var('updated', false);
98 foreach ($settings as $config_name => $var_type)
100 if (!isset($cfg_array[$config_name]))
105 // e.g. integer:4:12 (min 4, max 12)
106 $var_type = explode(':', $var_type);
108 $config_value = $cfg_array[$config_name];
109 settype($config_value, $var_type[0]);
111 if (isset($var_type[1]))
113 $config_value = max($var_type[1], $config_value);
116 if (isset($var_type[2]))
118 $config_value = min($var_type[2], $config_value);
121 // only change config if anything was actually changed
122 if ($submit && ($config[$config_name] != $config_value))
124 set_config($config_name, $config_value);
134 add_log('admin', 'LOG_CONFIG_SEARCH');
137 if (isset($cfg_array['search_type']) && in_array($cfg_array['search_type'], $search_types, true) && ($cfg_array['search_type'] != $config['search_type']))
142 if (!$this->init_search($cfg_array['search_type'], $search, $error))
144 if (confirm_box(true))
146 if (!method_exists($search, 'init') ||
!($error = $search->init()))
148 set_config('search_type', $cfg_array['search_type']);
152 add_log('admin', 'LOG_CONFIG_SEARCH');
154 $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>';
158 trigger_error($error . adm_back_link($this->u_action
), E_USER_WARNING
);
163 confirm_box(false, $user->lang
['CONFIRM_SEARCH_BACKEND'], build_hidden_fields(array(
167 'updated' => $updated,
168 'config' => array('search_type' => $cfg_array['search_type']),
174 trigger_error($error . adm_back_link($this->u_action
), E_USER_WARNING
);
178 trigger_error($user->lang
['CONFIG_UPDATED'] . $extra_message . adm_back_link($this->u_action
));
182 $this->tpl_name
= 'acp_search';
183 $this->page_title
= 'ACP_SEARCH_SETTINGS';
185 $template->assign_vars(array(
186 'LIMIT_SEARCH_LOAD' => (float) $config['limit_search_load'],
187 'MIN_SEARCH_AUTHOR_CHARS' => (int) $config['min_search_author_chars'],
188 'SEARCH_INTERVAL' => (float) $config['search_interval'],
189 'SEARCH_GUEST_INTERVAL' => (float) $config['search_anonymous_interval'],
190 'SEARCH_STORE_RESULTS' => (int) $config['search_store_results'],
192 'S_SEARCH_TYPES' => $search_options,
193 'S_YES_SEARCH' => (bool) $config['load_search'],
194 'S_SETTINGS' => true,
196 'U_ACTION' => $this->u_action
)
200 function index($id, $mode)
202 global $db, $user, $auth, $template, $cache;
203 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
205 if (isset($_REQUEST['action']) && is_array($_REQUEST['action']))
207 $action = request_var('action', array('' => false));
208 $action = key($action);
212 $action = request_var('action', '');
214 $this->state
= explode(',', $config['search_indexing_state']);
216 if (isset($_POST['cancel']))
219 $this->state
= array();
228 $type = request_var('type', '');
229 $this->display_progress_bar($type);
233 $this->state
[1] = 'delete';
237 $this->state
[1] = 'create';
241 trigger_error('NO_ACTION', E_USER_ERROR
);
245 if (empty($this->state
[0]))
247 $this->state
[0] = request_var('search_type', '');
250 $this->search
= null;
252 if ($this->init_search($this->state
[0], $this->search
, $error))
254 trigger_error($error . adm_back_link($this->u_action
), E_USER_WARNING
);
256 $name = ucfirst(strtolower(str_replace('_', ' ', $this->state
[0])));
258 $action = &$this->state
[1];
260 $this->max_post_id
= $this->get_max_post_id();
262 $post_counter = (isset($this->state
[2])) ?
$this->state
[2] : 0;
263 $this->state
[2] = &$post_counter;
269 if (method_exists($this->search
, 'delete_index'))
271 // pass a reference to myself so the $search object can make use of save_state() and attributes
272 if ($error = $this->search
->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
274 $this->state
= array('');
276 trigger_error($error . adm_back_link($this->u_action
) . $this->close_popup_js(), E_USER_WARNING
);
281 $starttime = explode(' ', microtime());
282 $starttime = $starttime[1] +
$starttime[0];
284 while (still_on_time() && $post_counter <= $this->max_post_id
)
286 $sql = 'SELECT post_id, poster_id, forum_id
287 FROM ' . POSTS_TABLE
. '
288 WHERE post_id >= ' . (int) ($post_counter +
1) . '
289 AND post_id <= ' . (int) ($post_counter +
$this->batch_size
);
290 $result = $db->sql_query($sql);
292 $ids = $posters = $forum_ids = array();
293 while ($row = $db->sql_fetchrow($result))
295 $ids[] = $row['post_id'];
296 $posters[] = $row['poster_id'];
297 $forum_ids[] = $row['forum_id'];
299 $db->sql_freeresult($result);
300 $row_count +
= sizeof($ids);
304 $this->search
->index_remove($ids, $posters, $forum_ids);
307 $post_counter +
= $this->batch_size
;
309 // save the current state
312 if ($post_counter <= $this->max_post_id
)
314 $mtime = explode(' ', microtime());
315 $totaltime = $mtime[0] +
$mtime[1] - $starttime;
316 $rows_per_second = $row_count / $totaltime;
317 meta_refresh(1, append_sid($this->u_action
. '&action=delete&skip_rows=' . $post_counter));
318 trigger_error(sprintf($user->lang
['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
322 $this->search
->tidy();
324 $this->state
= array('');
327 add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
328 trigger_error($user->lang
['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action
) . $this->close_popup_js());
332 if (method_exists($this->search
, 'create_index'))
334 // pass a reference to acp_search so the $search object can make use of save_state() and attributes
335 if ($error = $this->search
->create_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=create", false)))
337 $this->state
= array('');
339 trigger_error($error . adm_back_link($this->u_action
) . $this->close_popup_js(), E_USER_WARNING
);
344 $sql = 'SELECT forum_id, enable_indexing
345 FROM ' . FORUMS_TABLE
;
346 $result = $db->sql_query($sql, 3600);
348 while ($row = $db->sql_fetchrow($result))
350 $forums[$row['forum_id']] = (bool) $row['enable_indexing'];
352 $db->sql_freeresult($result);
354 $starttime = explode(' ', microtime());
355 $starttime = $starttime[1] +
$starttime[0];
357 while (still_on_time() && $post_counter <= $this->max_post_id
)
359 $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
360 FROM ' . POSTS_TABLE
. '
361 WHERE post_id >= ' . (int) ($post_counter +
1) . '
362 AND post_id <= ' . (int) ($post_counter +
$this->batch_size
);
363 $result = $db->sql_query($sql);
365 while ($row = $db->sql_fetchrow($result))
367 // Indexing enabled for this forum or global announcement?
368 // Global announcements get indexed by default.
369 if (!$row['forum_id'] ||
(isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]))
371 $this->search
->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
375 $db->sql_freeresult($result);
377 $post_counter +
= $this->batch_size
;
379 // save the current state
382 // pretend the number of posts was as big as the number of ids we indexed so far
383 // just an estimation as it includes deleted posts
384 $num_posts = $config['num_posts'];
385 $config['num_posts'] = min($config['num_posts'], $post_counter);
386 $this->search
->tidy();
387 $config['num_posts'] = $num_posts;
389 if ($post_counter <= $this->max_post_id
)
391 $mtime = explode(' ', microtime());
392 $totaltime = $mtime[0] +
$mtime[1] - $starttime;
393 $rows_per_second = $row_count / $totaltime;
394 meta_refresh(1, append_sid($this->u_action
. '&action=create&skip_rows=' . $post_counter));
395 trigger_error(sprintf($user->lang
['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
399 $this->search
->tidy();
401 $this->state
= array('');
404 add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
405 trigger_error($user->lang
['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action
) . $this->close_popup_js());
410 $search_types = $this->get_search_types();
414 $search_options = '';
415 foreach ($search_types as $type)
417 if ($this->init_search($type, $search, $error) ||
!method_exists($search, 'index_created'))
422 $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
425 if (method_exists($search, 'index_stats'))
427 $data = $search->index_stats();
430 $statistics = array();
431 foreach ($data as $statistic => $value)
433 $n = sizeof($statistics);
434 if ($n && sizeof($statistics[$n - 1]) < 3)
436 $statistics[$n - 1] +
= array('statistic_2' => $statistic, 'value_2' => $value);
440 $statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
444 $template->assign_block_vars('backend', array(
448 'S_ACTIVE' => ($type == $config['search_type']) ?
true : false,
449 'S_HIDDEN_FIELDS' => build_hidden_fields(array('search_type' => $type)),
450 'S_INDEXED' => (bool) $search->index_created(),
451 'S_STATS' => (bool) sizeof($statistics))
454 foreach ($statistics as $statistic)
456 $template->assign_block_vars('backend.data', array(
457 'STATISTIC_1' => $statistic['statistic_1'],
458 'VALUE_1' => $statistic['value_1'],
459 'STATISTIC_2' => (isset($statistic['statistic_2'])) ?
$statistic['statistic_2'] : '',
460 'VALUE_2' => (isset($statistic['value_2'])) ?
$statistic['value_2'] : '')
469 $this->tpl_name
= 'acp_search';
470 $this->page_title
= 'ACP_SEARCH_INDEX';
472 $template->assign_vars(array(
474 'U_ACTION' => $this->u_action
,
475 'U_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar"),
476 'UA_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar", false))
479 if (isset($this->state
[1]))
481 $template->assign_vars(array(
482 'S_CONTINUE_INDEXING' => $this->state
[1],
483 'U_CONTINUE_INDEXING' => $this->u_action
. '&action=' . $this->state
[1],
484 'L_CONTINUE' => ($this->state
[1] == 'create') ?
$user->lang
['CONTINUE_INDEXING'] : $user->lang
['CONTINUE_INDEX_DELETING'],
485 'L_CONTINUE_EXPLAIN' => ($this->state
[1] == 'create') ?
$user->lang
['CONTINUE_INDEXING_EXPLAIN'] : $user->lang
['CONTINUE_INDEX_DELETING_EXPLAIN'])
490 function display_progress_bar($type)
492 global $template, $user;
494 $l_type = ($type == 'create') ?
'INDEXING_IN_PROGRESS' : 'DELETING_INDEX_IN_PROGRESS';
496 adm_page_header($user->lang
[$l_type]);
498 $template->set_filenames(array(
499 'body' => 'progress_bar.html')
502 $template->assign_vars(array(
503 'L_PROGRESS' => $user->lang
[$l_type],
504 'L_PROGRESS_EXPLAIN' => $user->lang
[$l_type . '_EXPLAIN'])
510 function close_popup_js()
512 return "<script type=\"text/javascript\">\n" .
514 " close_waitscreen = 1;\n" .
519 function get_search_types()
521 global $phpbb_root_path, $phpEx;
523 $search_types = array();
525 $dp = @opendir
($phpbb_root_path . 'includes/search');
529 while (($file = readdir($dp)) !== false)
531 if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx"))
533 $search_types[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file);
541 return $search_types;
544 function get_max_post_id()
548 $sql = 'SELECT MAX(post_id) as max_post_id
550 $result = $db->sql_query($sql);
551 $max_post_id = (int) $db->sql_fetchfield('max_post_id');
552 $db->sql_freeresult($result);
557 function save_state($state = false)
561 $this->state
= $state;
566 set_config('search_indexing_state', implode(',', $this->state
));
570 * Initialises a search backend object
572 * @return false if no error occurred else an error message
574 function init_search($type, &$search, &$error)
576 global $phpbb_root_path, $phpEx, $user;
578 if (!preg_match('#^\w+$#', $type) ||
!file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
580 $error = $user->lang
['NO_SUCH_SEARCH_MODULE'];
584 include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
586 if (!class_exists($type))
588 $error = $user->lang
['NO_SUCH_SEARCH_MODULE'];
593 $search = new $type($error);