3 * Prefix search of page names.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Handles searching prefixes of titles and finding any page
25 * names that match. Used largely by the OpenSearch implementation.
29 abstract class PrefixSearch
{
31 * Do a prefix search of titles and return a list of matching page names.
32 * @deprecated: Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
34 * @param $search String
35 * @param $limit Integer
36 * @param array $namespaces used if query is not explicitly prefixed
37 * @return Array of strings
39 public static function titleSearch( $search, $limit, $namespaces = array() ) {
40 $prefixSearch = new StringPrefixSearch
;
41 return $prefixSearch->search( $search, $limit, $namespaces );
45 * Do a prefix search of titles and return a list of matching page names.
47 * @param $search String
48 * @param $limit Integer
49 * @param array $namespaces used if query is not explicitly prefixed
50 * @return Array of strings or Title objects
52 public function search( $search, $limit, $namespaces = array() ) {
53 $search = trim( $search );
54 if ( $search == '' ) {
55 return array(); // Return empty result
57 $namespaces = $this->validateNamespaces( $namespaces );
59 // Find a Title which is not an interwiki and is in NS_MAIN
60 $title = Title
::newFromText( $search );
61 if ( $title && !$title->isExternal() ) {
62 $ns = array( $title->getNamespace() );
63 if ( $ns[0] == NS_MAIN
) {
64 $ns = $namespaces; // no explicit prefix, use default namespaces
66 return $this->searchBackend(
67 $ns, $title->getText(), $limit );
70 // Is this a namespace prefix?
71 $title = Title
::newFromText( $search . 'Dummy' );
72 if ( $title && $title->getText() == 'Dummy'
73 && $title->getNamespace() != NS_MAIN
74 && !$title->isExternal() )
76 $namespaces = array( $title->getNamespace() );
80 return $this->searchBackend( $namespaces, $search, $limit );
84 * Do a prefix search for all possible variants of the prefix
85 * @param $search String
86 * @param $limit Integer
87 * @param array $namespaces
91 public function searchWithVariants( $search, $limit, array $namespaces ) {
92 wfProfileIn( __METHOD__
);
93 $searches = $this->search( $search, $limit, $namespaces );
95 // if the content language has variants, try to retrieve fallback results
96 $fallbackLimit = $limit - count( $searches );
97 if ( $fallbackLimit > 0 ) {
100 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
101 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
103 foreach ( $fallbackSearches as $fbs ) {
104 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
105 $searches = array_merge( $searches, $fallbackSearchResult );
106 $fallbackLimit -= count( $fallbackSearchResult );
108 if ( $fallbackLimit == 0 ) {
113 wfProfileOut( __METHOD__
);
118 * When implemented in a descendant class, receives an array of Title objects and returns
119 * either an unmodified array or an array of strings corresponding to titles passed to it.
121 * @param array $titles
124 protected abstract function titles( array $titles );
127 * When implemented in a descendant class, receives an array of titles as strings and returns
128 * either an unmodified array or an array of Title objects corresponding to strings received.
130 * @param array $strings
134 protected abstract function strings( array $strings );
137 * Do a prefix search of titles and return a list of matching page names.
138 * @param $namespaces Array
139 * @param $search String
140 * @param $limit Integer
141 * @return Array of strings
143 protected function searchBackend( $namespaces, $search, $limit ) {
144 if ( count( $namespaces ) == 1 ) {
145 $ns = $namespaces[0];
146 if ( $ns == NS_MEDIA
) {
147 $namespaces = array( NS_FILE
);
148 } elseif ( $ns == NS_SPECIAL
) {
149 return $this->titles( $this->specialSearch( $search, $limit ) );
153 if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
154 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
156 return $this->strings( $srchres );
160 * Prefix search special-case for Special: namespace.
162 * @param string $search term
163 * @param $limit Integer: max number of items to return
166 protected function specialSearch( $search, $limit ) {
169 # normalize searchKey, so aliases with spaces can be found - bug 25675
170 $search = str_replace( ' ', '_', $search );
172 $searchKey = $wgContLang->caseFold( $search );
174 // Unlike SpecialPage itself, we want the canonical forms of both
175 // canonical and alias title forms...
177 foreach ( SpecialPageFactory
::getList() as $page => $class ) {
178 $keys[$wgContLang->caseFold( $page )] = $page;
181 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
182 if ( !array_key_exists( $page, SpecialPageFactory
::getList() ) ) {# bug 20885
186 foreach ( $aliases as $alias ) {
187 $keys[$wgContLang->caseFold( $alias )] = $alias;
193 foreach ( $keys as $pageKey => $page ) {
194 if ( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
195 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
196 // localizes its input leading to searches for e.g. Special:All
197 // returning Spezial:MediaWiki-Systemnachrichten and returning
198 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
199 $srchres[] = Title
::makeTitleSafe( NS_SPECIAL
, $page );
202 if ( count( $srchres ) >= $limit ) {
211 * Unless overridden by PrefixSearchBackend hook...
212 * This is case-sensitive (First character may
213 * be automatically capitalized by Title::secureAndSpit()
214 * later on depending on $wgCapitalLinks)
216 * @param array $namespaces namespaces to search in
217 * @param string $search term
218 * @param $limit Integer: max number of items to return
219 * @return Array of Title objects
221 protected function defaultSearchBackend( $namespaces, $search, $limit ) {
222 $ns = array_shift( $namespaces ); // support only one namespace
223 if ( in_array( NS_MAIN
, $namespaces ) ) {
224 $ns = NS_MAIN
; // if searching on many always default to main
227 $t = Title
::newFromText( $search, $ns );
228 $prefix = $t ?
$t->getDBkey() : '';
229 $dbr = wfGetDB( DB_SLAVE
);
230 $res = $dbr->select( 'page',
231 array( 'page_id', 'page_namespace', 'page_title' ),
233 'page_namespace' => $ns,
234 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
237 array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
240 foreach ( $res as $row ) {
241 $srchres[] = Title
::newFromRow( $row );
247 * Validate an array of numerical namespace indexes
249 * @param $namespaces Array
250 * @return Array (default: contains only NS_MAIN)
252 protected function validateNamespaces( $namespaces ) {
255 // We will look at each given namespace against wgContLang namespaces
256 $validNamespaces = $wgContLang->getNamespaces();
257 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
259 foreach ( $namespaces as $ns ) {
260 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
264 if ( count( $valid ) > 0 ) {
269 return array( NS_MAIN
);
274 * Performs prefix search, returning Title objects
277 class TitlePrefixSearch
extends PrefixSearch
{
279 protected function titles( array $titles ) {
283 protected function strings( array $strings ) {
284 $titles = array_map( 'Title::newFromText', $strings );
285 $lb = new LinkBatch( $titles );
286 $lb->setCaller( __METHOD__
);
293 * Performs prefix search, returning strings
296 class StringPrefixSearch
extends PrefixSearch
{
298 protected function titles( array $titles ) {
299 return array_map( function( Title
$t ) { return $t->getPrefixedText(); }, $titles );
302 protected function strings( array $strings ) {