MediaWiki  master
ApiQueryPageProps.php
Go to the documentation of this file.
1 <?php
24 
31 
33  private $pageProps;
34 
40  public function __construct(
41  ApiQuery $query,
42  $moduleName,
43  PageProps $pageProps
44  ) {
45  parent::__construct( $query, $moduleName, 'pp' );
46  $this->pageProps = $pageProps;
47  }
48 
49  public function execute() {
50  # Only operate on existing pages
51  $pages = $this->getPageSet()->getGoodPages();
52 
53  $params = $this->extractRequestParams();
54  if ( $params['continue'] ) {
55  $continueValue = (int)$params['continue'];
56  $this->dieContinueUsageIf( strval( $continueValue ) !== $params['continue'] );
57  $filteredPages = [];
58  foreach ( $pages as $id => $page ) {
59  if ( $id >= $continueValue ) {
60  $filteredPages[$id] = $page;
61  }
62  }
63  $pages = $filteredPages;
64  }
65 
66  if ( $pages === [] ) {
67  # Nothing to do
68  return;
69  }
70 
71  if ( $params['prop'] ) {
72  $properties = $this->pageProps->getProperties( $pages, $params['prop'] );
73  } else {
74  $properties = $this->pageProps->getAllProperties( $pages );
75  }
76 
77  ksort( $properties );
78 
79  $result = $this->getResult();
80  foreach ( $properties as $pageid => $props ) {
81  if ( !$this->addPageProps( $result, $pageid, $props ) ) {
82  break;
83  }
84  }
85  }
86 
96  private function addPageProps( $result, $page, $props ) {
97  ApiResult::setArrayType( $props, 'assoc' );
98  $fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props );
99 
100  if ( !$fit ) {
101  $this->setContinueEnumParameter( 'continue', $page );
102  }
103 
104  return $fit;
105  }
106 
107  public function getCacheMode( $params ) {
108  return 'public';
109  }
110 
111  public function getAllowedParams() {
112  return [
113  'continue' => [
114  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
115  ],
116  'prop' => [
117  ParamValidator::PARAM_ISMULTI => true,
118  ],
119  ];
120  }
121 
122  protected function getExamplesMessages() {
123  return [
124  'action=query&prop=pageprops&titles=Main%20Page|MediaWiki'
125  => 'apihelp-query+pageprops-example-simple',
126  ];
127  }
128 
129  public function getHelpUrls() {
130  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops';
131  }
132 }
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1650
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
getPageSet()
Get the PageSet object to work on.
A query module to show basic page information.
__construct(ApiQuery $query, $moduleName, PageProps $pageProps)
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main query class.
Definition: ApiQuery.php:41
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:716
Gives access to properties of a page.
Definition: PageProps.php:33
Service for formatting and validating API parameters.