Merge "Document SearchDatabase::doSearchTextInDB to return null"
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Storage\NameTableAccessException;
25 use MediaWiki\Storage\RevisionRecord;
26
27 /**
28 * Query module to enumerate all deleted revisions.
29 *
30 * @ingroup API
31 * @deprecated since 1.25
32 */
33 class ApiQueryDeletedrevs extends ApiQueryBase {
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'dr' );
37 }
38
39 public function execute() {
40 // Before doing anything at all, let's check permissions
41 $this->checkUserRightsAny( 'deletedhistory' );
42
43 $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
44
45 $user = $this->getUser();
46 $db = $this->getDB();
47 $commentStore = CommentStore::getStore();
48 $params = $this->extractRequestParams( false );
49 $prop = array_flip( $params['prop'] );
50 $fld_parentid = isset( $prop['parentid'] );
51 $fld_revid = isset( $prop['revid'] );
52 $fld_user = isset( $prop['user'] );
53 $fld_userid = isset( $prop['userid'] );
54 $fld_comment = isset( $prop['comment'] );
55 $fld_parsedcomment = isset( $prop['parsedcomment'] );
56 $fld_minor = isset( $prop['minor'] );
57 $fld_len = isset( $prop['len'] );
58 $fld_sha1 = isset( $prop['sha1'] );
59 $fld_content = isset( $prop['content'] );
60 $fld_token = isset( $prop['token'] );
61 $fld_tags = isset( $prop['tags'] );
62
63 // If we're in a mode that breaks the same-origin policy, no tokens can
64 // be obtained
65 if ( $this->lacksSameOriginSecurity() ) {
66 $fld_token = false;
67 }
68
69 // If user can't undelete, no tokens
70 if ( !$user->isAllowed( 'undelete' ) ) {
71 $fld_token = false;
72 }
73
74 $result = $this->getResult();
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getTitles();
77
78 // This module operates in three modes:
79 // 'revs': List deleted revs for certain titles (1)
80 // 'user': List deleted revs by a certain user (2)
81 // 'all': List all deleted revs in NS (3)
82 $mode = 'all';
83 if ( count( $titles ) > 0 ) {
84 $mode = 'revs';
85 } elseif ( !is_null( $params['user'] ) ) {
86 $mode = 'user';
87 }
88
89 if ( $mode == 'revs' || $mode == 'user' ) {
90 // Ignore namespace and unique due to inability to know whether they were purposely set
91 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
92 if ( !is_null( $params[$p] ) ) {
93 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
94 }
95 }
96 } else {
97 foreach ( [ 'start', 'end' ] as $p ) {
98 if ( !is_null( $params[$p] ) ) {
99 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
100 }
101 }
102 }
103
104 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
105 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
106 }
107
108 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
109 $arQuery = $revisionStore->getArchiveQueryInfo();
110 $this->addTables( $arQuery['tables'] );
111 $this->addFields( $arQuery['fields'] );
112 $this->addJoinConds( $arQuery['joins'] );
113 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
114
115 if ( $fld_tags ) {
116 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
117 }
118
119 if ( !is_null( $params['tag'] ) ) {
120 $this->addTables( 'change_tag' );
121 $this->addJoinConds(
122 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
123 );
124 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
125 try {
126 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
127 } catch ( NameTableAccessException $exception ) {
128 // Return nothing.
129 $this->addWhere( '1=0' );
130 }
131 }
132
133 // This means stricter restrictions
134 if ( $fld_content ) {
135 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
136 }
137 // Check limits
138 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
139 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
140
141 $limit = $params['limit'];
142
143 if ( $limit == 'max' ) {
144 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
145 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
146 }
147
148 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
149
150 if ( $fld_token ) {
151 // Undelete tokens are identical for all pages, so we cache one here
152 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
153 }
154
155 $dir = $params['dir'];
156
157 // We need a custom WHERE clause that matches all titles.
158 if ( $mode == 'revs' ) {
159 $lb = new LinkBatch( $titles );
160 $where = $lb->constructSet( 'ar', $db );
161 $this->addWhere( $where );
162 } elseif ( $mode == 'all' ) {
163 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
164
165 $from = $params['from'] === null
166 ? null
167 : $this->titlePartToKey( $params['from'], $params['namespace'] );
168 $to = $params['to'] === null
169 ? null
170 : $this->titlePartToKey( $params['to'], $params['namespace'] );
171 $this->addWhereRange( 'ar_title', $dir, $from, $to );
172
173 if ( isset( $params['prefix'] ) ) {
174 $this->addWhere( 'ar_title' . $db->buildLike(
175 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
176 $db->anyString() ) );
177 }
178 }
179
180 if ( !is_null( $params['user'] ) ) {
181 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
182 $actorQuery = ActorMigration::newMigration()
183 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
184 $this->addTables( $actorQuery['tables'] );
185 $this->addJoinConds( $actorQuery['joins'] );
186 $this->addWhere( $actorQuery['conds'] );
187 } elseif ( !is_null( $params['excludeuser'] ) ) {
188 // Here there's no chance of using ar_usertext_timestamp.
189 $actorQuery = ActorMigration::newMigration()
190 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
191 $this->addTables( $actorQuery['tables'] );
192 $this->addJoinConds( $actorQuery['joins'] );
193 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
194 }
195
196 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
197 // Paranoia: avoid brute force searches (T19342)
198 // (shouldn't be able to get here without 'deletedhistory', but
199 // check it again just in case)
200 if ( !$user->isAllowed( 'deletedhistory' ) ) {
201 $bitmask = RevisionRecord::DELETED_USER;
202 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
203 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
204 } else {
205 $bitmask = 0;
206 }
207 if ( $bitmask ) {
208 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
209 }
210 }
211
212 if ( !is_null( $params['continue'] ) ) {
213 $cont = explode( '|', $params['continue'] );
214 $op = ( $dir == 'newer' ? '>' : '<' );
215 if ( $mode == 'all' || $mode == 'revs' ) {
216 $this->dieContinueUsageIf( count( $cont ) != 4 );
217 $ns = (int)$cont[0];
218 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
219 $title = $db->addQuotes( $cont[1] );
220 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
221 $ar_id = (int)$cont[3];
222 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
223 $this->addWhere( "ar_namespace $op $ns OR " .
224 "(ar_namespace = $ns AND " .
225 "(ar_title $op $title OR " .
226 "(ar_title = $title AND " .
227 "(ar_timestamp $op $ts OR " .
228 "(ar_timestamp = $ts AND " .
229 "ar_id $op= $ar_id)))))" );
230 } else {
231 $this->dieContinueUsageIf( count( $cont ) != 2 );
232 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
233 $ar_id = (int)$cont[1];
234 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
235 $this->addWhere( "ar_timestamp $op $ts OR " .
236 "(ar_timestamp = $ts AND " .
237 "ar_id $op= $ar_id)" );
238 }
239 }
240
241 $this->addOption( 'LIMIT', $limit + 1 );
242 if ( $mode == 'all' ) {
243 if ( $params['unique'] ) {
244 // @todo Does this work on non-MySQL?
245 $this->addOption( 'GROUP BY', 'ar_title' );
246 } else {
247 $sort = ( $dir == 'newer' ? '' : ' DESC' );
248 $this->addOption( 'ORDER BY', [
249 'ar_title' . $sort,
250 'ar_timestamp' . $sort,
251 'ar_id' . $sort,
252 ] );
253 }
254 } else {
255 if ( $mode == 'revs' ) {
256 // Sort by ns and title in the same order as timestamp for efficiency
257 $this->addWhereRange( 'ar_namespace', $dir, null, null );
258 $this->addWhereRange( 'ar_title', $dir, null, null );
259 }
260 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
261 // Include in ORDER BY for uniqueness
262 $this->addWhereRange( 'ar_id', $dir, null, null );
263 }
264 $res = $this->select( __METHOD__ );
265 $pageMap = []; // Maps ns&title to (fake) pageid
266 $count = 0;
267 $newPageID = 0;
268 foreach ( $res as $row ) {
269 if ( ++$count > $limit ) {
270 // We've had enough
271 if ( $mode == 'all' || $mode == 'revs' ) {
272 $this->setContinueEnumParameter( 'continue',
273 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
274 );
275 } else {
276 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
277 }
278 break;
279 }
280
281 $rev = [];
282 $anyHidden = false;
283
284 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
285 if ( $fld_revid ) {
286 $rev['revid'] = (int)$row->ar_rev_id;
287 }
288 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
289 $rev['parentid'] = (int)$row->ar_parent_id;
290 }
291 if ( $fld_user || $fld_userid ) {
292 if ( $row->ar_deleted & RevisionRecord::DELETED_USER ) {
293 $rev['userhidden'] = true;
294 $anyHidden = true;
295 }
296 if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_USER, $user ) ) {
297 if ( $fld_user ) {
298 $rev['user'] = $row->ar_user_text;
299 }
300 if ( $fld_userid ) {
301 $rev['userid'] = (int)$row->ar_user;
302 }
303 }
304 }
305
306 if ( $fld_comment || $fld_parsedcomment ) {
307 if ( $row->ar_deleted & RevisionRecord::DELETED_COMMENT ) {
308 $rev['commenthidden'] = true;
309 $anyHidden = true;
310 }
311 if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_COMMENT, $user ) ) {
312 $comment = $commentStore->getComment( 'ar_comment', $row )->text;
313 if ( $fld_comment ) {
314 $rev['comment'] = $comment;
315 }
316 if ( $fld_parsedcomment ) {
317 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
318 $rev['parsedcomment'] = Linker::formatComment( $comment, $title );
319 }
320 }
321 }
322
323 if ( $fld_minor ) {
324 $rev['minor'] = $row->ar_minor_edit == 1;
325 }
326 if ( $fld_len ) {
327 $rev['len'] = $row->ar_len;
328 }
329 if ( $fld_sha1 ) {
330 if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
331 $rev['sha1hidden'] = true;
332 $anyHidden = true;
333 }
334 if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_TEXT, $user ) ) {
335 if ( $row->ar_sha1 != '' ) {
336 $rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
337 } else {
338 $rev['sha1'] = '';
339 }
340 }
341 }
342 if ( $fld_content ) {
343 if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
344 $rev['texthidden'] = true;
345 $anyHidden = true;
346 }
347 if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_TEXT, $user ) ) {
348 ApiResult::setContentValue( $rev, 'text', Revision::getRevisionText( $row, 'ar_' ) );
349 }
350 }
351
352 if ( $fld_tags ) {
353 if ( $row->ts_tags ) {
354 $tags = explode( ',', $row->ts_tags );
355 ApiResult::setIndexedTagName( $tags, 'tag' );
356 $rev['tags'] = $tags;
357 } else {
358 $rev['tags'] = [];
359 }
360 }
361
362 if ( $anyHidden && ( $row->ar_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
363 $rev['suppressed'] = true;
364 }
365
366 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
367 $pageID = $newPageID++;
368 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
369 $a['revisions'] = [ $rev ];
370 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
371 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
372 ApiQueryBase::addTitleInfo( $a, $title );
373 if ( $fld_token ) {
374 $a['token'] = $token;
375 }
376 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
377 } else {
378 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
379 $fit = $result->addValue(
380 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
381 null, $rev );
382 }
383 if ( !$fit ) {
384 if ( $mode == 'all' || $mode == 'revs' ) {
385 $this->setContinueEnumParameter( 'continue',
386 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
387 );
388 } else {
389 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
390 }
391 break;
392 }
393 }
394 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
395 }
396
397 public function isDeprecated() {
398 return true;
399 }
400
401 public function getAllowedParams() {
402 return [
403 'start' => [
404 ApiBase::PARAM_TYPE => 'timestamp',
405 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
406 ],
407 'end' => [
408 ApiBase::PARAM_TYPE => 'timestamp',
409 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
410 ],
411 'dir' => [
412 ApiBase::PARAM_TYPE => [
413 'newer',
414 'older'
415 ],
416 ApiBase::PARAM_DFLT => 'older',
417 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
418 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 3 ] ],
419 ],
420 'from' => [
421 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
422 ],
423 'to' => [
424 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
425 ],
426 'prefix' => [
427 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
428 ],
429 'unique' => [
430 ApiBase::PARAM_DFLT => false,
431 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
432 ],
433 'namespace' => [
434 ApiBase::PARAM_TYPE => 'namespace',
435 ApiBase::PARAM_DFLT => NS_MAIN,
436 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
437 ],
438 'tag' => null,
439 'user' => [
440 ApiBase::PARAM_TYPE => 'user'
441 ],
442 'excludeuser' => [
443 ApiBase::PARAM_TYPE => 'user'
444 ],
445 'prop' => [
446 ApiBase::PARAM_DFLT => 'user|comment',
447 ApiBase::PARAM_TYPE => [
448 'revid',
449 'parentid',
450 'user',
451 'userid',
452 'comment',
453 'parsedcomment',
454 'minor',
455 'len',
456 'sha1',
457 'content',
458 'token',
459 'tags'
460 ],
461 ApiBase::PARAM_ISMULTI => true
462 ],
463 'limit' => [
464 ApiBase::PARAM_DFLT => 10,
465 ApiBase::PARAM_TYPE => 'limit',
466 ApiBase::PARAM_MIN => 1,
467 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
468 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
469 ],
470 'continue' => [
471 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
472 ],
473 ];
474 }
475
476 protected function getExamplesMessages() {
477 return [
478 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
479 'drprop=user|comment|content'
480 => 'apihelp-query+deletedrevs-example-mode1',
481 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
482 => 'apihelp-query+deletedrevs-example-mode2',
483 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
484 => 'apihelp-query+deletedrevs-example-mode3-main',
485 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
486 => 'apihelp-query+deletedrevs-example-mode3-talk',
487 ];
488 }
489
490 public function getHelpUrls() {
491 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
492 }
493 }