| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\HookContainer\HookRunner; |
| 24 |
use MediaWiki\MediaWikiServices; |
| 25 |
use MediaWiki\Title\MediaWikiTitleCodec; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use Wikimedia\Rdbms\IExpression; |
| 28 |
use Wikimedia\Rdbms\LikeValue; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
abstract class PrefixSearch { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function search( $search, $limit, $namespaces = [], $offset = 0 ) { |
| 49 |
$search = trim( $search ); |
| 50 |
if ( $search == '' ) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
$hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true ); |
| 55 |
if ( $hasNamespace !== false ) { |
| 56 |
[ $search, $namespaces ] = $hasNamespace; |
| 57 |
} |
| 58 |
|
| 59 |
return $this->searchBackend( $namespaces, $search, $limit, $offset ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) { |
| 72 |
$searches = $this->search( $search, $limit, $namespaces, $offset ); |
| 73 |
|
| 74 |
|
| 75 |
$fallbackLimit = $limit - count( $searches ); |
| 76 |
if ( $fallbackLimit > 0 ) { |
| 77 |
$services = MediaWikiServices::getInstance(); |
| 78 |
$fallbackSearches = $services->getLanguageConverterFactory() |
| 79 |
->getLanguageConverter( $services->getContentLanguage() ) |
| 80 |
->autoConvertToAllVariants( $search ); |
| 81 |
$fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] ); |
| 82 |
|
| 83 |
foreach ( $fallbackSearches as $fbs ) { |
| 84 |
$fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces ); |
| 85 |
$searches = array_merge( $searches, $fallbackSearchResult ); |
| 86 |
$fallbackLimit -= count( $fallbackSearchResult ); |
| 87 |
|
| 88 |
if ( $fallbackLimit == 0 ) { |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $searches; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
abstract protected function titles( array $titles ); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
abstract protected function strings( array $strings ); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected function searchBackend( $namespaces, $search, $limit, $offset ) { |
| 123 |
if ( count( $namespaces ) == 1 ) { |
| 124 |
$ns = $namespaces[0]; |
| 125 |
if ( $ns == NS_MEDIA ) { |
| 126 |
$namespaces = [ NS_FILE ]; |
| 127 |
} elseif ( $ns == NS_SPECIAL ) { |
| 128 |
return $this->titles( $this->specialSearch( $search, $limit, $offset ) ); |
| 129 |
} |
| 130 |
} |
| 131 |
$srchres = []; |
| 132 |
if ( ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onPrefixSearchBackend( |
| 133 |
$namespaces, $search, $limit, $srchres, $offset ) |
| 134 |
) { |
| 135 |
return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) ); |
| 136 |
} |
| 137 |
return $this->strings( |
| 138 |
$this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) ); |
| 139 |
} |
| 140 |
|
| 141 |
private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) { |
| 142 |
if ( $offset === 0 ) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
$rescorer = new SearchExactMatchRescorer(); |
| 148 |
$srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit ); |
| 149 |
} |
| 150 |
return $srchres; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
protected function specialSearch( $search, $limit, $offset ) { |
| 162 |
$searchParts = explode( '/', $search, 2 ); |
| 163 |
$searchKey = $searchParts[0]; |
| 164 |
$subpageSearch = $searchParts[1] ?? null; |
| 165 |
|
| 166 |
|
| 167 |
$spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory(); |
| 168 |
if ( $subpageSearch !== null ) { |
| 169 |
|
| 170 |
$specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey ); |
| 171 |
if ( !$specialTitle ) { |
| 172 |
return []; |
| 173 |
} |
| 174 |
$special = $spFactory->getPage( $specialTitle->getText() ); |
| 175 |
if ( $special ) { |
| 176 |
$subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset ); |
| 177 |
return array_map( [ $specialTitle, 'getSubpage' ], $subpages ); |
| 178 |
} else { |
| 179 |
return []; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 185 |
$searchKey = str_replace( ' ', '_', $searchKey ); |
| 186 |
$searchKey = $contLang->caseFold( $searchKey ); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
$keys = []; |
| 191 |
$listedPages = $spFactory->getListedPages(); |
| 192 |
foreach ( $listedPages as $page => $_obj ) { |
| 193 |
$keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ]; |
| 194 |
} |
| 195 |
|
| 196 |
foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) { |
| 197 |
|
| 198 |
|
| 199 |
if ( !in_array( $page, $spFactory->getNames() ) ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
if ( !isset( $listedPages[ $page ] ) ) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
foreach ( $aliases as $key => $alias ) { |
| 208 |
$keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ]; |
| 209 |
} |
| 210 |
} |
| 211 |
ksort( $keys ); |
| 212 |
|
| 213 |
$matches = []; |
| 214 |
foreach ( $keys as $pageKey => $page ) { |
| 215 |
if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
$matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] ); |
| 221 |
|
| 222 |
if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) { |
| 223 |
|
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
ksort( $matches ); |
| 232 |
|
| 233 |
$matches = array_reduce( $matches, 'array_merge', [] ); |
| 234 |
|
| 235 |
return array_slice( $matches, $offset, $limit ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) { |
| 251 |
if ( !$namespaces ) { |
| 252 |
$namespaces = [ NS_MAIN ]; |
| 253 |
} |
| 254 |
|
| 255 |
if ( in_array( NS_SPECIAL, $namespaces ) ) { |
| 256 |
|
| 257 |
return $this->specialSearch( $search, $limit, $offset ); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
$prefixes = []; |
| 263 |
|
| 264 |
if ( $search === '' ) { |
| 265 |
$prefixes[$search] = $namespaces; |
| 266 |
} else { |
| 267 |
|
| 268 |
$search = preg_replace( MediaWikiTitleCodec::getTitleInvalidRegex(), '', $search ); |
| 269 |
foreach ( $namespaces as $namespace ) { |
| 270 |
$title = Title::makeTitleSafe( $namespace, $search ); |
| 271 |
if ( $title ) { |
| 272 |
$prefixes[ $title->getDBkey() ][] = $namespace; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
if ( !$prefixes ) { |
| 277 |
return []; |
| 278 |
} |
| 279 |
|
| 280 |
$services = MediaWikiServices::getInstance(); |
| 281 |
$dbr = $services->getConnectionProvider()->getReplicaDatabase(); |
| 282 |
|
| 283 |
|
| 284 |
$conds = []; |
| 285 |
foreach ( $prefixes as $prefix => $namespaces ) { |
| 286 |
$expr = $dbr->expr( 'page_namespace', '=', $namespaces ); |
| 287 |
if ( $prefix !== '' ) { |
| 288 |
$expr = $expr->and( |
| 289 |
'page_title', |
| 290 |
IExpression::LIKE, |
| 291 |
new LikeValue( (string)$prefix, $dbr->anyString() ) |
| 292 |
); |
| 293 |
} |
| 294 |
$conds[] = $expr; |
| 295 |
} |
| 296 |
|
| 297 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 298 |
->select( [ 'page_id', 'page_namespace', 'page_title' ] ) |
| 299 |
->from( 'page' ) |
| 300 |
->where( $dbr->orExpr( $conds ) ) |
| 301 |
->orderBy( [ 'page_title', 'page_namespace' ] ) |
| 302 |
->limit( $limit ) |
| 303 |
->offset( $offset ); |
| 304 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 305 |
|
| 306 |
return iterator_to_array( $services->getTitleFactory()->newTitleArrayFromResult( $res ) ); |
| 307 |
} |
| 308 |
} |