[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/search/ -> base.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\search;
  15  
  16  /**
  17  * @ignore
  18  */
  19  define('SEARCH_RESULT_NOT_IN_CACHE', 0);
  20  define('SEARCH_RESULT_IN_CACHE', 1);
  21  define('SEARCH_RESULT_INCOMPLETE', 2);
  22  
  23  /**
  24  * optional base class for search plugins providing simple caching based on ACM
  25  * and functions to retrieve ignore_words and synonyms
  26  */
  27  class base
  28  {
  29      var $ignore_words = array();
  30      var $match_synonym = array();
  31      var $replace_synonym = array();
  32  
  33  	function search_backend(&$error)
  34      {
  35          // This class cannot be used as a search plugin
  36          $error = true;
  37      }
  38  
  39      /**
  40      * Retrieves cached search results
  41      *
  42      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
  43      * @param int    &$result_count    will contain the number of all results for the search (not only for the current page)
  44      * @param array     &$id_ary         is filled with the ids belonging to the requested page that are stored in the cache
  45      * @param int     &$start            indicates the first index of the page
  46      * @param int     $per_page        number of ids each page is supposed to contain
  47      * @param string $sort_dir        is either a or d representing ASC and DESC
  48      *
  49      * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
  50      */
  51  	function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $sort_dir)
  52      {
  53          global $cache;
  54  
  55          if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
  56          {
  57              // no search results cached for this search_key
  58              return SEARCH_RESULT_NOT_IN_CACHE;
  59          }
  60          else
  61          {
  62              $result_count = $stored_ids[-1];
  63              $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
  64              $complete = true;
  65  
  66              // Change start parameter in case out of bounds
  67              if ($result_count)
  68              {
  69                  if ($start < 0)
  70                  {
  71                      $start = 0;
  72                  }
  73                  else if ($start >= $result_count)
  74                  {
  75                      $start = floor(($result_count - 1) / $per_page) * $per_page;
  76                  }
  77              }
  78  
  79              // change the start to the actual end of the current request if the sort direction differs
  80              // from the dirction in the cache and reverse the ids later
  81              if ($reverse_ids)
  82              {
  83                  $start = $result_count - $start - $per_page;
  84  
  85                  // the user requested a page past the last index
  86                  if ($start < 0)
  87                  {
  88                      return SEARCH_RESULT_NOT_IN_CACHE;
  89                  }
  90              }
  91  
  92              for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
  93              {
  94                  if (!isset($stored_ids[$i]))
  95                  {
  96                      $complete = false;
  97                  }
  98                  else
  99                  {
 100                      $id_ary[] = $stored_ids[$i];
 101                  }
 102              }
 103              unset($stored_ids);
 104  
 105              if ($reverse_ids)
 106              {
 107                  $id_ary = array_reverse($id_ary);
 108              }
 109  
 110              if (!$complete)
 111              {
 112                  return SEARCH_RESULT_INCOMPLETE;
 113              }
 114              return SEARCH_RESULT_IN_CACHE;
 115          }
 116      }
 117  
 118      /**
 119      * Caches post/topic ids
 120      *
 121      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
 122      * @param string $keywords         contains the keywords as entered by the user
 123      * @param array    $author_ary        an array of author ids, if the author should be ignored during the search the array is empty
 124      * @param int     $result_count    contains the number of all results for the search (not only for the current page)
 125      * @param array    &$id_ary         contains a list of post or topic ids that shall be cached, the first element
 126      *     must have the absolute index $start in the result set.
 127      * @param int    $start            indicates the first index of the page
 128      * @param string $sort_dir        is either a or d representing ASC and DESC
 129      *
 130      * @return null
 131      */
 132  	function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
 133      {
 134          global $cache, $config, $db, $user;
 135  
 136          $length = min(count($id_ary), $config['search_block_size']);
 137  
 138          // nothing to cache so exit
 139          if (!$length)
 140          {
 141              return;
 142          }
 143  
 144          $store_ids = array_slice($id_ary, 0, $length);
 145  
 146          // create a new resultset if there is none for this search_key yet
 147          // or add the ids to the existing resultset
 148          if (!($store = $cache->get('_search_results_' . $search_key)))
 149          {
 150              // add the current keywords to the recent searches in the cache which are listed on the search page
 151              if (!empty($keywords) || count($author_ary))
 152              {
 153                  $sql = 'SELECT search_time
 154                      FROM ' . SEARCH_RESULTS_TABLE . '
 155                      WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
 156                  $result = $db->sql_query($sql);
 157  
 158                  if (!$db->sql_fetchrow($result))
 159                  {
 160                      $sql_ary = array(
 161                          'search_key'        => $search_key,
 162                          'search_time'        => time(),
 163                          'search_keywords'    => $keywords,
 164                          'search_authors'    => ' ' . implode(' ', $author_ary) . ' '
 165                      );
 166  
 167                      $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 168                      $db->sql_query($sql);
 169                  }
 170                  $db->sql_freeresult($result);
 171              }
 172  
 173              $sql = 'UPDATE ' . USERS_TABLE . '
 174                  SET user_last_search = ' . time() . '
 175                  WHERE user_id = ' . $user->data['user_id'];
 176              $db->sql_query($sql);
 177  
 178              $store = array(-1 => $result_count, -2 => $sort_dir);
 179              $id_range = range($start, $start + $length - 1);
 180          }
 181          else
 182          {
 183              // we use one set of results for both sort directions so we have to calculate the indizes
 184              // for the reversed array and we also have to reverse the ids themselves
 185              if ($store[-2] != $sort_dir)
 186              {
 187                  $store_ids = array_reverse($store_ids);
 188                  $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
 189              }
 190              else
 191              {
 192                  $id_range = range($start, $start + $length - 1);
 193              }
 194          }
 195  
 196          $store_ids = array_combine($id_range, $store_ids);
 197  
 198          // append the ids
 199          if (is_array($store_ids))
 200          {
 201              $store += $store_ids;
 202  
 203              // if the cache is too big
 204              if (count($store) - 2 > 20 * $config['search_block_size'])
 205              {
 206                  // remove everything in front of two blocks in front of the current start index
 207                  for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
 208                  {
 209                      if (isset($store[$i]))
 210                      {
 211                          unset($store[$i]);
 212                      }
 213                  }
 214  
 215                  // remove everything after two blocks after the current stop index
 216                  end($id_range);
 217                  for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
 218                  {
 219                      if (isset($store[$i]))
 220                      {
 221                          unset($store[$i]);
 222                      }
 223                  }
 224              }
 225              $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
 226  
 227              $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
 228                  SET search_time = ' . time() . '
 229                  WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
 230              $db->sql_query($sql);
 231          }
 232  
 233          unset($store);
 234          unset($store_ids);
 235          unset($id_range);
 236      }
 237  
 238      /**
 239      * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
 240      */
 241  	function destroy_cache($words, $authors = false)
 242      {
 243          global $db, $cache, $config;
 244  
 245          // clear all searches that searched for the specified words
 246          if (count($words))
 247          {
 248              $sql_where = '';
 249              foreach ($words as $word)
 250              {
 251                  $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->get_any_char() . $word . $db->get_any_char());
 252              }
 253  
 254              $sql = 'SELECT search_key
 255                  FROM ' . SEARCH_RESULTS_TABLE . "
 256                  WHERE search_keywords LIKE '%*%' $sql_where";
 257              $result = $db->sql_query($sql);
 258  
 259              while ($row = $db->sql_fetchrow($result))
 260              {
 261                  $cache->destroy('_search_results_' . $row['search_key']);
 262              }
 263              $db->sql_freeresult($result);
 264          }
 265  
 266          // clear all searches that searched for the specified authors
 267          if (is_array($authors) && count($authors))
 268          {
 269              $sql_where = '';
 270              foreach ($authors as $author)
 271              {
 272                  $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors ' . $db->sql_like_expression($db->get_any_char() . ' ' . (int) $author . ' ' . $db->get_any_char());
 273              }
 274  
 275              $sql = 'SELECT search_key
 276                  FROM ' . SEARCH_RESULTS_TABLE . "
 277                  WHERE $sql_where";
 278              $result = $db->sql_query($sql);
 279  
 280              while ($row = $db->sql_fetchrow($result))
 281              {
 282                  $cache->destroy('_search_results_' . $row['search_key']);
 283              }
 284              $db->sql_freeresult($result);
 285          }
 286  
 287          $sql = 'DELETE
 288              FROM ' . SEARCH_RESULTS_TABLE . '
 289              WHERE search_time < ' . (time() - (int) $config['search_store_results']);
 290          $db->sql_query($sql);
 291      }
 292  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1