00001 <?php
00002
00010 class PrefixSearch {
00018 public static function titleSearch( $search, $limit, $namespaces=array() ) {
00019 $search = trim( $search );
00020 if( $search == '' ) {
00021 return array();
00022 }
00023 $namespaces = self::validateNamespaces( $namespaces );
00024
00025 $title = Title::newFromText( $search );
00026 if( $title && $title->getInterwiki() == '' ) {
00027 $ns = array($title->getNamespace());
00028 if($ns[0] == NS_MAIN)
00029 $ns = $namespaces;
00030 return self::searchBackend(
00031 $ns, $title->getText(), $limit );
00032 }
00033
00034
00035 $title = Title::newFromText( $search . 'Dummy' );
00036 if( $title && $title->getText() == 'Dummy'
00037 && $title->getNamespace() != NS_MAIN
00038 && $title->getInterwiki() == '' ) {
00039 return self::searchBackend(
00040 array($title->getNamespace()), '', $limit );
00041 }
00042
00043 return self::searchBackend( $namespaces, $search, $limit );
00044 }
00045
00046
00054 protected static function searchBackend( $namespaces, $search, $limit ) {
00055 if( count($namespaces) == 1 ){
00056 $ns = $namespaces[0];
00057 if( $ns == NS_MEDIA ) {
00058 $namespaces = array(NS_FILE);
00059 } elseif( $ns == NS_SPECIAL ) {
00060 return self::specialSearch( $search, $limit );
00061 }
00062 }
00063 $srchres = array();
00064 if( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
00065 return self::defaultSearchBackend( $namespaces, $search, $limit );
00066 }
00067 return $srchres;
00068 }
00069
00073 protected static function specialSearch( $search, $limit ) {
00074 global $wgContLang;
00075 $searchKey = $wgContLang->caseFold( $search );
00076
00077
00078
00079 SpecialPage::initList();
00080 SpecialPage::initAliasList();
00081 $keys = array();
00082 foreach( array_keys( SpecialPage::$mList ) as $page ) {
00083 $keys[$wgContLang->caseFold( $page )] = $page;
00084 }
00085 foreach( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
00086 foreach( $aliases as $alias ) {
00087 $keys[$wgContLang->caseFold( $alias )] = $alias;
00088 }
00089 }
00090 ksort( $keys );
00091
00092 $srchres = array();
00093 foreach( $keys as $pageKey => $page ) {
00094 if( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
00095 $srchres[] = Title::makeTitle( NS_SPECIAL, $page )->getPrefixedText();
00096 }
00097 if( count( $srchres ) >= $limit ) {
00098 break;
00099 }
00100 }
00101 return $srchres;
00102 }
00103
00115 protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
00116 $ns = array_shift($namespaces);
00117 if( in_array(NS_MAIN,$namespaces))
00118 $ns = NS_MAIN;
00119
00120
00121 $req = new FauxRequest(array (
00122 'action' => 'query',
00123 'list' => 'allpages',
00124 'apnamespace' => $ns,
00125 'aplimit' => $limit,
00126 'apprefix' => $search
00127 ));
00128
00129
00130 $module = new ApiMain($req);
00131 $module->execute();
00132
00133
00134 $data = $module->getResultData();
00135
00136
00137 $srchres = array ();
00138 foreach ((array)$data['query']['allpages'] as $pageinfo) {
00139
00140
00141 $srchres[] = $pageinfo['title'];
00142 }
00143
00144 return $srchres;
00145 }
00146
00152 protected static function validateNamespaces($namespaces){
00153 global $wgContLang;
00154 $validNamespaces = $wgContLang->getNamespaces();
00155 if( is_array($namespaces) && count($namespaces)>0 ){
00156 $valid = array();
00157 foreach ($namespaces as $ns){
00158 if( is_numeric($ns) && array_key_exists($ns, $validNamespaces) )
00159 $valid[] = $ns;
00160 }
00161 if( count($valid) > 0 )
00162 return $valid;
00163 }
00164
00165 return array( NS_MAIN );
00166 }
00167 }