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