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