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