5 * Do a prefix search of titles and return a list of matching page names.
6 * @param string $search
8 * @return array of strings
10 public static function titleSearch( $search, $limit ) {
11 $search = trim( $search );
13 return array(); // Return empty result
16 $title = Title
::newFromText( $search );
17 if( $title && $title->getInterwiki() == '' ) {
18 $ns = $title->getNamespace();
19 return self
::searchBackend(
20 $title->getNamespace(), $title->getText(), $limit );
23 // Is this a namespace prefix?
24 $title = Title
::newFromText( $search . 'Dummy' );
25 if( $title && $title->getText() == 'Dummy'
26 && $title->getNamespace() != NS_MAIN
27 && $title->getInterwiki() == '' ) {
28 return self
::searchBackend(
29 $title->getNamespace(), '', $limit );
32 return self
::searchBackend( 0, $search, $limit );
37 * Do a prefix search of titles and return a list of matching page names.
38 * @param string $search
40 * @return array of strings
42 protected static function searchBackend( $ns, $search, $limit ) {
43 if( $ns == NS_MEDIA
) {
45 } elseif( $ns == NS_SPECIAL
) {
46 return self
::specialSearch( $search, $limit );
50 if( wfRunHooks( 'PrefixSearchBackend', array( $ns, $search, $limit, &$srchres ) ) ) {
51 return self
::defaultSearchBackend( $ns, $search, $limit );
57 * Prefix search special-case for Special: namespace.
59 protected static function specialSearch( $search, $limit ) {
61 $searchKey = $wgContLang->caseFold( $search );
63 // Unlike SpecialPage itself, we want the canonical forms of both
64 // canonical and alias title forms...
65 SpecialPage
::initList();
66 SpecialPage
::initAliasList();
68 foreach( array_keys( SpecialPage
::$mList ) as $page ) {
69 $keys[$wgContLang->caseFold( $page )] = $page;
71 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
72 foreach( $aliases as $alias ) {
73 $keys[$wgContLang->caseFold( $alias )] = $alias;
79 foreach( $keys as $pageKey => $page ) {
80 if( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
81 $srchres[] = Title
::makeTitle( NS_SPECIAL
, $page )->getPrefixedText();
83 if( count( $srchres ) >= $limit ) {
91 * Unless overridden by PrefixSearchBackend hook...
92 * This is case-sensitive except the first letter (per $wgCapitalLinks)
94 * @param int $ns Namespace to search in
95 * @param string $search term
96 * @param int $limit max number of items to return
97 * @return array of title strings
99 protected static function defaultSearchBackend( $ns, $search, $limit ) {
100 global $wgCapitalLinks, $wgContLang;
102 if( $wgCapitalLinks ) {
103 $search = $wgContLang->ucfirst( $search );
106 // Prepare nested request
107 $req = new FauxRequest(array (
109 'list' => 'allpages',
110 'apnamespace' => $ns,
112 'apprefix' => $search
116 $module = new ApiMain($req);
119 // Get resulting data
120 $data = $module->getResultData();
122 // Reformat useful data for future printing by JSON engine
124 foreach ($data['query']['allpages'] as & $pageinfo) {
125 // Note: this data will no be printable by the xml engine
126 // because it does not support lists of unnamed items
127 $srchres[] = $pageinfo['title'];