mediawiki-extensions-TextEx.../includes/Hooks.php
Brad Jorsch c3eb02a9a6 Update ApiResult handling for mediawiki/core change I7b37295e
Change I7b37295e for mediawiki/core deprecates several methods, and more
importantly changes the format of the data returned from
ApiResult::getData(). This change should handle these differences in a
backwards-compatible manner.

Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678e
2015-02-17 14:37:22 -05:00

57 lines
1.5 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(
array(
'action' => 'query',
'prop' => 'extracts',
'explaintext' => true,
'exintro' => true,
'exlimit' => count( $results ),
'pageids' => implode( '|', $pageIds ),
) )
);
$api->execute();
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;
}
}
}
return true;
}
}