2014-12-31 23:02:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace TextExtracts;
|
|
|
|
|
2017-05-26 21:17:21 +00:00
|
|
|
use ApiBase;
|
2014-12-31 23:02:05 +00:00
|
|
|
use ApiMain;
|
2014-12-30 22:20:55 +00:00
|
|
|
use ApiResult;
|
2014-12-31 23:02:05 +00:00
|
|
|
use FauxRequest;
|
2016-09-23 01:51:12 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2014-12-31 23:02:05 +00:00
|
|
|
|
2019-04-24 16:26:53 +00:00
|
|
|
/**
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2014-12-31 23:02:05 +00:00
|
|
|
class Hooks {
|
2015-12-09 23:33:24 +00:00
|
|
|
|
2014-12-31 23:02:05 +00:00
|
|
|
/**
|
|
|
|
* ApiOpenSearchSuggest hook handler
|
2017-11-30 02:56:29 +00:00
|
|
|
* @param array &$results Array of search results
|
2014-12-31 23:02:05 +00:00
|
|
|
*/
|
|
|
|
public static function onApiOpenSearchSuggest( &$results ) {
|
2016-09-23 01:51:12 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
|
2018-06-28 18:45:20 +00:00
|
|
|
if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || $results === [] ) {
|
|
|
|
return;
|
2014-12-31 23:02:05 +00:00
|
|
|
}
|
2018-06-28 18:45:20 +00:00
|
|
|
|
2017-05-26 21:17:21 +00:00
|
|
|
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 ) {
|
2019-02-02 20:53:08 +00:00
|
|
|
$contentKey = $data[$id]['extract'][ApiResult::META_CONTENT] ?? '*';
|
2017-05-26 21:17:21 +00:00
|
|
|
if ( isset( $data[$id]['extract'][$contentKey] ) ) {
|
|
|
|
$results[$id]['extract'] = $data[$id]['extract'][$contentKey];
|
|
|
|
$results[$id]['extract trimmed'] = false;
|
|
|
|
}
|
2014-12-31 23:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 22:20:55 +00:00
|
|
|
}
|