MediaWiki  1.27.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  if ( !$user->isAllowed( 'deletedhistory' ) ) {
43  $this->dieUsage(
44  'You don\'t have permission to view deleted revision information',
45  'permissiondenied'
46  );
47  }
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 
66  if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
67  $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
68  }
69 
70  $this->addTables( 'archive' );
71  if ( $resultPageSet === null ) {
72  $this->parseParameters( $params );
74  $this->addFields( [ 'ar_title', 'ar_namespace' ] );
75  } else {
76  $this->limit = $this->getParameter( 'limit' ) ?: 10;
77  $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
78  }
79 
80  if ( $this->fld_tags ) {
81  $this->addTables( 'tag_summary' );
82  $this->addJoinConds(
83  [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
84  );
85  $this->addFields( 'ts_tags' );
86  }
87 
88  if ( !is_null( $params['tag'] ) ) {
89  $this->addTables( 'change_tag' );
90  $this->addJoinConds(
91  [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
92  );
93  $this->addWhereFld( 'ct_tag', $params['tag'] );
94  }
95 
96  if ( $this->fetchContent ) {
97  // Modern MediaWiki has the content for deleted revs in the 'text'
98  // table using fields old_text and old_flags. But revisions deleted
99  // pre-1.5 store the content in the 'archive' table directly using
100  // fields ar_text and ar_flags, and no corresponding 'text' row. So
101  // we have to LEFT JOIN and fetch all four fields.
102  $this->addTables( 'text' );
103  $this->addJoinConds(
104  [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
105  );
106  $this->addFields( [ 'ar_text', 'ar_flags', 'old_text', 'old_flags' ] );
107 
108  // This also means stricter restrictions
109  if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
110  $this->dieUsage(
111  'You don\'t have permission to view deleted revision content',
112  'permissiondenied'
113  );
114  }
115  }
116 
117  $dir = $params['dir'];
118 
119  if ( $revCount !== 0 ) {
120  $this->addWhere( [
121  'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
122  ] );
123  } else {
124  // We need a custom WHERE clause that matches all titles.
125  $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
126  $where = $lb->constructSet( 'ar', $db );
127  $this->addWhere( $where );
128  }
129 
130  if ( !is_null( $params['user'] ) ) {
131  $this->addWhereFld( 'ar_user_text', $params['user'] );
132  } elseif ( !is_null( $params['excludeuser'] ) ) {
133  $this->addWhere( 'ar_user_text != ' .
134  $db->addQuotes( $params['excludeuser'] ) );
135  }
136 
137  if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
138  // Paranoia: avoid brute force searches (bug 17342)
139  // (shouldn't be able to get here without 'deletedhistory', but
140  // check it again just in case)
141  if ( !$user->isAllowed( 'deletedhistory' ) ) {
142  $bitmask = Revision::DELETED_USER;
143  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
145  } else {
146  $bitmask = 0;
147  }
148  if ( $bitmask ) {
149  $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
150  }
151  }
152 
153  if ( !is_null( $params['continue'] ) ) {
154  $cont = explode( '|', $params['continue'] );
155  $op = ( $dir == 'newer' ? '>' : '<' );
156  if ( $revCount !== 0 ) {
157  $this->dieContinueUsageIf( count( $cont ) != 2 );
158  $rev = intval( $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 = intval( $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( Revision::newFromArchiveRow( $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  ],
283  'excludeuser' => [
284  ApiBase::PARAM_TYPE => 'user'
285  ],
286  'continue' => [
287  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
288  ],
289  ];
290  }
291 
292  protected function getExamplesMessages() {
293  return [
294  'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
295  'drvprop=user|comment|content'
296  => 'apihelp-query+deletedrevisions-example-titles',
297  'action=query&prop=deletedrevisions&revids=123456'
298  => 'apihelp-query+deletedrevisions-example-revids',
299  ];
300  }
301 
302  public function getHelpUrls() {
303  return 'https://www.mediawiki.org/wiki/API:Deletedrevisions';
304  }
305 }
select($method, $extraQuery=[])
Execute a SELECT query based on the values in the internal arrays.
Query module to enumerate deleted revisions for pages.
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
getDB()
Get the Query database connection (read-only)
null for the local 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:1418
if(count($args)==0) $dir
getParameter($paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:709
addWhereFld($field, $value)
Equivalent to addWhere(array($field => $value))
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:41
addPageSubItem($pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
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...
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:685
A base class for functions common to producing a list of revisions.
static selectArchiveFields()
Return the list of revision fields that should be selected to create a new revision from an archive r...
Definition: Revision.php:460
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
addTimestampWhereRange($field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
setContinueEnumParameter($paramName, $paramValue)
Overridden to set the generator param if in generator mode.
addWhere($value)
Add a set of WHERE clauses to the internal array.
addJoinConds($join_conds)
Add a set of JOIN conditions to the internal array.
parseParameters($params)
Parse the parameters into the various instance fields.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:31
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
$res
Definition: database.txt:21
addOption($name, $value=null)
Add an option such as LIMIT or USE INDEX.
$params
const DELETED_RESTRICTED
Definition: Revision.php:79
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
This is the main query class.
Definition: ApiQuery.php:38
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:1584
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
dieContinueUsageIf($condition)
Die with the $prefix.
Definition: ApiBase.php:2181
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:125
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 local account $user
Definition: hooks.txt:242
__construct(ApiQuery $query, $moduleName)
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
const DELETED_USER
Definition: Revision.php:78
addFields($value)
Add a set of fields to select to the internal array.
dieUsage($description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1526
run(ApiPageSet $resultPageSet=null)
$count
static dieDebug($method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2230
static newFromArchiveRow($row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:172
getUser()
Get the User object.
getPageSet()
Get the PageSet object to work on.
addTables($tables, $alias=null)
Add a set of tables to the internal array.
static & makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:524