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