Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryDeletedRevisions.php
blob26ae26684305673386cb8fdb07590cf1441b56c4
1 <?php
2 /**
3 * Created on Oct 3, 2014
5 * Copyright © 2014 Brad Jorsch "bjorsch@wikimedia.org"
7 * Heavily based on ApiQueryDeletedrevs,
8 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
25 * @file
28 /**
29 * Query module to enumerate deleted revisions for pages.
31 * @ingroup API
33 class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase {
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'drv' );
39 protected function run( ApiPageSet $resultPageSet = null ) {
40 $user = $this->getUser();
41 // Before doing anything at all, let's check permissions
42 if ( !$user->isAllowed( 'deletedhistory' ) ) {
43 $this->dieUsage(
44 'You don\'t have permission to view deleted revision information',
45 'permissiondenied'
49 $result = $this->getResult();
50 $pageSet = $this->getPageSet();
51 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
52 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
53 $revCount = $pageSet->getRevisionCount();
54 if ( $revCount === 0 && $pageCount === 0 ) {
55 // Nothing to do
56 return;
58 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
59 // Nothing to do, revisions were supplied but none are deleted
60 return;
63 $params = $this->extractRequestParams( false );
65 $db = $this->getDB();
67 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
68 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
71 $this->addTables( 'archive' );
72 if ( $resultPageSet === null ) {
73 $this->parseParameters( $params );
74 $this->addFields( Revision::selectArchiveFields() );
75 $this->addFields( array( 'ar_title', 'ar_namespace' ) );
76 } else {
77 $this->limit = $this->getParameter( 'limit' ) ?: 10;
78 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ) );
81 if ( $this->fld_tags ) {
82 $this->addTables( 'tag_summary' );
83 $this->addJoinConds(
84 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
86 $this->addFields( 'ts_tags' );
89 if ( !is_null( $params['tag'] ) ) {
90 $this->addTables( 'change_tag' );
91 $this->addJoinConds(
92 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
94 $this->addWhereFld( 'ct_tag', $params['tag'] );
97 if ( $this->fetchContent ) {
98 // Modern MediaWiki has the content for deleted revs in the 'text'
99 // table using fields old_text and old_flags. But revisions deleted
100 // pre-1.5 store the content in the 'archive' table directly using
101 // fields ar_text and ar_flags, and no corresponding 'text' row. So
102 // we have to LEFT JOIN and fetch all four fields.
103 $this->addTables( 'text' );
104 $this->addJoinConds(
105 array( 'text' => array( 'LEFT JOIN', array( 'ar_text_id=old_id' ) ) )
107 $this->addFields( array( 'ar_text', 'ar_flags', 'old_text', 'old_flags' ) );
109 // This also means stricter restrictions
110 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
111 $this->dieUsage(
112 'You don\'t have permission to view deleted revision content',
113 'permissiondenied'
118 $dir = $params['dir'];
120 if ( $revCount !== 0 ) {
121 $this->addWhere( array(
122 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
123 ) );
124 } else {
125 // We need a custom WHERE clause that matches all titles.
126 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
127 $where = $lb->constructSet( 'ar', $db );
128 $this->addWhere( $where );
131 if ( !is_null( $params['user'] ) ) {
132 $this->addWhereFld( 'ar_user_text', $params['user'] );
133 } elseif ( !is_null( $params['excludeuser'] ) ) {
134 $this->addWhere( 'ar_user_text != ' .
135 $db->addQuotes( $params['excludeuser'] ) );
138 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
139 // Paranoia: avoid brute force searches (bug 17342)
140 // (shouldn't be able to get here without 'deletedhistory', but
141 // check it again just in case)
142 if ( !$user->isAllowed( 'deletedhistory' ) ) {
143 $bitmask = Revision::DELETED_USER;
144 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
145 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
146 } else {
147 $bitmask = 0;
149 if ( $bitmask ) {
150 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
154 if ( !is_null( $params['continue'] ) ) {
155 $cont = explode( '|', $params['continue'] );
156 $op = ( $dir == 'newer' ? '>' : '<' );
157 if ( $revCount !== 0 ) {
158 $this->dieContinueUsageIf( count( $cont ) != 2 );
159 $rev = intval( $cont[0] );
160 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
161 $ar_id = (int)$cont[1];
162 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
163 $this->addWhere( "ar_rev_id $op $rev OR " .
164 "(ar_rev_id = $rev AND " .
165 "ar_id $op= $ar_id)" );
166 } else {
167 $this->dieContinueUsageIf( count( $cont ) != 4 );
168 $ns = intval( $cont[0] );
169 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
170 $title = $db->addQuotes( $cont[1] );
171 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
172 $ar_id = (int)$cont[3];
173 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
174 $this->addWhere( "ar_namespace $op $ns OR " .
175 "(ar_namespace = $ns AND " .
176 "(ar_title $op $title OR " .
177 "(ar_title = $title AND " .
178 "(ar_timestamp $op $ts OR " .
179 "(ar_timestamp = $ts AND " .
180 "ar_id $op= $ar_id)))))" );
184 $this->addOption( 'LIMIT', $this->limit + 1 );
186 if ( $revCount !== 0 ) {
187 // Sort by ar_rev_id when querying by ar_rev_id
188 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
189 } else {
190 // Sort by ns and title in the same order as timestamp for efficiency
191 // But only when not already unique in the query
192 if ( count( $pageMap ) > 1 ) {
193 $this->addWhereRange( 'ar_namespace', $dir, null, null );
195 $oneTitle = key( reset( $pageMap ) );
196 foreach ( $pageMap as $pages ) {
197 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
198 $this->addWhereRange( 'ar_title', $dir, null, null );
199 break;
202 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
204 // Include in ORDER BY for uniqueness
205 $this->addWhereRange( 'ar_id', $dir, null, null );
207 $res = $this->select( __METHOD__ );
208 $count = 0;
209 $generated = array();
210 foreach ( $res as $row ) {
211 if ( ++$count > $this->limit ) {
212 // We've had enough
213 $this->setContinueEnumParameter( 'continue',
214 $revCount
215 ? "$row->ar_rev_id|$row->ar_id"
216 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
218 break;
221 if ( $resultPageSet !== null ) {
222 $generated[] = $row->ar_rev_id;
223 } else {
224 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
225 // Was it converted?
226 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
227 $converted = $pageSet->getConvertedTitles();
228 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
229 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
230 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
231 $pageMap[$row->ar_namespace][$row->ar_title] =
232 $pageMap[$title->getNamespace()][$title->getDBkey()];
236 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
237 ApiBase::dieDebug( "Found row in archive (ar_id={$row->ar_id}) that didn't " .
238 "get processed by ApiPageSet" );
241 $fit = $this->addPageSubItem(
242 $pageMap[$row->ar_namespace][$row->ar_title],
243 $this->extractRevisionInfo( Revision::newFromArchiveRow( $row ), $row ),
244 'rev'
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"
252 break;
257 if ( $resultPageSet !== null ) {
258 $resultPageSet->populateFromRevisionIDs( $generated );
262 public function getAllowedParams() {
263 return parent::getAllowedParams() + array(
264 'start' => array(
265 ApiBase::PARAM_TYPE => 'timestamp',
267 'end' => array(
268 ApiBase::PARAM_TYPE => 'timestamp',
270 'dir' => array(
271 ApiBase::PARAM_TYPE => array(
272 'newer',
273 'older'
275 ApiBase::PARAM_DFLT => 'older',
276 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
278 'tag' => null,
279 'user' => array(
280 ApiBase::PARAM_TYPE => 'user'
282 'excludeuser' => array(
283 ApiBase::PARAM_TYPE => 'user'
285 'continue' => array(
286 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
291 protected function getExamplesMessages() {
292 return array(
293 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
294 'drvprop=user|comment|content'
295 => 'apihelp-query+deletedrevisions-example-titles',
296 'action=query&prop=deletedrevisions&revids=123456'
297 => 'apihelp-query+deletedrevisions-example-revids',
301 public function getHelpUrls() {
302 return 'https://www.mediawiki.org/wiki/API:Properties#deletedrevisions_.2F_drv';