* Document a bit
[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 * @param string $search
14 * @param int $limit
15 * @param array $namespaces - 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 array $namespaces
50 * @param string $search
51 * @param int $limit
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 protected static function specialSearch( $search, $limit ) {
74 global $wgContLang;
75 $searchKey = $wgContLang->caseFold( $search );
76
77 // Unlike SpecialPage itself, we want the canonical forms of both
78 // canonical and alias title forms...
79 SpecialPage::initList();
80 SpecialPage::initAliasList();
81 $keys = array();
82 foreach( array_keys( SpecialPage::$mList ) as $page ) {
83 $keys[$wgContLang->caseFold( $page )] = $page;
84 }
85 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
86 if( !array_key_exists( $page, SpecialPage::$mList ) ) # bug 20885
87 continue;
88
89 foreach( $aliases as $alias ) {
90 $keys[$wgContLang->caseFold( $alias )] = $alias;
91 }
92 }
93 ksort( $keys );
94
95 $srchres = array();
96 foreach( $keys as $pageKey => $page ) {
97 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
98 $srchres[] = Title::makeTitle( NS_SPECIAL, $page )->getPrefixedText();
99 }
100 if( count( $srchres ) >= $limit ) {
101 break;
102 }
103 }
104 return $srchres;
105 }
106
107 /**
108 * Unless overridden by PrefixSearchBackend hook...
109 * This is case-sensitive (First character may
110 * be automatically capitalized by Title::secureAndSpit()
111 * later on depending on $wgCapitalLinks)
112 *
113 * @param array $namespaces Namespaces to search in
114 * @param string $search term
115 * @param int $limit max number of items to return
116 * @return array of title strings
117 */
118 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
119 $ns = array_shift($namespaces); // support only one namespace
120 if( in_array(NS_MAIN,$namespaces))
121 $ns = NS_MAIN; // if searching on many always default to main
122
123 // Prepare nested request
124 $req = new FauxRequest(array (
125 'action' => 'query',
126 'list' => 'allpages',
127 'apnamespace' => $ns,
128 'aplimit' => $limit,
129 'apprefix' => $search
130 ));
131
132 // Execute
133 $module = new ApiMain($req);
134 $module->execute();
135
136 // Get resulting data
137 $data = $module->getResultData();
138
139 // Reformat useful data for future printing by JSON engine
140 $srchres = array ();
141 foreach ((array)$data['query']['allpages'] as $pageinfo) {
142 // Note: this data will no be printable by the xml engine
143 // because it does not support lists of unnamed items
144 $srchres[] = $pageinfo['title'];
145 }
146
147 return $srchres;
148 }
149
150 /**
151 * Validate an array of numerical namespace indexes
152 *
153 * @param array $namespaces
154 */
155 protected static function validateNamespaces($namespaces){
156 global $wgContLang;
157 $validNamespaces = $wgContLang->getNamespaces();
158 if( is_array($namespaces) && count($namespaces)>0 ){
159 $valid = array();
160 foreach ($namespaces as $ns){
161 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
162 $valid[] = $ns;
163 }
164 if( count($valid) > 0 )
165 return $valid;
166 }
167
168 return array( NS_MAIN );
169 }
170 }