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