Update oyejorge/less.php to 1.7.0.10
[lhc/web/wiklou.git] / includes / PrefixSearch.php
1 <?php
2 /**
3 * Prefix search of page names.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Handles searching prefixes of titles and finding any page
25 * names that match. Used largely by the OpenSearch implementation.
26 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
27 *
28 * @ingroup Search
29 */
30 abstract class PrefixSearch {
31 /**
32 * Do a prefix search of titles and return a list of matching page names.
33 * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
34 *
35 * @param string $search
36 * @param int $limit
37 * @param array $namespaces Used if query is not explicitly prefixed
38 * @param int $offset How many results to offset from the beginning
39 * @return array Array of strings
40 */
41 public static function titleSearch( $search, $limit, $namespaces = array(), $offset = 0 ) {
42 $prefixSearch = new StringPrefixSearch;
43 return $prefixSearch->search( $search, $limit, $namespaces, $offset );
44 }
45
46 /**
47 * Do a prefix search of titles and return a list of matching page names.
48 *
49 * @param string $search
50 * @param int $limit
51 * @param array $namespaces Used if query is not explicitly prefixed
52 * @param int $offset How many results to offset from the beginning
53 * @return array Array of strings or Title objects
54 */
55 public function search( $search, $limit, $namespaces = array(), $offset = 0 ) {
56 $search = trim( $search );
57 if ( $search == '' ) {
58 return array(); // Return empty result
59 }
60 $namespaces = $this->validateNamespaces( $namespaces );
61
62 // Find a Title which is not an interwiki and is in NS_MAIN
63 $title = Title::newFromText( $search );
64 if ( $title && !$title->isExternal() ) {
65 $ns = array( $title->getNamespace() );
66 $search = $title->getText();
67 if ( $ns[0] == NS_MAIN ) {
68 $ns = $namespaces; // no explicit prefix, use default namespaces
69 Hooks::run( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) );
70 }
71 return $this->searchBackend( $ns, $search, $limit, $offset );
72 }
73
74 // Is this a namespace prefix?
75 $title = Title::newFromText( $search . 'Dummy' );
76 if ( $title && $title->getText() == 'Dummy'
77 && $title->getNamespace() != NS_MAIN
78 && !$title->isExternal() )
79 {
80 $namespaces = array( $title->getNamespace() );
81 $search = '';
82 } else {
83 Hooks::run( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) );
84 }
85
86 return $this->searchBackend( $namespaces, $search, $limit, $offset );
87 }
88
89 /**
90 * Do a prefix search for all possible variants of the prefix
91 * @param string $search
92 * @param int $limit
93 * @param array $namespaces
94 * @param int $offset How many results to offset from the beginning
95 *
96 * @return array
97 */
98 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
99 $searches = $this->search( $search, $limit, $namespaces, $offset );
100
101 // if the content language has variants, try to retrieve fallback results
102 $fallbackLimit = $limit - count( $searches );
103 if ( $fallbackLimit > 0 ) {
104 global $wgContLang;
105
106 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
107 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
108
109 foreach ( $fallbackSearches as $fbs ) {
110 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
111 $searches = array_merge( $searches, $fallbackSearchResult );
112 $fallbackLimit -= count( $fallbackSearchResult );
113
114 if ( $fallbackLimit == 0 ) {
115 break;
116 }
117 }
118 }
119 return $searches;
120 }
121
122 /**
123 * When implemented in a descendant class, receives an array of Title objects and returns
124 * either an unmodified array or an array of strings corresponding to titles passed to it.
125 *
126 * @param array $titles
127 * @return array
128 */
129 abstract protected function titles( array $titles );
130
131 /**
132 * When implemented in a descendant class, receives an array of titles as strings and returns
133 * either an unmodified array or an array of Title objects corresponding to strings received.
134 *
135 * @param array $strings
136 *
137 * @return array
138 */
139 abstract protected function strings( array $strings );
140
141 /**
142 * Do a prefix search of titles and return a list of matching page names.
143 * @param array $namespaces
144 * @param string $search
145 * @param int $limit
146 * @param int $offset How many results to offset from the beginning
147 * @return array Array of strings
148 */
149 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
150 if ( count( $namespaces ) == 1 ) {
151 $ns = $namespaces[0];
152 if ( $ns == NS_MEDIA ) {
153 $namespaces = array( NS_FILE );
154 } elseif ( $ns == NS_SPECIAL ) {
155 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
156 }
157 }
158 $srchres = array();
159 if ( Hooks::run(
160 'PrefixSearchBackend',
161 array( $namespaces, $search, $limit, &$srchres, $offset )
162 ) ) {
163 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
164 }
165 return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
166 }
167
168 private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) {
169 $rescorer = new SearchExactMatchRescorer();
170 return $rescorer->rescore( $search, $namespaces, $srchres, $limit );
171 }
172
173 /**
174 * Prefix search special-case for Special: namespace.
175 *
176 * @param string $search Term
177 * @param int $limit Max number of items to return
178 * @param int $offset Number of items to offset
179 * @return array
180 */
181 protected function specialSearch( $search, $limit, $offset ) {
182 global $wgContLang;
183
184 $searchParts = explode( '/', $search, 2 );
185 $searchKey = $searchParts[0];
186 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
187
188 // Handle subpage search separately.
189 if ( $subpageSearch !== null ) {
190 // Try matching the full search string as a page name
191 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
192 if ( !$specialTitle ) {
193 return array();
194 }
195 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
196 if ( $special ) {
197 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
198 return array_map( function ( $sub ) use ( $specialTitle ) {
199 return $specialTitle->getSubpage( $sub );
200 }, $subpages );
201 } else {
202 return array();
203 }
204 }
205
206 # normalize searchKey, so aliases with spaces can be found - bug 25675
207 $searchKey = str_replace( ' ', '_', $searchKey );
208 $searchKey = $wgContLang->caseFold( $searchKey );
209
210 // Unlike SpecialPage itself, we want the canonical forms of both
211 // canonical and alias title forms...
212 $keys = array();
213 foreach ( SpecialPageFactory::getNames() as $page ) {
214 $keys[$wgContLang->caseFold( $page )] = $page;
215 }
216
217 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
218 if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# bug 20885
219 continue;
220 }
221
222 foreach ( $aliases as $alias ) {
223 $keys[$wgContLang->caseFold( $alias )] = $alias;
224 }
225 }
226 ksort( $keys );
227
228 $srchres = array();
229 $skipped = 0;
230 foreach ( $keys as $pageKey => $page ) {
231 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
232 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
233 // localizes its input leading to searches for e.g. Special:All
234 // returning Spezial:MediaWiki-Systemnachrichten and returning
235 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
236 if ( $offset > 0 && $skipped < $offset ) {
237 $skipped++;
238 continue;
239 }
240 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
241 }
242
243 if ( count( $srchres ) >= $limit ) {
244 break;
245 }
246 }
247
248 return $srchres;
249 }
250
251 /**
252 * Unless overridden by PrefixSearchBackend hook...
253 * This is case-sensitive (First character may
254 * be automatically capitalized by Title::secureAndSpit()
255 * later on depending on $wgCapitalLinks)
256 *
257 * @param array $namespaces Namespaces to search in
258 * @param string $search Term
259 * @param int $limit Max number of items to return
260 * @param int $offset Number of items to skip
261 * @return array Array of Title objects
262 */
263 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
264 $ns = array_shift( $namespaces ); // support only one namespace
265 if ( is_null( $ns ) || in_array( NS_MAIN, $namespaces ) ) {
266 $ns = NS_MAIN; // if searching on many always default to main
267 }
268
269 if ( $ns == NS_SPECIAL ) {
270 return $this->specialSearch( $search, $limit, $offset );
271 }
272
273 $t = Title::newFromText( $search, $ns );
274 $prefix = $t ? $t->getDBkey() : '';
275 $dbr = wfGetDB( DB_SLAVE );
276 $res = $dbr->select( 'page',
277 array( 'page_id', 'page_namespace', 'page_title' ),
278 array(
279 'page_namespace' => $ns,
280 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
281 ),
282 __METHOD__,
283 array(
284 'LIMIT' => $limit,
285 'ORDER BY' => 'page_title',
286 'OFFSET' => $offset
287 )
288 );
289 $srchres = array();
290 foreach ( $res as $row ) {
291 $srchres[] = Title::newFromRow( $row );
292 }
293 return $srchres;
294 }
295
296 /**
297 * Validate an array of numerical namespace indexes
298 *
299 * @param array $namespaces
300 * @return array (default: contains only NS_MAIN)
301 */
302 protected function validateNamespaces( $namespaces ) {
303 global $wgContLang;
304
305 // We will look at each given namespace against wgContLang namespaces
306 $validNamespaces = $wgContLang->getNamespaces();
307 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
308 $valid = array();
309 foreach ( $namespaces as $ns ) {
310 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
311 $valid[] = $ns;
312 }
313 }
314 if ( count( $valid ) > 0 ) {
315 return $valid;
316 }
317 }
318
319 return array( NS_MAIN );
320 }
321 }
322
323 /**
324 * Performs prefix search, returning Title objects
325 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
326 * @ingroup Search
327 */
328 class TitlePrefixSearch extends PrefixSearch {
329
330 protected function titles( array $titles ) {
331 return $titles;
332 }
333
334 protected function strings( array $strings ) {
335 $titles = array_map( 'Title::newFromText', $strings );
336 $lb = new LinkBatch( $titles );
337 $lb->setCaller( __METHOD__ );
338 $lb->execute();
339 return $titles;
340 }
341 }
342
343 /**
344 * Performs prefix search, returning strings
345 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
346 * @ingroup Search
347 */
348 class StringPrefixSearch extends PrefixSearch {
349
350 protected function titles( array $titles ) {
351 return array_map( function ( Title $t ) {
352 return $t->getPrefixedText();
353 }, $titles );
354 }
355
356 protected function strings( array $strings ) {
357 return $strings;
358 }
359 }