Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / PrefixSearch.php
blob3c464c5821fb58405558fbf343d0ea7845a3f8eb
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 class PrefixSearch {
30 /**
31 * Do a prefix search of titles and return a list of matching page names.
33 * @param $search String
34 * @param $limit Integer
35 * @param array $namespaces used if query is not explicitly prefixed
36 * @return Array of strings
38 public static function titleSearch( $search, $limit, $namespaces = array() ) {
39 $search = trim( $search );
40 if ( $search == '' ) {
41 return array(); // Return empty result
43 $namespaces = self::validateNamespaces( $namespaces );
45 // Find a Title which is not an interwiki and is in NS_MAIN
46 $title = Title::newFromText( $search );
47 if ( $title && $title->getInterwiki() == '' ) {
48 $ns = array( $title->getNamespace() );
49 if ( $ns[0] == NS_MAIN ) {
50 $ns = $namespaces; // no explicit prefix, use default namespaces
52 return self::searchBackend(
53 $ns, $title->getText(), $limit );
56 // Is this a namespace prefix?
57 $title = Title::newFromText( $search . 'Dummy' );
58 if ( $title && $title->getText() == 'Dummy'
59 && $title->getNamespace() != NS_MAIN
60 && $title->getInterwiki() == '' ) {
61 return self::searchBackend(
62 array( $title->getNamespace() ), '', $limit );
65 return self::searchBackend( $namespaces, $search, $limit );
68 /**
69 * Do a prefix search of titles and return a list of matching page names.
70 * @param $namespaces Array
71 * @param $search String
72 * @param $limit Integer
73 * @return Array of strings
75 protected static function searchBackend( $namespaces, $search, $limit ) {
76 if ( count( $namespaces ) == 1 ) {
77 $ns = $namespaces[0];
78 if ( $ns == NS_MEDIA ) {
79 $namespaces = array( NS_FILE );
80 } elseif ( $ns == NS_SPECIAL ) {
81 return self::specialSearch( $search, $limit );
84 $srchres = array();
85 if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
86 return self::defaultSearchBackend( $namespaces, $search, $limit );
88 return $srchres;
91 /**
92 * Prefix search special-case for Special: namespace.
94 * @param string $search term
95 * @param $limit Integer: max number of items to return
96 * @return Array
98 protected static function specialSearch( $search, $limit ) {
99 global $wgContLang;
101 # normalize searchKey, so aliases with spaces can be found - bug 25675
102 $search = str_replace( ' ', '_', $search );
104 $searchKey = $wgContLang->caseFold( $search );
106 // Unlike SpecialPage itself, we want the canonical forms of both
107 // canonical and alias title forms...
108 $keys = array();
109 foreach ( SpecialPageFactory::getList() as $page => $class ) {
110 $keys[$wgContLang->caseFold( $page )] = $page;
113 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
114 if ( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885
115 continue;
118 foreach ( $aliases as $alias ) {
119 $keys[$wgContLang->caseFold( $alias )] = $alias;
122 ksort( $keys );
124 $srchres = array();
125 foreach ( $keys as $pageKey => $page ) {
126 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
127 wfSuppressWarnings();
128 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
129 // localizes its input leading to searches for e.g. Special:All
130 // returning Spezial:MediaWiki-Systemnachrichten and returning
131 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
132 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page )->getPrefixedText();
133 wfRestoreWarnings();
136 if ( count( $srchres ) >= $limit ) {
137 break;
141 return $srchres;
145 * Unless overridden by PrefixSearchBackend hook...
146 * This is case-sensitive (First character may
147 * be automatically capitalized by Title::secureAndSpit()
148 * later on depending on $wgCapitalLinks)
150 * @param array $namespaces namespaces to search in
151 * @param string $search term
152 * @param $limit Integer: max number of items to return
153 * @return Array of title strings
155 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
156 $ns = array_shift( $namespaces ); // support only one namespace
157 if ( in_array( NS_MAIN, $namespaces ) ) {
158 $ns = NS_MAIN; // if searching on many always default to main
161 // Prepare nested request
162 $req = new FauxRequest( array(
163 'action' => 'query',
164 'list' => 'allpages',
165 'apnamespace' => $ns,
166 'aplimit' => $limit,
167 'apprefix' => $search
170 // Execute
171 $module = new ApiMain( $req );
172 $module->execute();
174 // Get resulting data
175 $data = $module->getResultData();
177 // Reformat useful data for future printing by JSON engine
178 $srchres = array();
179 foreach ( (array)$data['query']['allpages'] as $pageinfo ) {
180 // Note: this data will no be printable by the xml engine
181 // because it does not support lists of unnamed items
182 $srchres[] = $pageinfo['title'];
185 return $srchres;
189 * Validate an array of numerical namespace indexes
191 * @param $namespaces Array
192 * @return Array (default: contains only NS_MAIN)
194 protected static function validateNamespaces( $namespaces ) {
195 global $wgContLang;
197 // We will look at each given namespace against wgContLang namespaces
198 $validNamespaces = $wgContLang->getNamespaces();
199 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
200 $valid = array();
201 foreach ( $namespaces as $ns ) {
202 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
203 $valid[] = $ns;
206 if ( count( $valid ) > 0 ) {
207 return $valid;
211 return array( NS_MAIN );