Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryPagesWithProp
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 8
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
240
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2012 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @since 1.21
22 */
23
24namespace MediaWiki\Api;
25
26use MediaWiki\Title\Title;
27use Wikimedia\ParamValidator\ParamValidator;
28use Wikimedia\ParamValidator\TypeDef\IntegerDef;
29
30/**
31 * A query module to enumerate pages that use a particular prop
32 *
33 * @ingroup API
34 * @since 1.21
35 */
36class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
37
38    public function __construct( ApiQuery $query, string $moduleName ) {
39        parent::__construct( $query, $moduleName, 'pwp' );
40    }
41
42    public function execute() {
43        $this->run();
44    }
45
46    public function getCacheMode( $params ) {
47        return 'public';
48    }
49
50    public function executeGenerator( $resultPageSet ) {
51        $this->run( $resultPageSet );
52    }
53
54    /**
55     * @param ApiPageSet|null $resultPageSet
56     * @return void
57     */
58    private function run( $resultPageSet = null ) {
59        $params = $this->extractRequestParams();
60
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'] );
65
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() );
72        }
73        $this->addTables( [ 'page_props', 'page' ] );
74        $this->addWhere( 'pp_page=page_id' );
75        $this->addWhereFld( 'pp_propname', $params['propname'] );
76
77        $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
78
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 );
83        }
84
85        $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
86        $this->addOption( 'ORDER BY', 'pp_page' . $sort );
87
88        $limit = $params['limit'];
89        $this->addOption( 'LIMIT', $limit + 1 );
90
91        $result = $this->getResult();
92        $count = 0;
93        $res = $this->select( __METHOD__ );
94
95        if ( $fld_title && $resultPageSet === null ) {
96            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
97        }
98
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;
105            }
106
107            if ( $resultPageSet === null ) {
108                $vals = [
109                    ApiResult::META_TYPE => 'assoc',
110                ];
111                if ( $fld_ids ) {
112                    $vals['pageid'] = (int)$row->page_id;
113                }
114                if ( $fld_title ) {
115                    $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116                    ApiQueryBase::addTitleInfo( $vals, $title );
117                }
118                if ( $fld_value ) {
119                    $vals['value'] = $row->pp_value;
120                }
121                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
122                if ( !$fit ) {
123                    $this->setContinueEnumParameter( 'continue', $row->page_id );
124                    break;
125                }
126            } else {
127                $resultPageSet->processDbRow( $row );
128            }
129        }
130
131        if ( $resultPageSet === null ) {
132            $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
133        }
134    }
135
136    public function getAllowedParams() {
137        return [
138            'propname' => [
139                ParamValidator::PARAM_TYPE => 'string',
140                ParamValidator::PARAM_REQUIRED => true,
141            ],
142            'prop' => [
143                ParamValidator::PARAM_DEFAULT => 'ids|title',
144                ParamValidator::PARAM_ISMULTI => true,
145                ParamValidator::PARAM_TYPE => [
146                    'ids',
147                    'title',
148                    'value',
149                ],
150                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
151            ],
152            'continue' => [
153                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
154            ],
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
161            ],
162            'dir' => [
163                ParamValidator::PARAM_DEFAULT => 'ascending',
164                ParamValidator::PARAM_TYPE => [
165                    'ascending',
166                    'descending',
167                ]
168            ],
169        ];
170    }
171
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',
178        ];
179    }
180
181    public function getHelpUrls() {
182        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
183    }
184}
185
186/** @deprecated class alias since 1.43 */
187class_alias( ApiQueryPagesWithProp::class, 'ApiQueryPagesWithProp' );