Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 152
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryProtectedTitles
0.00% covered (danger)
0.00%
0 / 151
0.00% covered (danger)
0.00%
0 / 8
1056
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
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
 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 / 83
0.00% covered (danger)
0.00%
0 / 1
552
 getCacheMode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 53
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 © 2009 Roan Kattouw <roan.kattouw@gmail.com>
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 */
22
23namespace MediaWiki\Api;
24
25use MediaWiki\CommentFormatter\RowCommentFormatter;
26use MediaWiki\CommentStore\CommentStore;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Title\Title;
29use Wikimedia\ParamValidator\ParamValidator;
30use Wikimedia\ParamValidator\TypeDef\IntegerDef;
31
32/**
33 * Query module to enumerate all create-protected pages.
34 *
35 * @ingroup API
36 */
37class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
38
39    private CommentStore $commentStore;
40    private RowCommentFormatter $commentFormatter;
41
42    public function __construct(
43        ApiQuery $query,
44        string $moduleName,
45        CommentStore $commentStore,
46        RowCommentFormatter $commentFormatter
47    ) {
48        parent::__construct( $query, $moduleName, 'pt' );
49        $this->commentStore = $commentStore;
50        $this->commentFormatter = $commentFormatter;
51    }
52
53    public function execute() {
54        $this->run();
55    }
56
57    public function executeGenerator( $resultPageSet ) {
58        $this->run( $resultPageSet );
59    }
60
61    /**
62     * @param ApiPageSet|null $resultPageSet
63     * @return void
64     */
65    private function run( $resultPageSet = null ) {
66        $params = $this->extractRequestParams();
67
68        $this->addTables( 'protected_titles' );
69        $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
70
71        $prop = array_fill_keys( $params['prop'], true );
72        $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
73        $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
74        $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
75
76        if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
77            $commentQuery = $this->commentStore->getJoin( 'pt_reason' );
78            $this->addTables( $commentQuery['tables'] );
79            $this->addFields( $commentQuery['fields'] );
80            $this->addJoinConds( $commentQuery['joins'] );
81        }
82
83        $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
84        $this->addWhereFld( 'pt_namespace', $params['namespace'] );
85        $this->addWhereFld( 'pt_create_perm', $params['level'] );
86
87        // Include in ORDER BY for uniqueness
88        $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
89        $this->addWhereRange( 'pt_title', $params['dir'], null, null );
90
91        if ( $params['continue'] !== null ) {
92            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int', 'string' ] );
93            $op = ( $params['dir'] === 'newer' ? '>=' : '<=' );
94            $db = $this->getDB();
95            $this->addWhere( $db->buildComparison( $op, [
96                'pt_timestamp' => $db->timestamp( $cont[0] ),
97                'pt_namespace' => $cont[1],
98                'pt_title' => $cont[2],
99            ] ) );
100        }
101
102        if ( isset( $prop['user'] ) ) {
103            $this->addTables( 'user' );
104            $this->addFields( 'user_name' );
105            $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
106                'user_id=pt_user'
107            ] ] );
108        }
109
110        $this->addOption( 'LIMIT', $params['limit'] + 1 );
111        $res = $this->select( __METHOD__ );
112
113        if ( $resultPageSet === null ) {
114            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pt' );
115            if ( isset( $prop['parsedcomment'] ) ) {
116                $formattedComments = $this->commentFormatter->formatItems(
117                    $this->commentFormatter->rows( $res )
118                        ->commentKey( 'pt_reason' )
119                        ->namespaceField( 'pt_namespace' )
120                        ->titleField( 'pt_title' )
121                );
122            }
123        }
124
125        $count = 0;
126        $result = $this->getResult();
127
128        $titles = [];
129
130        foreach ( $res as $rowOffset => $row ) {
131            if ( ++$count > $params['limit'] ) {
132                // We've reached the one extra which shows that there are
133                // additional pages to be had. Stop here...
134                $this->setContinueEnumParameter( 'continue',
135                    "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
136                );
137                break;
138            }
139
140            $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
141            if ( $resultPageSet === null ) {
142                $vals = [];
143                ApiQueryBase::addTitleInfo( $vals, $title );
144                if ( isset( $prop['timestamp'] ) ) {
145                    $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
146                }
147
148                if ( isset( $prop['user'] ) && $row->user_name !== null ) {
149                    $vals['user'] = $row->user_name;
150                }
151
152                if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
153                    $vals['userid'] = (int)$row->pt_user;
154                }
155
156                if ( isset( $prop['comment'] ) ) {
157                    $vals['comment'] = $this->commentStore->getComment( 'pt_reason', $row )->text;
158                }
159
160                if ( isset( $prop['parsedcomment'] ) ) {
161                    // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
162                    $vals['parsedcomment'] = $formattedComments[$rowOffset];
163                }
164
165                if ( isset( $prop['expiry'] ) ) {
166                    $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
167                }
168
169                if ( isset( $prop['level'] ) ) {
170                    $vals['level'] = $row->pt_create_perm;
171                }
172
173                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
174                if ( !$fit ) {
175                    $this->setContinueEnumParameter( 'continue',
176                        "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
177                    );
178                    break;
179                }
180            } else {
181                $titles[] = $title;
182            }
183        }
184
185        if ( $resultPageSet === null ) {
186            $result->addIndexedTagName(
187                [ 'query', $this->getModuleName() ],
188                $this->getModulePrefix()
189            );
190        } else {
191            $resultPageSet->populateFromTitles( $titles );
192        }
193    }
194
195    public function getCacheMode( $params ) {
196        if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) {
197            // MediaWiki\CommentFormatter\CommentFormatter::formatItems() calls wfMessage() among other things
198            return 'anon-public-user-private';
199        } else {
200            return 'public';
201        }
202    }
203
204    public function getAllowedParams() {
205        return [
206            'namespace' => [
207                ParamValidator::PARAM_ISMULTI => true,
208                ParamValidator::PARAM_TYPE => 'namespace',
209            ],
210            'level' => [
211                ParamValidator::PARAM_ISMULTI => true,
212                ParamValidator::PARAM_TYPE => array_diff(
213                    $this->getConfig()->get( MainConfigNames::RestrictionLevels ), [ '' ] )
214            ],
215            'limit' => [
216                ParamValidator::PARAM_DEFAULT => 10,
217                ParamValidator::PARAM_TYPE => 'limit',
218                IntegerDef::PARAM_MIN => 1,
219                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
220                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
221            ],
222            'dir' => [
223                ParamValidator::PARAM_DEFAULT => 'older',
224                ParamValidator::PARAM_TYPE => [
225                    'newer',
226                    'older'
227                ],
228                ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
229                ApiBase::PARAM_HELP_MSG_PER_VALUE => [
230                    'newer' => 'api-help-paramvalue-direction-newer',
231                    'older' => 'api-help-paramvalue-direction-older',
232                ],
233            ],
234            'start' => [
235                ParamValidator::PARAM_TYPE => 'timestamp'
236            ],
237            'end' => [
238                ParamValidator::PARAM_TYPE => 'timestamp'
239            ],
240            'prop' => [
241                ParamValidator::PARAM_ISMULTI => true,
242                ParamValidator::PARAM_DEFAULT => 'timestamp|level',
243                ParamValidator::PARAM_TYPE => [
244                    'timestamp',
245                    'user',
246                    'userid',
247                    'comment',
248                    'parsedcomment',
249                    'expiry',
250                    'level'
251                ],
252                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
253            ],
254            'continue' => [
255                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
256            ],
257        ];
258    }
259
260    protected function getExamplesMessages() {
261        return [
262            'action=query&list=protectedtitles'
263                => 'apihelp-query+protectedtitles-example-simple',
264            'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
265                => 'apihelp-query+protectedtitles-example-generator',
266        ];
267    }
268
269    public function getHelpUrls() {
270        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
271    }
272}
273
274/** @deprecated class alias since 1.43 */
275class_alias( ApiQueryProtectedTitles::class, 'ApiQueryProtectedTitles' );