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 'max_num_search_keywords' => 'integer',
67 'search_store_results' => 'integer',
73 foreach ($search_types as $type)
75 if ($this->init_search($type, $search, $error))
80 $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
81 $selected = ($config['search_type'] == $type) ? ' selected="selected"' : '';
82 $search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
84 if (method_exists($search, 'acp'))
86 $vars = $search->acp();
90 $template->assign_block_vars('backend', array(
92 'SETTINGS' => $vars['tpl'])
95 else if (is_array($vars['config']))
97 $settings = array_merge($settings, $vars['config']);
104 $cfg_array = (isset($_REQUEST['config'])) ? request_var('config', array('' => ''), true) : array();
105 $updated = request_var('updated', false);
107 foreach ($settings as $config_name => $var_type)
109 if (!isset($cfg_array[$config_name]))
114 // e.g. integer:4:12 (min 4, max 12)
115 $var_type = explode(':', $var_type);
117 $config_value = $cfg_array[$config_name];
118 settype($config_value, $var_type[0]);
120 if (isset($var_type[1]))
122 $config_value = max($var_type[1], $config_value);
125 if (isset($var_type[2]))
127 $config_value = min($var_type[2], $config_value);
130 // only change config if anything was actually changed
131 if ($submit && ($config[$config_name] != $config_value))
133 set_config($config_name, $config_value);
143 add_log('admin', 'LOG_CONFIG_SEARCH');
146 if (isset($cfg_array['search_type']) && in_array($cfg_array['search_type'], $search_types, true) && ($cfg_array['search_type'] != $config['search_type']))
151 if (!$this->init_search($cfg_array['search_type'], $search, $error))
153 if (confirm_box(true))
155 if (!method_exists($search, 'init') || !($error = $search->init()))
157 set_config('search_type', $cfg_array['search_type']);
161 add_log('admin', 'LOG_CONFIG_SEARCH');
163 $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>';
167 trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
172 confirm_box(false, $user->lang['CONFIRM_SEARCH_BACKEND'], build_hidden_fields(array(
176 'updated' => $updated,
177 'config' => array('search_type' => $cfg_array['search_type']),
183 trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
189 if (!$this->init_search($config['search_type'], $search, $error))
193 if (method_exists($search, 'config_updated'))
195 if ($search->config_updated())
197 trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
204 trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
207 trigger_error($user->lang['CONFIG_UPDATED'] . $extra_message . adm_back_link($this->u_action));
211 $this->tpl_name = 'acp_search';
212 $this->page_title = 'ACP_SEARCH_SETTINGS';
214 $template->assign_vars(array(
215 'LIMIT_SEARCH_LOAD' => (float) $config['limit_search_load'],
216 'MIN_SEARCH_AUTHOR_CHARS' => (int) $config['min_search_author_chars'],
217 'SEARCH_INTERVAL' => (float) $config['search_interval'],
218 'SEARCH_GUEST_INTERVAL' => (float) $config['search_anonymous_interval'],
219 'SEARCH_STORE_RESULTS' => (int) $config['search_store_results'],
220 'MAX_NUM_SEARCH_KEYWORDS' => (int) $config['max_num_search_keywords'],
222 'S_SEARCH_TYPES' => $search_options,
223 'S_YES_SEARCH' => (bool) $config['load_search'],
224 'S_SETTINGS' => true,
226 'U_ACTION' => $this->u_action)
230 function index($id, $mode)
232 global $db, $user, $auth, $template, $cache;
233 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
235 if (isset($_REQUEST['action']) && is_array($_REQUEST['action']))
237 $action = request_var('action', array('' => false));
238 $action = key($action);
242 $action = request_var('action', '');
244 $this->state = explode(',', $config['search_indexing_state']);
246 if (isset($_POST['cancel']))
249 $this->state = array();
258 $type = request_var('type', '');
259 $this->display_progress_bar($type);
263 $this->state[1] = 'delete';
267 $this->state[1] = 'create';
271 trigger_error('NO_ACTION', E_USER_ERROR);
275 if (empty($this->state[0]))
277 $this->state[0] = request_var('search_type', '');
280 $this->search = null;
282 if ($this->init_search($this->state[0], $this->search, $error))
284 trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
286 $name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0])));
288 $action = &$this->state[1];
290 $this->max_post_id = $this->get_max_post_id();
292 $post_counter = (isset($this->state[2])) ? $this->state[2] : 0;
293 $this->state[2] = &$post_counter;
299 if (method_exists($this->search, 'delete_index'))
301 // pass a reference to myself so the $search object can make use of save_state() and attributes
302 if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
304 $this->state = array('');
306 trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
311 $starttime = explode(' ', microtime());
312 $starttime = $starttime[1] + $starttime[0];
314 while (still_on_time() && $post_counter <= $this->max_post_id)
316 $sql = 'SELECT post_id, poster_id, forum_id
317 FROM ' . POSTS_TABLE . '
318 WHERE post_id >= ' . (int) ($post_counter + 1) . '
319 AND post_id <= ' . (int) ($post_counter + $this->batch_size);
320 $result = $db->sql_query($sql);
322 $ids = $posters = $forum_ids = array();
323 while ($row = $db->sql_fetchrow($result))
325 $ids[] = $row['post_id'];
326 $posters[] = $row['poster_id'];
327 $forum_ids[] = $row['forum_id'];
329 $db->sql_freeresult($result);
330 $row_count += sizeof($ids);
334 $this->search->index_remove($ids, $posters, $forum_ids);
337 $post_counter += $this->batch_size;
339 // save the current state
342 if ($post_counter <= $this->max_post_id)
344 $mtime = explode(' ', microtime());
345 $totaltime = $mtime[0] + $mtime[1] - $starttime;
346 $rows_per_second = $row_count / $totaltime;
347 meta_refresh(1, append_sid($this->u_action . '&action=delete&skip_rows=' . $post_counter));
348 trigger_error(sprintf($user->lang['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
352 $this->search->tidy();
354 $this->state = array('');
357 add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
358 trigger_error($user->lang['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action) . $this->close_popup_js());
362 if (method_exists($this->search, 'create_index'))
364 // pass a reference to acp_search so the $search object can make use of save_state() and attributes
365 if ($error = $this->search->create_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=create", false)))
367 $this->state = array('');
369 trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
374 $sql = 'SELECT forum_id, enable_indexing
375 FROM ' . FORUMS_TABLE;
376 $result = $db->sql_query($sql, 3600);
378 while ($row = $db->sql_fetchrow($result))
380 $forums[$row['forum_id']] = (bool) $row['enable_indexing'];
382 $db->sql_freeresult($result);
384 $starttime = explode(' ', microtime());
385 $starttime = $starttime[1] + $starttime[0];
387 while (still_on_time() && $post_counter <= $this->max_post_id)
389 $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
390 FROM ' . POSTS_TABLE . '
391 WHERE post_id >= ' . (int) ($post_counter + 1) . '
392 AND post_id <= ' . (int) ($post_counter + $this->batch_size);
393 $result = $db->sql_query($sql);
395 $buffer = $db->sql_buffer_nested_transactions();
399 $rows = $db->sql_fetchrowset($result);
400 $rows[] = false; // indicate end of array for while loop below
402 $db->sql_freeresult($result);
406 while ($row = ($buffer ? $rows[$i++] : $db->sql_fetchrow($result)))
408 // Indexing enabled for this forum or global announcement?
409 // Global announcements get indexed by default.
410 if (!$row['forum_id'] || (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]))
412 $this->search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
418 $db->sql_freeresult($result);
421 $post_counter += $this->batch_size;
423 // save the current state
426 // pretend the number of posts was as big as the number of ids we indexed so far
427 // just an estimation as it includes deleted posts
428 $num_posts = $config['num_posts'];
429 $config['num_posts'] = min($config['num_posts'], $post_counter);
430 $this->search->tidy();
431 $config['num_posts'] = $num_posts;
433 if ($post_counter <= $this->max_post_id)
435 $mtime = explode(' ', microtime());
436 $totaltime = $mtime[0] + $mtime[1] - $starttime;
437 $rows_per_second = $row_count / $totaltime;
438 meta_refresh(1, append_sid($this->u_action . '&action=create&skip_rows=' . $post_counter));
439 trigger_error(sprintf($user->lang['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
443 $this->search->tidy();
445 $this->state = array('');
448 add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
449 trigger_error($user->lang['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action) . $this->close_popup_js());
454 $search_types = $this->get_search_types();
458 $search_options = '';
459 foreach ($search_types as $type)
461 if ($this->init_search($type, $search, $error) || !method_exists($search, 'index_created'))
466 $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
469 if (method_exists($search, 'index_stats'))
471 $data = $search->index_stats();
474 $statistics = array();
475 foreach ($data as $statistic => $value)
477 $n = sizeof($statistics);
478 if ($n && sizeof($statistics[$n - 1]) < 3)
480 $statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
484 $statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
488 $template->assign_block_vars('backend', array(
492 'S_ACTIVE' => ($type == $config['search_type']) ? true : false,
493 'S_HIDDEN_FIELDS' => build_hidden_fields(array('search_type' => $type)),
494 'S_INDEXED' => (bool) $search->index_created(),
495 'S_STATS' => (bool) sizeof($statistics))
498 foreach ($statistics as $statistic)
500 $template->assign_block_vars('backend.data', array(
501 'STATISTIC_1' => $statistic['statistic_1'],
502 'VALUE_1' => $statistic['value_1'],
503 'STATISTIC_2' => (isset($statistic['statistic_2'])) ? $statistic['statistic_2'] : '',
504 'VALUE_2' => (isset($statistic['value_2'])) ? $statistic['value_2'] : '')
513 $this->tpl_name = 'acp_search';
514 $this->page_title = 'ACP_SEARCH_INDEX';
516 $template->assign_vars(array(
518 'U_ACTION' => $this->u_action,
519 'U_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar"),
520 'UA_PROGRESS_BAR' => addslashes(append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=progress_bar")),
523 if (isset($this->state[1]))
525 $template->assign_vars(array(
526 'S_CONTINUE_INDEXING' => $this->state[1],
527 'U_CONTINUE_INDEXING' => $this->u_action . '&action=' . $this->state[1],
528 'L_CONTINUE' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'],
529 'L_CONTINUE_EXPLAIN' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN'])
534 function display_progress_bar($type)
536 global $template, $user;
538 $l_type = ($type == 'create') ? 'INDEXING_IN_PROGRESS' : 'DELETING_INDEX_IN_PROGRESS';
540 adm_page_header($user->lang[$l_type]);
542 $template->set_filenames(array(
543 'body' => 'progress_bar.html')
546 $template->assign_vars(array(
547 'L_PROGRESS' => $user->lang[$l_type],
548 'L_PROGRESS_EXPLAIN' => $user->lang[$l_type . '_EXPLAIN'])
554 function close_popup_js()
556 return "<script type=\"text/javascript\">\n" .
558 " close_waitscreen = 1;\n" .
563 function get_search_types()
565 global $phpbb_root_path, $phpEx;
567 $search_types = array();
569 $dp = @opendir($phpbb_root_path . 'includes/search');
573 while (($file = readdir($dp)) !== false)
575 if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx"))
577 $search_types[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file);
585 return $search_types;
588 function get_max_post_id()
592 $sql = 'SELECT MAX(post_id) as max_post_id
594 $result = $db->sql_query($sql);
595 $max_post_id = (int) $db->sql_fetchfield('max_post_id');
596 $db->sql_freeresult($result);
601 function save_state($state = false)
605 $this->state = $state;
610 set_config('search_indexing_state', implode(',', $this->state), true);
614 * Initialises a search backend object
616 * @return false if no error occurred else an error message
618 function init_search($type, &$search, &$error)
620 global $phpbb_root_path, $phpEx, $user;
622 if (!preg_match('#^\w+$#', $type) || !file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
624 $error = $user->lang['NO_SUCH_SEARCH_MODULE'];
628 include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
630 if (!class_exists($type))
632 $error = $user->lang['NO_SUCH_SEARCH_MODULE'];
637 $search = new $type($error);