MediaWiki master
ApiQueryPagesWithProp.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
15
23
24 public function __construct( ApiQuery $query, string $moduleName ) {
25 parent::__construct( $query, $moduleName, 'pwp' );
26 }
27
28 public function execute() {
29 $this->run();
30 }
31
33 public function getCacheMode( $params ) {
34 return 'public';
35 }
36
38 public function executeGenerator( $resultPageSet ) {
39 $this->run( $resultPageSet );
40 }
41
46 private function run( $resultPageSet = null ) {
47 $params = $this->extractRequestParams();
48
49 $prop = array_fill_keys( $params['prop'], true );
50 $fld_ids = isset( $prop['ids'] );
51 $fld_title = isset( $prop['title'] );
52 $fld_value = isset( $prop['value'] );
53
54 if ( $resultPageSet === null ) {
55 $this->addFields( [ 'page_id' ] );
56 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
57 $this->addFieldsIf( 'pp_value', $fld_value );
58 } else {
59 $this->addFields( $resultPageSet->getPageTableFields() );
60 }
61 $this->addTables( [ 'page_props', 'page' ] );
62 $this->addWhere( 'pp_page=page_id' );
63 $this->addWhereFld( 'pp_propname', $params['propname'] );
64
65 $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
66
67 if ( $params['continue'] ) {
68 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
69 // Add a WHERE clause
70 $this->addWhereRange( 'pp_page', $dir, $cont[0], null );
71 }
72
73 $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
74 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
75
76 $limit = $params['limit'];
77 $this->addOption( 'LIMIT', $limit + 1 );
78
79 $result = $this->getResult();
80 $count = 0;
81 $res = $this->select( __METHOD__ );
82
83 if ( $fld_title && $resultPageSet === null ) {
84 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
85 }
86
87 foreach ( $res as $row ) {
88 if ( ++$count > $limit ) {
89 // We've reached the one extra which shows that there are
90 // additional pages to be had. Stop here...
91 $this->setContinueEnumParameter( 'continue', $row->page_id );
92 break;
93 }
94
95 if ( $resultPageSet === null ) {
96 $vals = [
97 ApiResult::META_TYPE => 'assoc',
98 ];
99 if ( $fld_ids ) {
100 $vals['pageid'] = (int)$row->page_id;
101 }
102 if ( $fld_title ) {
103 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
104 ApiQueryBase::addTitleInfo( $vals, $title );
105 }
106 if ( $fld_value ) {
107 $vals['value'] = $row->pp_value;
108 }
109 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
110 if ( !$fit ) {
111 $this->setContinueEnumParameter( 'continue', $row->page_id );
112 break;
113 }
114 } else {
115 $resultPageSet->processDbRow( $row );
116 }
117 }
118
119 if ( $resultPageSet === null ) {
120 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
121 }
122 }
123
125 public function getAllowedParams() {
126 return [
127 'propname' => [
128 ParamValidator::PARAM_TYPE => 'string',
129 ParamValidator::PARAM_REQUIRED => true,
130 ],
131 'prop' => [
132 ParamValidator::PARAM_DEFAULT => 'ids|title',
133 ParamValidator::PARAM_ISMULTI => true,
134 ParamValidator::PARAM_TYPE => [
135 'ids',
136 'title',
137 'value',
138 ],
140 ],
141 'continue' => [
142 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
143 ],
144 'limit' => [
145 ParamValidator::PARAM_TYPE => 'limit',
146 ParamValidator::PARAM_DEFAULT => 10,
147 IntegerDef::PARAM_MIN => 1,
148 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
149 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
150 ],
151 'dir' => [
152 ParamValidator::PARAM_DEFAULT => 'ascending',
153 ParamValidator::PARAM_TYPE => [
154 'ascending',
155 'descending',
156 ]
157 ],
158 ];
159 }
160
162 protected function getExamplesMessages() {
163 return [
164 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
165 => 'apihelp-query+pageswithprop-example-simple',
166 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
167 => 'apihelp-query+pageswithprop-example-generator',
168 ];
169 }
170
172 public function getHelpUrls() {
173 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
174 }
175}
176
178class_alias( ApiQueryPagesWithProp::class, 'ApiQueryPagesWithProp' );
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1696
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:234
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:232
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.Return value has query strings as keys, with values being eith...
__construct(ApiQuery $query, string $moduleName)
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
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.Override this in the module subclass....
This is the main query class.
Definition ApiQuery.php:36
const META_TYPE
Key for the 'type' metadata item.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Type definition for integer types.
array $params
The job parameters.