coding style tweaks, doc updates, marked some public functions as such
[lhc/web/wiklou.git] / includes / PrefixSearch.php
1 <?php
2 /**
3 * PrefixSearch - Handles searching prefixes of titles and finding any page
4 * names that match. Used largely by the OpenSearch implementation.
5 *
6 * @ingroup Search
7 */
8
9 class PrefixSearch {
10 /**
11 * Do a prefix search of titles and return a list of matching page names.
12 *
13 * @param $search String
14 * @param $limit Integer
15 * @param $namespaces Array: used if query is not explicitely prefixed
16 * @return Array of strings
17 */
18 public static function titleSearch( $search, $limit, $namespaces = array() ) {
19 $search = trim( $search );
20 if( $search == '' ) {
21 return array(); // Return empty result
22 }
23 $namespaces = self::validateNamespaces( $namespaces );
24
25 // Find a Title which is not an interwiki and is in NS_MAIN
26 $title = Title::newFromText( $search );
27 if( $title && $title->getInterwiki() == '' ) {
28 $ns = array($title->getNamespace());
29 if( $ns[0] == NS_MAIN ) {
30 $ns = $namespaces; // no explicit prefix, use default namespaces
31 }
32 return self::searchBackend(
33 $ns, $title->getText(), $limit );
34 }
35
36 // Is this a namespace prefix?
37 $title = Title::newFromText( $search . 'Dummy' );
38 if( $title && $title->getText() == 'Dummy'
39 && $title->getNamespace() != NS_MAIN
40 && $title->getInterwiki() == '' ) {
41 return self::searchBackend(
42 array( $title->getNamespace() ), '', $limit );
43 }
44
45 return self::searchBackend( $namespaces, $search, $limit );
46 }
47
48 /**
49 * Do a prefix search of titles and return a list of matching page names.
50 * @param $namespaces Array
51 * @param $search String
52 * @param $limit Integer
53 * @return Array of strings
54 */
55 protected static function searchBackend( $namespaces, $search, $limit ) {
56 if( count( $namespaces ) == 1 ) {
57 $ns = $namespaces[0];
58 if( $ns == NS_MEDIA ) {
59 $namespaces = array( NS_FILE );
60 } elseif( $ns == NS_SPECIAL ) {
61 return self::specialSearch( $search, $limit );
62 }
63 }
64 $srchres = array();
65 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
66 return self::defaultSearchBackend( $namespaces, $search, $limit );
67 }
68 return $srchres;
69 }
70
71 /**
72 * Prefix search special-case for Special: namespace.
73 *
74 * @param $search String: term
75 * @param $limit Integer: max number of items to return
76 * @return Array
77 */
78 protected static function specialSearch( $search, $limit ) {
79 global $wgContLang;
80
81 # normalize searchKey, so aliases with spaces can be found - bug 25675
82 $search = str_replace( ' ', '_', $search );
83
84 $searchKey = $wgContLang->caseFold( $search );
85
86 // Unlike SpecialPage itself, we want the canonical forms of both
87 // canonical and alias title forms...
88 SpecialPage::initList();
89 SpecialPage::initAliasList();
90 $keys = array();
91 foreach( array_keys( SpecialPage::$mList ) as $page ) {
92 $keys[$wgContLang->caseFold( $page )] = $page;
93 }
94
95 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
96 if( !array_key_exists( $page, SpecialPage::$mList ) ) {# bug 20885
97 continue;
98 }
99
100 foreach( $aliases as $alias ) {
101 $keys[$wgContLang->caseFold( $alias )] = $alias;
102 }
103 }
104 ksort( $keys );
105
106 $srchres = array();
107 foreach( $keys as $pageKey => $page ) {
108 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
109 wfSuppressWarnings();
110 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
111 // localizes its input leading to searches for e.g. Special:All
112 // returning Spezial:MediaWiki-Systemnachrichten and returning
113 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
114 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page )->getPrefixedText();
115 wfRestoreWarnings();
116 }
117
118 if( count( $srchres ) >= $limit ) {
119 break;
120 }
121 }
122
123 return $srchres;
124 }
125
126 /**
127 * Unless overridden by PrefixSearchBackend hook...
128 * This is case-sensitive (First character may
129 * be automatically capitalized by Title::secureAndSpit()
130 * later on depending on $wgCapitalLinks)
131 *
132 * @param $namespaces Array: namespaces to search in
133 * @param $search String: term
134 * @param $limit Integer: max number of items to return
135 * @return Array of title strings
136 */
137 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
138 $ns = array_shift( $namespaces ); // support only one namespace
139 if( in_array( NS_MAIN, $namespaces ) ) {
140 $ns = NS_MAIN; // if searching on many always default to main
141 }
142
143 // Prepare nested request
144 $req = new FauxRequest( array(
145 'action' => 'query',
146 'list' => 'allpages',
147 'apnamespace' => $ns,
148 'aplimit' => $limit,
149 'apprefix' => $search
150 ));
151
152 // Execute
153 $module = new ApiMain( $req );
154 $module->execute();
155
156 // Get resulting data
157 $data = $module->getResultData();
158
159 // Reformat useful data for future printing by JSON engine
160 $srchres = array ();
161 foreach ( (array)$data['query']['allpages'] as $pageinfo ) {
162 // Note: this data will no be printable by the xml engine
163 // because it does not support lists of unnamed items
164 $srchres[] = $pageinfo['title'];
165 }
166
167 return $srchres;
168 }
169
170 /**
171 * Validate an array of numerical namespace indexes
172 *
173 * @param $namespaces Array
174 * @return Array (default: contains only NS_MAIN)
175 */
176 protected static function validateNamespaces( $namespaces ) {
177 global $wgContLang;
178
179 // We will look at each given namespace against wgContLang namespaces
180 $validNamespaces = $wgContLang->getNamespaces();
181 if( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
182 $valid = array();
183 foreach ( $namespaces as $ns ) {
184 if( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
185 $valid[] = $ns;
186 }
187 }
188 if( count( $valid ) > 0 ) {
189 return $valid;
190 }
191 }
192
193 return array( NS_MAIN );
194 }
195 }