Merge "PrefixSearch: Remove unnecessary wfSuppressWarnings()"
[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 *
27 * @ingroup Search
28 */
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
33 *
34 * @param $search String
35 * @param $limit Integer
36 * @param array $namespaces used if query is not explicitly prefixed
37 * @return Array of strings
38 */
39 public static function titleSearch( $search, $limit, $namespaces = array() ) {
40 $search = new StringPrefixSearch;
41 return $search->search( $search, $limit, $namespaces );
42 }
43
44 /**
45 * Do a prefix search of titles and return a list of matching page names.
46 *
47 * @param $search String
48 * @param $limit Integer
49 * @param array $namespaces used if query is not explicitly prefixed
50 * @return Array of strings or Title objects
51 */
52 public function search( $search, $limit, $namespaces = array() ) {
53 $search = trim( $search );
54 if ( $search == '' ) {
55 return array(); // Return empty result
56 }
57 $namespaces = $this->validateNamespaces( $namespaces );
58
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
65 }
66 return $this->searchBackend(
67 $ns, $title->getText(), $limit );
68 }
69
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() )
75 {
76 $namespaces = array( $title->getNamespace() );
77 $search = '';
78 }
79
80 return $this->searchBackend( $namespaces, $search, $limit );
81 }
82
83 /**
84 * Do a prefix search for all possible variants of the prefix
85 * @param $search String
86 * @param $limit Integer
87 * @param array $namespaces
88 *
89 * @return array
90 */
91 public function searchWithVariants( $search, $limit, array $namespaces ) {
92 wfProfileIn( __METHOD__ );
93 $searches = $this->search( $search, $limit, $namespaces );
94
95 // if the content language has variants, try to retrieve fallback results
96 $fallbackLimit = $limit - count( $searches );
97 if ( $fallbackLimit > 0 ) {
98 global $wgContLang;
99
100 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
101 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
102
103 foreach ( $fallbackSearches as $fbs ) {
104 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
105 $searches = array_merge( $searches, $fallbackSearchResult );
106 $fallbackLimit -= count( $fallbackSearchResult );
107
108 if ( $fallbackLimit == 0 ) {
109 break;
110 }
111 }
112 }
113 wfProfileOut( __METHOD__ );
114 return $searches;
115 }
116
117 /**
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.
120 *
121 * @param array $titles
122 * @return array
123 */
124 protected abstract function titles( array $titles );
125
126 /**
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.
129 *
130 * @param array $strings
131 *
132 * @return array
133 */
134 protected abstract function strings( array $strings );
135
136 /**
137 * Do a prefix search of titles and return a list of matching page names.
138 * @param $namespaces Array
139 * @param $search String
140 * @param $limit Integer
141 * @return Array of strings
142 */
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 ) );
150 }
151 }
152 $srchres = array();
153 if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
154 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
155 }
156 return $this->strings( $srchres );
157 }
158
159 /**
160 * Prefix search special-case for Special: namespace.
161 *
162 * @param string $search term
163 * @param $limit Integer: max number of items to return
164 * @return Array
165 */
166 protected function specialSearch( $search, $limit ) {
167 global $wgContLang;
168
169 # normalize searchKey, so aliases with spaces can be found - bug 25675
170 $search = str_replace( ' ', '_', $search );
171
172 $searchKey = $wgContLang->caseFold( $search );
173
174 // Unlike SpecialPage itself, we want the canonical forms of both
175 // canonical and alias title forms...
176 $keys = array();
177 foreach ( SpecialPageFactory::getList() as $page => $class ) {
178 $keys[$wgContLang->caseFold( $page )] = $page;
179 }
180
181 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
182 if ( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885
183 continue;
184 }
185
186 foreach ( $aliases as $alias ) {
187 $keys[$wgContLang->caseFold( $alias )] = $alias;
188 }
189 }
190 ksort( $keys );
191
192 $srchres = array();
193 foreach ( $keys as $pageKey => $page ) {
194 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
195 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
196 // localizes its input leading to searches for e.g. Special:All
197 // returning Spezial:MediaWiki-Systemnachrichten and returning
198 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
199 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
200 }
201
202 if ( count( $srchres ) >= $limit ) {
203 break;
204 }
205 }
206
207 return $srchres;
208 }
209
210 /**
211 * Unless overridden by PrefixSearchBackend hook...
212 * This is case-sensitive (First character may
213 * be automatically capitalized by Title::secureAndSpit()
214 * later on depending on $wgCapitalLinks)
215 *
216 * @param array $namespaces namespaces to search in
217 * @param string $search term
218 * @param $limit Integer: max number of items to return
219 * @return Array of Title objects
220 */
221 protected function defaultSearchBackend( $namespaces, $search, $limit ) {
222 $ns = array_shift( $namespaces ); // support only one namespace
223 if ( in_array( NS_MAIN, $namespaces ) ) {
224 $ns = NS_MAIN; // if searching on many always default to main
225 }
226
227 $t = Title::newFromText( $search, $ns );
228 $prefix = $t ? $t->getDBkey() : '';
229 $dbr = wfGetDB( DB_SLAVE );
230 $res = $dbr->select( 'page',
231 array( 'page_id', 'page_namespace', 'page_title' ),
232 array(
233 'page_namespace' => $ns,
234 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
235 ),
236 __METHOD__,
237 array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
238 );
239 $srchres = array();
240 foreach ( $res as $row ) {
241 $srchres[] = Title::newFromRow( $row );
242 }
243 return $srchres;
244 }
245
246 /**
247 * Validate an array of numerical namespace indexes
248 *
249 * @param $namespaces Array
250 * @return Array (default: contains only NS_MAIN)
251 */
252 protected function validateNamespaces( $namespaces ) {
253 global $wgContLang;
254
255 // We will look at each given namespace against wgContLang namespaces
256 $validNamespaces = $wgContLang->getNamespaces();
257 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
258 $valid = array();
259 foreach ( $namespaces as $ns ) {
260 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
261 $valid[] = $ns;
262 }
263 }
264 if ( count( $valid ) > 0 ) {
265 return $valid;
266 }
267 }
268
269 return array( NS_MAIN );
270 }
271 }
272
273 /**
274 * Performs prefix search, returning Title objects
275 * @ingroup Search
276 */
277 class TitlePrefixSearch extends PrefixSearch {
278
279 protected function titles( array $titles ) {
280 return $titles;
281 }
282
283 protected function strings( array $strings ) {
284 $titles = array_map( 'Title::newFromText', $strings );
285 $lb = new LinkBatch( $titles );
286 $lb->setCaller( __METHOD__ );
287 $lb->execute();
288 return $titles;
289 }
290 }
291
292 /**
293 * Performs prefix search, returning strings
294 * @ingroup Search
295 */
296 class StringPrefixSearch extends PrefixSearch {
297
298 protected function titles( array $titles ) {
299 return array_map( function( Title $t ) { return $t->getPrefixedText(); }, $titles );
300 }
301
302 protected function strings( array $strings ) {
303 return $strings;
304 }
305 }