MediaWiki master
ApiQueryProtectedTitles.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
25
26 private CommentStore $commentStore;
27 private RowCommentFormatter $commentFormatter;
28
29 public function __construct(
30 ApiQuery $query,
31 string $moduleName,
32 CommentStore $commentStore,
33 RowCommentFormatter $commentFormatter
34 ) {
35 parent::__construct( $query, $moduleName, 'pt' );
36 $this->commentStore = $commentStore;
37 $this->commentFormatter = $commentFormatter;
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
47 }
48
53 private function run( $resultPageSet = null ) {
54 $params = $this->extractRequestParams();
55
56 $this->addTables( 'protected_titles' );
57 $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
58
59 $prop = array_fill_keys( $params['prop'], true );
60 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
61 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
62 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
63
64 if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
65 $commentQuery = $this->commentStore->getJoin( 'pt_reason' );
66 $this->addTables( $commentQuery['tables'] );
67 $this->addFields( $commentQuery['fields'] );
68 $this->addJoinConds( $commentQuery['joins'] );
69 }
70
71 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
72 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
73 $this->addWhereFld( 'pt_create_perm', $params['level'] );
74
75 // Include in ORDER BY for uniqueness
76 $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
77 $this->addWhereRange( 'pt_title', $params['dir'], null, null );
78
79 if ( $params['continue'] !== null ) {
80 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int', 'string' ] );
81 $op = ( $params['dir'] === 'newer' ? '>=' : '<=' );
82 $db = $this->getDB();
83 $this->addWhere( $db->buildComparison( $op, [
84 'pt_timestamp' => $db->timestamp( $cont[0] ),
85 'pt_namespace' => $cont[1],
86 'pt_title' => $cont[2],
87 ] ) );
88 }
89
90 if ( isset( $prop['user'] ) ) {
91 $this->addTables( 'user' );
92 $this->addFields( 'user_name' );
93 $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
94 'user_id=pt_user'
95 ] ] );
96 }
97
98 $this->addOption( 'LIMIT', $params['limit'] + 1 );
99 $res = $this->select( __METHOD__ );
100
101 if ( $resultPageSet === null ) {
102 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pt' );
103 if ( isset( $prop['parsedcomment'] ) ) {
104 $formattedComments = $this->commentFormatter->formatItems(
105 $this->commentFormatter->rows( $res )
106 ->commentKey( 'pt_reason' )
107 ->namespaceField( 'pt_namespace' )
108 ->titleField( 'pt_title' )
109 );
110 }
111 }
112
113 $count = 0;
114 $result = $this->getResult();
115
116 $titles = [];
117
118 foreach ( $res as $rowOffset => $row ) {
119 if ( ++$count > $params['limit'] ) {
120 // We've reached the one extra which shows that there are
121 // additional pages to be had. Stop here...
122 $this->setContinueEnumParameter( 'continue',
123 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
124 );
125 break;
126 }
127
128 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
129 if ( $resultPageSet === null ) {
130 $vals = [];
131 ApiQueryBase::addTitleInfo( $vals, $title );
132 if ( isset( $prop['timestamp'] ) ) {
133 $vals['timestamp'] = wfTimestamp( TS::ISO_8601, $row->pt_timestamp );
134 }
135
136 if ( isset( $prop['user'] ) && $row->user_name !== null ) {
137 $vals['user'] = $row->user_name;
138 }
139
140 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
141 $vals['userid'] = (int)$row->pt_user;
142 }
143
144 if ( isset( $prop['comment'] ) ) {
145 $vals['comment'] = $this->commentStore->getComment( 'pt_reason', $row )->text;
146 }
147
148 if ( isset( $prop['parsedcomment'] ) ) {
149 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
150 $vals['parsedcomment'] = $formattedComments[$rowOffset];
151 }
152
153 if ( isset( $prop['expiry'] ) ) {
154 $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
155 }
156
157 if ( isset( $prop['level'] ) ) {
158 $vals['level'] = $row->pt_create_perm;
159 }
160
161 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
162 if ( !$fit ) {
163 $this->setContinueEnumParameter( 'continue',
164 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
165 );
166 break;
167 }
168 } else {
169 $titles[] = $title;
170 }
171 }
172
173 if ( $resultPageSet === null ) {
174 $result->addIndexedTagName(
175 [ 'query', $this->getModuleName() ],
176 $this->getModulePrefix()
177 );
178 } else {
179 $resultPageSet->populateFromTitles( $titles );
180 }
181 }
182
184 public function getCacheMode( $params ) {
185 if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) {
186 // MediaWiki\CommentFormatter\CommentFormatter::formatItems() calls wfMessage() among other things
187 return 'anon-public-user-private';
188 } else {
189 return 'public';
190 }
191 }
192
194 public function getAllowedParams() {
195 return [
196 'namespace' => [
197 ParamValidator::PARAM_ISMULTI => true,
198 ParamValidator::PARAM_TYPE => 'namespace',
199 ],
200 'level' => [
201 ParamValidator::PARAM_ISMULTI => true,
202 ParamValidator::PARAM_TYPE => array_diff(
203 $this->getConfig()->get( MainConfigNames::RestrictionLevels ), [ '' ] )
204 ],
205 'limit' => [
206 ParamValidator::PARAM_DEFAULT => 10,
207 ParamValidator::PARAM_TYPE => 'limit',
208 IntegerDef::PARAM_MIN => 1,
209 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
210 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
211 ],
212 'dir' => [
213 ParamValidator::PARAM_DEFAULT => 'older',
214 ParamValidator::PARAM_TYPE => [
215 'newer',
216 'older'
217 ],
218 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
220 'newer' => 'api-help-paramvalue-direction-newer',
221 'older' => 'api-help-paramvalue-direction-older',
222 ],
223 ],
224 'start' => [
225 ParamValidator::PARAM_TYPE => 'timestamp'
226 ],
227 'end' => [
228 ParamValidator::PARAM_TYPE => 'timestamp'
229 ],
230 'prop' => [
231 ParamValidator::PARAM_ISMULTI => true,
232 ParamValidator::PARAM_DEFAULT => 'timestamp|level',
233 ParamValidator::PARAM_TYPE => [
234 'timestamp',
235 'user',
236 'userid',
237 'comment',
238 'parsedcomment',
239 'expiry',
240 'level'
241 ],
243 ],
244 'continue' => [
245 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
246 ],
247 ];
248 }
249
251 protected function getExamplesMessages() {
252 return [
253 'action=query&list=protectedtitles'
254 => 'apihelp-query+protectedtitles-example-simple',
255 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
256 => 'apihelp-query+protectedtitles-example-generator',
257 ];
258 }
259
261 public function getHelpUrls() {
262 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
263 }
264}
265
267class_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:552
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1696
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:207
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:167
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:234
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:232
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.Override this in the module subclass....
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
executeGenerator( $resultPageSet)
Execute this module as a generator.
This is the main query class.
Definition ApiQuery.php:36
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.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
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:69
Service for formatting and validating API parameters.
Type definition for integer types.