MediaWiki REL1_34
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}
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:2208
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:131
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addPageSubItems( $pageId, $data)
Add a sub-element under the page element with the given page ID.
getPageSet()
Get the PageSet object to work on.
__construct( $query, $moduleName)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getCacheMode( $params)
Get the cache mode for the data generated by this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
static getStoredReferences(Title $title)
Fetch references stored for the given title in page_props For performance, results are cached.
Definition Cite.php:1402
static getReferencesKey( $key)
Return an id for use in wikitext output based on a key and optionally the number of it,...
Definition Cite.php:1017
MediaWikiServices is the service locator for the application scope of MediaWiki.