Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryProtectedTitles.php
blobec8fba5f03eb1179a6060512a8ab81ee1fcc0049
1 <?php
2 /**
3 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
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
23 namespace MediaWiki\Api;
25 use MediaWiki\CommentFormatter\RowCommentFormatter;
26 use MediaWiki\CommentStore\CommentStore;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Title\Title;
29 use Wikimedia\ParamValidator\ParamValidator;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
32 /**
33 * Query module to enumerate all create-protected pages.
35 * @ingroup API
37 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
39 private CommentStore $commentStore;
40 private RowCommentFormatter $commentFormatter;
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;
53 public function execute() {
54 $this->run();
57 public function executeGenerator( $resultPageSet ) {
58 $this->run( $resultPageSet );
61 /**
62 * @param ApiPageSet|null $resultPageSet
63 * @return void
65 private function run( $resultPageSet = null ) {
66 $params = $this->extractRequestParams();
68 $this->addTables( 'protected_titles' );
69 $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
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'] ) );
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'] );
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'] );
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 );
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 ] ) );
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 ] ] );
110 $this->addOption( 'LIMIT', $params['limit'] + 1 );
111 $res = $this->select( __METHOD__ );
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' )
125 $count = 0;
126 $result = $this->getResult();
128 $titles = [];
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"
137 break;
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 );
148 if ( isset( $prop['user'] ) && $row->user_name !== null ) {
149 $vals['user'] = $row->user_name;
152 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
153 $vals['userid'] = (int)$row->pt_user;
156 if ( isset( $prop['comment'] ) ) {
157 $vals['comment'] = $this->commentStore->getComment( 'pt_reason', $row )->text;
160 if ( isset( $prop['parsedcomment'] ) ) {
161 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
162 $vals['parsedcomment'] = $formattedComments[$rowOffset];
165 if ( isset( $prop['expiry'] ) ) {
166 $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
169 if ( isset( $prop['level'] ) ) {
170 $vals['level'] = $row->pt_create_perm;
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"
178 break;
180 } else {
181 $titles[] = $title;
185 if ( $resultPageSet === null ) {
186 $result->addIndexedTagName(
187 [ 'query', $this->getModuleName() ],
188 $this->getModulePrefix()
190 } else {
191 $resultPageSet->populateFromTitles( $titles );
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';
204 public function getAllowedParams() {
205 return [
206 'namespace' => [
207 ParamValidator::PARAM_ISMULTI => true,
208 ParamValidator::PARAM_TYPE => 'namespace',
210 'level' => [
211 ParamValidator::PARAM_ISMULTI => true,
212 ParamValidator::PARAM_TYPE => array_diff(
213 $this->getConfig()->get( MainConfigNames::RestrictionLevels ), [ '' ] )
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
222 'dir' => [
223 ParamValidator::PARAM_DEFAULT => 'older',
224 ParamValidator::PARAM_TYPE => [
225 'newer',
226 'older'
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',
234 'start' => [
235 ParamValidator::PARAM_TYPE => 'timestamp'
237 'end' => [
238 ParamValidator::PARAM_TYPE => 'timestamp'
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'
252 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
254 'continue' => [
255 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
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',
269 public function getHelpUrls() {
270 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
274 /** @deprecated class alias since 1.43 */
275 class_alias( ApiQueryProtectedTitles::class, 'ApiQueryProtectedTitles' );