Move MW_LC_VERSION to class constant instead of global define
[mediawiki.git] / includes / PrefixSearch.php
blob35be2a9d8792e2b206ec6f303a4fd98bb65ee8cd
1 <?php
2 /**
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
20 * @file
23 /**
24 * Handles searching prefixes of titles and finding any page
25 * names that match. Used largely by the OpenSearch implementation.
27 * @ingroup Search
29 abstract class PrefixSearch {
30 /**
31 * Do a prefix search of titles and return a list of matching page names.
32 * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
34 * @param string $search
35 * @param int $limit
36 * @param array $namespaces Used if query is not explicitly prefixed
37 * @return array Array of strings
39 public static function titleSearch( $search, $limit, $namespaces = array() ) {
40 $prefixSearch = new StringPrefixSearch;
41 return $prefixSearch->search( $search, $limit, $namespaces );
44 /**
45 * Do a prefix search of titles and return a list of matching page names.
47 * @param string $search
48 * @param int $limit
49 * @param array $namespaces Used if query is not explicitly prefixed
50 * @return array Array of strings or Title objects
52 public function search( $search, $limit, $namespaces = array() ) {
53 $search = trim( $search );
54 if ( $search == '' ) {
55 return array(); // Return empty result
57 $namespaces = $this->validateNamespaces( $namespaces );
59 // Find a Title which is not an interwiki and is in NS_MAIN
60 $title = Title::newFromText( $search );
61 if ( $title && !$title->isExternal() ) {
62 $ns = array( $title->getNamespace() );
63 if ( $ns[0] == NS_MAIN ) {
64 $ns = $namespaces; // no explicit prefix, use default namespaces
66 return $this->searchBackend(
67 $ns, $title->getText(), $limit );
70 // Is this a namespace prefix?
71 $title = Title::newFromText( $search . 'Dummy' );
72 if ( $title && $title->getText() == 'Dummy'
73 && $title->getNamespace() != NS_MAIN
74 && !$title->isExternal() )
76 $namespaces = array( $title->getNamespace() );
77 $search = '';
80 return $this->searchBackend( $namespaces, $search, $limit );
83 /**
84 * Do a prefix search for all possible variants of the prefix
85 * @param string $search
86 * @param int $limit
87 * @param array $namespaces
89 * @return array
91 public function searchWithVariants( $search, $limit, array $namespaces ) {
92 wfProfileIn( __METHOD__ );
93 $searches = $this->search( $search, $limit, $namespaces );
95 // if the content language has variants, try to retrieve fallback results
96 $fallbackLimit = $limit - count( $searches );
97 if ( $fallbackLimit > 0 ) {
98 global $wgContLang;
100 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
101 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
103 foreach ( $fallbackSearches as $fbs ) {
104 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
105 $searches = array_merge( $searches, $fallbackSearchResult );
106 $fallbackLimit -= count( $fallbackSearchResult );
108 if ( $fallbackLimit == 0 ) {
109 break;
113 wfProfileOut( __METHOD__ );
114 return $searches;
118 * When implemented in a descendant class, receives an array of Title objects and returns
119 * either an unmodified array or an array of strings corresponding to titles passed to it.
121 * @param array $titles
122 * @return array
124 abstract protected function titles( array $titles );
127 * When implemented in a descendant class, receives an array of titles as strings and returns
128 * either an unmodified array or an array of Title objects corresponding to strings received.
130 * @param array $strings
132 * @return array
134 abstract protected function strings( array $strings );
137 * Do a prefix search of titles and return a list of matching page names.
138 * @param array $namespaces
139 * @param string $search
140 * @param int $limit
141 * @return array Array of strings
143 protected function searchBackend( $namespaces, $search, $limit ) {
144 if ( count( $namespaces ) == 1 ) {
145 $ns = $namespaces[0];
146 if ( $ns == NS_MEDIA ) {
147 $namespaces = array( NS_FILE );
148 } elseif ( $ns == NS_SPECIAL ) {
149 return $this->titles( $this->specialSearch( $search, $limit ) );
152 $srchres = array();
153 if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
154 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
156 return $this->strings( $srchres );
160 * Prefix search special-case for Special: namespace.
162 * @param string $search Term
163 * @param int $limit Max number of items to return
164 * @return array
166 protected function specialSearch( $search, $limit ) {
167 global $wgContLang;
169 $searchParts = explode( '/', $search, 2 );
170 $searchKey = $searchParts[0];
171 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
173 // Handle subpage search separately.
174 if ( $subpageSearch !== null ) {
175 // Try matching the full search string as a page name
176 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
177 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
178 if ( $special ) {
179 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit );
180 return array_map( function ( $sub ) use ( $specialTitle ) {
181 return $specialTitle->getSubpage( $sub );
182 }, $subpages );
183 } else {
184 return array();
188 # normalize searchKey, so aliases with spaces can be found - bug 25675
189 $searchKey = str_replace( ' ', '_', $searchKey );
190 $searchKey = $wgContLang->caseFold( $searchKey );
192 // Unlike SpecialPage itself, we want the canonical forms of both
193 // canonical and alias title forms...
194 $keys = array();
195 foreach ( SpecialPageFactory::getList() as $page => $class ) {
196 $keys[$wgContLang->caseFold( $page )] = $page;
199 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
200 if ( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885
201 continue;
204 foreach ( $aliases as $alias ) {
205 $keys[$wgContLang->caseFold( $alias )] = $alias;
208 ksort( $keys );
210 $srchres = array();
211 foreach ( $keys as $pageKey => $page ) {
212 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
213 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
214 // localizes its input leading to searches for e.g. Special:All
215 // returning Spezial:MediaWiki-Systemnachrichten and returning
216 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
217 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
220 if ( count( $srchres ) >= $limit ) {
221 break;
225 return $srchres;
229 * Unless overridden by PrefixSearchBackend hook...
230 * This is case-sensitive (First character may
231 * be automatically capitalized by Title::secureAndSpit()
232 * later on depending on $wgCapitalLinks)
234 * @param array $namespaces Namespaces to search in
235 * @param string $search Term
236 * @param int $limit Max number of items to return
237 * @return array Array of Title objects
239 protected function defaultSearchBackend( $namespaces, $search, $limit ) {
240 $ns = array_shift( $namespaces ); // support only one namespace
241 if ( in_array( NS_MAIN, $namespaces ) ) {
242 $ns = NS_MAIN; // if searching on many always default to main
245 $t = Title::newFromText( $search, $ns );
246 $prefix = $t ? $t->getDBkey() : '';
247 $dbr = wfGetDB( DB_SLAVE );
248 $res = $dbr->select( 'page',
249 array( 'page_id', 'page_namespace', 'page_title' ),
250 array(
251 'page_namespace' => $ns,
252 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
254 __METHOD__,
255 array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
257 $srchres = array();
258 foreach ( $res as $row ) {
259 $srchres[] = Title::newFromRow( $row );
261 return $srchres;
265 * Validate an array of numerical namespace indexes
267 * @param array $namespaces
268 * @return array (default: contains only NS_MAIN)
270 protected function validateNamespaces( $namespaces ) {
271 global $wgContLang;
273 // We will look at each given namespace against wgContLang namespaces
274 $validNamespaces = $wgContLang->getNamespaces();
275 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
276 $valid = array();
277 foreach ( $namespaces as $ns ) {
278 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
279 $valid[] = $ns;
282 if ( count( $valid ) > 0 ) {
283 return $valid;
287 return array( NS_MAIN );
292 * Performs prefix search, returning Title objects
293 * @ingroup Search
295 class TitlePrefixSearch extends PrefixSearch {
297 protected function titles( array $titles ) {
298 return $titles;
301 protected function strings( array $strings ) {
302 $titles = array_map( 'Title::newFromText', $strings );
303 $lb = new LinkBatch( $titles );
304 $lb->setCaller( __METHOD__ );
305 $lb->execute();
306 return $titles;
311 * Performs prefix search, returning strings
312 * @ingroup Search
314 class StringPrefixSearch extends PrefixSearch {
316 protected function titles( array $titles ) {
317 return array_map( function ( Title $t ) {
318 return $t->getPrefixedText();
319 }, $titles );
322 protected function strings( array $strings ) {
323 return $strings;