4 * PrefixSearch - Handles searching prefixes of titles and finding any page
5 * names that match. Used largely by the OpenSearch implementation.
12 * Do a prefix search of titles and return a list of matching page names.
14 * @param $search String
15 * @param $limit Integer
16 * @param $namespaces Array: used if query is not explicitely prefixed
17 * @return Array of strings
19 public static function titleSearch( $search, $limit, $namespaces=array() ) {
20 $search = trim( $search );
22 return array(); // Return empty result
24 $namespaces = self
::validateNamespaces( $namespaces );
26 $title = Title
::newFromText( $search );
27 if( $title && $title->getInterwiki() == '' ) {
28 $ns = array($title->getNamespace());
30 $ns = $namespaces; // no explicit prefix, use default namespaces
31 return self
::searchBackend(
32 $ns, $title->getText(), $limit );
35 // Is this a namespace prefix?
36 $title = Title
::newFromText( $search . 'Dummy' );
37 if( $title && $title->getText() == 'Dummy'
38 && $title->getNamespace() != NS_MAIN
39 && $title->getInterwiki() == '' ) {
40 return self
::searchBackend(
41 array($title->getNamespace()), '', $limit );
44 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 ) {
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;
90 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
91 if( !array_key_exists( $page, SpecialPage
::$mList ) ) # bug 20885
94 foreach( $aliases as $alias ) {
95 $keys[$wgContLang->caseFold( $alias )] = $alias;
101 foreach( $keys as $pageKey => $page ) {
102 if( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
103 $srchres[] = Title
::makeTitle( NS_SPECIAL
, $page )->getPrefixedText();
105 if( count( $srchres ) >= $limit ) {
113 * Unless overridden by PrefixSearchBackend hook...
114 * This is case-sensitive (First character may
115 * be automatically capitalized by Title::secureAndSpit()
116 * later on depending on $wgCapitalLinks)
118 * @param $namespaces Array: namespaces to search in
119 * @param $search String: term
120 * @param $limit Integer: max number of items to return
121 * @return Array of title strings
123 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
124 $ns = array_shift($namespaces); // support only one namespace
125 if( in_array(NS_MAIN
,$namespaces))
126 $ns = NS_MAIN
; // if searching on many always default to main
128 // Prepare nested request
129 $req = new FauxRequest(array (
131 'list' => 'allpages',
132 'apnamespace' => $ns,
134 'apprefix' => $search
138 $module = new ApiMain($req);
141 // Get resulting data
142 $data = $module->getResultData();
144 // Reformat useful data for future printing by JSON engine
146 foreach ((array)$data['query']['allpages'] as $pageinfo) {
147 // Note: this data will no be printable by the xml engine
148 // because it does not support lists of unnamed items
149 $srchres[] = $pageinfo['title'];
156 * Validate an array of numerical namespace indexes
158 * @param $namespaces Array
161 protected static function validateNamespaces($namespaces){
163 $validNamespaces = $wgContLang->getNamespaces();
164 if( is_array($namespaces) && count($namespaces)>0 ){
166 foreach ($namespaces as $ns){
167 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
170 if( count($valid) > 0 )
174 return array( NS_MAIN
);