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