e251ee684e786ac1d5a3f3f845b5811de89b62c0
[lhc/web/wiklou.git] / includes / PrefixSearch.php
1 <?php
2 /**
3 * PrefixSearch - Handles searching prefixes of titles and finding any page
4 * names that match. Used largely by the OpenSearch implementation.
5 *
6 * @ingroup Search
7 */
8
9 class PrefixSearch {
10 /**
11 * Do a prefix search of titles and return a list of matching page names.
12 *
13 * @param $search String
14 * @param $limit Integer
15 * @param $namespaces Array: used if query is not explicitely prefixed
16 * @return Array of strings
17 */
18 public static function titleSearch( $search, $limit, $namespaces=array() ) {
19 $search = trim( $search );
20 if( $search == '' ) {
21 return array(); // Return empty result
22 }
23 $namespaces = self::validateNamespaces( $namespaces );
24
25 $title = Title::newFromText( $search );
26 if( $title && $title->getInterwiki() == '' ) {
27 $ns = array($title->getNamespace());
28 if($ns[0] == NS_MAIN)
29 $ns = $namespaces; // no explicit prefix, use default namespaces
30 return self::searchBackend(
31 $ns, $title->getText(), $limit );
32 }
33
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 );
41 }
42
43 return self::searchBackend( $namespaces, $search, $limit );
44 }
45
46
47 /**
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
53 */
54 protected static function searchBackend( $namespaces, $search, $limit ) {
55 if( count($namespaces) == 1 ){
56 $ns = $namespaces[0];
57 if( $ns == NS_MEDIA ) {
58 $namespaces = array(NS_FILE);
59 } elseif( $ns == NS_SPECIAL ) {
60 return self::specialSearch( $search, $limit );
61 }
62 }
63 $srchres = array();
64 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
65 return self::defaultSearchBackend( $namespaces, $search, $limit );
66 }
67 return $srchres;
68 }
69
70 /**
71 * Prefix search special-case for Special: namespace.
72 *
73 * @param $search String: term
74 * @param $limit Integer: max number of items to return
75 * @return Array
76 */
77 protected static function specialSearch( $search, $limit ) {
78 global $wgContLang;
79
80 $searchKey = $wgContLang->caseFold( $search );
81
82 // Unlike SpecialPage itself, we want the canonical forms of both
83 // canonical and alias title forms...
84 SpecialPage::initList();
85 SpecialPage::initAliasList();
86 $keys = array();
87 foreach( array_keys( SpecialPage::$mList ) as $page ) {
88 $keys[$wgContLang->caseFold( $page )] = $page;
89 }
90
91 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
92 if( !array_key_exists( $page, SpecialPage::$mList ) ) {# bug 20885
93 continue;
94 }
95
96 foreach( $aliases as $alias ) {
97 $keys[$wgContLang->caseFold( $alias )] = $alias;
98 }
99 }
100 ksort( $keys );
101
102 $srchres = array();
103 foreach( $keys as $pageKey => $page ) {
104 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
105 $srchres[] = SpecialPage::getTitleFor( $page )->getPrefixedText();
106 }
107
108 if( count( $srchres ) >= $limit ) {
109 break;
110 }
111 }
112
113 return $srchres;
114 }
115
116 /**
117 * Unless overridden by PrefixSearchBackend hook...
118 * This is case-sensitive (First character may
119 * be automatically capitalized by Title::secureAndSpit()
120 * later on depending on $wgCapitalLinks)
121 *
122 * @param $namespaces Array: namespaces to search in
123 * @param $search String: term
124 * @param $limit Integer: max number of items to return
125 * @return Array of title strings
126 */
127 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
128 $ns = array_shift($namespaces); // support only one namespace
129 if( in_array(NS_MAIN,$namespaces))
130 $ns = NS_MAIN; // if searching on many always default to main
131
132 // Prepare nested request
133 $req = new FauxRequest(array (
134 'action' => 'query',
135 'list' => 'allpages',
136 'apnamespace' => $ns,
137 'aplimit' => $limit,
138 'apprefix' => $search
139 ));
140
141 // Execute
142 $module = new ApiMain($req);
143 $module->execute();
144
145 // Get resulting data
146 $data = $module->getResultData();
147
148 // Reformat useful data for future printing by JSON engine
149 $srchres = array ();
150 foreach ((array)$data['query']['allpages'] as $pageinfo) {
151 // Note: this data will no be printable by the xml engine
152 // because it does not support lists of unnamed items
153 $srchres[] = $pageinfo['title'];
154 }
155
156 return $srchres;
157 }
158
159 /**
160 * Validate an array of numerical namespace indexes
161 *
162 * @param $namespaces Array
163 * @return Array
164 */
165 protected static function validateNamespaces($namespaces){
166 global $wgContLang;
167 $validNamespaces = $wgContLang->getNamespaces();
168 if( is_array($namespaces) && count($namespaces)>0 ){
169 $valid = array();
170 foreach ($namespaces as $ns){
171 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
172 $valid[] = $ns;
173 }
174 if( count($valid) > 0 )
175 return $valid;
176 }
177
178 return array( NS_MAIN );
179 }
180 }