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
|
|
|
|
|
|
|
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
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onApiOpenSearchSuggest( &$results ) {
|
2016-09-23 01:51:12 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
|
2014-12-31 23:02:05 +00:00
|
|
|
if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || !count( $results ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
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 ) {
|
|
|
|
$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;
|
|
|
|
}
|
2014-12-31 23:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-30 22:20:55 +00:00
|
|
|
}
|