Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryPagesWithProp.php
blobd75866509b0cf2234ea4572248fb7f79475f87da
1 <?php
2 /**
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
20 * @file
21 * @since 1.21
24 namespace MediaWiki\Api;
26 use MediaWiki\Title\Title;
27 use Wikimedia\ParamValidator\ParamValidator;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30 /**
31 * A query module to enumerate pages that use a particular prop
33 * @ingroup API
34 * @since 1.21
36 class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
38 public function __construct( ApiQuery $query, string $moduleName ) {
39 parent::__construct( $query, $moduleName, 'pwp' );
42 public function execute() {
43 $this->run();
46 public function getCacheMode( $params ) {
47 return 'public';
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
54 /**
55 * @param ApiPageSet|null $resultPageSet
56 * @return void
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 );
70 } else {
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' ] );
81 // Add a WHERE clause
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();
92 $count = 0;
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 );
104 break;
107 if ( $resultPageSet === null ) {
108 $vals = [
109 ApiResult::META_TYPE => 'assoc',
111 if ( $fld_ids ) {
112 $vals['pageid'] = (int)$row->page_id;
114 if ( $fld_title ) {
115 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116 ApiQueryBase::addTitleInfo( $vals, $title );
118 if ( $fld_value ) {
119 $vals['value'] = $row->pp_value;
121 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'continue', $row->page_id );
124 break;
126 } else {
127 $resultPageSet->processDbRow( $row );
131 if ( $resultPageSet === null ) {
132 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
136 public function getAllowedParams() {
137 return [
138 'propname' => [
139 ParamValidator::PARAM_TYPE => 'string',
140 ParamValidator::PARAM_REQUIRED => true,
142 'prop' => [
143 ParamValidator::PARAM_DEFAULT => 'ids|title',
144 ParamValidator::PARAM_ISMULTI => true,
145 ParamValidator::PARAM_TYPE => [
146 'ids',
147 'title',
148 'value',
150 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
152 'continue' => [
153 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
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
162 'dir' => [
163 ParamValidator::PARAM_DEFAULT => 'ascending',
164 ParamValidator::PARAM_TYPE => [
165 'ascending',
166 'descending',
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',
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' );