MediaWiki  master
ApiQueryPagesWithProp.php
Go to the documentation of this file.
1 <?php
26 
34 
39  public function __construct( ApiQuery $query, $moduleName ) {
40  parent::__construct( $query, $moduleName, 'pwp' );
41  }
42 
43  public function execute() {
44  $this->run();
45  }
46 
47  public function getCacheMode( $params ) {
48  return 'public';
49  }
50 
51  public function executeGenerator( $resultPageSet ) {
52  $this->run( $resultPageSet );
53  }
54 
59  private function run( $resultPageSet = null ) {
60  $params = $this->extractRequestParams();
61 
62  $prop = array_fill_keys( $params['prop'], true );
63  $fld_ids = isset( $prop['ids'] );
64  $fld_title = isset( $prop['title'] );
65  $fld_value = isset( $prop['value'] );
66 
67  if ( $resultPageSet === null ) {
68  $this->addFields( [ 'page_id' ] );
69  $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
70  $this->addFieldsIf( 'pp_value', $fld_value );
71  } else {
72  $this->addFields( $resultPageSet->getPageTableFields() );
73  }
74  $this->addTables( [ 'page_props', 'page' ] );
75  $this->addWhere( 'pp_page=page_id' );
76  $this->addWhereFld( 'pp_propname', $params['propname'] );
77 
78  $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
79 
80  if ( $params['continue'] ) {
81  $cont = explode( '|', $params['continue'] );
82  $this->dieContinueUsageIf( count( $cont ) != 1 );
83 
84  // Add a WHERE clause
85  $from = (int)$cont[0];
86  $this->addWhereRange( 'pp_page', $dir, $from, null );
87  }
88 
89  $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
90  $this->addOption( 'ORDER BY', 'pp_page' . $sort );
91 
92  $limit = $params['limit'];
93  $this->addOption( 'LIMIT', $limit + 1 );
94 
95  $result = $this->getResult();
96  $count = 0;
97  $res = $this->select( __METHOD__ );
98 
99  if ( $fld_title && $resultPageSet === null ) {
100  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
101  }
102 
103  foreach ( $res as $row ) {
104  if ( ++$count > $limit ) {
105  // We've reached the one extra which shows that there are
106  // additional pages to be had. Stop here...
107  $this->setContinueEnumParameter( 'continue', $row->page_id );
108  break;
109  }
110 
111  if ( $resultPageSet === null ) {
112  $vals = [
113  ApiResult::META_TYPE => 'assoc',
114  ];
115  if ( $fld_ids ) {
116  $vals['pageid'] = (int)$row->page_id;
117  }
118  if ( $fld_title ) {
119  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
121  }
122  if ( $fld_value ) {
123  $vals['value'] = $row->pp_value;
124  }
125  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
126  if ( !$fit ) {
127  $this->setContinueEnumParameter( 'continue', $row->page_id );
128  break;
129  }
130  } else {
131  $resultPageSet->processDbRow( $row );
132  }
133  }
134 
135  if ( $resultPageSet === null ) {
136  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
137  }
138  }
139 
140  public function getAllowedParams() {
141  return [
142  'propname' => [
143  ParamValidator::PARAM_TYPE => 'string',
144  ParamValidator::PARAM_REQUIRED => true,
145  ],
146  'prop' => [
147  ParamValidator::PARAM_DEFAULT => 'ids|title',
148  ParamValidator::PARAM_ISMULTI => true,
149  ParamValidator::PARAM_TYPE => [
150  'ids',
151  'title',
152  'value',
153  ],
155  ],
156  'continue' => [
157  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
158  ],
159  'limit' => [
160  ParamValidator::PARAM_TYPE => 'limit',
161  ParamValidator::PARAM_DEFAULT => 10,
162  IntegerDef::PARAM_MIN => 1,
163  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
164  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
165  ],
166  'dir' => [
167  ParamValidator::PARAM_DEFAULT => 'ascending',
168  ParamValidator::PARAM_TYPE => [
169  'ascending',
170  'descending',
171  ]
172  ],
173  ];
174  }
175 
176  protected function getExamplesMessages() {
177  return [
178  'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
179  => 'apihelp-query+pageswithprop-example-simple',
180  'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
181  => 'apihelp-query+pageswithprop-example-generator',
182  ];
183  }
184 
185  public function getHelpUrls() {
186  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
187  }
188 }
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1650
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:196
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
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
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
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:41
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:110
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23