MediaWiki  1.34.0
ApiQueryReferences.php
Go to the documentation of this file.
1 <?php
25 
27 
28  public function __construct( $query, $moduleName ) {
29  parent::__construct( $query, $moduleName, 'rf' );
30  }
31 
32  public function getAllowedParams() {
33  return [
34  'continue' => [
35  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
36  ],
37  ];
38  }
39 
40  public function execute() {
41  $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'cite' );
42  if ( !$config->get( 'CiteStoreReferencesData' ) ) {
43  $this->dieWithError( 'apierror-citestoragedisabled' );
44  }
45  $params = $this->extractRequestParams();
46  $titles = $this->getPageSet()->getGoodTitles();
47  ksort( $titles );
48  if ( !is_null( $params['continue'] ) ) {
49  $startId = (int)$params['continue'];
50  // check it is definitely an int
51  $this->dieContinueUsageIf( strval( $startId ) !== $params['continue'] );
52  } else {
53  $startId = false;
54  }
55 
56  foreach ( $titles as $pageId => $title ) {
57  // Skip until you have the correct starting point
58  if ( $startId !== false && $startId !== $pageId ) {
59  continue;
60  } else {
61  $startId = false;
62  }
63  $storedRefs = Cite::getStoredReferences( $title );
64  $allReferences = [];
65  // some pages may not have references stored
66  if ( $storedRefs !== false ) {
67  // a page can have multiple <references> tags but they all have unique keys
68  foreach ( $storedRefs['refs'] as $index => $grouping ) {
69  foreach ( $grouping as $group => $members ) {
70  foreach ( $members as $name => $ref ) {
71  $ref['name'] = $name;
72  $key = $ref['key'];
73  if ( is_string( $name ) ) {
74  $id = Cite::getReferencesKey( $name . '-' . $key );
75  } else {
76  $id = Cite::getReferencesKey( $key );
77  }
78  $ref['group'] = $group;
79  $ref['reflist'] = $index;
80  $allReferences[$id] = $ref;
81  }
82  }
83  }
84  }
85  // set some metadata since its an assoc data structure
86  ApiResult::setArrayType( $allReferences, 'kvp', 'id' );
87  // Ship a data representation of the combined references.
88  $fit = $this->addPageSubItems( $pageId, $allReferences );
89  if ( !$fit ) {
90  $this->setContinueEnumParameter( 'continue', $pageId );
91  break;
92  }
93  }
94  }
95 
102  public function getCacheMode( $params ) {
103  return 'public';
104  }
105 
110  protected function getExamplesMessages() {
111  return [
112  'action=query&prop=references&titles=Albert%20Einstein' =>
113  'apihelp-query+references-example-1',
114  ];
115  }
116 
117 }
ApiQueryBase\addPageSubItems
addPageSubItems( $pageId, $data)
Add a sub-element under the page element with the given page ID.
Definition: ApiQueryBase.php:454
ApiQueryReferences\getExamplesMessages
getExamplesMessages()
Definition: ApiQueryReferences.php:110
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:131
ApiQueryReferences\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryReferences.php:32
ApiQueryReferences\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryReferences.php:40
ApiResult\setArrayType
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:728
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
ApiQueryReferences\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryReferences.php:102
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:2208
ApiQueryBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:132
ApiQueryReferences\__construct
__construct( $query, $moduleName)
Definition: ApiQueryReferences.php:28
ApiQueryReferences
Definition: ApiQueryReferences.php:26
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:492