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