3 * Prefix search of page names.
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.
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.
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
23 use MediaWiki\MediaWikiServices
;
26 * Handles searching prefixes of titles and finding any page
27 * names that match. Used largely by the OpenSearch implementation.
28 * @deprecated Since 1.27, Use SearchEngine::defaultPrefixSearch or SearchEngine::completionSearch
32 abstract class PrefixSearch
{
34 * Do a prefix search of titles and return a list of matching page names.
35 * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
37 * @param string $search
39 * @param array $namespaces Used if query is not explicitly prefixed
40 * @param int $offset How many results to offset from the beginning
41 * @return array Array of strings
43 public static function titleSearch( $search, $limit, $namespaces = [], $offset = 0 ) {
44 wfDeprecated( __METHOD__
, '1.34' );
45 $prefixSearch = new StringPrefixSearch
;
46 return $prefixSearch->search( $search, $limit, $namespaces, $offset );
50 * Do a prefix search of titles and return a list of matching page names.
52 * @param string $search
54 * @param array $namespaces Used if query is not explicitly prefixed
55 * @param int $offset How many results to offset from the beginning
56 * @return array Array of strings or Title objects
58 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
59 $search = trim( $search );
60 if ( $search == '' ) {
61 return []; // Return empty result
64 $hasNamespace = SearchEngine
::parseNamespacePrefixes( $search, false, true );
65 if ( $hasNamespace !== false ) {
66 list( $search, $namespaces ) = $hasNamespace;
69 return $this->searchBackend( $namespaces, $search, $limit, $offset );
73 * Do a prefix search for all possible variants of the prefix
74 * @param string $search
76 * @param array $namespaces
77 * @param int $offset How many results to offset from the beginning
81 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
82 $searches = $this->search( $search, $limit, $namespaces, $offset );
84 // if the content language has variants, try to retrieve fallback results
85 $fallbackLimit = $limit - count( $searches );
86 if ( $fallbackLimit > 0 ) {
87 $fallbackSearches = MediaWikiServices
::getInstance()->getContentLanguage()->
88 autoConvertToAllVariants( $search );
89 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
91 foreach ( $fallbackSearches as $fbs ) {
92 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
93 $searches = array_merge( $searches, $fallbackSearchResult );
94 $fallbackLimit -= count( $fallbackSearchResult );
96 if ( $fallbackLimit == 0 ) {
105 * When implemented in a descendant class, receives an array of Title objects and returns
106 * either an unmodified array or an array of strings corresponding to titles passed to it.
108 * @param array $titles
111 abstract protected function titles( array $titles );
114 * When implemented in a descendant class, receives an array of titles as strings and returns
115 * either an unmodified array or an array of Title objects corresponding to strings received.
117 * @param array $strings
121 abstract protected function strings( array $strings );
124 * Do a prefix search of titles and return a list of matching page names.
125 * @param array $namespaces
126 * @param string $search
128 * @param int $offset How many results to offset from the beginning
129 * @return array Array of strings
131 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
132 if ( count( $namespaces ) == 1 ) {
133 $ns = $namespaces[0];
134 if ( $ns == NS_MEDIA
) {
135 $namespaces = [ NS_FILE
];
136 } elseif ( $ns == NS_SPECIAL
) {
137 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
142 'PrefixSearchBackend',
143 [ $namespaces, $search, $limit, &$srchres, $offset ]
145 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
147 return $this->strings(
148 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
151 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
152 if ( $offset === 0 ) {
153 // Only perform exact db match if offset === 0
154 // This is still far from perfect but at least we avoid returning the
155 // same title afain and again when the user is scrolling with a query
156 // that matches a title in the db.
157 $rescorer = new SearchExactMatchRescorer();
158 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
164 * Prefix search special-case for Special: namespace.
166 * @param string $search Term
167 * @param int $limit Max number of items to return
168 * @param int $offset Number of items to offset
171 protected function specialSearch( $search, $limit, $offset ) {
172 $searchParts = explode( '/', $search, 2 );
173 $searchKey = $searchParts[0];
174 $subpageSearch = $searchParts[1] ??
null;
176 // Handle subpage search separately.
177 $spFactory = MediaWikiServices
::getInstance()->getSpecialPageFactory();
178 if ( $subpageSearch !== null ) {
179 // Try matching the full search string as a page name
180 $specialTitle = Title
::makeTitleSafe( NS_SPECIAL
, $searchKey );
181 if ( !$specialTitle ) {
184 $special = $spFactory->getPage( $specialTitle->getText() );
186 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
187 return array_map( function ( $sub ) use ( $specialTitle ) {
188 return $specialTitle->getSubpage( $sub );
195 # normalize searchKey, so aliases with spaces can be found - T27675
196 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
197 $searchKey = str_replace( ' ', '_', $searchKey );
198 $searchKey = $contLang->caseFold( $searchKey );
200 // Unlike SpecialPage itself, we want the canonical forms of both
201 // canonical and alias title forms...
203 foreach ( $spFactory->getNames() as $page ) {
204 $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
207 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
208 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
212 foreach ( $aliases as $key => $alias ) {
213 $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
219 foreach ( $keys as $pageKey => $page ) {
220 if ( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
221 // T29671: Don't use SpecialPage::getTitleFor() here because it
222 // localizes its input leading to searches for e.g. Special:All
223 // returning Spezial:MediaWiki-Systemnachrichten and returning
224 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
225 $matches[$page['rank']][] = Title
::makeTitleSafe( NS_SPECIAL
, $page['page'] );
227 if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit +
$offset ) {
228 // We have enough items in primary rank, no use to continue
235 // Ensure keys are in order
238 $matches = array_reduce( $matches, 'array_merge', [] );
240 return array_slice( $matches, $offset, $limit );
244 * Unless overridden by PrefixSearchBackend hook...
245 * This is case-sensitive (First character may
246 * be automatically capitalized by Title::secureAndSpit()
247 * later on depending on $wgCapitalLinks)
249 * @param array|null $namespaces Namespaces to search in
250 * @param string $search Term
251 * @param int $limit Max number of items to return
252 * @param int $offset Number of items to skip
253 * @return Title[] Array of Title objects
255 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
256 // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
257 if ( $namespaces === null ) {
260 if ( !$namespaces ) {
261 $namespaces[] = NS_MAIN
;
264 // Construct suitable prefix for each namespace. They differ in cases where
265 // some namespaces always capitalize and some don't.
267 foreach ( $namespaces as $namespace ) {
268 // For now, if special is included, ignore the other namespaces
269 if ( $namespace == NS_SPECIAL
) {
270 return $this->specialSearch( $search, $limit, $offset );
273 $title = Title
::makeTitleSafe( $namespace, $search );
274 // Why does the prefix default to empty?
275 $prefix = $title ?
$title->getDBkey() : '';
276 $prefixes[$prefix][] = $namespace;
279 $dbr = wfGetDB( DB_REPLICA
);
280 // Often there is only one prefix that applies to all requested namespaces,
281 // but sometimes there are two if some namespaces do not always capitalize.
283 foreach ( $prefixes as $prefix => $namespaces ) {
285 'page_namespace' => $namespaces,
286 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
288 $conds[] = $dbr->makeList( $condition, LIST_AND
);
292 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
293 $conds = $dbr->makeList( $conds, LIST_OR
);
296 'ORDER BY' => [ 'page_title', 'page_namespace' ],
300 $res = $dbr->select( $table, $fields, $conds, __METHOD__
, $options );
302 return iterator_to_array( TitleArray
::newFromResult( $res ) );
306 * Validate an array of numerical namespace indexes
308 * @param array $namespaces
309 * @return array (default: contains only NS_MAIN)
311 protected function validateNamespaces( $namespaces ) {
312 // We will look at each given namespace against content language namespaces
313 $validNamespaces = MediaWikiServices
::getInstance()->getContentLanguage()->getNamespaces();
314 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
316 foreach ( $namespaces as $ns ) {
317 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
321 if ( count( $valid ) > 0 ) {