MediaWiki  master
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1 <?php
30 
37 
38  public function __construct( ApiQuery $query, $moduleName ) {
39  parent::__construct( $query, $moduleName, 'drv' );
40  }
41 
42  protected function run( ApiPageSet $resultPageSet = null ) {
43  $user = $this->getUser();
44 
45  $pageSet = $this->getPageSet();
46  $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
47  $pageCount = count( $pageSet->getGoodAndMissingTitles() );
48  $revCount = $pageSet->getRevisionCount();
49  if ( $revCount === 0 && $pageCount === 0 ) {
50  // Nothing to do
51  return;
52  }
53  if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
54  // Nothing to do, revisions were supplied but none are deleted
55  return;
56  }
57 
58  $params = $this->extractRequestParams( false );
59 
60  $db = $this->getDB();
61  $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
62 
63  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
64 
65  if ( $resultPageSet === null ) {
66  $this->parseParameters( $params );
67  $arQuery = $revisionStore->getArchiveQueryInfo();
68  $this->addTables( $arQuery['tables'] );
69  $this->addFields( $arQuery['fields'] );
70  $this->addJoinConds( $arQuery['joins'] );
71  $this->addFields( [ 'ar_title', 'ar_namespace' ] );
72  } else {
73  $this->limit = $this->getParameter( 'limit' ) ?: 10;
74  $this->addTables( 'archive' );
75  $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
76  }
77 
78  if ( $this->fld_tags ) {
79  $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
80  }
81 
82  if ( $params['tag'] !== null ) {
83  $this->addTables( 'change_tag' );
84  $this->addJoinConds(
85  [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
86  );
87  $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
88  try {
89  $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
90  } catch ( NameTableAccessException $exception ) {
91  // Return nothing.
92  $this->addWhere( '1=0' );
93  }
94  }
95 
96  // This means stricter restrictions
97  if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
98  !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' )
99  ) {
100  $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
101  }
102  if ( $this->fetchContent &&
103  !$this->getPermissionManager()->userHasAnyRight( $user, 'deletedtext', 'undelete' )
104  ) {
105  $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
106  }
107 
108  $dir = $params['dir'];
109 
110  if ( $revCount !== 0 ) {
111  $this->addWhere( [
112  'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
113  ] );
114  } else {
115  // We need a custom WHERE clause that matches all titles.
116  $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
117  $where = $lb->constructSet( 'ar', $db );
118  $this->addWhere( $where );
119  }
120 
121  if ( $params['user'] !== null ) {
122  // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
123  $actorQuery = ActorMigration::newMigration()
124  ->getWhere( $db, 'ar_user', $params['user'], false );
125  $this->addTables( $actorQuery['tables'] );
126  $this->addJoinConds( $actorQuery['joins'] );
127  $this->addWhere( $actorQuery['conds'] );
128  } elseif ( $params['excludeuser'] !== null ) {
129  // Here there's no chance of using ar_usertext_timestamp.
130  $actorQuery = ActorMigration::newMigration()
131  ->getWhere( $db, 'ar_user', $params['excludeuser'] );
132  $this->addTables( $actorQuery['tables'] );
133  $this->addJoinConds( $actorQuery['joins'] );
134  $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
135  }
136 
137  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
138  // Paranoia: avoid brute force searches (T19342)
139  if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
140  $bitmask = RevisionRecord::DELETED_USER;
141  } elseif ( !$this->getPermissionManager()
142  ->userHasAnyRight( $this->getUser(), 'suppressrevision', 'viewsuppressed' )
143  ) {
144  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
145  } else {
146  $bitmask = 0;
147  }
148  if ( $bitmask ) {
149  $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
150  }
151  }
152 
153  if ( $params['continue'] !== null ) {
154  $cont = explode( '|', $params['continue'] );
155  $op = ( $dir == 'newer' ? '>' : '<' );
156  if ( $revCount !== 0 ) {
157  $this->dieContinueUsageIf( count( $cont ) != 2 );
158  $rev = (int)$cont[0];
159  $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
160  $ar_id = (int)$cont[1];
161  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
162  $this->addWhere( "ar_rev_id $op $rev OR " .
163  "(ar_rev_id = $rev AND " .
164  "ar_id $op= $ar_id)" );
165  } else {
166  $this->dieContinueUsageIf( count( $cont ) != 4 );
167  $ns = (int)$cont[0];
168  $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
169  $title = $db->addQuotes( $cont[1] );
170  $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
171  $ar_id = (int)$cont[3];
172  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
173  $this->addWhere( "ar_namespace $op $ns OR " .
174  "(ar_namespace = $ns AND " .
175  "(ar_title $op $title OR " .
176  "(ar_title = $title AND " .
177  "(ar_timestamp $op $ts OR " .
178  "(ar_timestamp = $ts AND " .
179  "ar_id $op= $ar_id)))))" );
180  }
181  }
182 
183  $this->addOption( 'LIMIT', $this->limit + 1 );
184 
185  if ( $revCount !== 0 ) {
186  // Sort by ar_rev_id when querying by ar_rev_id
187  $this->addWhereRange( 'ar_rev_id', $dir, null, null );
188  } else {
189  // Sort by ns and title in the same order as timestamp for efficiency
190  // But only when not already unique in the query
191  if ( count( $pageMap ) > 1 ) {
192  $this->addWhereRange( 'ar_namespace', $dir, null, null );
193  }
194  $oneTitle = key( reset( $pageMap ) );
195  foreach ( $pageMap as $pages ) {
196  if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
197  $this->addWhereRange( 'ar_title', $dir, null, null );
198  break;
199  }
200  }
201  $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
202  }
203  // Include in ORDER BY for uniqueness
204  $this->addWhereRange( 'ar_id', $dir, null, null );
205 
206  $res = $this->select( __METHOD__ );
207  $count = 0;
208  $generated = [];
209  foreach ( $res as $row ) {
210  if ( ++$count > $this->limit ) {
211  // We've had enough
212  $this->setContinueEnumParameter( 'continue',
213  $revCount
214  ? "$row->ar_rev_id|$row->ar_id"
215  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
216  );
217  break;
218  }
219 
220  if ( $resultPageSet !== null ) {
221  $generated[] = $row->ar_rev_id;
222  } else {
223  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
224  // Was it converted?
225  $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
226  $converted = $pageSet->getConvertedTitles();
227  if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
228  $title = Title::newFromText( $converted[$title->getPrefixedText()] );
229  if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
230  $pageMap[$row->ar_namespace][$row->ar_title] =
231  $pageMap[$title->getNamespace()][$title->getDBkey()];
232  }
233  }
234  }
235  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
237  __METHOD__,
238  "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
239  );
240  }
241 
242  $fit = $this->addPageSubItem(
243  $pageMap[$row->ar_namespace][$row->ar_title],
244  $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
245  'rev'
246  );
247  if ( !$fit ) {
248  $this->setContinueEnumParameter( 'continue',
249  $revCount
250  ? "$row->ar_rev_id|$row->ar_id"
251  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
252  );
253  break;
254  }
255  }
256  }
257 
258  if ( $resultPageSet !== null ) {
259  $resultPageSet->populateFromRevisionIDs( $generated );
260  }
261  }
262 
263  public function getAllowedParams() {
264  return parent::getAllowedParams() + [
265  'start' => [
266  ApiBase::PARAM_TYPE => 'timestamp',
267  ],
268  'end' => [
269  ApiBase::PARAM_TYPE => 'timestamp',
270  ],
271  'dir' => [
273  'newer',
274  'older'
275  ],
276  ApiBase::PARAM_DFLT => 'older',
277  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
278  ],
279  'tag' => null,
280  'user' => [
281  ApiBase::PARAM_TYPE => 'user',
282  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
283  UserDef::PARAM_RETURN_OBJECT => true,
284  ],
285  'excludeuser' => [
286  ApiBase::PARAM_TYPE => 'user',
287  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
288  UserDef::PARAM_RETURN_OBJECT => true,
289  ],
290  'continue' => [
291  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
292  ],
293  ];
294  }
295 
296  protected function getExamplesMessages() {
297  return [
298  'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
299  'drvslots=*&drvprop=user|comment|content'
300  => 'apihelp-query+deletedrevisions-example-titles',
301  'action=query&prop=deletedrevisions&revids=123456'
302  => 'apihelp-query+deletedrevisions-example-revids',
303  ];
304  }
305 
306  public function getHelpUrls() {
307  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
308  }
309 }
ApiQueryDeletedRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryDeletedRevisions.php:42
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:82
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:887
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:215
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:35
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
ApiQueryDeletedRevisions
Query module to enumerate deleted revisions for pages.
Definition: ApiQueryDeletedRevisions.php:36
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
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:369
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:107
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:381
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:140
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:42
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
ApiQueryRevisionsBase
A base class for functions common to producing a list of revisions.
Definition: ApiQueryRevisionsBase.php:39
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiQueryDeletedRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryDeletedRevisions.php:296
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:57
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only) Stable to override.
Definition: ApiQueryBase.php:119
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:185
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:402
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
$title
$title
Definition: testCompression.php:38
ApiQueryDeletedRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryDeletedRevisions.php:306
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
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:340
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:889
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1562
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:637
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:204
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:285
ApiQueryDeletedRevisions\getAllowedParams
getAllowedParams()
Stable to override.
Definition: ApiQueryDeletedRevisions.php:263
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:837
MediaWiki\Storage\NameTableAccessException
Exception representing a failure to look up a row from a name table.
Definition: NameTableAccessException.php:33
ApiQueryDeletedRevisions\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryDeletedRevisions.php:38
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:248
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:498
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:1574