Merge "Http::getProxy() method to get proxy configuration"
[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 > $this->limit ) {
176 // We've had enough
177 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
178 break;
179 }
180
181 // Miser mode namespace check
182 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
183 continue;
184 }
185
186 if ( $resultPageSet !== null ) {
187 if ( $params['generatetitles'] ) {
188 $generated[$row->rev_page] = $row->rev_page;
189 } else {
190 $generated[] = $row->rev_id;
191 }
192 } else {
193 $revision = Revision::newFromRow( $row );
194 $rev = $this->extractRevisionInfo( $revision, $row );
195
196 if ( !isset( $pageMap[$row->rev_page] ) ) {
197 $index = $nextIndex++;
198 $pageMap[$row->rev_page] = $index;
199 $title = $revision->getTitle();
200 $a = [
201 'pageid' => $title->getArticleID(),
202 'revisions' => [ $rev ],
203 ];
204 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
205 ApiQueryBase::addTitleInfo( $a, $title );
206 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
207 } else {
208 $index = $pageMap[$row->rev_page];
209 $fit = $result->addValue(
210 [ 'query', $this->getModuleName(), $index, 'revisions' ],
211 null, $rev );
212 }
213 if ( !$fit ) {
214 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
215 break;
216 }
217 }
218 }
219
220 if ( $resultPageSet !== null ) {
221 if ( $params['generatetitles'] ) {
222 $resultPageSet->populateFromPageIDs( $generated );
223 } else {
224 $resultPageSet->populateFromRevisionIDs( $generated );
225 }
226 } else {
227 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
228 }
229 }
230
231 public function getAllowedParams() {
232 $ret = parent::getAllowedParams() + [
233 'user' => [
234 ApiBase::PARAM_TYPE => 'user',
235 ],
236 'namespace' => [
237 ApiBase::PARAM_ISMULTI => true,
238 ApiBase::PARAM_TYPE => 'namespace',
239 ApiBase::PARAM_DFLT => null,
240 ],
241 'start' => [
242 ApiBase::PARAM_TYPE => 'timestamp',
243 ],
244 'end' => [
245 ApiBase::PARAM_TYPE => 'timestamp',
246 ],
247 'dir' => [
248 ApiBase::PARAM_TYPE => [
249 'newer',
250 'older'
251 ],
252 ApiBase::PARAM_DFLT => 'older',
253 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
254 ],
255 'excludeuser' => [
256 ApiBase::PARAM_TYPE => 'user',
257 ],
258 'continue' => [
259 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
260 ],
261 'generatetitles' => [
262 ApiBase::PARAM_DFLT => false,
263 ],
264 ];
265
266 if ( $this->getConfig()->get( 'MiserMode' ) ) {
267 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
268 'api-help-param-limited-in-miser-mode',
269 ];
270 }
271
272 return $ret;
273 }
274
275 protected function getExamplesMessages() {
276 return [
277 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
278 => 'apihelp-query+allrevisions-example-user',
279 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
280 => 'apihelp-query+allrevisions-example-ns-main',
281 ];
282 }
283
284 public function getHelpUrls() {
285 return 'https://www.mediawiki.org/wiki/API:Allrevisions';
286 }
287 }