mediawiki-extensions-TextEx.../includes/Hooks.php
gerritbot bdd77c5c06 Update moved class FauxRequest
See T321882. Moved in I832b133aaf61ee

Bug: T321681
Change-Id: I6ce2acc3961db1b9663efd62ebdcf57c905caaf0
2023-05-19 10:25:20 +00:00

49 lines
1.2 KiB
PHP

<?php
namespace TextExtracts;
use ApiBase;
use ApiMain;
use ApiResult;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\FauxRequest;
/**
* @license GPL-2.0-or-later
*/
class Hooks {
/**
* ApiOpenSearchSuggest hook handler
* @param array &$results Array of search results
*/
public static function onApiOpenSearchSuggest( &$results ) {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || $results === [] ) {
return;
}
foreach ( array_chunk( array_keys( $results ), ApiBase::LIMIT_SML1 ) as $pageIds ) {
$api = new ApiMain( new FauxRequest(
[
'action' => 'query',
'prop' => 'extracts',
'explaintext' => true,
'exintro' => true,
'exlimit' => count( $pageIds ),
'pageids' => implode( '|', $pageIds ),
] )
);
$api->execute();
$data = $api->getResult()->getResultData( [ 'query', 'pages' ] );
foreach ( $pageIds as $id ) {
$contentKey = $data[$id]['extract'][ApiResult::META_CONTENT] ?? '*';
if ( isset( $data[$id]['extract'][$contentKey] ) ) {
$results[$id]['extract'] = $data[$id]['extract'][$contentKey];
$results[$id]['extract trimmed'] = false;
}
}
}
}
}