MediaWiki REL1_37
PrefixSearch.php
Go to the documentation of this file.
1<?php
24
33abstract class PrefixSearch {
43 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
44 $search = trim( $search );
45 if ( $search == '' ) {
46 return []; // Return empty result
47 }
48
49 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
50 if ( $hasNamespace !== false ) {
51 list( $search, $namespaces ) = $hasNamespace;
52 }
53
54 return $this->searchBackend( $namespaces, $search, $limit, $offset );
55 }
56
66 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
67 $searches = $this->search( $search, $limit, $namespaces, $offset );
68
69 // if the content language has variants, try to retrieve fallback results
70 $fallbackLimit = $limit - count( $searches );
71 if ( $fallbackLimit > 0 ) {
72 $services = MediaWikiServices::getInstance();
73 $fallbackSearches = $services->getLanguageConverterFactory()
74 ->getLanguageConverter( $services->getContentLanguage() )
75 ->autoConvertToAllVariants( $search );
76 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
77
78 foreach ( $fallbackSearches as $fbs ) {
79 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
80 $searches = array_merge( $searches, $fallbackSearchResult );
81 $fallbackLimit -= count( $fallbackSearchResult );
82
83 if ( $fallbackLimit == 0 ) {
84 break;
85 }
86 }
87 }
88 return $searches;
89 }
90
98 abstract protected function titles( array $titles );
99
107 abstract protected function strings( array $strings );
108
117 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
118 if ( count( $namespaces ) == 1 ) {
119 $ns = $namespaces[0];
120 if ( $ns == NS_MEDIA ) {
121 $namespaces = [ NS_FILE ];
122 } elseif ( $ns == NS_SPECIAL ) {
123 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
124 }
125 }
126 $srchres = [];
127 if ( Hooks::runner()->onPrefixSearchBackend(
128 $namespaces, $search, $limit, $srchres, $offset )
129 ) {
130 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
131 }
132 return $this->strings(
133 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
134 }
135
136 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
137 if ( $offset === 0 ) {
138 // Only perform exact db match if offset === 0
139 // This is still far from perfect but at least we avoid returning the
140 // same title afain and again when the user is scrolling with a query
141 // that matches a title in the db.
142 $rescorer = new SearchExactMatchRescorer();
143 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
144 }
145 return $srchres;
146 }
147
156 protected function specialSearch( $search, $limit, $offset ) {
157 $searchParts = explode( '/', $search, 2 );
158 $searchKey = $searchParts[0];
159 $subpageSearch = $searchParts[1] ?? null;
160
161 // Handle subpage search separately.
162 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
163 if ( $subpageSearch !== null ) {
164 // Try matching the full search string as a page name
165 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
166 if ( !$specialTitle ) {
167 return [];
168 }
169 $special = $spFactory->getPage( $specialTitle->getText() );
170 if ( $special ) {
171 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
172 return array_map( static function ( $sub ) use ( $specialTitle ) {
173 return $specialTitle->getSubpage( $sub );
174 }, $subpages );
175 } else {
176 return [];
177 }
178 }
179
180 # normalize searchKey, so aliases with spaces can be found - T27675
181 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
182 $searchKey = str_replace( ' ', '_', $searchKey );
183 $searchKey = $contLang->caseFold( $searchKey );
184
185 // Unlike SpecialPage itself, we want the canonical forms of both
186 // canonical and alias title forms...
187 $keys = [];
188 foreach ( $spFactory->getNames() as $page ) {
189 $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
190 }
191
192 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
193 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
194 continue;
195 }
196
197 foreach ( $aliases as $key => $alias ) {
198 $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
199 }
200 }
201 ksort( $keys );
202
203 $matches = [];
204 foreach ( $keys as $pageKey => $page ) {
205 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
206 // T29671: Don't use SpecialPage::getTitleFor() here because it
207 // localizes its input leading to searches for e.g. Special:All
208 // returning Spezial:MediaWiki-Systemnachrichten and returning
209 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
210 $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] );
211
212 if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) {
213 // We have enough items in primary rank, no use to continue
214 break;
215 }
216 }
217
218 }
219
220 // Ensure keys are in order
221 ksort( $matches );
222 // Flatten the array
223 $matches = array_reduce( $matches, 'array_merge', [] );
224
225 return array_slice( $matches, $offset, $limit );
226 }
227
240 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
241 if ( !$namespaces ) {
242 $namespaces = [ NS_MAIN ];
243 }
244
245 if ( in_array( NS_SPECIAL, $namespaces ) ) {
246 // For now, if special is included, ignore the other namespaces
247 return $this->specialSearch( $search, $limit, $offset );
248 }
249
250 // Construct suitable prefix for each namespace. They differ in cases where
251 // some namespaces always capitalize and some don't.
252 $prefixes = [];
253 // Allow to do a prefix search for e.g. "Talk:"
254 if ( $search === '' ) {
255 $prefixes[$search] = $namespaces;
256 } else {
257 // Don't just ignore input like "[[Foo]]", but try to search for "Foo"
258 $search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(), '', $search );
259 foreach ( $namespaces as $namespace ) {
260 $title = Title::makeTitleSafe( $namespace, $search );
261 if ( $title ) {
262 $prefixes[ $title->getDBkey() ][] = $namespace;
263 }
264 }
265 }
266 if ( !$prefixes ) {
267 return [];
268 }
269
271 // Often there is only one prefix that applies to all requested namespaces,
272 // but sometimes there are two if some namespaces do not always capitalize.
273 $conds = [];
274 foreach ( $prefixes as $prefix => $namespaces ) {
275 $condition = [ 'page_namespace' => $namespaces ];
276 if ( $prefix !== '' ) {
277 $condition[] = 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() );
278 }
279 $conds[] = $dbr->makeList( $condition, LIST_AND );
280 }
281
282 $table = 'page';
283 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
284 $conds = $dbr->makeList( $conds, LIST_OR );
285 $options = [
286 'LIMIT' => $limit,
287 'ORDER BY' => [ 'page_title', 'page_namespace' ],
288 'OFFSET' => $offset
289 ];
290
291 $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
292
293 return iterator_to_array( TitleArray::newFromResult( $res ) );
294 }
295
302 protected function validateNamespaces( $namespaces ) {
303 // We will look at each given namespace against content language namespaces
304 $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
305 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
306 $valid = [];
307 foreach ( $namespaces as $ns ) {
308 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
309 $valid[] = $ns;
310 }
311 }
312 if ( count( $valid ) > 0 ) {
313 return $valid;
314 }
315 }
316
317 return [ NS_MAIN ];
318 }
319}
const NS_FILE
Definition Defines.php:70
const NS_MAIN
Definition Defines.php:64
const NS_SPECIAL
Definition Defines.php:53
const LIST_OR
Definition Defines.php:46
const NS_MEDIA
Definition Defines.php:52
const LIST_AND
Definition Defines.php:43
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Handles searching prefixes of titles and finding any page names that match.
searchWithVariants( $search, $limit, array $namespaces, $offset=0)
Do a prefix search for all possible variants of the prefix.
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
specialSearch( $search, $limit, $offset)
Prefix search special-case for Special: namespace.
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
defaultSearchBackend( $namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook... This is case-sensitive (First character may be autom...
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset)
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
An utility class to rescore search results by looking for an exact match in the db and add the page f...
static newFromResult( $res)
const DB_REPLICA
Definition defines.php:25