MediaWiki master
ApiQueryProtectedTitles.php
Go to the documentation of this file.
1<?php
29
36
37 private CommentStore $commentStore;
38 private RowCommentFormatter $commentFormatter;
39
46 public function __construct(
47 ApiQuery $query,
48 $moduleName,
49 CommentStore $commentStore,
50 RowCommentFormatter $commentFormatter
51 ) {
52 parent::__construct( $query, $moduleName, 'pt' );
53 $this->commentStore = $commentStore;
54 $this->commentFormatter = $commentFormatter;
55 }
56
57 public function execute() {
58 $this->run();
59 }
60
61 public function executeGenerator( $resultPageSet ) {
62 $this->run( $resultPageSet );
63 }
64
69 private function run( $resultPageSet = null ) {
71
72 $this->addTables( 'protected_titles' );
73 $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
74
75 $prop = array_fill_keys( $params['prop'], true );
76 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
77 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
78 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
79
80 if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
81 $commentQuery = $this->commentStore->getJoin( 'pt_reason' );
82 $this->addTables( $commentQuery['tables'] );
83 $this->addFields( $commentQuery['fields'] );
84 $this->addJoinConds( $commentQuery['joins'] );
85 }
86
87 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
88 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
89 $this->addWhereFld( 'pt_create_perm', $params['level'] );
90
91 // Include in ORDER BY for uniqueness
92 $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
93 $this->addWhereRange( 'pt_title', $params['dir'], null, null );
94
95 if ( $params['continue'] !== null ) {
96 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int', 'string' ] );
97 $op = ( $params['dir'] === 'newer' ? '>=' : '<=' );
98 $db = $this->getDB();
99 $this->addWhere( $db->buildComparison( $op, [
100 'pt_timestamp' => $db->timestamp( $cont[0] ),
101 'pt_namespace' => $cont[1],
102 'pt_title' => $cont[2],
103 ] ) );
104 }
105
106 if ( isset( $prop['user'] ) ) {
107 $this->addTables( 'user' );
108 $this->addFields( 'user_name' );
109 $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
110 'user_id=pt_user'
111 ] ] );
112 }
113
114 $this->addOption( 'LIMIT', $params['limit'] + 1 );
115 $res = $this->select( __METHOD__ );
116
117 if ( $resultPageSet === null ) {
118 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pt' );
119 if ( isset( $prop['parsedcomment'] ) ) {
120 $formattedComments = $this->commentFormatter->formatItems(
121 $this->commentFormatter->rows( $res )
122 ->commentKey( 'pt_reason' )
123 ->namespaceField( 'pt_namespace' )
124 ->titleField( 'pt_title' )
125 );
126 }
127 }
128
129 $count = 0;
130 $result = $this->getResult();
131
132 $titles = [];
133
134 foreach ( $res as $rowOffset => $row ) {
135 if ( ++$count > $params['limit'] ) {
136 // We've reached the one extra which shows that there are
137 // additional pages to be had. Stop here...
138 $this->setContinueEnumParameter( 'continue',
139 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
140 );
141 break;
142 }
143
144 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
145 if ( $resultPageSet === null ) {
146 $vals = [];
147 ApiQueryBase::addTitleInfo( $vals, $title );
148 if ( isset( $prop['timestamp'] ) ) {
149 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
150 }
151
152 if ( isset( $prop['user'] ) && $row->user_name !== null ) {
153 $vals['user'] = $row->user_name;
154 }
155
156 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
157 $vals['userid'] = (int)$row->pt_user;
158 }
159
160 if ( isset( $prop['comment'] ) ) {
161 $vals['comment'] = $this->commentStore->getComment( 'pt_reason', $row )->text;
162 }
163
164 if ( isset( $prop['parsedcomment'] ) ) {
165 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
166 $vals['parsedcomment'] = $formattedComments[$rowOffset];
167 }
168
169 if ( isset( $prop['expiry'] ) ) {
170 $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
171 }
172
173 if ( isset( $prop['level'] ) ) {
174 $vals['level'] = $row->pt_create_perm;
175 }
176
177 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
178 if ( !$fit ) {
179 $this->setContinueEnumParameter( 'continue',
180 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
181 );
182 break;
183 }
184 } else {
185 $titles[] = $title;
186 }
187 }
188
189 if ( $resultPageSet === null ) {
190 $result->addIndexedTagName(
191 [ 'query', $this->getModuleName() ],
192 $this->getModulePrefix()
193 );
194 } else {
195 $resultPageSet->populateFromTitles( $titles );
196 }
197 }
198
199 public function getCacheMode( $params ) {
200 if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) {
201 // MediaWiki\CommentFormatter\CommentFormatter::formatItems() calls wfMessage() among other things
202 return 'anon-public-user-private';
203 } else {
204 return 'public';
205 }
206 }
207
208 public function getAllowedParams() {
209 return [
210 'namespace' => [
211 ParamValidator::PARAM_ISMULTI => true,
212 ParamValidator::PARAM_TYPE => 'namespace',
213 ],
214 'level' => [
215 ParamValidator::PARAM_ISMULTI => true,
216 ParamValidator::PARAM_TYPE => array_diff(
217 $this->getConfig()->get( MainConfigNames::RestrictionLevels ), [ '' ] )
218 ],
219 'limit' => [
220 ParamValidator::PARAM_DEFAULT => 10,
221 ParamValidator::PARAM_TYPE => 'limit',
222 IntegerDef::PARAM_MIN => 1,
223 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
224 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
225 ],
226 'dir' => [
227 ParamValidator::PARAM_DEFAULT => 'older',
228 ParamValidator::PARAM_TYPE => [
229 'newer',
230 'older'
231 ],
232 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
234 'newer' => 'api-help-paramvalue-direction-newer',
235 'older' => 'api-help-paramvalue-direction-older',
236 ],
237 ],
238 'start' => [
239 ParamValidator::PARAM_TYPE => 'timestamp'
240 ],
241 'end' => [
242 ParamValidator::PARAM_TYPE => 'timestamp'
243 ],
244 'prop' => [
245 ParamValidator::PARAM_ISMULTI => true,
246 ParamValidator::PARAM_DEFAULT => 'timestamp|level',
247 ParamValidator::PARAM_TYPE => [
248 'timestamp',
249 'user',
250 'userid',
251 'comment',
252 'parsedcomment',
253 'expiry',
254 'level'
255 ],
257 ],
258 'continue' => [
259 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
260 ],
261 ];
262 }
263
264 protected function getExamplesMessages() {
265 return [
266 'action=query&list=protectedtitles'
267 => 'apihelp-query+protectedtitles-example-simple',
268 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
269 => 'apihelp-query+protectedtitles-example-generator',
270 ];
271 }
272
273 public function getHelpUrls() {
274 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
275 }
276}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
run()
Run the job.
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:550
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1734
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:211
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:236
getResult()
Get the result object.
Definition ApiBase.php:680
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:820
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:238
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:541
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result 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.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Query module to enumerate all create-protected pages.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
__construct(ApiQuery $query, $moduleName, CommentStore $commentStore, RowCommentFormatter $commentFormatter)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
This is the main query class.
Definition ApiQuery.php:43
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.
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.