(bug 33471) compare detectProtocol to 'https'
[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 class PrefixSearch {
30 /**
31 * Do a prefix search of titles and return a list of matching page names.
32 *
33 * @param $search String
34 * @param $limit Integer
35 * @param $namespaces Array: used if query is not explicitely prefixed
36 * @return Array of strings
37 */
38 public static function titleSearch( $search, $limit, $namespaces = array() ) {
39 $search = trim( $search );
40 if( $search == '' ) {
41 return array(); // Return empty result
42 }
43 $namespaces = self::validateNamespaces( $namespaces );
44
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
51 }
52 return self::searchBackend(
53 $ns, $title->getText(), $limit );
54 }
55
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 );
63 }
64
65 return self::searchBackend( $namespaces, $search, $limit );
66 }
67
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
74 */
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 );
82 }
83 }
84 $srchres = array();
85 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
86 return self::defaultSearchBackend( $namespaces, $search, $limit );
87 }
88 return $srchres;
89 }
90
91 /**
92 * Prefix search special-case for Special: namespace.
93 *
94 * @param $search String: term
95 * @param $limit Integer: max number of items to return
96 * @return Array
97 */
98 protected static function specialSearch( $search, $limit ) {
99 global $wgContLang;
100
101 # normalize searchKey, so aliases with spaces can be found - bug 25675
102 $search = str_replace( ' ', '_', $search );
103
104 $searchKey = $wgContLang->caseFold( $search );
105
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;
111 }
112
113 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
114 if( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885
115 continue;
116 }
117
118 foreach( $aliases as $alias ) {
119 $keys[$wgContLang->caseFold( $alias )] = $alias;
120 }
121 }
122 ksort( $keys );
123
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();
134 }
135
136 if( count( $srchres ) >= $limit ) {
137 break;
138 }
139 }
140
141 return $srchres;
142 }
143
144 /**
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)
149 *
150 * @param $namespaces Array: namespaces to search in
151 * @param $search String: term
152 * @param $limit Integer: max number of items to return
153 * @return Array of title strings
154 */
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
159 }
160
161 // Prepare nested request
162 $req = new FauxRequest( array(
163 'action' => 'query',
164 'list' => 'allpages',
165 'apnamespace' => $ns,
166 'aplimit' => $limit,
167 'apprefix' => $search
168 ));
169
170 // Execute
171 $module = new ApiMain( $req );
172 $module->execute();
173
174 // Get resulting data
175 $data = $module->getResultData();
176
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'];
183 }
184
185 return $srchres;
186 }
187
188 /**
189 * Validate an array of numerical namespace indexes
190 *
191 * @param $namespaces Array
192 * @return Array (default: contains only NS_MAIN)
193 */
194 protected static function validateNamespaces( $namespaces ) {
195 global $wgContLang;
196
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;
204 }
205 }
206 if( count( $valid ) > 0 ) {
207 return $valid;
208 }
209 }
210
211 return array( NS_MAIN );
212 }
213 }