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