Merge "Add attributes parameter to ShowSearchHitTitle"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllRevisions.php
1 <?php
2 /**
3 * Created on Sep 27, 2015
4 *
5 * Copyright © 2015 Wikimedia Foundation and contributors
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 if ( $resultPageSet === null ) {
67 $this->parseParameters( $params );
68 $revQuery = Revision::getQueryInfo(
69 $this->fetchContent ? [ 'page', 'text' ] : [ 'page' ]
70 );
71 $this->addTables( $revQuery['tables'] );
72 $this->addFields( $revQuery['fields'] );
73 $this->addJoinConds( $revQuery['joins'] );
74
75 // Review this depeneding on the outcome of T113901
76 $this->addOption( 'STRAIGHT_JOIN' );
77 } else {
78 $this->limit = $this->getParameter( 'limit' ) ?: 10;
79 $this->addTables( 'revision' );
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 ( $params['user'] !== null ) {
109 $id = User::idFromName( $params['user'] );
110 if ( $id ) {
111 $this->addWhereFld( 'rev_user', $id );
112 } else {
113 $this->addWhereFld( 'rev_user_text', $params['user'] );
114 }
115 } elseif ( $params['excludeuser'] !== null ) {
116 $id = User::idFromName( $params['excludeuser'] );
117 if ( $id ) {
118 $this->addWhere( 'rev_user != ' . $id );
119 } else {
120 $this->addWhere( 'rev_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
121 }
122 }
123
124 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
125 // Paranoia: avoid brute force searches (T19342)
126 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
127 $bitmask = Revision::DELETED_USER;
128 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
129 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
130 } else {
131 $bitmask = 0;
132 }
133 if ( $bitmask ) {
134 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
135 }
136 }
137
138 if ( $params['continue'] !== null ) {
139 $op = ( $dir == 'newer' ? '>' : '<' );
140 $cont = explode( '|', $params['continue'] );
141 $this->dieContinueUsageIf( count( $cont ) != 2 );
142 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
143 $rev_id = (int)$cont[1];
144 $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
145 $this->addWhere( "rev_timestamp $op $ts OR " .
146 "(rev_timestamp = $ts AND " .
147 "rev_id $op= $rev_id)" );
148 }
149
150 $this->addOption( 'LIMIT', $this->limit + 1 );
151
152 $sort = ( $dir == 'newer' ? '' : ' DESC' );
153 $orderby = [];
154 // Targeting index rev_timestamp, user_timestamp, or usertext_timestamp
155 // But 'user' is always constant for the latter two, so it doesn't matter here.
156 $orderby[] = "rev_timestamp $sort";
157 $orderby[] = "rev_id $sort";
158 $this->addOption( 'ORDER BY', $orderby );
159
160 $hookData = [];
161 $res = $this->select( __METHOD__, [], $hookData );
162 $pageMap = []; // Maps rev_page to array index
163 $count = 0;
164 $nextIndex = 0;
165 $generated = [];
166 foreach ( $res as $row ) {
167 if ( $count === 0 && $resultPageSet !== null ) {
168 // Set the non-continue since the list of all revisions is
169 // prone to having entries added at the start frequently.
170 $this->getContinuationManager()->addGeneratorNonContinueParam(
171 $this, 'continue', "$row->rev_timestamp|$row->rev_id"
172 );
173 }
174 if ( ++$count > $this->limit ) {
175 // We've had enough
176 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
177 break;
178 }
179
180 // Miser mode namespace check
181 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
182 continue;
183 }
184
185 if ( $resultPageSet !== null ) {
186 if ( $params['generatetitles'] ) {
187 $generated[$row->rev_page] = $row->rev_page;
188 } else {
189 $generated[] = $row->rev_id;
190 }
191 } else {
192 $revision = Revision::newFromRow( $row );
193 $rev = $this->extractRevisionInfo( $revision, $row );
194
195 if ( !isset( $pageMap[$row->rev_page] ) ) {
196 $index = $nextIndex++;
197 $pageMap[$row->rev_page] = $index;
198 $title = $revision->getTitle();
199 $a = [
200 'pageid' => $title->getArticleID(),
201 'revisions' => [ $rev ],
202 ];
203 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
204 ApiQueryBase::addTitleInfo( $a, $title );
205 $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
206 $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
207 } else {
208 $index = $pageMap[$row->rev_page];
209 $fit = $this->processRow( $row, $rev, $hookData ) &&
210 $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
211 }
212 if ( !$fit ) {
213 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
214 break;
215 }
216 }
217 }
218
219 if ( $resultPageSet !== null ) {
220 if ( $params['generatetitles'] ) {
221 $resultPageSet->populateFromPageIDs( $generated );
222 } else {
223 $resultPageSet->populateFromRevisionIDs( $generated );
224 }
225 } else {
226 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
227 }
228 }
229
230 public function getAllowedParams() {
231 $ret = parent::getAllowedParams() + [
232 'user' => [
233 ApiBase::PARAM_TYPE => 'user',
234 ],
235 'namespace' => [
236 ApiBase::PARAM_ISMULTI => true,
237 ApiBase::PARAM_TYPE => 'namespace',
238 ApiBase::PARAM_DFLT => null,
239 ],
240 'start' => [
241 ApiBase::PARAM_TYPE => 'timestamp',
242 ],
243 'end' => [
244 ApiBase::PARAM_TYPE => 'timestamp',
245 ],
246 'dir' => [
247 ApiBase::PARAM_TYPE => [
248 'newer',
249 'older'
250 ],
251 ApiBase::PARAM_DFLT => 'older',
252 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
253 ],
254 'excludeuser' => [
255 ApiBase::PARAM_TYPE => 'user',
256 ],
257 'continue' => [
258 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
259 ],
260 'generatetitles' => [
261 ApiBase::PARAM_DFLT => false,
262 ],
263 ];
264
265 if ( $this->getConfig()->get( 'MiserMode' ) ) {
266 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
267 'api-help-param-limited-in-miser-mode',
268 ];
269 }
270
271 return $ret;
272 }
273
274 protected function getExamplesMessages() {
275 return [
276 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
277 => 'apihelp-query+allrevisions-example-user',
278 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
279 => 'apihelp-query+allrevisions-example-ns-main',
280 ];
281 }
282
283 public function getHelpUrls() {
284 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';
285 }
286 }