Bleh, didn't press save again (r80350)
[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 // Find a Title which is not an interwiki and is in NS_MAIN
26 $title = Title::newFromText( $search );
27 if( $title && $title->getInterwiki() == '' ) {
28 $ns = array($title->getNamespace());
29 if($ns[0] == NS_MAIN)
30 $ns = $namespaces; // no explicit prefix, use default namespaces
31 return self::searchBackend(
32 $ns, $title->getText(), $limit );
33 }
34
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 );
42 }
43
44 return self::searchBackend( $namespaces, $search, $limit );
45 }
46
47
48 /**
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
54 */
55 protected static function searchBackend( $namespaces, $search, $limit ) {
56 if( count($namespaces) == 1 ){
57 $ns = $namespaces[0];
58 if( $ns == NS_MEDIA ) {
59 $namespaces = array(NS_FILE);
60 } elseif( $ns == NS_SPECIAL ) {
61 return self::specialSearch( $search, $limit );
62 }
63 }
64 $srchres = array();
65 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
66 return self::defaultSearchBackend( $namespaces, $search, $limit );
67 }
68 return $srchres;
69 }
70
71 /**
72 * Prefix search special-case for Special: namespace.
73 *
74 * @param $search String: term
75 * @param $limit Integer: max number of items to return
76 * @return Array
77 */
78 protected static function specialSearch( $search, $limit ) {
79 global $wgContLang;
80
81 $searchKey = $wgContLang->caseFold( $search );
82
83 // Unlike SpecialPage itself, we want the canonical forms of both
84 // canonical and alias title forms...
85 SpecialPage::initList();
86 SpecialPage::initAliasList();
87 $keys = array();
88 foreach( array_keys( SpecialPage::$mList ) as $page ) {
89 $keys[$wgContLang->caseFold( $page )] = $page;
90 }
91
92 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
93 if( !array_key_exists( $page, SpecialPage::$mList ) ) {# bug 20885
94 continue;
95 }
96
97 foreach( $aliases as $alias ) {
98 $keys[$wgContLang->caseFold( $alias )] = $alias;
99 }
100 }
101 ksort( $keys );
102
103 $srchres = array();
104 foreach( $keys as $pageKey => $page ) {
105 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
106 wfSuppressWarnings();
107 $srchres[] = SpecialPage::getTitleFor( $page )->getPrefixedText();
108 wfRestoreWarnings();
109 }
110
111 if( count( $srchres ) >= $limit ) {
112 break;
113 }
114 }
115
116 return $srchres;
117 }
118
119 /**
120 * Unless overridden by PrefixSearchBackend hook...
121 * This is case-sensitive (First character may
122 * be automatically capitalized by Title::secureAndSpit()
123 * later on depending on $wgCapitalLinks)
124 *
125 * @param $namespaces Array: namespaces to search in
126 * @param $search String: term
127 * @param $limit Integer: max number of items to return
128 * @return Array of title strings
129 */
130 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
131 $ns = array_shift($namespaces); // support only one namespace
132 if( in_array(NS_MAIN,$namespaces))
133 $ns = NS_MAIN; // if searching on many always default to main
134
135 // Prepare nested request
136 $req = new FauxRequest(array (
137 'action' => 'query',
138 'list' => 'allpages',
139 'apnamespace' => $ns,
140 'aplimit' => $limit,
141 'apprefix' => $search
142 ));
143
144 // Execute
145 $module = new ApiMain($req);
146 $module->execute();
147
148 // Get resulting data
149 $data = $module->getResultData();
150
151 // Reformat useful data for future printing by JSON engine
152 $srchres = array ();
153 foreach ((array)$data['query']['allpages'] as $pageinfo) {
154 // Note: this data will no be printable by the xml engine
155 // because it does not support lists of unnamed items
156 $srchres[] = $pageinfo['title'];
157 }
158
159 return $srchres;
160 }
161
162 /**
163 * Validate an array of numerical namespace indexes
164 *
165 * @param $namespaces Array
166 * @return Array (default: contains only NS_MAIN)
167 */
168 protected static function validateNamespaces($namespaces){
169 global $wgContLang;
170
171 // We will look at each given namespace against wgContLang namespaces
172 $validNamespaces = $wgContLang->getNamespaces();
173 if( is_array($namespaces) && count($namespaces)>0 ){
174 $valid = array();
175 foreach ($namespaces as $ns){
176 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
177 $valid[] = $ns;
178 }
179 if( count($valid) > 0 ) {
180 return $valid;
181 }
182 }
183
184 return array( NS_MAIN );
185 }
186 }