MediaWiki master
ApiQueryProtectedTitles.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
31
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
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',
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 ],
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
275class_alias( ApiQueryProtectedTitles::class, 'ApiQueryProtectedTitles' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:566
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:221
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:181
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:248
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:246
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Query module to enumerate all create-protected pages.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, string $moduleName, CommentStore $commentStore, RowCommentFormatter $commentFormatter)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
This is the main query class.
Definition ApiQuery.php:48
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
This is basically a CommentFormatter with a CommentStore dependency, allowing it to retrieve comment ...
Handle database storage of comments such as edit summaries and log reasons.
A class containing constants representing the names of configuration variables.
const RestrictionLevels
Name constant for the RestrictionLevels setting, for use with Config::get()
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.