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