[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/acp/ -> acp_search.php (source)

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


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6