d548c46ce028539ac26d977b3988bf06a5881ba0
[lhc/web/wiklou.git] / includes / api / ApiQueryAllRevisions.php
1 <?php
2 /**
3 * Created on Sep 27, 2015
4 *
5 * Copyright © 2015 Brad Jorsch "bjorsch@wikimedia.org"
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /**
26 * Query module to enumerate all revisions.
27 *
28 * @ingroup API
29 * @since 1.27
30 */
31 class ApiQueryAllRevisions extends ApiQueryRevisionsBase {
32
33 public function __construct( ApiQuery $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'arv' );
35 }
36
37 /**
38 * @param ApiPageSet $resultPageSet
39 * @return void
40 */
41 protected function run( ApiPageSet $resultPageSet = null ) {
42 $db = $this->getDB();
43 $params = $this->extractRequestParams( false );
44
45 $result = $this->getResult();
46
47 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
48
49 // Namespace check is likely to be desired, but can't be done
50 // efficiently in SQL.
51 $miser_ns = null;
52 $needPageTable = false;
53 if ( $params['namespace'] !== null ) {
54 $params['namespace'] = array_unique( $params['namespace'] );
55 sort( $params['namespace'] );
56 if ( $params['namespace'] != MWNamespace::getValidNamespaces() ) {
57 $needPageTable = true;
58 if ( $this->getConfig()->get( 'MiserMode' ) ) {
59 $miser_ns = $params['namespace'];
60 } else {
61 $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
62 }
63 }
64 }
65
66 $this->addTables( 'revision' );
67 if ( $resultPageSet === null ) {
68 $this->parseParameters( $params );
69 $this->addTables( 'page' );
70 $this->addJoinConds(
71 [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ]
72 );
73 $this->addFields( Revision::selectFields() );
74 $this->addFields( Revision::selectPageFields() );
75
76 // Review this depeneding on the outcome of T113901
77 $this->addOption( 'STRAIGHT_JOIN' );
78 } else {
79 $this->limit = $this->getParameter( 'limit' ) ?: 10;
80 $this->addFields( [ 'rev_timestamp', 'rev_id' ] );
81 if ( $params['generatetitles'] ) {
82 $this->addFields( [ 'rev_page' ] );
83 }
84
85 if ( $needPageTable ) {
86 $this->addTables( 'page' );
87 $this->addJoinConds(
88 [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ]
89 );
90 $this->addFieldsIf( [ 'page_namespace' ], (bool)$miser_ns );
91
92 // Review this depeneding on the outcome of T113901
93 $this->addOption( 'STRAIGHT_JOIN' );
94 }
95 }
96
97 $dir = $params['dir'];
98 $this->addTimestampWhereRange( 'rev_timestamp', $dir, $params['start'], $params['end'] );
99
100 if ( $this->fld_tags ) {
101 $this->addTables( 'tag_summary' );
102 $this->addJoinConds(
103 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
104 );
105 $this->addFields( 'ts_tags' );
106 }
107
108 if ( $this->fetchContent ) {
109 $this->addTables( 'text' );
110 $this->addJoinConds(
111 [ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ] ]
112 );
113 $this->addFields( 'old_id' );
114 $this->addFields( Revision::selectTextFields() );
115 }
116
117 if ( $params['user'] !== null ) {
118 $id = User::idFromName( $params['user'] );
119 if ( $id ) {
120 $this->addWhereFld( 'rev_user', $id );
121 } else {
122 $this->addWhereFld( 'rev_user_text', $params['user'] );
123 }
124 } elseif ( $params['excludeuser'] !== null ) {
125 $id = User::idFromName( $params['excludeuser'] );
126 if ( $id ) {
127 $this->addWhere( 'rev_user != ' . $id );
128 } else {
129 $this->addWhere( 'rev_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
130 }
131 }
132
133 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
134 // Paranoia: avoid brute force searches (bug 17342)
135 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
136 $bitmask = Revision::DELETED_USER;
137 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
138 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
139 } else {
140 $bitmask = 0;
141 }
142 if ( $bitmask ) {
143 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
144 }
145 }
146
147 if ( $params['continue'] !== null ) {
148 $op = ( $dir == 'newer' ? '>' : '<' );
149 $cont = explode( '|', $params['continue'] );
150 $this->dieContinueUsageIf( count( $cont ) != 2 );
151 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
152 $rev_id = (int)$cont[1];
153 $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
154 $this->addWhere( "rev_timestamp $op $ts OR " .
155 "(rev_timestamp = $ts AND " .
156 "rev_id $op= $rev_id)" );
157 }
158
159 $this->addOption( 'LIMIT', $this->limit + 1 );
160
161 $sort = ( $dir == 'newer' ? '' : ' DESC' );
162 $orderby = [];
163 // Targeting index rev_timestamp, user_timestamp, or usertext_timestamp
164 // But 'user' is always constant for the latter two, so it doesn't matter here.
165 $orderby[] = "rev_timestamp $sort";
166 $orderby[] = "rev_id $sort";
167 $this->addOption( 'ORDER BY', $orderby );
168
169 $res = $this->select( __METHOD__ );
170 $pageMap = []; // Maps rev_page to array index
171 $count = 0;
172 $nextIndex = 0;
173 $generated = [];
174 foreach ( $res as $row ) {
175 if ( $count === 0 && $resultPageSet !== null ) {
176 // Set the non-continue since the list of all revisions is
177 // prone to having entries added at the start frequently.
178 $this->getContinuationManager()->addGeneratorNonContinueParam(
179 $this, 'continue', "$row->rev_timestamp|$row->rev_id"
180 );
181 }
182 if ( ++$count > $this->limit ) {
183 // We've had enough
184 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
185 break;
186 }
187
188 // Miser mode namespace check
189 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
190 continue;
191 }
192
193 if ( $resultPageSet !== null ) {
194 if ( $params['generatetitles'] ) {
195 $generated[$row->rev_page] = $row->rev_page;
196 } else {
197 $generated[] = $row->rev_id;
198 }
199 } else {
200 $revision = Revision::newFromRow( $row );
201 $rev = $this->extractRevisionInfo( $revision, $row );
202
203 if ( !isset( $pageMap[$row->rev_page] ) ) {
204 $index = $nextIndex++;
205 $pageMap[$row->rev_page] = $index;
206 $title = $revision->getTitle();
207 $a = [
208 'pageid' => $title->getArticleID(),
209 'revisions' => [ $rev ],
210 ];
211 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
212 ApiQueryBase::addTitleInfo( $a, $title );
213 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
214 } else {
215 $index = $pageMap[$row->rev_page];
216 $fit = $result->addValue(
217 [ 'query', $this->getModuleName(), $index, 'revisions' ],
218 null, $rev );
219 }
220 if ( !$fit ) {
221 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
222 break;
223 }
224 }
225 }
226
227 if ( $resultPageSet !== null ) {
228 if ( $params['generatetitles'] ) {
229 $resultPageSet->populateFromPageIDs( $generated );
230 } else {
231 $resultPageSet->populateFromRevisionIDs( $generated );
232 }
233 } else {
234 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
235 }
236 }
237
238 public function getAllowedParams() {
239 $ret = parent::getAllowedParams() + [
240 'user' => [
241 ApiBase::PARAM_TYPE => 'user',
242 ],
243 'namespace' => [
244 ApiBase::PARAM_ISMULTI => true,
245 ApiBase::PARAM_TYPE => 'namespace',
246 ApiBase::PARAM_DFLT => null,
247 ],
248 'start' => [
249 ApiBase::PARAM_TYPE => 'timestamp',
250 ],
251 'end' => [
252 ApiBase::PARAM_TYPE => 'timestamp',
253 ],
254 'dir' => [
255 ApiBase::PARAM_TYPE => [
256 'newer',
257 'older'
258 ],
259 ApiBase::PARAM_DFLT => 'older',
260 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
261 ],
262 'excludeuser' => [
263 ApiBase::PARAM_TYPE => 'user',
264 ],
265 'continue' => [
266 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
267 ],
268 'generatetitles' => [
269 ApiBase::PARAM_DFLT => false,
270 ],
271 ];
272
273 if ( $this->getConfig()->get( 'MiserMode' ) ) {
274 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
275 'api-help-param-limited-in-miser-mode',
276 ];
277 }
278
279 return $ret;
280 }
281
282 protected function getExamplesMessages() {
283 return [
284 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
285 => 'apihelp-query+allrevisions-example-user',
286 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
287 => 'apihelp-query+allrevisions-example-ns-main',
288 ];
289 }
290
291 public function getHelpUrls() {
292 return 'https://www.mediawiki.org/wiki/API:Allrevisions';
293 }
294 }