Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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 // change the start to the actual end of the current request if the sort direction differs
080 // from the dirction in the cache and reverse the ids later
081 if ($reverse_ids)
082 {
083 $start = $result_count - $start - $per_page;
084
085 // the user requested a page past the last index
086 if ($start < 0)
087 {
088 return SEARCH_RESULT_NOT_IN_CACHE;
089 }
090 }
091
092 for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
093 {
094 if (!isset($stored_ids[$i]))
095 {
096 $complete = false;
097 }
098 else
099 {
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(sizeof($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) || sizeof($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 (sizeof($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 (sizeof($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) && sizeof($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 }
293