mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-24 00:04:35 +00:00
264f65215b
* Annotations * Deprecated functions * Namespace tests Change-Id: I521f6af6074a454cec5322ab4cd46db08350c2c3
47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace TextExtracts;
|
|
|
|
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;
|
|
}
|
|
$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;
|
|
}
|
|
}
|