3 * Copyright © 2012 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Api
;
26 use MediaWiki\Title\Title
;
27 use Wikimedia\ParamValidator\ParamValidator
;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
31 * A query module to enumerate pages that use a particular prop
36 class ApiQueryPagesWithProp
extends ApiQueryGeneratorBase
{
38 public function __construct( ApiQuery
$query, string $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'pwp' );
42 public function execute() {
46 public function getCacheMode( $params ) {
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
55 * @param ApiPageSet|null $resultPageSet
58 private function run( $resultPageSet = null ) {
59 $params = $this->extractRequestParams();
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'] );
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 );
71 $this->addFields( $resultPageSet->getPageTableFields() );
73 $this->addTables( [ 'page_props', 'page' ] );
74 $this->addWhere( 'pp_page=page_id' );
75 $this->addWhereFld( 'pp_propname', $params['propname'] );
77 $dir = ( $params['dir'] == 'ascending' ) ?
'newer' : 'older';
79 if ( $params['continue'] ) {
80 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
82 $this->addWhereRange( 'pp_page', $dir, $cont[0], null );
85 $sort = ( $params['dir'] === 'descending' ?
' DESC' : '' );
86 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
88 $limit = $params['limit'];
89 $this->addOption( 'LIMIT', $limit +
1 );
91 $result = $this->getResult();
93 $res = $this->select( __METHOD__
);
95 if ( $fld_title && $resultPageSet === null ) {
96 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
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
);
107 if ( $resultPageSet === null ) {
109 ApiResult
::META_TYPE
=> 'assoc',
112 $vals['pageid'] = (int)$row->page_id
;
115 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
116 ApiQueryBase
::addTitleInfo( $vals, $title );
119 $vals['value'] = $row->pp_value
;
121 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
123 $this->setContinueEnumParameter( 'continue', $row->page_id
);
127 $resultPageSet->processDbRow( $row );
131 if ( $resultPageSet === null ) {
132 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
136 public function getAllowedParams() {
139 ParamValidator
::PARAM_TYPE
=> 'string',
140 ParamValidator
::PARAM_REQUIRED
=> true,
143 ParamValidator
::PARAM_DEFAULT
=> 'ids|title',
144 ParamValidator
::PARAM_ISMULTI
=> true,
145 ParamValidator
::PARAM_TYPE
=> [
150 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
153 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
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
163 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
164 ParamValidator
::PARAM_TYPE
=> [
172 protected function getExamplesMessages() {
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',
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
186 /** @deprecated class alias since 1.43 */
187 class_alias( ApiQueryPagesWithProp
::class, 'ApiQueryPagesWithProp' );