MediaWiki  1.34.0
Hooks.php
Go to the documentation of this file.
1 <?php
2 
3 namespace TextExtracts;
4 
5 use ApiBase;
6 use ApiMain;
7 use ApiResult;
8 use FauxRequest;
10 
14 class Hooks {
15 
20  public static function onApiOpenSearchSuggest( &$results ) {
21  $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
22  if ( !$config->get( 'ExtractsExtendOpenSearchXml' ) || $results === [] ) {
23  return;
24  }
25 
26  foreach ( array_chunk( array_keys( $results ), ApiBase::LIMIT_SML1 ) as $pageIds ) {
27  $api = new ApiMain( new FauxRequest(
28  [
29  'action' => 'query',
30  'prop' => 'extracts',
31  'explaintext' => true,
32  'exintro' => true,
33  'exlimit' => count( $pageIds ),
34  'pageids' => implode( '|', $pageIds ),
35  ] )
36  );
37  $api->execute();
38  $data = $api->getResult()->getResultData( [ 'query', 'pages' ] );
39  foreach ( $pageIds as $id ) {
40  $contentKey = $data[$id]['extract'][ApiResult::META_CONTENT] ?? '*';
41  if ( isset( $data[$id]['extract'][$contentKey] ) ) {
42  $results[$id]['extract'] = $data[$id]['extract'][$contentKey];
43  $results[$id]['extract trimmed'] = false;
44  }
45  }
46  }
47  }
48 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
TextExtracts\Hooks\onApiOpenSearchSuggest
static onApiOpenSearchSuggest(&$results)
ApiOpenSearchSuggest hook handler.
Definition: Hooks.php:20
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiResult
This class represents the result of the API operations.
Definition: ApiResult.php:35
TextExtracts
Definition: ApiQueryExtracts.php:3
TextExtracts\Hooks
Definition: Hooks.php:14
ApiResult\META_CONTENT
const META_CONTENT
Key for the 'content' metadata item.
Definition: ApiResult.php:90
ApiBase\LIMIT_SML1
const LIMIT_SML1
Slow query, standard limit.
Definition: ApiBase.php:263