Migrate Api modules from tag_summary table to change_tag
[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 global $wgChangeTagsSchemaMigrationStage;
43
44 $user = $this->getUser();
45 // Before doing anything at all, let's check permissions
46 $this->checkUserRightsAny( 'deletedhistory' );
47
48 $pageSet = $this->getPageSet();
49 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
50 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
51 $revCount = $pageSet->getRevisionCount();
52 if ( $revCount === 0 && $pageCount === 0 ) {
53 // Nothing to do
54 return;
55 }
56 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
57 // Nothing to do, revisions were supplied but none are deleted
58 return;
59 }
60
61 $params = $this->extractRequestParams( false );
62
63 $db = $this->getDB();
64 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
65
66 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
67
68 if ( $resultPageSet === null ) {
69 $this->parseParameters( $params );
70 $arQuery = $revisionStore->getArchiveQueryInfo();
71 $this->addTables( $arQuery['tables'] );
72 $this->addFields( $arQuery['fields'] );
73 $this->addJoinConds( $arQuery['joins'] );
74 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
75 } else {
76 $this->limit = $this->getParameter( 'limit' ) ?: 10;
77 $this->addTables( 'archive' );
78 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
79 }
80
81 if ( $this->fld_tags ) {
82 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
83 }
84
85 if ( !is_null( $params['tag'] ) ) {
86 $this->addTables( 'change_tag' );
87 $this->addJoinConds(
88 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
89 );
90 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
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 } else {
99 $this->addWhereFld( 'ct_tag', $params['tag'] );
100 }
101 }
102
103 if ( $this->fetchContent ) {
104 $this->addTables( 'text' );
105 $this->addJoinConds(
106 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
107 );
108 $this->addFields( [ 'old_text', 'old_flags' ] );
109
110 // This also means stricter restrictions
111 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
112 }
113
114 $dir = $params['dir'];
115
116 if ( $revCount !== 0 ) {
117 $this->addWhere( [
118 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
119 ] );
120 } else {
121 // We need a custom WHERE clause that matches all titles.
122 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
123 $where = $lb->constructSet( 'ar', $db );
124 $this->addWhere( $where );
125 }
126
127 if ( !is_null( $params['user'] ) ) {
128 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
129 $actorQuery = ActorMigration::newMigration()
130 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
131 $this->addTables( $actorQuery['tables'] );
132 $this->addJoinConds( $actorQuery['joins'] );
133 $this->addWhere( $actorQuery['conds'] );
134 } elseif ( !is_null( $params['excludeuser'] ) ) {
135 // Here there's no chance of using ar_usertext_timestamp.
136 $actorQuery = ActorMigration::newMigration()
137 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
138 $this->addTables( $actorQuery['tables'] );
139 $this->addJoinConds( $actorQuery['joins'] );
140 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
141 }
142
143 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
144 // Paranoia: avoid brute force searches (T19342)
145 // (shouldn't be able to get here without 'deletedhistory', but
146 // check it again just in case)
147 if ( !$user->isAllowed( 'deletedhistory' ) ) {
148 $bitmask = RevisionRecord::DELETED_USER;
149 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
150 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
151 } else {
152 $bitmask = 0;
153 }
154 if ( $bitmask ) {
155 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
156 }
157 }
158
159 if ( !is_null( $params['continue'] ) ) {
160 $cont = explode( '|', $params['continue'] );
161 $op = ( $dir == 'newer' ? '>' : '<' );
162 if ( $revCount !== 0 ) {
163 $this->dieContinueUsageIf( count( $cont ) != 2 );
164 $rev = intval( $cont[0] );
165 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
166 $ar_id = (int)$cont[1];
167 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
168 $this->addWhere( "ar_rev_id $op $rev OR " .
169 "(ar_rev_id = $rev AND " .
170 "ar_id $op= $ar_id)" );
171 } else {
172 $this->dieContinueUsageIf( count( $cont ) != 4 );
173 $ns = intval( $cont[0] );
174 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
175 $title = $db->addQuotes( $cont[1] );
176 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
177 $ar_id = (int)$cont[3];
178 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
179 $this->addWhere( "ar_namespace $op $ns OR " .
180 "(ar_namespace = $ns AND " .
181 "(ar_title $op $title OR " .
182 "(ar_title = $title AND " .
183 "(ar_timestamp $op $ts OR " .
184 "(ar_timestamp = $ts AND " .
185 "ar_id $op= $ar_id)))))" );
186 }
187 }
188
189 $this->addOption( 'LIMIT', $this->limit + 1 );
190
191 if ( $revCount !== 0 ) {
192 // Sort by ar_rev_id when querying by ar_rev_id
193 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
194 } else {
195 // Sort by ns and title in the same order as timestamp for efficiency
196 // But only when not already unique in the query
197 if ( count( $pageMap ) > 1 ) {
198 $this->addWhereRange( 'ar_namespace', $dir, null, null );
199 }
200 $oneTitle = key( reset( $pageMap ) );
201 foreach ( $pageMap as $pages ) {
202 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
203 $this->addWhereRange( 'ar_title', $dir, null, null );
204 break;
205 }
206 }
207 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
208 }
209 // Include in ORDER BY for uniqueness
210 $this->addWhereRange( 'ar_id', $dir, null, null );
211
212 $res = $this->select( __METHOD__ );
213 $count = 0;
214 $generated = [];
215 foreach ( $res as $row ) {
216 if ( ++$count > $this->limit ) {
217 // We've had enough
218 $this->setContinueEnumParameter( 'continue',
219 $revCount
220 ? "$row->ar_rev_id|$row->ar_id"
221 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
222 );
223 break;
224 }
225
226 if ( $resultPageSet !== null ) {
227 $generated[] = $row->ar_rev_id;
228 } else {
229 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
230 // Was it converted?
231 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
232 $converted = $pageSet->getConvertedTitles();
233 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
234 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
235 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
236 $pageMap[$row->ar_namespace][$row->ar_title] =
237 $pageMap[$title->getNamespace()][$title->getDBkey()];
238 }
239 }
240 }
241 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
242 ApiBase::dieDebug(
243 __METHOD__,
244 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
245 );
246 }
247
248 $fit = $this->addPageSubItem(
249 $pageMap[$row->ar_namespace][$row->ar_title],
250 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
251 'rev'
252 );
253 if ( !$fit ) {
254 $this->setContinueEnumParameter( 'continue',
255 $revCount
256 ? "$row->ar_rev_id|$row->ar_id"
257 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
258 );
259 break;
260 }
261 }
262 }
263
264 if ( $resultPageSet !== null ) {
265 $resultPageSet->populateFromRevisionIDs( $generated );
266 }
267 }
268
269 public function getAllowedParams() {
270 return parent::getAllowedParams() + [
271 'start' => [
272 ApiBase::PARAM_TYPE => 'timestamp',
273 ],
274 'end' => [
275 ApiBase::PARAM_TYPE => 'timestamp',
276 ],
277 'dir' => [
278 ApiBase::PARAM_TYPE => [
279 'newer',
280 'older'
281 ],
282 ApiBase::PARAM_DFLT => 'older',
283 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
284 ],
285 'tag' => null,
286 'user' => [
287 ApiBase::PARAM_TYPE => 'user'
288 ],
289 'excludeuser' => [
290 ApiBase::PARAM_TYPE => 'user'
291 ],
292 'continue' => [
293 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
294 ],
295 ];
296 }
297
298 protected function getExamplesMessages() {
299 return [
300 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
301 'drvslots=*&drvprop=user|comment|content'
302 => 'apihelp-query+deletedrevisions-example-titles',
303 'action=query&prop=deletedrevisions&revids=123456'
304 => 'apihelp-query+deletedrevisions-example-revids',
305 ];
306 }
307
308 public function getHelpUrls() {
309 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
310 }
311 }