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