mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-15 03:35:20 +00:00
1f1c7e639d
One of many reasons that internal API calls are bad. Bug: T41936 Change-Id: I3d2cf2b4f619f590e74a88fa4a78832b8be8495e
49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace TextExtracts;
|
|
|
|
use ApiBase;
|
|
use ApiMain;
|
|
use ApiResult;
|
|
use FauxRequest;
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
class Hooks {
|
|
|
|
/**
|
|
* ApiOpenSearchSuggest hook handler
|
|
* @param array $results
|
|
* @return bool
|
|
*/
|
|
public static function onApiOpenSearchSuggest( &$results ) {
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
|
|
if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || !count( $results ) ) {
|
|
return true;
|
|
}
|
|
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 = isset( $data[$id]['extract'][ApiResult::META_CONTENT] )
|
|
? $data[$id]['extract'][ApiResult::META_CONTENT]
|
|
: '*';
|
|
if ( isset( $data[$id]['extract'][$contentKey] ) ) {
|
|
$results[$id]['extract'] = $data[$id]['extract'][$contentKey];
|
|
$results[$id]['extract trimmed'] = false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|