MediaWiki master
ApiQueryPagesWithProp.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Api;
25
29
37
38 public function __construct( ApiQuery $query, string $moduleName ) {
39 parent::__construct( $query, $moduleName, 'pwp' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function getCacheMode( $params ) {
47 return 'public';
48 }
49
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
52 }
53
58 private function run( $resultPageSet = null ) {
60
61 $prop = array_fill_keys( $params['prop'], true );
62 $fld_ids = isset( $prop['ids'] );
63 $fld_title = isset( $prop['title'] );
64 $fld_value = isset( $prop['value'] );
65
66 if ( $resultPageSet === null ) {
67 $this->addFields( [ 'page_id' ] );
68 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
69 $this->addFieldsIf( 'pp_value', $fld_value );
70 } else {
71 $this->addFields( $resultPageSet->getPageTableFields() );
72 }
73 $this->addTables( [ 'page_props', 'page' ] );
74 $this->addWhere( 'pp_page=page_id' );
75 $this->addWhereFld( 'pp_propname', $params['propname'] );
76
77 $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
78
79 if ( $params['continue'] ) {
80 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
81 // Add a WHERE clause
82 $this->addWhereRange( 'pp_page', $dir, $cont[0], null );
83 }
84
85 $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
86 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
87
88 $limit = $params['limit'];
89 $this->addOption( 'LIMIT', $limit + 1 );
90
91 $result = $this->getResult();
92 $count = 0;
93 $res = $this->select( __METHOD__ );
94
95 if ( $fld_title && $resultPageSet === null ) {
96 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
97 }
98
99 foreach ( $res as $row ) {
100 if ( ++$count > $limit ) {
101 // We've reached the one extra which shows that there are
102 // additional pages to be had. Stop here...
103 $this->setContinueEnumParameter( 'continue', $row->page_id );
104 break;
105 }
106
107 if ( $resultPageSet === null ) {
108 $vals = [
109 ApiResult::META_TYPE => 'assoc',
110 ];
111 if ( $fld_ids ) {
112 $vals['pageid'] = (int)$row->page_id;
113 }
114 if ( $fld_title ) {
115 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116 ApiQueryBase::addTitleInfo( $vals, $title );
117 }
118 if ( $fld_value ) {
119 $vals['value'] = $row->pp_value;
120 }
121 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'continue', $row->page_id );
124 break;
125 }
126 } else {
127 $resultPageSet->processDbRow( $row );
128 }
129 }
130
131 if ( $resultPageSet === null ) {
132 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
133 }
134 }
135
136 public function getAllowedParams() {
137 return [
138 'propname' => [
139 ParamValidator::PARAM_TYPE => 'string',
140 ParamValidator::PARAM_REQUIRED => true,
141 ],
142 'prop' => [
143 ParamValidator::PARAM_DEFAULT => 'ids|title',
144 ParamValidator::PARAM_ISMULTI => true,
145 ParamValidator::PARAM_TYPE => [
146 'ids',
147 'title',
148 'value',
149 ],
151 ],
152 'continue' => [
153 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
154 ],
155 'limit' => [
156 ParamValidator::PARAM_TYPE => 'limit',
157 ParamValidator::PARAM_DEFAULT => 10,
158 IntegerDef::PARAM_MIN => 1,
159 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
160 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
161 ],
162 'dir' => [
163 ParamValidator::PARAM_DEFAULT => 'ascending',
164 ParamValidator::PARAM_TYPE => [
165 'ascending',
166 'descending',
167 ]
168 ],
169 ];
170 }
171
172 protected function getExamplesMessages() {
173 return [
174 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
175 => 'apihelp-query+pageswithprop-example-simple',
176 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
177 => 'apihelp-query+pageswithprop-example-generator',
178 ];
179 }
180
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
183 }
184}
185
187class_alias( ApiQueryPagesWithProp::class, 'ApiQueryPagesWithProp' );
array $params
The job parameters.
run()
Run the job.
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1756
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:251
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:249
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
A query module to enumerate pages that use a particular prop.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, string $moduleName)
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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.
This is the main query class.
Definition ApiQuery.php:48
const META_TYPE
Key for the 'type' metadata item.
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.