mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-27 17:40:16 +00:00
60cd40b975
This patch fixes two styls issues I could not separate: * Hook handler functions do not need to return true. This is the default anyway, and meaningless. * Counting is possibly expensive and not needed when all we need to know is if an array is empty or not. Change-Id: I460776c981638806a606d9bf88fc8579d6da8c0e
48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace TextExtracts;
|
|
|
|
use ApiBase;
|
|
use ApiMain;
|
|
use ApiResult;
|
|
use FauxRequest;
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
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 = 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|