MediaWiki 1.40.4
PrefixSearch.php
Go to the documentation of this file.
1<?php
26
35abstract class PrefixSearch {
45 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
46 $search = trim( $search );
47 if ( $search == '' ) {
48 return []; // Return empty result
49 }
50
51 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
52 if ( $hasNamespace !== false ) {
53 [ $search, $namespaces ] = $hasNamespace;
54 }
55
56 return $this->searchBackend( $namespaces, $search, $limit, $offset );
57 }
58
68 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
69 $searches = $this->search( $search, $limit, $namespaces, $offset );
70
71 // if the content language has variants, try to retrieve fallback results
72 $fallbackLimit = $limit - count( $searches );
73 if ( $fallbackLimit > 0 ) {
74 $services = MediaWikiServices::getInstance();
75 $fallbackSearches = $services->getLanguageConverterFactory()
76 ->getLanguageConverter( $services->getContentLanguage() )
77 ->autoConvertToAllVariants( $search );
78 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
79
80 foreach ( $fallbackSearches as $fbs ) {
81 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
82 $searches = array_merge( $searches, $fallbackSearchResult );
83 $fallbackLimit -= count( $fallbackSearchResult );
84
85 if ( $fallbackLimit == 0 ) {
86 break;
87 }
88 }
89 }
90 return $searches;
91 }
92
100 abstract protected function titles( array $titles );
101
109 abstract protected function strings( array $strings );
110
119 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
120 if ( count( $namespaces ) == 1 ) {
121 $ns = $namespaces[0];
122 if ( $ns == NS_MEDIA ) {
123 $namespaces = [ NS_FILE ];
124 } elseif ( $ns == NS_SPECIAL ) {
125 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
126 }
127 }
128 $srchres = [];
129 if ( Hooks::runner()->onPrefixSearchBackend(
130 $namespaces, $search, $limit, $srchres, $offset )
131 ) {
132 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
133 }
134 return $this->strings(
135 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
136 }
137
138 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
139 if ( $offset === 0 ) {
140 // Only perform exact db match if offset === 0
141 // This is still far from perfect but at least we avoid returning the
142 // same title again and again when the user is scrolling with a query
143 // that matches a title in the db.
144 $rescorer = new SearchExactMatchRescorer();
145 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
146 }
147 return $srchres;
148 }
149
158 protected function specialSearch( $search, $limit, $offset ) {
159 $searchParts = explode( '/', $search, 2 );
160 $searchKey = $searchParts[0];
161 $subpageSearch = $searchParts[1] ?? null;
162
163 // Handle subpage search separately.
164 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
165 if ( $subpageSearch !== null ) {
166 // Try matching the full search string as a page name
167 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
168 if ( !$specialTitle ) {
169 return [];
170 }
171 $special = $spFactory->getPage( $specialTitle->getText() );
172 if ( $special ) {
173 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
174 return array_map( [ $specialTitle, 'getSubpage' ], $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.
Service locator for MediaWiki core services.
The TitleArray class only exists to provide the newFromResult method at pre- sent.
Represents a title within MediaWiki.
Definition Title.php:82
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 ...
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...
const DB_REPLICA
Definition defines.php:26