API: fix copyright symbol, coding style cleanup, more braces
[lhc/web/wiklou.git] / includes / PrefixSearch.php
1 <?php
2
3 /**
4 * PrefixSearch - Handles searching prefixes of titles and finding any page
5 * names that match. Used largely by the OpenSearch implementation.
6 *
7 * @ingroup Search
8 */
9
10 class PrefixSearch {
11 /**
12 * Do a prefix search of titles and return a list of matching page names.
13 *
14 * @param $search String
15 * @param $limit Integer
16 * @param $namespaces Array: used if query is not explicitely prefixed
17 * @return Array of strings
18 */
19 public static function titleSearch( $search, $limit, $namespaces=array() ) {
20 $search = trim( $search );
21 if( $search == '' ) {
22 return array(); // Return empty result
23 }
24 $namespaces = self::validateNamespaces( $namespaces );
25
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 $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 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
91 if( !array_key_exists( $page, SpecialPage::$mList ) ) # bug 20885
92 continue;
93
94 foreach( $aliases as $alias ) {
95 $keys[$wgContLang->caseFold( $alias )] = $alias;
96 }
97 }
98 ksort( $keys );
99
100 $srchres = array();
101 foreach( $keys as $pageKey => $page ) {
102 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
103 $srchres[] = Title::makeTitle( NS_SPECIAL, $page )->getPrefixedText();
104 }
105 if( count( $srchres ) >= $limit ) {
106 break;
107 }
108 }
109 return $srchres;
110 }
111
112 /**
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)
117 *
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
122 */
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
127
128 // Prepare nested request
129 $req = new FauxRequest(array (
130 'action' => 'query',
131 'list' => 'allpages',
132 'apnamespace' => $ns,
133 'aplimit' => $limit,
134 'apprefix' => $search
135 ));
136
137 // Execute
138 $module = new ApiMain($req);
139 $module->execute();
140
141 // Get resulting data
142 $data = $module->getResultData();
143
144 // Reformat useful data for future printing by JSON engine
145 $srchres = array ();
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'];
150 }
151
152 return $srchres;
153 }
154
155 /**
156 * Validate an array of numerical namespace indexes
157 *
158 * @param $namespaces Array
159 * @return Array
160 */
161 protected static function validateNamespaces($namespaces){
162 global $wgContLang;
163 $validNamespaces = $wgContLang->getNamespaces();
164 if( is_array($namespaces) && count($namespaces)>0 ){
165 $valid = array();
166 foreach ($namespaces as $ns){
167 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
168 $valid[] = $ns;
169 }
170 if( count($valid) > 0 )
171 return $valid;
172 }
173
174 return array( NS_MAIN );
175 }
176 }