Merge "Use ParserCache in CategoryMembershipChangeJob"
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedRevisions.php
1 <?php
2 /**
3 * Copyright © 2014 Wikimedia Foundation and contributors
4 *
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Revision\RevisionRecord;
28 use MediaWiki\Storage\NameTableAccessException;
29
30 /**
31 * Query module to enumerate deleted revisions for pages.
32 *
33 * @ingroup API
34 */
35 class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase {
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 // Before doing anything at all, let's check permissions
44 $this->checkUserRightsAny( 'deletedhistory' );
45
46 $pageSet = $this->getPageSet();
47 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
48 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
49 $revCount = $pageSet->getRevisionCount();
50 if ( $revCount === 0 && $pageCount === 0 ) {
51 // Nothing to do
52 return;
53 }
54 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
55 // Nothing to do, revisions were supplied but none are deleted
56 return;
57 }
58
59 $params = $this->extractRequestParams( false );
60
61 $db = $this->getDB();
62 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
63
64 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
65
66 if ( $resultPageSet === null ) {
67 $this->parseParameters( $params );
68 $arQuery = $revisionStore->getArchiveQueryInfo();
69 $this->addTables( $arQuery['tables'] );
70 $this->addFields( $arQuery['fields'] );
71 $this->addJoinConds( $arQuery['joins'] );
72 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
73 } else {
74 $this->limit = $this->getParameter( 'limit' ) ?: 10;
75 $this->addTables( 'archive' );
76 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
77 }
78
79 if ( $this->fld_tags ) {
80 $this->addTables( 'tag_summary' );
81 $this->addJoinConds(
82 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
83 );
84 $this->addFields( 'ts_tags' );
85 }
86
87 if ( !is_null( $params['tag'] ) ) {
88 $this->addTables( 'change_tag' );
89 $this->addJoinConds(
90 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
91 );
92 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
93 try {
94 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
95 } catch ( NameTableAccessException $exception ) {
96 // Return nothing.
97 $this->addWhere( '1=0' );
98 }
99 }
100
101 if ( $this->fetchContent ) {
102 $this->addTables( 'text' );
103 $this->addJoinConds(
104 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
105 );
106 $this->addFields( [ 'old_text', 'old_flags' ] );
107
108 // This also means stricter restrictions
109 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
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 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
121 $where = $lb->constructSet( 'ar', $db );
122 $this->addWhere( $where );
123 }
124
125 if ( !is_null( $params['user'] ) ) {
126 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
127 $actorQuery = ActorMigration::newMigration()
128 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
129 $this->addTables( $actorQuery['tables'] );
130 $this->addJoinConds( $actorQuery['joins'] );
131 $this->addWhere( $actorQuery['conds'] );
132 } elseif ( !is_null( $params['excludeuser'] ) ) {
133 // Here there's no chance of using ar_usertext_timestamp.
134 $actorQuery = ActorMigration::newMigration()
135 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
136 $this->addTables( $actorQuery['tables'] );
137 $this->addJoinConds( $actorQuery['joins'] );
138 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
139 }
140
141 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
142 // Paranoia: avoid brute force searches (T19342)
143 // (shouldn't be able to get here without 'deletedhistory', but
144 // check it again just in case)
145 if ( !$user->isAllowed( 'deletedhistory' ) ) {
146 $bitmask = RevisionRecord::DELETED_USER;
147 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
148 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
149 } else {
150 $bitmask = 0;
151 }
152 if ( $bitmask ) {
153 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
154 }
155 }
156
157 if ( !is_null( $params['continue'] ) ) {
158 $cont = explode( '|', $params['continue'] );
159 $op = ( $dir == 'newer' ? '>' : '<' );
160 if ( $revCount !== 0 ) {
161 $this->dieContinueUsageIf( count( $cont ) != 2 );
162 $rev = intval( $cont[0] );
163 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
164 $ar_id = (int)$cont[1];
165 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
166 $this->addWhere( "ar_rev_id $op $rev OR " .
167 "(ar_rev_id = $rev AND " .
168 "ar_id $op= $ar_id)" );
169 } else {
170 $this->dieContinueUsageIf( count( $cont ) != 4 );
171 $ns = intval( $cont[0] );
172 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
173 $title = $db->addQuotes( $cont[1] );
174 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
175 $ar_id = (int)$cont[3];
176 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
177 $this->addWhere( "ar_namespace $op $ns OR " .
178 "(ar_namespace = $ns AND " .
179 "(ar_title $op $title OR " .
180 "(ar_title = $title AND " .
181 "(ar_timestamp $op $ts OR " .
182 "(ar_timestamp = $ts AND " .
183 "ar_id $op= $ar_id)))))" );
184 }
185 }
186
187 $this->addOption( 'LIMIT', $this->limit + 1 );
188
189 if ( $revCount !== 0 ) {
190 // Sort by ar_rev_id when querying by ar_rev_id
191 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
192 } else {
193 // Sort by ns and title in the same order as timestamp for efficiency
194 // But only when not already unique in the query
195 if ( count( $pageMap ) > 1 ) {
196 $this->addWhereRange( 'ar_namespace', $dir, null, null );
197 }
198 $oneTitle = key( reset( $pageMap ) );
199 foreach ( $pageMap as $pages ) {
200 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
201 $this->addWhereRange( 'ar_title', $dir, null, null );
202 break;
203 }
204 }
205 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
206 }
207 // Include in ORDER BY for uniqueness
208 $this->addWhereRange( 'ar_id', $dir, null, null );
209
210 $res = $this->select( __METHOD__ );
211 $count = 0;
212 $generated = [];
213 foreach ( $res as $row ) {
214 if ( ++$count > $this->limit ) {
215 // We've had enough
216 $this->setContinueEnumParameter( 'continue',
217 $revCount
218 ? "$row->ar_rev_id|$row->ar_id"
219 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
220 );
221 break;
222 }
223
224 if ( $resultPageSet !== null ) {
225 $generated[] = $row->ar_rev_id;
226 } else {
227 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
228 // Was it converted?
229 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
230 $converted = $pageSet->getConvertedTitles();
231 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
232 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
233 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
234 $pageMap[$row->ar_namespace][$row->ar_title] =
235 $pageMap[$title->getNamespace()][$title->getDBkey()];
236 }
237 }
238 }
239 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
240 ApiBase::dieDebug(
241 __METHOD__,
242 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
243 );
244 }
245
246 $fit = $this->addPageSubItem(
247 $pageMap[$row->ar_namespace][$row->ar_title],
248 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
249 'rev'
250 );
251 if ( !$fit ) {
252 $this->setContinueEnumParameter( 'continue',
253 $revCount
254 ? "$row->ar_rev_id|$row->ar_id"
255 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
256 );
257 break;
258 }
259 }
260 }
261
262 if ( $resultPageSet !== null ) {
263 $resultPageSet->populateFromRevisionIDs( $generated );
264 }
265 }
266
267 public function getAllowedParams() {
268 return parent::getAllowedParams() + [
269 'start' => [
270 ApiBase::PARAM_TYPE => 'timestamp',
271 ],
272 'end' => [
273 ApiBase::PARAM_TYPE => 'timestamp',
274 ],
275 'dir' => [
276 ApiBase::PARAM_TYPE => [
277 'newer',
278 'older'
279 ],
280 ApiBase::PARAM_DFLT => 'older',
281 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
282 ],
283 'tag' => null,
284 'user' => [
285 ApiBase::PARAM_TYPE => 'user'
286 ],
287 'excludeuser' => [
288 ApiBase::PARAM_TYPE => 'user'
289 ],
290 'continue' => [
291 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
292 ],
293 ];
294 }
295
296 protected function getExamplesMessages() {
297 return [
298 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
299 'drvslots=*&drvprop=user|comment|content'
300 => 'apihelp-query+deletedrevisions-example-titles',
301 'action=query&prop=deletedrevisions&revids=123456'
302 => 'apihelp-query+deletedrevisions-example-revids',
303 ];
304 }
305
306 public function getHelpUrls() {
307 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
308 }
309 }