MediaWiki  1.29.2
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1 <?php
34 
35  public function __construct( ApiQuery $query, $moduleName ) {
36  parent::__construct( $query, $moduleName, 'drv' );
37  }
38 
39  protected function run( ApiPageSet $resultPageSet = null ) {
40  $user = $this->getUser();
41  // Before doing anything at all, let's check permissions
42  $this->checkUserRightsAny( 'deletedhistory' );
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 
61  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
62 
63  $this->addTables( 'archive' );
64  if ( $resultPageSet === null ) {
65  $this->parseParameters( $params );
67  $this->addFields( [ 'ar_title', 'ar_namespace' ] );
68  } else {
69  $this->limit = $this->getParameter( 'limit' ) ?: 10;
70  $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
71  }
72 
73  if ( $this->fld_tags ) {
74  $this->addTables( 'tag_summary' );
75  $this->addJoinConds(
76  [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
77  );
78  $this->addFields( 'ts_tags' );
79  }
80 
81  if ( !is_null( $params['tag'] ) ) {
82  $this->addTables( 'change_tag' );
83  $this->addJoinConds(
84  [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
85  );
86  $this->addWhereFld( 'ct_tag', $params['tag'] );
87  }
88 
89  if ( $this->fetchContent ) {
90  // Modern MediaWiki has the content for deleted revs in the 'text'
91  // table using fields old_text and old_flags. But revisions deleted
92  // pre-1.5 store the content in the 'archive' table directly using
93  // fields ar_text and ar_flags, and no corresponding 'text' row. So
94  // we have to LEFT JOIN and fetch all four fields.
95  $this->addTables( 'text' );
96  $this->addJoinConds(
97  [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
98  );
99  $this->addFields( [ 'ar_text', 'ar_flags', 'old_text', 'old_flags' ] );
100 
101  // This also means stricter restrictions
102  $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
103  }
104 
105  $dir = $params['dir'];
106 
107  if ( $revCount !== 0 ) {
108  $this->addWhere( [
109  'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
110  ] );
111  } else {
112  // We need a custom WHERE clause that matches all titles.
113  $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
114  $where = $lb->constructSet( 'ar', $db );
115  $this->addWhere( $where );
116  }
117 
118  if ( !is_null( $params['user'] ) ) {
119  $this->addWhereFld( 'ar_user_text', $params['user'] );
120  } elseif ( !is_null( $params['excludeuser'] ) ) {
121  $this->addWhere( 'ar_user_text != ' .
122  $db->addQuotes( $params['excludeuser'] ) );
123  }
124 
125  if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
126  // Paranoia: avoid brute force searches (T19342)
127  // (shouldn't be able to get here without 'deletedhistory', but
128  // check it again just in case)
129  if ( !$user->isAllowed( 'deletedhistory' ) ) {
130  $bitmask = Revision::DELETED_USER;
131  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
133  } else {
134  $bitmask = 0;
135  }
136  if ( $bitmask ) {
137  $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
138  }
139  }
140 
141  if ( !is_null( $params['continue'] ) ) {
142  $cont = explode( '|', $params['continue'] );
143  $op = ( $dir == 'newer' ? '>' : '<' );
144  if ( $revCount !== 0 ) {
145  $this->dieContinueUsageIf( count( $cont ) != 2 );
146  $rev = intval( $cont[0] );
147  $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
148  $ar_id = (int)$cont[1];
149  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
150  $this->addWhere( "ar_rev_id $op $rev OR " .
151  "(ar_rev_id = $rev AND " .
152  "ar_id $op= $ar_id)" );
153  } else {
154  $this->dieContinueUsageIf( count( $cont ) != 4 );
155  $ns = intval( $cont[0] );
156  $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
157  $title = $db->addQuotes( $cont[1] );
158  $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
159  $ar_id = (int)$cont[3];
160  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
161  $this->addWhere( "ar_namespace $op $ns OR " .
162  "(ar_namespace = $ns AND " .
163  "(ar_title $op $title OR " .
164  "(ar_title = $title AND " .
165  "(ar_timestamp $op $ts OR " .
166  "(ar_timestamp = $ts AND " .
167  "ar_id $op= $ar_id)))))" );
168  }
169  }
170 
171  $this->addOption( 'LIMIT', $this->limit + 1 );
172 
173  if ( $revCount !== 0 ) {
174  // Sort by ar_rev_id when querying by ar_rev_id
175  $this->addWhereRange( 'ar_rev_id', $dir, null, null );
176  } else {
177  // Sort by ns and title in the same order as timestamp for efficiency
178  // But only when not already unique in the query
179  if ( count( $pageMap ) > 1 ) {
180  $this->addWhereRange( 'ar_namespace', $dir, null, null );
181  }
182  $oneTitle = key( reset( $pageMap ) );
183  foreach ( $pageMap as $pages ) {
184  if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
185  $this->addWhereRange( 'ar_title', $dir, null, null );
186  break;
187  }
188  }
189  $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
190  }
191  // Include in ORDER BY for uniqueness
192  $this->addWhereRange( 'ar_id', $dir, null, null );
193 
194  $res = $this->select( __METHOD__ );
195  $count = 0;
196  $generated = [];
197  foreach ( $res as $row ) {
198  if ( ++$count > $this->limit ) {
199  // We've had enough
200  $this->setContinueEnumParameter( 'continue',
201  $revCount
202  ? "$row->ar_rev_id|$row->ar_id"
203  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
204  );
205  break;
206  }
207 
208  if ( $resultPageSet !== null ) {
209  $generated[] = $row->ar_rev_id;
210  } else {
211  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
212  // Was it converted?
213  $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
214  $converted = $pageSet->getConvertedTitles();
215  if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
216  $title = Title::newFromText( $converted[$title->getPrefixedText()] );
217  if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
218  $pageMap[$row->ar_namespace][$row->ar_title] =
219  $pageMap[$title->getNamespace()][$title->getDBkey()];
220  }
221  }
222  }
223  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
225  __METHOD__,
226  "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
227  );
228  }
229 
230  $fit = $this->addPageSubItem(
231  $pageMap[$row->ar_namespace][$row->ar_title],
232  $this->extractRevisionInfo( Revision::newFromArchiveRow( $row ), $row ),
233  'rev'
234  );
235  if ( !$fit ) {
236  $this->setContinueEnumParameter( 'continue',
237  $revCount
238  ? "$row->ar_rev_id|$row->ar_id"
239  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
240  );
241  break;
242  }
243  }
244  }
245 
246  if ( $resultPageSet !== null ) {
247  $resultPageSet->populateFromRevisionIDs( $generated );
248  }
249  }
250 
251  public function getAllowedParams() {
252  return parent::getAllowedParams() + [
253  'start' => [
254  ApiBase::PARAM_TYPE => 'timestamp',
255  ],
256  'end' => [
257  ApiBase::PARAM_TYPE => 'timestamp',
258  ],
259  'dir' => [
261  'newer',
262  'older'
263  ],
264  ApiBase::PARAM_DFLT => 'older',
265  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
266  ],
267  'tag' => null,
268  'user' => [
269  ApiBase::PARAM_TYPE => 'user'
270  ],
271  'excludeuser' => [
272  ApiBase::PARAM_TYPE => 'user'
273  ],
274  'continue' => [
275  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
276  ],
277  ];
278  }
279 
280  protected function getExamplesMessages() {
281  return [
282  'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
283  'drvprop=user|comment|content'
284  => 'apihelp-query+deletedrevisions-example-titles',
285  'action=query&prop=deletedrevisions&revids=123456'
286  => 'apihelp-query+deletedrevisions-example-revids',
287  ];
288  }
289 
290  public function getHelpUrls() {
291  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
292  }
293 }
ApiQueryDeletedRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryDeletedRevisions.php:39
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:189
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:61
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:92
Revision\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: Revision.php:93
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:265
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:198
ApiQuery
This is the main query class.
Definition: ApiQuery.php:40
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:33
captcha-old.count
count
Definition: captcha-old.py:225
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:321
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:128
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:91
$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:246
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1890
$params
$params
Definition: styleTest.css.php:40
$res
$res
Definition: database.txt:21
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:333
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:133
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:44
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:32
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:88
$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:1572
ApiQueryDeletedRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryDeletedRevisions.php:280
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:62
$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:111
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:164
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:358
ApiQueryDeletedRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryDeletedRevisions.php:290
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
$dir
$dir
Definition: Autoload.php:8
ApiQueryBase\$where
$where
Definition: ApiQueryBase.php:39
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:286
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
Revision\selectArchiveFields
static selectArchiveFields()
Return the list of revision fields that should be selected to create a new revision from an archive r...
Definition: Revision.php:479
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1950
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:187
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:266
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:792
ApiQueryDeletedRevisions\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryDeletedRevisions.php:251
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
$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:1741
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:742
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:35
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:233
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:514
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:1962