Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
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
24use MediaWiki\Title\Title;
25use Wikimedia\ParamValidator\ParamValidator;
26use Wikimedia\ParamValidator\TypeDef\IntegerDef;
27
28/**
29 * A query module to enumerate pages that use a particular prop
30 *
31 * @ingroup API
32 * @since 1.21
33 */
34class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
35
36    /**
37     * @param ApiQuery $query
38     * @param string $moduleName
39     */
40    public function __construct( ApiQuery $query, $moduleName ) {
41        parent::__construct( $query, $moduleName, 'pwp' );
42    }
43
44    public function execute() {
45        $this->run();
46    }
47
48    public function getCacheMode( $params ) {
49        return 'public';
50    }
51
52    public function executeGenerator( $resultPageSet ) {
53        $this->run( $resultPageSet );
54    }
55
56    /**
57     * @param ApiPageSet|null $resultPageSet
58     * @return void
59     */
60    private function run( $resultPageSet = null ) {
61        $params = $this->extractRequestParams();
62
63        $prop = array_fill_keys( $params['prop'], true );
64        $fld_ids = isset( $prop['ids'] );
65        $fld_title = isset( $prop['title'] );
66        $fld_value = isset( $prop['value'] );
67
68        if ( $resultPageSet === null ) {
69            $this->addFields( [ 'page_id' ] );
70            $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
71            $this->addFieldsIf( 'pp_value', $fld_value );
72        } else {
73            $this->addFields( $resultPageSet->getPageTableFields() );
74        }
75        $this->addTables( [ 'page_props', 'page' ] );
76        $this->addWhere( 'pp_page=page_id' );
77        $this->addWhereFld( 'pp_propname', $params['propname'] );
78
79        $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
80
81        if ( $params['continue'] ) {
82            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
83            // Add a WHERE clause
84            $this->addWhereRange( 'pp_page', $dir, $cont[0], null );
85        }
86
87        $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
88        $this->addOption( 'ORDER BY', 'pp_page' . $sort );
89
90        $limit = $params['limit'];
91        $this->addOption( 'LIMIT', $limit + 1 );
92
93        $result = $this->getResult();
94        $count = 0;
95        $res = $this->select( __METHOD__ );
96
97        if ( $fld_title && $resultPageSet === null ) {
98            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
99        }
100
101        foreach ( $res as $row ) {
102            if ( ++$count > $limit ) {
103                // We've reached the one extra which shows that there are
104                // additional pages to be had. Stop here...
105                $this->setContinueEnumParameter( 'continue', $row->page_id );
106                break;
107            }
108
109            if ( $resultPageSet === null ) {
110                $vals = [
111                    ApiResult::META_TYPE => 'assoc',
112                ];
113                if ( $fld_ids ) {
114                    $vals['pageid'] = (int)$row->page_id;
115                }
116                if ( $fld_title ) {
117                    $title = Title::makeTitle( $row->page_namespace, $row->page_title );
118                    ApiQueryBase::addTitleInfo( $vals, $title );
119                }
120                if ( $fld_value ) {
121                    $vals['value'] = $row->pp_value;
122                }
123                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
124                if ( !$fit ) {
125                    $this->setContinueEnumParameter( 'continue', $row->page_id );
126                    break;
127                }
128            } else {
129                $resultPageSet->processDbRow( $row );
130            }
131        }
132
133        if ( $resultPageSet === null ) {
134            $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
135        }
136    }
137
138    public function getAllowedParams() {
139        return [
140            'propname' => [
141                ParamValidator::PARAM_TYPE => 'string',
142                ParamValidator::PARAM_REQUIRED => true,
143            ],
144            'prop' => [
145                ParamValidator::PARAM_DEFAULT => 'ids|title',
146                ParamValidator::PARAM_ISMULTI => true,
147                ParamValidator::PARAM_TYPE => [
148                    'ids',
149                    'title',
150                    'value',
151                ],
152                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
153            ],
154            'continue' => [
155                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
156            ],
157            'limit' => [
158                ParamValidator::PARAM_TYPE => 'limit',
159                ParamValidator::PARAM_DEFAULT => 10,
160                IntegerDef::PARAM_MIN => 1,
161                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
162                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
163            ],
164            'dir' => [
165                ParamValidator::PARAM_DEFAULT => 'ascending',
166                ParamValidator::PARAM_TYPE => [
167                    'ascending',
168                    'descending',
169                ]
170            ],
171        ];
172    }
173
174    protected function getExamplesMessages() {
175        return [
176            'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
177                => 'apihelp-query+pageswithprop-example-simple',
178            'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
179                => 'apihelp-query+pageswithprop-example-generator',
180        ];
181    }
182
183    public function getHelpUrls() {
184        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
185    }
186}