1a43afe7f62c555c5c12b72164232ef578edd264
[lhc/web/wiklou.git] / includes / PrefixSearch.php
1 <?php
2
3 class PrefixSearch {
4 /**
5 * Do a prefix search of titles and return a list of matching page names.
6 * @param string $search
7 * @param int $limit
8 * @return array of strings
9 */
10 public static function titleSearch( $search, $limit ) {
11 $search = trim( $search );
12 if( $search == '' ) {
13 return array(); // Return empty result
14 }
15
16 $title = Title::newFromText( $search );
17 if( $title && $title->getInterwiki() == '' ) {
18 $ns = $title->getNamespace();
19 return self::searchBackend(
20 $title->getNamespace(), $title->getText(), $limit );
21 }
22
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 );
30 }
31
32 return self::searchBackend( 0, $search, $limit );
33 }
34
35
36 /**
37 * Do a prefix search of titles and return a list of matching page names.
38 * @param string $search
39 * @param int $limit
40 * @return array of strings
41 */
42 protected static function searchBackend( $ns, $search, $limit ) {
43 if( $ns == NS_MEDIA ) {
44 $ns = NS_IMAGE;
45 } elseif( $ns == NS_SPECIAL ) {
46 return self::specialSearch( $search, $limit );
47 }
48
49 $srchres = array();
50 if( wfRunHooks( 'PrefixSearchBackend', array( $ns, $search, $limit, &$srchres ) ) ) {
51 return self::defaultSearchBackend( $ns, $search, $limit );
52 }
53 return $srchres;
54 }
55
56 /**
57 * Prefix search special-case for Special: namespace.
58 */
59 protected static function specialSearch( $search, $limit ) {
60 global $wgContLang;
61 $searchKey = $wgContLang->caseFold( $search );
62
63 // Unlike SpecialPage itself, we want the canonical forms of both
64 // canonical and alias title forms...
65 SpecialPage::initList();
66 SpecialPage::initAliasList();
67 $keys = array();
68 foreach( array_keys( SpecialPage::$mList ) as $page ) {
69 $keys[$wgContLang->caseFold( $page )] = $page;
70 }
71 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
72 foreach( $aliases as $alias ) {
73 $keys[$wgContLang->caseFold( $alias )] = $alias;
74 }
75 }
76 ksort( $keys );
77
78 $srchres = array();
79 foreach( $keys as $pageKey => $page ) {
80 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
81 $srchres[] = Title::makeTitle( NS_SPECIAL, $page )->getPrefixedText();
82 }
83 if( count( $srchres ) >= $limit ) {
84 break;
85 }
86 }
87 return $srchres;
88 }
89
90 /**
91 * Unless overridden by PrefixSearchBackend hook...
92 * This is case-sensitive except the first letter (per $wgCapitalLinks)
93 *
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
98 */
99 protected static function defaultSearchBackend( $ns, $search, $limit ) {
100 global $wgCapitalLinks, $wgContLang;
101
102 if( $wgCapitalLinks ) {
103 $search = $wgContLang->ucfirst( $search );
104 }
105
106 // Prepare nested request
107 $req = new FauxRequest(array (
108 'action' => 'query',
109 'list' => 'allpages',
110 'apnamespace' => $ns,
111 'aplimit' => $limit,
112 'apprefix' => $search
113 ));
114
115 // Execute
116 $module = new ApiMain($req);
117 $module->execute();
118
119 // Get resulting data
120 $data = $module->getResultData();
121
122 // Reformat useful data for future printing by JSON engine
123 $srchres = array ();
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'];
128 }
129
130 return $srchres;
131 }
132
133 }