3 * PrefixSearch - Handles searching prefixes of titles and finding any page
4 * names that match. Used largely by the OpenSearch implementation.
11 * Do a prefix search of titles and return a list of matching page names.
13 * @param $search String
14 * @param $limit Integer
15 * @param $namespaces Array: used if query is not explicitely prefixed
16 * @return Array of strings
18 public static function titleSearch( $search, $limit, $namespaces = array() ) {
19 $search = trim( $search );
21 return array(); // Return empty result
23 $namespaces = self
::validateNamespaces( $namespaces );
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
32 return self
::searchBackend(
33 $ns, $title->getText(), $limit );
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 );
45 return self
::searchBackend( $namespaces, $search, $limit );
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
55 protected static function searchBackend( $namespaces, $search, $limit ) {
56 if( count( $namespaces ) == 1 ) {
58 if( $ns == NS_MEDIA
) {
59 $namespaces = array( NS_FILE
);
60 } elseif( $ns == NS_SPECIAL
) {
61 return self
::specialSearch( $search, $limit );
65 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
66 return self
::defaultSearchBackend( $namespaces, $search, $limit );
72 * Prefix search special-case for Special: namespace.
74 * @param $search String: term
75 * @param $limit Integer: max number of items to return
78 protected static function specialSearch( $search, $limit ) {
81 # normalize searchKey, so aliases with spaces can be found - bug 25675
82 $search = str_replace( ' ', '_', $search );
84 $searchKey = $wgContLang->caseFold( $search );
86 // Unlike SpecialPage itself, we want the canonical forms of both
87 // canonical and alias title forms...
89 foreach( SpecialPageFactory
::getList() as $page => $class ) {
90 $keys[$wgContLang->caseFold( $page )] = $page;
93 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
94 if( !array_key_exists( $page, SpecialPageFactory
::getList() ) ) {# bug 20885
98 foreach( $aliases as $alias ) {
99 $keys[$wgContLang->caseFold( $alias )] = $alias;
105 foreach( $keys as $pageKey => $page ) {
106 if( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
107 wfSuppressWarnings();
108 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
109 // localizes its input leading to searches for e.g. Special:All
110 // returning Spezial:MediaWiki-Systemnachrichten and returning
111 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
112 $srchres[] = Title
::makeTitleSafe( NS_SPECIAL
, $page )->getPrefixedText();
116 if( count( $srchres ) >= $limit ) {
125 * Unless overridden by PrefixSearchBackend hook...
126 * This is case-sensitive (First character may
127 * be automatically capitalized by Title::secureAndSpit()
128 * later on depending on $wgCapitalLinks)
130 * @param $namespaces Array: namespaces to search in
131 * @param $search String: term
132 * @param $limit Integer: max number of items to return
133 * @return Array of title strings
135 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
136 $ns = array_shift( $namespaces ); // support only one namespace
137 if( in_array( NS_MAIN
, $namespaces ) ) {
138 $ns = NS_MAIN
; // if searching on many always default to main
141 // Prepare nested request
142 $req = new FauxRequest( array(
144 'list' => 'allpages',
145 'apnamespace' => $ns,
147 'apprefix' => $search
151 $module = new ApiMain( $req );
154 // Get resulting data
155 $data = $module->getResultData();
157 // Reformat useful data for future printing by JSON engine
159 foreach ( (array)$data['query']['allpages'] as $pageinfo ) {
160 // Note: this data will no be printable by the xml engine
161 // because it does not support lists of unnamed items
162 $srchres[] = $pageinfo['title'];
169 * Validate an array of numerical namespace indexes
171 * @param $namespaces Array
172 * @return Array (default: contains only NS_MAIN)
174 protected static function validateNamespaces( $namespaces ) {
177 // We will look at each given namespace against wgContLang namespaces
178 $validNamespaces = $wgContLang->getNamespaces();
179 if( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
181 foreach ( $namespaces as $ns ) {
182 if( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
186 if( count( $valid ) > 0 ) {
191 return array( NS_MAIN
);