Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

base.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 9.19 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\search;
 015   
 016  /**
 017  * @ignore
 018  */
 019  define('SEARCH_RESULT_NOT_IN_CACHE', 0);
 020  define('SEARCH_RESULT_IN_CACHE', 1);
 021  define('SEARCH_RESULT_INCOMPLETE', 2);
 022   
 023  /**
 024  * optional base class for search plugins providing simple caching based on ACM
 025  * and functions to retrieve ignore_words and synonyms
 026  */
 027  class base
 028  {
 029      var $ignore_words = array();
 030      var $match_synonym = array();
 031      var $replace_synonym = array();
 032   
 033      function search_backend(&$error)
 034      {
 035          // This class cannot be used as a search plugin
 036          $error = true;
 037      }
 038   
 039      /**
 040      * Retrieves a language dependend list of words that should be ignored by the search
 041      */
 042      function get_ignore_words()
 043      {
 044          if (!sizeof($this->ignore_words))
 045          {
 046              global $user, $phpEx;
 047   
 048              $words = array();
 049   
 050              if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx"))
 051              {
 052                  // include the file containing ignore words
 053                  include("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx");
 054              }
 055   
 056              $this->ignore_words = $words;
 057              unset($words);
 058          }
 059      }
 060   
 061      /**
 062      * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
 063      */
 064      function get_synonyms()
 065      {
 066          if (!sizeof($this->match_synonym))
 067          {
 068              global $user, $phpEx;
 069   
 070              $synonyms = array();
 071   
 072              if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx"))
 073              {
 074                  // include the file containing synonyms
 075                  include("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx");
 076              }
 077   
 078              $this->match_synonym = array_keys($synonyms);
 079              $this->replace_synonym = array_values($synonyms);
 080   
 081              unset($synonyms);
 082          }
 083      }
 084   
 085      /**
 086      * Retrieves cached search results
 087      *
 088      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
 089      * @param int    &$result_count    will contain the number of all results for the search (not only for the current page)
 090      * @param array     &$id_ary         is filled with the ids belonging to the requested page that are stored in the cache
 091      * @param int     &$start            indicates the first index of the page
 092      * @param int     $per_page        number of ids each page is supposed to contain
 093      * @param string $sort_dir        is either a or d representing ASC and DESC
 094      *
 095      * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
 096      */
 097      function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $sort_dir)
 098      {
 099          global $cache;
 100   
 101          if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
 102          {
 103              // no search results cached for this search_key
 104              return SEARCH_RESULT_NOT_IN_CACHE;
 105          }
 106          else
 107          {
 108              $result_count = $stored_ids[-1];
 109              $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
 110              $complete = true;
 111   
 112              // Change start parameter in case out of bounds
 113              if ($result_count)
 114              {
 115                  if ($start < 0)
 116                  {
 117                      $start = 0;
 118                  }
 119                  else if ($start >= $result_count)
 120                  {
 121                      $start = floor(($result_count - 1) / $per_page) * $per_page;
 122                  }
 123              }
 124   
 125              // change the start to the actual end of the current request if the sort direction differs
 126              // from the dirction in the cache and reverse the ids later
 127              if ($reverse_ids)
 128              {
 129                  $start = $result_count - $start - $per_page;
 130   
 131                  // the user requested a page past the last index
 132                  if ($start < 0)
 133                  {
 134                      return SEARCH_RESULT_NOT_IN_CACHE;
 135                  }
 136              }
 137   
 138              for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
 139              {
 140                  if (!isset($stored_ids[$i]))
 141                  {
 142                      $complete = false;
 143                  }
 144                  else
 145                  {
 146                      $id_ary[] = $stored_ids[$i];
 147                  }
 148              }
 149              unset($stored_ids);
 150   
 151              if ($reverse_ids)
 152              {
 153                  $id_ary = array_reverse($id_ary);
 154              }
 155   
 156              if (!$complete)
 157              {
 158                  return SEARCH_RESULT_INCOMPLETE;
 159              }
 160              return SEARCH_RESULT_IN_CACHE;
 161          }
 162      }
 163   
 164      /**
 165      * Caches post/topic ids
 166      *
 167      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
 168      * @param string $keywords         contains the keywords as entered by the user
 169      * @param array    $author_ary        an array of author ids, if the author should be ignored during the search the array is empty
 170      * @param int     $result_count    contains the number of all results for the search (not only for the current page)
 171      * @param array    &$id_ary         contains a list of post or topic ids that shall be cached, the first element
 172      *     must have the absolute index $start in the result set.
 173      * @param int    $start            indicates the first index of the page
 174      * @param string $sort_dir        is either a or d representing ASC and DESC
 175      *
 176      * @return null
 177      */
 178      function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
 179      {
 180          global $cache, $config, $db, $user;
 181   
 182          $length = min(sizeof($id_ary), $config['search_block_size']);
 183   
 184          // nothing to cache so exit
 185          if (!$length)
 186          {
 187              return;
 188          }
 189   
 190          $store_ids = array_slice($id_ary, 0, $length);
 191   
 192          // create a new resultset if there is none for this search_key yet
 193          // or add the ids to the existing resultset
 194          if (!($store = $cache->get('_search_results_' . $search_key)))
 195          {
 196              // add the current keywords to the recent searches in the cache which are listed on the search page
 197              if (!empty($keywords) || sizeof($author_ary))
 198              {
 199                  $sql = 'SELECT search_time
 200                      FROM ' . SEARCH_RESULTS_TABLE . '
 201                      WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
 202                  $result = $db->sql_query($sql);
 203   
 204                  if (!$db->sql_fetchrow($result))
 205                  {
 206                      $sql_ary = array(
 207                          'search_key'        => $search_key,
 208                          'search_time'        => time(),
 209                          'search_keywords'    => $keywords,
 210                          'search_authors'    => ' ' . implode(' ', $author_ary) . ' '
 211                      );
 212   
 213                      $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 214                      $db->sql_query($sql);
 215                  }
 216                  $db->sql_freeresult($result);
 217              }
 218   
 219              $sql = 'UPDATE ' . USERS_TABLE . '
 220                  SET user_last_search = ' . time() . '
 221                  WHERE user_id = ' . $user->data['user_id'];
 222              $db->sql_query($sql);
 223   
 224              $store = array(-1 => $result_count, -2 => $sort_dir);
 225              $id_range = range($start, $start + $length - 1);
 226          }
 227          else
 228          {
 229              // we use one set of results for both sort directions so we have to calculate the indizes
 230              // for the reversed array and we also have to reverse the ids themselves
 231              if ($store[-2] != $sort_dir)
 232              {
 233                  $store_ids = array_reverse($store_ids);
 234                  $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
 235              }
 236              else
 237              {
 238                  $id_range = range($start, $start + $length - 1);
 239              }
 240          }
 241   
 242          $store_ids = array_combine($id_range, $store_ids);
 243   
 244          // append the ids
 245          if (is_array($store_ids))
 246          {
 247              $store += $store_ids;
 248   
 249              // if the cache is too big
 250              if (sizeof($store) - 2 > 20 * $config['search_block_size'])
 251              {
 252                  // remove everything in front of two blocks in front of the current start index
 253                  for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
 254                  {
 255                      if (isset($store[$i]))
 256                      {
 257                          unset($store[$i]);
 258                      }
 259                  }
 260   
 261                  // remove everything after two blocks after the current stop index
 262                  end($id_range);
 263                  for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
 264                  {
 265                      if (isset($store[$i]))
 266                      {
 267                          unset($store[$i]);
 268                      }
 269                  }
 270              }
 271              $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
 272   
 273              $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
 274                  SET search_time = ' . time() . '
 275                  WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
 276              $db->sql_query($sql);
 277          }
 278   
 279          unset($store);
 280          unset($store_ids);
 281          unset($id_range);
 282      }
 283   
 284      /**
 285      * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
 286      */
 287      function destroy_cache($words, $authors = false)
 288      {
 289          global $db, $cache, $config;
 290   
 291          // clear all searches that searched for the specified words
 292          if (sizeof($words))
 293          {
 294              $sql_where = '';
 295              foreach ($words as $word)
 296              {
 297                  $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->get_any_char() . $word . $db->get_any_char());
 298              }
 299   
 300              $sql = 'SELECT search_key
 301                  FROM ' . SEARCH_RESULTS_TABLE . "
 302                  WHERE search_keywords LIKE '%*%' $sql_where";
 303              $result = $db->sql_query($sql);
 304   
 305              while ($row = $db->sql_fetchrow($result))
 306              {
 307                  $cache->destroy('_search_results_' . $row['search_key']);
 308              }
 309              $db->sql_freeresult($result);
 310          }
 311   
 312          // clear all searches that searched for the specified authors
 313          if (is_array($authors) && sizeof($authors))
 314          {
 315              $sql_where = '';
 316              foreach ($authors as $author)
 317              {
 318                  $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors ' . $db->sql_like_expression($db->get_any_char() . ' ' . (int) $author . ' ' . $db->get_any_char());
 319              }
 320   
 321              $sql = 'SELECT search_key
 322                  FROM ' . SEARCH_RESULTS_TABLE . "
 323                  WHERE $sql_where";
 324              $result = $db->sql_query($sql);
 325   
 326              while ($row = $db->sql_fetchrow($result))
 327              {
 328                  $cache->destroy('_search_results_' . $row['search_key']);
 329              }
 330              $db->sql_freeresult($result);
 331          }
 332   
 333          $sql = 'DELETE
 334              FROM ' . SEARCH_RESULTS_TABLE . '
 335              WHERE search_time < ' . (time() - $config['search_store_results']);
 336          $db->sql_query($sql);
 337      }
 338  }
 339