PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/acp/acp_search.php

http://seo-phpbb.googlecode.com/
PHP | 627 lines | 498 code | 99 blank | 30 comment | 73 complexity | 21805febb3fa16aed871c3692ba6bd49 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package acp
  5. * @version $Id: acp_search.php 8479 2008-03-29 00:22:48Z naderman $
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * @package acp
  19. */
  20. class acp_search
  21. {
  22. var $u_action;
  23. var $state;
  24. var $search;
  25. var $max_post_id;
  26. var $batch_size = 100;
  27. function main($id, $mode)
  28. {
  29. global $user;
  30. $user->add_lang('acp/search');
  31. // For some this may be of help...
  32. @ini_set('memory_limit', '128M');
  33. switch ($mode)
  34. {
  35. case 'settings':
  36. $this->settings($id, $mode);
  37. break;
  38. case 'index':
  39. $this->index($id, $mode);
  40. break;
  41. }
  42. }
  43. function settings($id, $mode)
  44. {
  45. global $db, $user, $auth, $template, $cache;
  46. global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  47. $submit = (isset($_POST['submit'])) ? true : false;
  48. $search_types = $this->get_search_types();
  49. $settings = array(
  50. 'search_interval' => 'float',
  51. 'search_anonymous_interval' => 'float',
  52. 'load_search' => 'bool',
  53. 'limit_search_load' => 'float',
  54. 'min_search_author_chars' => 'integer',
  55. 'search_store_results' => 'integer',
  56. );
  57. $search = null;
  58. $error = false;
  59. $search_options = '';
  60. foreach ($search_types as $type)
  61. {
  62. if ($this->init_search($type, $search, $error))
  63. {
  64. continue;
  65. }
  66. $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
  67. $selected = ($config['search_type'] == $type) ? ' selected="selected"' : '';
  68. $search_options .= '<option value="' . $type . '"' . $selected . '>' . $name . '</option>';
  69. if (method_exists($search, 'acp'))
  70. {
  71. $vars = $search->acp();
  72. if (!$submit)
  73. {
  74. $template->assign_block_vars('backend', array(
  75. 'NAME' => $name,
  76. 'SETTINGS' => $vars['tpl'])
  77. );
  78. }
  79. else if (is_array($vars['config']))
  80. {
  81. $settings = array_merge($settings, $vars['config']);
  82. }
  83. }
  84. }
  85. unset($search);
  86. unset($error);
  87. $cfg_array = (isset($_REQUEST['config'])) ? request_var('config', array('' => ''), true) : array();
  88. $updated = request_var('updated', false);
  89. foreach ($settings as $config_name => $var_type)
  90. {
  91. if (!isset($cfg_array[$config_name]))
  92. {
  93. continue;
  94. }
  95. // e.g. integer:4:12 (min 4, max 12)
  96. $var_type = explode(':', $var_type);
  97. $config_value = $cfg_array[$config_name];
  98. settype($config_value, $var_type[0]);
  99. if (isset($var_type[1]))
  100. {
  101. $config_value = max($var_type[1], $config_value);
  102. }
  103. if (isset($var_type[2]))
  104. {
  105. $config_value = min($var_type[2], $config_value);
  106. }
  107. // only change config if anything was actually changed
  108. if ($submit && ($config[$config_name] != $config_value))
  109. {
  110. set_config($config_name, $config_value);
  111. $updated = true;
  112. }
  113. }
  114. if ($submit)
  115. {
  116. $extra_message = '';
  117. if ($updated)
  118. {
  119. add_log('admin', 'LOG_CONFIG_SEARCH');
  120. }
  121. if (isset($cfg_array['search_type']) && in_array($cfg_array['search_type'], $search_types, true) && ($cfg_array['search_type'] != $config['search_type']))
  122. {
  123. $search = null;
  124. $error = false;
  125. if (!$this->init_search($cfg_array['search_type'], $search, $error))
  126. {
  127. if (confirm_box(true))
  128. {
  129. if (!method_exists($search, 'init') || !($error = $search->init()))
  130. {
  131. set_config('search_type', $cfg_array['search_type']);
  132. if (!$updated)
  133. {
  134. add_log('admin', 'LOG_CONFIG_SEARCH');
  135. }
  136. $extra_message = '<br />' . $user->lang['SWITCHED_SEARCH_BACKEND'] . '<br /><a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=search&amp;mode=index') . '">&raquo; ' . $user->lang['GO_TO_SEARCH_INDEX'] . '</a>';
  137. }
  138. else
  139. {
  140. trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
  141. }
  142. }
  143. else
  144. {
  145. confirm_box(false, $user->lang['CONFIRM_SEARCH_BACKEND'], build_hidden_fields(array(
  146. 'i' => $id,
  147. 'mode' => $mode,
  148. 'submit' => true,
  149. 'updated' => $updated,
  150. 'config' => array('search_type' => $cfg_array['search_type']),
  151. )));
  152. }
  153. }
  154. else
  155. {
  156. trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
  157. }
  158. }
  159. $search = null;
  160. $error = false;
  161. if (!$this->init_search($config['search_type'], $search, $error))
  162. {
  163. if ($updated)
  164. {
  165. if (method_exists($search, 'config_updated'))
  166. {
  167. if ($search->config_updated())
  168. {
  169. trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
  170. }
  171. }
  172. }
  173. }
  174. else
  175. {
  176. trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
  177. }
  178. trigger_error($user->lang['CONFIG_UPDATED'] . $extra_message . adm_back_link($this->u_action));
  179. }
  180. unset($cfg_array);
  181. $this->tpl_name = 'acp_search';
  182. $this->page_title = 'ACP_SEARCH_SETTINGS';
  183. $template->assign_vars(array(
  184. 'LIMIT_SEARCH_LOAD' => (float) $config['limit_search_load'],
  185. 'MIN_SEARCH_AUTHOR_CHARS' => (int) $config['min_search_author_chars'],
  186. 'SEARCH_INTERVAL' => (float) $config['search_interval'],
  187. 'SEARCH_GUEST_INTERVAL' => (float) $config['search_anonymous_interval'],
  188. 'SEARCH_STORE_RESULTS' => (int) $config['search_store_results'],
  189. 'S_SEARCH_TYPES' => $search_options,
  190. 'S_YES_SEARCH' => (bool) $config['load_search'],
  191. 'S_SETTINGS' => true,
  192. 'U_ACTION' => $this->u_action)
  193. );
  194. }
  195. function index($id, $mode)
  196. {
  197. global $db, $user, $auth, $template, $cache;
  198. global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  199. if (isset($_REQUEST['action']) && is_array($_REQUEST['action']))
  200. {
  201. $action = request_var('action', array('' => false));
  202. $action = key($action);
  203. }
  204. else
  205. {
  206. $action = request_var('action', '');
  207. }
  208. $this->state = explode(',', $config['search_indexing_state']);
  209. if (isset($_POST['cancel']))
  210. {
  211. $action = '';
  212. $this->state = array();
  213. $this->save_state();
  214. }
  215. if ($action)
  216. {
  217. switch ($action)
  218. {
  219. case 'progress_bar':
  220. $type = request_var('type', '');
  221. $this->display_progress_bar($type);
  222. break;
  223. case 'delete':
  224. $this->state[1] = 'delete';
  225. break;
  226. case 'create':
  227. $this->state[1] = 'create';
  228. break;
  229. default:
  230. trigger_error('NO_ACTION', E_USER_ERROR);
  231. break;
  232. }
  233. if (empty($this->state[0]))
  234. {
  235. $this->state[0] = request_var('search_type', '');
  236. }
  237. $this->search = null;
  238. $error = false;
  239. if ($this->init_search($this->state[0], $this->search, $error))
  240. {
  241. trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
  242. }
  243. $name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0])));
  244. $action = &$this->state[1];
  245. $this->max_post_id = $this->get_max_post_id();
  246. $post_counter = (isset($this->state[2])) ? $this->state[2] : 0;
  247. $this->state[2] = &$post_counter;
  248. $this->save_state();
  249. switch ($action)
  250. {
  251. case 'delete':
  252. if (method_exists($this->search, 'delete_index'))
  253. {
  254. // pass a reference to myself so the $search object can make use of save_state() and attributes
  255. if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=delete", false)))
  256. {
  257. $this->state = array('');
  258. $this->save_state();
  259. trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
  260. }
  261. }
  262. else
  263. {
  264. $starttime = explode(' ', microtime());
  265. $starttime = $starttime[1] + $starttime[0];
  266. $row_count = 0;
  267. while (still_on_time() && $post_counter <= $this->max_post_id)
  268. {
  269. $sql = 'SELECT post_id, poster_id, forum_id
  270. FROM ' . POSTS_TABLE . '
  271. WHERE post_id >= ' . (int) ($post_counter + 1) . '
  272. AND post_id <= ' . (int) ($post_counter + $this->batch_size);
  273. $result = $db->sql_query($sql);
  274. $ids = $posters = $forum_ids = array();
  275. while ($row = $db->sql_fetchrow($result))
  276. {
  277. $ids[] = $row['post_id'];
  278. $posters[] = $row['poster_id'];
  279. $forum_ids[] = $row['forum_id'];
  280. }
  281. $db->sql_freeresult($result);
  282. $row_count += sizeof($ids);
  283. if (sizeof($ids))
  284. {
  285. $this->search->index_remove($ids, $posters, $forum_ids);
  286. }
  287. $post_counter += $this->batch_size;
  288. }
  289. // save the current state
  290. $this->save_state();
  291. if ($post_counter <= $this->max_post_id)
  292. {
  293. $mtime = explode(' ', microtime());
  294. $totaltime = $mtime[0] + $mtime[1] - $starttime;
  295. $rows_per_second = $row_count / $totaltime;
  296. meta_refresh(1, append_sid($this->u_action . '&amp;action=delete&amp;skip_rows=' . $post_counter));
  297. trigger_error(sprintf($user->lang['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
  298. }
  299. }
  300. $this->search->tidy();
  301. $this->state = array('');
  302. $this->save_state();
  303. add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
  304. trigger_error($user->lang['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action) . $this->close_popup_js());
  305. break;
  306. case 'create':
  307. if (method_exists($this->search, 'create_index'))
  308. {
  309. // pass a reference to acp_search so the $search object can make use of save_state() and attributes
  310. if ($error = $this->search->create_index($this, append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=$mode&action=create", false)))
  311. {
  312. $this->state = array('');
  313. $this->save_state();
  314. trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
  315. }
  316. }
  317. else
  318. {
  319. $sql = 'SELECT forum_id, enable_indexing
  320. FROM ' . FORUMS_TABLE;
  321. $result = $db->sql_query($sql, 3600);
  322. while ($row = $db->sql_fetchrow($result))
  323. {
  324. $forums[$row['forum_id']] = (bool) $row['enable_indexing'];
  325. }
  326. $db->sql_freeresult($result);
  327. $starttime = explode(' ', microtime());
  328. $starttime = $starttime[1] + $starttime[0];
  329. $row_count = 0;
  330. while (still_on_time() && $post_counter <= $this->max_post_id)
  331. {
  332. $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
  333. FROM ' . POSTS_TABLE . '
  334. WHERE post_id >= ' . (int) ($post_counter + 1) . '
  335. AND post_id <= ' . (int) ($post_counter + $this->batch_size);
  336. $result = $db->sql_query($sql);
  337. while ($row = $db->sql_fetchrow($result))
  338. {
  339. // Indexing enabled for this forum or global announcement?
  340. // Global announcements get indexed by default.
  341. if (!$row['forum_id'] || (isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]))
  342. {
  343. $this->search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
  344. }
  345. $row_count++;
  346. }
  347. $db->sql_freeresult($result);
  348. $post_counter += $this->batch_size;
  349. }
  350. // save the current state
  351. $this->save_state();
  352. // pretend the number of posts was as big as the number of ids we indexed so far
  353. // just an estimation as it includes deleted posts
  354. $num_posts = $config['num_posts'];
  355. $config['num_posts'] = min($config['num_posts'], $post_counter);
  356. $this->search->tidy();
  357. $config['num_posts'] = $num_posts;
  358. if ($post_counter <= $this->max_post_id)
  359. {
  360. $mtime = explode(' ', microtime());
  361. $totaltime = $mtime[0] + $mtime[1] - $starttime;
  362. $rows_per_second = $row_count / $totaltime;
  363. meta_refresh(1, append_sid($this->u_action . '&amp;action=create&amp;skip_rows=' . $post_counter));
  364. trigger_error(sprintf($user->lang['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
  365. }
  366. }
  367. $this->search->tidy();
  368. $this->state = array('');
  369. $this->save_state();
  370. add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
  371. trigger_error($user->lang['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action) . $this->close_popup_js());
  372. break;
  373. }
  374. }
  375. $search_types = $this->get_search_types();
  376. $search = null;
  377. $error = false;
  378. $search_options = '';
  379. foreach ($search_types as $type)
  380. {
  381. if ($this->init_search($type, $search, $error) || !method_exists($search, 'index_created'))
  382. {
  383. continue;
  384. }
  385. $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
  386. $data = array();
  387. if (method_exists($search, 'index_stats'))
  388. {
  389. $data = $search->index_stats();
  390. }
  391. $statistics = array();
  392. foreach ($data as $statistic => $value)
  393. {
  394. $n = sizeof($statistics);
  395. if ($n && sizeof($statistics[$n - 1]) < 3)
  396. {
  397. $statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
  398. }
  399. else
  400. {
  401. $statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
  402. }
  403. }
  404. $template->assign_block_vars('backend', array(
  405. 'L_NAME' => $name,
  406. 'NAME' => $type,
  407. 'S_ACTIVE' => ($type == $config['search_type']) ? true : false,
  408. 'S_HIDDEN_FIELDS' => build_hidden_fields(array('search_type' => $type)),
  409. 'S_INDEXED' => (bool) $search->index_created(),
  410. 'S_STATS' => (bool) sizeof($statistics))
  411. );
  412. foreach ($statistics as $statistic)
  413. {
  414. $template->assign_block_vars('backend.data', array(
  415. 'STATISTIC_1' => $statistic['statistic_1'],
  416. 'VALUE_1' => $statistic['value_1'],
  417. 'STATISTIC_2' => (isset($statistic['statistic_2'])) ? $statistic['statistic_2'] : '',
  418. 'VALUE_2' => (isset($statistic['value_2'])) ? $statistic['value_2'] : '')
  419. );
  420. }
  421. }
  422. unset($search);
  423. unset($error);
  424. unset($statistics);
  425. unset($data);
  426. $this->tpl_name = 'acp_search';
  427. $this->page_title = 'ACP_SEARCH_INDEX';
  428. $template->assign_vars(array(
  429. 'S_INDEX' => true,
  430. 'U_ACTION' => $this->u_action,
  431. 'U_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;mode=$mode&amp;action=progress_bar"),
  432. 'UA_PROGRESS_BAR' => addslashes(append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;mode=$mode&amp;action=progress_bar")),
  433. ));
  434. if (isset($this->state[1]))
  435. {
  436. $template->assign_vars(array(
  437. 'S_CONTINUE_INDEXING' => $this->state[1],
  438. 'U_CONTINUE_INDEXING' => $this->u_action . '&amp;action=' . $this->state[1],
  439. 'L_CONTINUE' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'],
  440. 'L_CONTINUE_EXPLAIN' => ($this->state[1] == 'create') ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN'])
  441. );
  442. }
  443. }
  444. function display_progress_bar($type)
  445. {
  446. global $template, $user;
  447. $l_type = ($type == 'create') ? 'INDEXING_IN_PROGRESS' : 'DELETING_INDEX_IN_PROGRESS';
  448. adm_page_header($user->lang[$l_type]);
  449. $template->set_filenames(array(
  450. 'body' => 'progress_bar.html')
  451. );
  452. $template->assign_vars(array(
  453. 'L_PROGRESS' => $user->lang[$l_type],
  454. 'L_PROGRESS_EXPLAIN' => $user->lang[$l_type . '_EXPLAIN'])
  455. );
  456. adm_page_footer();
  457. }
  458. function close_popup_js()
  459. {
  460. return "<script type=\"text/javascript\">\n" .
  461. "// <![CDATA[\n" .
  462. " close_waitscreen = 1;\n" .
  463. "// ]]>\n" .
  464. "</script>\n";
  465. }
  466. function get_search_types()
  467. {
  468. global $phpbb_root_path, $phpEx;
  469. $search_types = array();
  470. $dp = @opendir($phpbb_root_path . 'includes/search');
  471. if ($dp)
  472. {
  473. while (($file = readdir($dp)) !== false)
  474. {
  475. if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx"))
  476. {
  477. $search_types[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file);
  478. }
  479. }
  480. closedir($dp);
  481. sort($search_types);
  482. }
  483. return $search_types;
  484. }
  485. function get_max_post_id()
  486. {
  487. global $db;
  488. $sql = 'SELECT MAX(post_id) as max_post_id
  489. FROM '. POSTS_TABLE;
  490. $result = $db->sql_query($sql);
  491. $max_post_id = (int) $db->sql_fetchfield('max_post_id');
  492. $db->sql_freeresult($result);
  493. return $max_post_id;
  494. }
  495. function save_state($state = false)
  496. {
  497. if ($state)
  498. {
  499. $this->state = $state;
  500. }
  501. ksort($this->state);
  502. set_config('search_indexing_state', implode(',', $this->state));
  503. }
  504. /**
  505. * Initialises a search backend object
  506. *
  507. * @return false if no error occurred else an error message
  508. */
  509. function init_search($type, &$search, &$error)
  510. {
  511. global $phpbb_root_path, $phpEx, $user;
  512. if (!preg_match('#^\w+$#', $type) || !file_exists("{$phpbb_root_path}includes/search/$type.$phpEx"))
  513. {
  514. $error = $user->lang['NO_SUCH_SEARCH_MODULE'];
  515. return $error;
  516. }
  517. include_once("{$phpbb_root_path}includes/search/$type.$phpEx");
  518. if (!class_exists($type))
  519. {
  520. $error = $user->lang['NO_SUCH_SEARCH_MODULE'];
  521. return $error;
  522. }
  523. $error = false;
  524. $search = new $type($error);
  525. return $error;
  526. }
  527. }
  528. ?>