MediaWiki  1.31.0
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1 <?php
32 
33  public function __construct( ApiQuery $query, $moduleName ) {
34  parent::__construct( $query, $moduleName, 'drv' );
35  }
36 
37  protected function run( ApiPageSet $resultPageSet = null ) {
38  $user = $this->getUser();
39  // Before doing anything at all, let's check permissions
40  $this->checkUserRightsAny( 'deletedhistory' );
41 
42  $pageSet = $this->getPageSet();
43  $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
44  $pageCount = count( $pageSet->getGoodAndMissingTitles() );
45  $revCount = $pageSet->getRevisionCount();
46  if ( $revCount === 0 && $pageCount === 0 ) {
47  // Nothing to do
48  return;
49  }
50  if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
51  // Nothing to do, revisions were supplied but none are deleted
52  return;
53  }
54 
55  $params = $this->extractRequestParams( false );
56 
57  $db = $this->getDB();
58 
59  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
60 
61  if ( $resultPageSet === null ) {
62  $this->parseParameters( $params );
63  $arQuery = Revision::getArchiveQueryInfo();
64  $this->addTables( $arQuery['tables'] );
65  $this->addFields( $arQuery['fields'] );
66  $this->addJoinConds( $arQuery['joins'] );
67  $this->addFields( [ 'ar_title', 'ar_namespace' ] );
68  } else {
69  $this->limit = $this->getParameter( 'limit' ) ?: 10;
70  $this->addTables( 'archive' );
71  $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
72  }
73 
74  if ( $this->fld_tags ) {
75  $this->addTables( 'tag_summary' );
76  $this->addJoinConds(
77  [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
78  );
79  $this->addFields( 'ts_tags' );
80  }
81 
82  if ( !is_null( $params['tag'] ) ) {
83  $this->addTables( 'change_tag' );
84  $this->addJoinConds(
85  [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
86  );
87  $this->addWhereFld( 'ct_tag', $params['tag'] );
88  }
89 
90  if ( $this->fetchContent ) {
91  $this->addTables( 'text' );
92  $this->addJoinConds(
93  [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
94  );
95  $this->addFields( [ 'old_text', 'old_flags' ] );
96 
97  // This also means stricter restrictions
98  $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
99  }
100 
101  $dir = $params['dir'];
102 
103  if ( $revCount !== 0 ) {
104  $this->addWhere( [
105  'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
106  ] );
107  } else {
108  // We need a custom WHERE clause that matches all titles.
109  $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
110  $where = $lb->constructSet( 'ar', $db );
111  $this->addWhere( $where );
112  }
113 
114  if ( !is_null( $params['user'] ) ) {
115  // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
116  $actorQuery = ActorMigration::newMigration()
117  ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
118  $this->addTables( $actorQuery['tables'] );
119  $this->addJoinConds( $actorQuery['joins'] );
120  $this->addWhere( $actorQuery['conds'] );
121  } elseif ( !is_null( $params['excludeuser'] ) ) {
122  // Here there's no chance of using ar_usertext_timestamp.
123  $actorQuery = ActorMigration::newMigration()
124  ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
125  $this->addTables( $actorQuery['tables'] );
126  $this->addJoinConds( $actorQuery['joins'] );
127  $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
128  }
129 
130  if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
131  // Paranoia: avoid brute force searches (T19342)
132  // (shouldn't be able to get here without 'deletedhistory', but
133  // check it again just in case)
134  if ( !$user->isAllowed( 'deletedhistory' ) ) {
135  $bitmask = Revision::DELETED_USER;
136  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
138  } else {
139  $bitmask = 0;
140  }
141  if ( $bitmask ) {
142  $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
143  }
144  }
145 
146  if ( !is_null( $params['continue'] ) ) {
147  $cont = explode( '|', $params['continue'] );
148  $op = ( $dir == 'newer' ? '>' : '<' );
149  if ( $revCount !== 0 ) {
150  $this->dieContinueUsageIf( count( $cont ) != 2 );
151  $rev = intval( $cont[0] );
152  $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
153  $ar_id = (int)$cont[1];
154  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
155  $this->addWhere( "ar_rev_id $op $rev OR " .
156  "(ar_rev_id = $rev AND " .
157  "ar_id $op= $ar_id)" );
158  } else {
159  $this->dieContinueUsageIf( count( $cont ) != 4 );
160  $ns = intval( $cont[0] );
161  $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
162  $title = $db->addQuotes( $cont[1] );
163  $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
164  $ar_id = (int)$cont[3];
165  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
166  $this->addWhere( "ar_namespace $op $ns OR " .
167  "(ar_namespace = $ns AND " .
168  "(ar_title $op $title OR " .
169  "(ar_title = $title AND " .
170  "(ar_timestamp $op $ts OR " .
171  "(ar_timestamp = $ts AND " .
172  "ar_id $op= $ar_id)))))" );
173  }
174  }
175 
176  $this->addOption( 'LIMIT', $this->limit + 1 );
177 
178  if ( $revCount !== 0 ) {
179  // Sort by ar_rev_id when querying by ar_rev_id
180  $this->addWhereRange( 'ar_rev_id', $dir, null, null );
181  } else {
182  // Sort by ns and title in the same order as timestamp for efficiency
183  // But only when not already unique in the query
184  if ( count( $pageMap ) > 1 ) {
185  $this->addWhereRange( 'ar_namespace', $dir, null, null );
186  }
187  $oneTitle = key( reset( $pageMap ) );
188  foreach ( $pageMap as $pages ) {
189  if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
190  $this->addWhereRange( 'ar_title', $dir, null, null );
191  break;
192  }
193  }
194  $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
195  }
196  // Include in ORDER BY for uniqueness
197  $this->addWhereRange( 'ar_id', $dir, null, null );
198 
199  $res = $this->select( __METHOD__ );
200  $count = 0;
201  $generated = [];
202  foreach ( $res as $row ) {
203  if ( ++$count > $this->limit ) {
204  // We've had enough
205  $this->setContinueEnumParameter( 'continue',
206  $revCount
207  ? "$row->ar_rev_id|$row->ar_id"
208  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
209  );
210  break;
211  }
212 
213  if ( $resultPageSet !== null ) {
214  $generated[] = $row->ar_rev_id;
215  } else {
216  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
217  // Was it converted?
218  $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
219  $converted = $pageSet->getConvertedTitles();
220  if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
221  $title = Title::newFromText( $converted[$title->getPrefixedText()] );
222  if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
223  $pageMap[$row->ar_namespace][$row->ar_title] =
224  $pageMap[$title->getNamespace()][$title->getDBkey()];
225  }
226  }
227  }
228  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
230  __METHOD__,
231  "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
232  );
233  }
234 
235  $fit = $this->addPageSubItem(
236  $pageMap[$row->ar_namespace][$row->ar_title],
237  $this->extractRevisionInfo( Revision::newFromArchiveRow( $row ), $row ),
238  'rev'
239  );
240  if ( !$fit ) {
241  $this->setContinueEnumParameter( 'continue',
242  $revCount
243  ? "$row->ar_rev_id|$row->ar_id"
244  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
245  );
246  break;
247  }
248  }
249  }
250 
251  if ( $resultPageSet !== null ) {
252  $resultPageSet->populateFromRevisionIDs( $generated );
253  }
254  }
255 
256  public function getAllowedParams() {
257  return parent::getAllowedParams() + [
258  'start' => [
259  ApiBase::PARAM_TYPE => 'timestamp',
260  ],
261  'end' => [
262  ApiBase::PARAM_TYPE => 'timestamp',
263  ],
264  'dir' => [
266  'newer',
267  'older'
268  ],
269  ApiBase::PARAM_DFLT => 'older',
270  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
271  ],
272  'tag' => null,
273  'user' => [
274  ApiBase::PARAM_TYPE => 'user'
275  ],
276  'excludeuser' => [
277  ApiBase::PARAM_TYPE => 'user'
278  ],
279  'continue' => [
280  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
281  ],
282  ];
283  }
284 
285  protected function getExamplesMessages() {
286  return [
287  'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
288  'drvprop=user|comment|content'
289  => 'apihelp-query+deletedrevisions-example-titles',
290  'action=query&prop=deletedrevisions&revids=123456'
291  => 'apihelp-query+deletedrevisions-example-revids',
292  ];
293  }
294 
295  public function getHelpUrls() {
296  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
297  }
298 }
ApiQueryDeletedRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryDeletedRevisions.php:37
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:167
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:57
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:49
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
Revision\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: Revision.php:50
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:273
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:192
ApiQuery
This is the main query class.
Definition: ApiQuery.php:36
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
ApiQueryDeletedRevisions
Query module to enumerate deleted revisions for pages.
Definition: ApiQueryDeletedRevisions.php:31
captcha-old.count
count
Definition: captcha-old.py:249
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:313
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:124
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:87
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:2006
$params
$params
Definition: styleTest.css.php:40
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
Revision\getArchiveQueryInfo
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:506
$res
$res
Definition: database.txt:21
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:325
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:89
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:40
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
ApiQueryRevisionsBase
A base class for functions common to producing a list of revisions.
Definition: ApiQueryRevisionsBase.php:28
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:84
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1591
ApiQueryDeletedRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryDeletedRevisions.php:285
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:58
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:105
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:158
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:350
ApiQueryDeletedRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryDeletedRevisions.php:295
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:534
ApiQueryBase\$where
$where
Definition: ApiQueryBase.php:35
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:278
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:749
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:2066
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:181
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:260
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:823
ApiQueryDeletedRevisions\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryDeletedRevisions.php:256
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:48
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1767
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:773
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiQueryDeletedRevisions\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryDeletedRevisions.php:33
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:227
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:510
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2078