Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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: 02.04.2025, 15:02 - Dateigröße: 7.87 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 cached search results
041      *
042      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
043      * @param int    &$result_count    will contain the number of all results for the search (not only for the current page)
044      * @param array     &$id_ary         is filled with the ids belonging to the requested page that are stored in the cache
045      * @param int     &$start            indicates the first index of the page
046      * @param int     $per_page        number of ids each page is supposed to contain
047      * @param string $sort_dir        is either a or d representing ASC and DESC
048      *
049      * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
050      */
051      function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $sort_dir)
052      {
053          global $cache;
054   
055          if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
056          {
057              // no search results cached for this search_key
058              return SEARCH_RESULT_NOT_IN_CACHE;
059          }
060          else
061          {
062              $result_count = $stored_ids[-1];
063              $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
064              $complete = true;
065   
066              // Change start parameter in case out of bounds
067              if ($result_count)
068              {
069                  if ($start < 0)
070                  {
071                      $start = 0;
072                  }
073                  else if ($start >= $result_count)
074                  {
075                      $start = floor(($result_count - 1) / $per_page) * $per_page;
076                  }
077              }
078   
079              // If the sort direction differs from the direction in the cache, then reverse the ids array
080              if ($reverse_ids)
081              {
082                  $stored_ids = array_reverse($stored_ids);
083              }
084   
085              for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
086              {
087                  if (!isset($stored_ids[$i]))
088                  {
089                      $complete = false;
090                  }
091                  else
092                  {
093                      $id_ary[] = $stored_ids[$i];
094                  }
095              }
096              unset($stored_ids);
097   
098              if (!$complete)
099              {
100                  return SEARCH_RESULT_INCOMPLETE;
101              }
102              return SEARCH_RESULT_IN_CACHE;
103          }
104      }
105   
106      /**
107      * Caches post/topic ids
108      *
109      * @param string $search_key        an md5 string generated from all the passed search options to identify the results
110      * @param string $keywords         contains the keywords as entered by the user
111      * @param array    $author_ary        an array of author ids, if the author should be ignored during the search the array is empty
112      * @param int     $result_count    contains the number of all results for the search (not only for the current page)
113      * @param array    &$id_ary         contains a list of post or topic ids that shall be cached, the first element
114      *     must have the absolute index $start in the result set.
115      * @param int    $start            indicates the first index of the page
116      * @param string $sort_dir        is either a or d representing ASC and DESC
117      *
118      * @return null
119      */
120      function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
121      {
122          global $cache, $config, $db, $user;
123   
124          $length = min(count($id_ary), $config['search_block_size']);
125   
126          // nothing to cache so exit
127          if (!$length)
128          {
129              return;
130          }
131   
132          $store_ids = array_slice($id_ary, 0, $length);
133   
134          // create a new resultset if there is none for this search_key yet
135          // or add the ids to the existing resultset
136          if (!($store = $cache->get('_search_results_' . $search_key)))
137          {
138              // add the current keywords to the recent searches in the cache which are listed on the search page
139              if (!empty($keywords) || count($author_ary))
140              {
141                  $sql = 'SELECT search_time
142                      FROM ' . SEARCH_RESULTS_TABLE . '
143                      WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
144                  $result = $db->sql_query($sql);
145   
146                  if (!$db->sql_fetchrow($result))
147                  {
148                      $sql_ary = array(
149                          'search_key'        => $search_key,
150                          'search_time'        => time(),
151                          'search_keywords'    => $keywords,
152                          'search_authors'    => ' ' . implode(' ', $author_ary) . ' '
153                      );
154   
155                      $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
156                      $db->sql_query($sql);
157                  }
158                  $db->sql_freeresult($result);
159              }
160   
161              $sql = 'UPDATE ' . USERS_TABLE . '
162                  SET user_last_search = ' . time() . '
163                  WHERE user_id = ' . $user->data['user_id'];
164              $db->sql_query($sql);
165   
166              $store = array(-1 => $result_count, -2 => $sort_dir);
167              $id_range = range($start, $start + $length - 1);
168          }
169          else
170          {
171              // we use one set of results for both sort directions so we have to calculate the indizes
172              // for the reversed array and we also have to reverse the ids themselves
173              if ($store[-2] != $sort_dir)
174              {
175                  $store_ids = array_reverse($store_ids);
176                  $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
177              }
178              else
179              {
180                  $id_range = range($start, $start + $length - 1);
181              }
182          }
183   
184          $store_ids = array_combine($id_range, $store_ids);
185   
186          // append the ids
187          if (is_array($store_ids))
188          {
189              $store += $store_ids;
190   
191              // if the cache is too big
192              if (count($store) - 2 > 20 * $config['search_block_size'])
193              {
194                  // remove everything in front of two blocks in front of the current start index
195                  for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
196                  {
197                      if (isset($store[$i]))
198                      {
199                          unset($store[$i]);
200                      }
201                  }
202   
203                  // remove everything after two blocks after the current stop index
204                  end($id_range);
205                  for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
206                  {
207                      if (isset($store[$i]))
208                      {
209                          unset($store[$i]);
210                      }
211                  }
212              }
213              $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
214   
215              $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
216                  SET search_time = ' . time() . '
217                  WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
218              $db->sql_query($sql);
219          }
220   
221          unset($store);
222          unset($store_ids);
223          unset($id_range);
224      }
225   
226      /**
227      * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
228      */
229      function destroy_cache($words, $authors = false)
230      {
231          global $db, $cache, $config;
232   
233          // clear all searches that searched for the specified words
234          if (count($words))
235          {
236              $sql_where = '';
237              foreach ($words as $word)
238              {
239                  $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->get_any_char() . $word . $db->get_any_char());
240              }
241   
242              $sql = 'SELECT search_key
243                  FROM ' . SEARCH_RESULTS_TABLE . "
244                  WHERE search_keywords LIKE '%*%' $sql_where";
245              $result = $db->sql_query($sql);
246   
247              while ($row = $db->sql_fetchrow($result))
248              {
249                  $cache->destroy('_search_results_' . $row['search_key']);
250              }
251              $db->sql_freeresult($result);
252          }
253   
254          // clear all searches that searched for the specified authors
255          if (is_array($authors) && count($authors))
256          {
257              $sql_where = '';
258              foreach ($authors as $author)
259              {
260                  $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors ' . $db->sql_like_expression($db->get_any_char() . ' ' . (int) $author . ' ' . $db->get_any_char());
261              }
262   
263              $sql = 'SELECT search_key
264                  FROM ' . SEARCH_RESULTS_TABLE . "
265                  WHERE $sql_where";
266              $result = $db->sql_query($sql);
267   
268              while ($row = $db->sql_fetchrow($result))
269              {
270                  $cache->destroy('_search_results_' . $row['search_key']);
271              }
272              $db->sql_freeresult($result);
273          }
274   
275          $sql = 'DELETE
276              FROM ' . SEARCH_RESULTS_TABLE . '
277              WHERE search_time < ' . (time() - (int) $config['search_store_results']);
278          $db->sql_query($sql);
279      }
280  }
281