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