mediawiki-extensions-TextEx.../includes/Hooks.php
Max Semenik 754c9e4f19 CodeSniffer fixes
Change-Id: I8bdcd2250bd3163fe40ce4685eb04bffe53afdca
2016-09-22 18:38:27 -07:00

47 lines
1.1 KiB
PHP

<?php
namespace TextExtracts;
use ApiMain;
use ApiResult;
use ConfigFactory;
use FauxRequest;
class Hooks {
/**
* ApiOpenSearchSuggest hook handler
* @param array $results
* @return bool
*/
public static function onApiOpenSearchSuggest( &$results ) {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'textextracts' );
if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || !count( $results ) ) {
return true;
}
$pageIds = array_keys( $results );
$api = new ApiMain( new FauxRequest(
[
'action' => 'query',
'prop' => 'extracts',
'explaintext' => true,
'exintro' => true,
'exlimit' => count( $results ),
'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;
}
}