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 $title = Title
::newFromText( $search );
26 if( $title && $title->getInterwiki() == '' ) {
27 $ns = array($title->getNamespace());
29 $ns = $namespaces; // no explicit prefix, use default namespaces
30 return self
::searchBackend(
31 $ns, $title->getText(), $limit );
34 // Is this a namespace prefix?
35 $title = Title
::newFromText( $search . 'Dummy' );
36 if( $title && $title->getText() == 'Dummy'
37 && $title->getNamespace() != NS_MAIN
38 && $title->getInterwiki() == '' ) {
39 return self
::searchBackend(
40 array($title->getNamespace()), '', $limit );
43 return self
::searchBackend( $namespaces, $search, $limit );
48 * Do a prefix search of titles and return a list of matching page names.
49 * @param $namespaces Array
50 * @param $search String
51 * @param $limit Integer
52 * @return Array of strings
54 protected static function searchBackend( $namespaces, $search, $limit ) {
55 if( count($namespaces) == 1 ){
57 if( $ns == NS_MEDIA
) {
58 $namespaces = array(NS_FILE
);
59 } elseif( $ns == NS_SPECIAL
) {
60 return self
::specialSearch( $search, $limit );
64 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
65 return self
::defaultSearchBackend( $namespaces, $search, $limit );
71 * Prefix search special-case for Special: namespace.
73 * @param $search String: term
74 * @param $limit Integer: max number of items to return
77 protected static function specialSearch( $search, $limit ) {
80 $searchKey = $wgContLang->caseFold( $search );
82 // Unlike SpecialPage itself, we want the canonical forms of both
83 // canonical and alias title forms...
84 SpecialPage
::initList();
85 SpecialPage
::initAliasList();
87 foreach( array_keys( SpecialPage
::$mList ) as $page ) {
88 $keys[$wgContLang->caseFold( $page )] = $page;
91 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
92 if( !array_key_exists( $page, SpecialPage
::$mList ) ) {# bug 20885
96 foreach( $aliases as $alias ) {
97 $keys[$wgContLang->caseFold( $alias )] = $alias;
103 foreach( $keys as $pageKey => $page ) {
104 if( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
105 wfSuppressWarnings();
106 $srchres[] = SpecialPage
::getTitleFor( $page )->getPrefixedText();
110 if( count( $srchres ) >= $limit ) {
119 * Unless overridden by PrefixSearchBackend hook...
120 * This is case-sensitive (First character may
121 * be automatically capitalized by Title::secureAndSpit()
122 * later on depending on $wgCapitalLinks)
124 * @param $namespaces Array: namespaces to search in
125 * @param $search String: term
126 * @param $limit Integer: max number of items to return
127 * @return Array of title strings
129 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
130 $ns = array_shift($namespaces); // support only one namespace
131 if( in_array(NS_MAIN
,$namespaces))
132 $ns = NS_MAIN
; // if searching on many always default to main
134 // Prepare nested request
135 $req = new FauxRequest(array (
137 'list' => 'allpages',
138 'apnamespace' => $ns,
140 'apprefix' => $search
144 $module = new ApiMain($req);
147 // Get resulting data
148 $data = $module->getResultData();
150 // Reformat useful data for future printing by JSON engine
152 foreach ((array)$data['query']['allpages'] as $pageinfo) {
153 // Note: this data will no be printable by the xml engine
154 // because it does not support lists of unnamed items
155 $srchres[] = $pageinfo['title'];
162 * Validate an array of numerical namespace indexes
164 * @param $namespaces Array
167 protected static function validateNamespaces($namespaces){
169 $validNamespaces = $wgContLang->getNamespaces();
170 if( is_array($namespaces) && count($namespaces)>0 ){
172 foreach ($namespaces as $ns){
173 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
176 if( count($valid) > 0 )
180 return array( NS_MAIN
);