2014-12-31 23:02:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace TextExtracts;
|
|
|
|
|
|
|
|
|
|
|
|
use ApiMain;
|
2014-12-30 22:20:55 +00:00
|
|
|
use ApiResult;
|
2014-12-31 23:02:05 +00:00
|
|
|
use ConfigFactory;
|
|
|
|
use FauxRequest;
|
|
|
|
|
|
|
|
class Hooks {
|
2015-12-09 23:33:24 +00:00
|
|
|
|
2014-12-31 23:02:05 +00:00
|
|
|
/**
|
|
|
|
* 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(
|
|
|
|
array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'extracts',
|
|
|
|
'explaintext' => true,
|
|
|
|
'exintro' => true,
|
|
|
|
'exlimit' => count( $results ),
|
|
|
|
'pageids' => implode( '|', $pageIds ),
|
|
|
|
) )
|
|
|
|
);
|
|
|
|
$api->execute();
|
2014-12-30 22:20:55 +00:00
|
|
|
if ( defined( 'ApiResult::META_CONTENT' ) ) {
|
|
|
|
$data = $api->getResult()->getResultData( array( '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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = $api->getResultData();
|
|
|
|
foreach ( $pageIds as $id ) {
|
|
|
|
if ( isset( $data['query']['pages'][$id]['extract']['*'] ) ) {
|
|
|
|
$results[$id]['extract'] = $data['query']['pages'][$id]['extract']['*'];
|
|
|
|
$results[$id]['extract trimmed'] = false;
|
|
|
|
}
|
2014-12-31 23:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-30 22:20:55 +00:00
|
|
|
}
|