MediaWiki  master
ApiQueryProtectedTitles.php
Go to the documentation of this file.
1 <?php
29 
34  public function __construct( ApiQuery $query, $moduleName ) {
35  parent::__construct( $query, $moduleName, 'pt' );
36  }
37 
38  public function execute() {
39  $this->run();
40  }
41 
42  public function executeGenerator( $resultPageSet ) {
43  $this->run( $resultPageSet );
44  }
45 
50  private function run( $resultPageSet = null ) {
51  $params = $this->extractRequestParams();
52 
53  $this->addTables( 'protected_titles' );
54  $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
55 
56  $prop = array_flip( $params['prop'] );
57  $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
58  $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
59  $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
60 
61  if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
62  $commentStore = CommentStore::getStore();
63  $commentQuery = $commentStore->getJoin( 'pt_reason' );
64  $this->addTables( $commentQuery['tables'] );
65  $this->addFields( $commentQuery['fields'] );
66  $this->addJoinConds( $commentQuery['joins'] );
67  }
68 
69  $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
70  $this->addWhereFld( 'pt_namespace', $params['namespace'] );
71  $this->addWhereFld( 'pt_create_perm', $params['level'] );
72 
73  // Include in ORDER BY for uniqueness
74  $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
75  $this->addWhereRange( 'pt_title', $params['dir'], null, null );
76 
77  if ( $params['continue'] !== null ) {
78  $cont = explode( '|', $params['continue'] );
79  $this->dieContinueUsageIf( count( $cont ) != 3 );
80  $op = ( $params['dir'] === 'newer' ? '>' : '<' );
81  $db = $this->getDB();
82  $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
83  $continueNs = (int)$cont[1];
84  $this->dieContinueUsageIf( $continueNs != $cont[1] );
85  $continueTitle = $db->addQuotes( $cont[2] );
86  $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
87  "(pt_timestamp = $continueTimestamp AND " .
88  "(pt_namespace $op $continueNs OR " .
89  "(pt_namespace = $continueNs AND " .
90  "pt_title $op= $continueTitle)))"
91  );
92  }
93 
94  if ( isset( $prop['user'] ) ) {
95  $this->addTables( 'user' );
96  $this->addFields( 'user_name' );
97  $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
98  'user_id=pt_user'
99  ] ] );
100  }
101 
102  $this->addOption( 'LIMIT', $params['limit'] + 1 );
103  $res = $this->select( __METHOD__ );
104 
105  if ( $resultPageSet === null ) {
106  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pt' );
107  }
108 
109  $count = 0;
110  $result = $this->getResult();
111 
112  $titles = [];
113 
114  foreach ( $res as $row ) {
115  if ( ++$count > $params['limit'] ) {
116  // We've reached the one extra which shows that there are
117  // additional pages to be had. Stop here...
118  $this->setContinueEnumParameter( 'continue',
119  "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
120  );
121  break;
122  }
123 
124  $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
125  if ( $resultPageSet === null ) {
126  $vals = [];
128  if ( isset( $prop['timestamp'] ) ) {
129  $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
130  }
131 
132  if ( isset( $prop['user'] ) && $row->user_name !== null ) {
133  $vals['user'] = $row->user_name;
134  }
135 
136  if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
137  $vals['userid'] = (int)$row->pt_user;
138  }
139 
140  if ( isset( $prop['comment'] ) ) {
141  $vals['comment'] = $commentStore->getComment( 'pt_reason', $row )->text;
142  }
143 
144  if ( isset( $prop['parsedcomment'] ) ) {
145  $vals['parsedcomment'] = Linker::formatComment(
146  $commentStore->getComment( 'pt_reason', $row )->text
147  );
148  }
149 
150  if ( isset( $prop['expiry'] ) ) {
151  $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
152  }
153 
154  if ( isset( $prop['level'] ) ) {
155  $vals['level'] = $row->pt_create_perm;
156  }
157 
158  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
159  if ( !$fit ) {
160  $this->setContinueEnumParameter( 'continue',
161  "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
162  );
163  break;
164  }
165  } else {
166  $titles[] = $title;
167  }
168  }
169 
170  if ( $resultPageSet === null ) {
171  $result->addIndexedTagName(
172  [ 'query', $this->getModuleName() ],
173  $this->getModulePrefix()
174  );
175  } else {
176  $resultPageSet->populateFromTitles( $titles );
177  }
178  }
179 
180  public function getCacheMode( $params ) {
181  if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) {
182  // formatComment() calls wfMessage() among other things
183  return 'anon-public-user-private';
184  } else {
185  return 'public';
186  }
187  }
188 
189  public function getAllowedParams() {
190  return [
191  'namespace' => [
192  ApiBase::PARAM_ISMULTI => true,
193  ApiBase::PARAM_TYPE => 'namespace',
194  ],
195  'level' => [
196  ApiBase::PARAM_ISMULTI => true,
197  ApiBase::PARAM_TYPE => array_diff( $this->getConfig()->get( 'RestrictionLevels' ), [ '' ] )
198  ],
199  'limit' => [
200  ApiBase::PARAM_DFLT => 10,
201  ApiBase::PARAM_TYPE => 'limit',
202  ApiBase::PARAM_MIN => 1,
205  ],
206  'dir' => [
207  ApiBase::PARAM_DFLT => 'older',
209  'newer',
210  'older'
211  ],
212  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
213  ],
214  'start' => [
215  ApiBase::PARAM_TYPE => 'timestamp'
216  ],
217  'end' => [
218  ApiBase::PARAM_TYPE => 'timestamp'
219  ],
220  'prop' => [
221  ApiBase::PARAM_ISMULTI => true,
222  ApiBase::PARAM_DFLT => 'timestamp|level',
224  'timestamp',
225  'user',
226  'userid',
227  'comment',
228  'parsedcomment',
229  'expiry',
230  'level'
231  ],
233  ],
234  'continue' => [
235  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
236  ],
237  ];
238  }
239 
240  protected function getExamplesMessages() {
241  return [
242  'action=query&list=protectedtitles'
243  => 'apihelp-query+protectedtitles-example-simple',
244  'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
245  => 'apihelp-query+protectedtitles-example-generator',
246  ];
247  }
248 
249  public function getHelpUrls() {
250  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
251  }
252 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:71
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiQueryProtectedTitles\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryProtectedTitles.php:249
ApiQueryProtectedTitles\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryProtectedTitles.php:189
ApiQueryBase\addTimestampWhereRange
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
Definition: ApiQueryBase.php:366
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:102
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1831
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:69
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:558
ApiQueryProtectedTitles\run
run( $resultPageSet=null)
Definition: ApiQueryProtectedTitles.php:50
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
ApiQueryProtectedTitles
Query module to enumerate all create-protected pages.
Definition: ApiQueryProtectedTitles.php:28
$res
$res
Definition: testCompression.php:57
ApiQueryBase\addFieldsIf
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
Definition: ApiQueryBase.php:222
ApiQueryProtectedTitles\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryProtectedTitles.php:240
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:72
ApiQueryBase\executeGenderCacheFromResultWrapper
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
Definition: ApiQueryBase.php:626
ApiQueryProtectedTitles\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryProtectedTitles.php:34
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:160
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only) Stable to override.
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:70
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:706
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
ApiQueryProtectedTitles\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryProtectedTitles.php:38
ApiQueryProtectedTitles\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryProtectedTitles.php:180
ApiBase\getModulePrefix
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:445
ApiQueryBase\addWhereRange
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.
Definition: ApiQueryBase.php:337
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1550
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:201
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
ApiQueryProtectedTitles\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryProtectedTitles.php:42
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1198
ApiQueryGeneratorBase
Stable to extend.
Definition: ApiQueryGeneratorBase.php:28
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:162
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:67
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:437
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:68
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:71
ApiResult\formatExpiry
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
Definition: ApiResult.php:1189
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:135
CommentStore\getStore
static getStore()
Definition: CommentStore.php:120
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466