Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.31% |
124 / 187 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ApiQueryAllRevisions | |
66.67% |
124 / 186 |
|
20.00% |
1 / 5 |
107.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
54.78% |
63 / 115 |
|
0.00% |
0 / 1 |
163.57 | |||
| getAllowedParams | |
93.48% |
43 / 46 |
|
0.00% |
0 / 1 |
2.00 | |||
| getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2015 Wikimedia Foundation and contributors |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\ChangeTags\ChangeTagsStore; |
| 12 | use MediaWiki\CommentFormatter\CommentFormatter; |
| 13 | use MediaWiki\Content\IContentHandlerFactory; |
| 14 | use MediaWiki\Content\Renderer\ContentRenderer; |
| 15 | use MediaWiki\Content\Transform\ContentTransformer; |
| 16 | use MediaWiki\MainConfigNames; |
| 17 | use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 18 | use MediaWiki\Parser\ParserFactory; |
| 19 | use MediaWiki\Revision\RevisionRecord; |
| 20 | use MediaWiki\Revision\RevisionStore; |
| 21 | use MediaWiki\Revision\SlotRoleRegistry; |
| 22 | use MediaWiki\Title\NamespaceInfo; |
| 23 | use MediaWiki\Title\Title; |
| 24 | use MediaWiki\User\ActorMigration; |
| 25 | use MediaWiki\User\TempUser\TempUserCreator; |
| 26 | use MediaWiki\User\UserFactory; |
| 27 | use Wikimedia\ParamValidator\ParamValidator; |
| 28 | |
| 29 | /** |
| 30 | * Query module to enumerate all revisions. |
| 31 | * |
| 32 | * @ingroup API |
| 33 | * @since 1.27 |
| 34 | */ |
| 35 | class ApiQueryAllRevisions extends ApiQueryRevisionsBase { |
| 36 | |
| 37 | private RevisionStore $revisionStore; |
| 38 | private ActorMigration $actorMigration; |
| 39 | private NamespaceInfo $namespaceInfo; |
| 40 | private ChangeTagsStore $changeTagsStore; |
| 41 | |
| 42 | public function __construct( |
| 43 | ApiQuery $query, |
| 44 | string $moduleName, |
| 45 | RevisionStore $revisionStore, |
| 46 | IContentHandlerFactory $contentHandlerFactory, |
| 47 | ParserFactory $parserFactory, |
| 48 | SlotRoleRegistry $slotRoleRegistry, |
| 49 | ActorMigration $actorMigration, |
| 50 | NamespaceInfo $namespaceInfo, |
| 51 | ChangeTagsStore $changeTagsStore, |
| 52 | ContentRenderer $contentRenderer, |
| 53 | ContentTransformer $contentTransformer, |
| 54 | CommentFormatter $commentFormatter, |
| 55 | TempUserCreator $tempUserCreator, |
| 56 | UserFactory $userFactory |
| 57 | ) { |
| 58 | parent::__construct( |
| 59 | $query, |
| 60 | $moduleName, |
| 61 | 'arv', |
| 62 | $revisionStore, |
| 63 | $contentHandlerFactory, |
| 64 | $parserFactory, |
| 65 | $slotRoleRegistry, |
| 66 | $contentRenderer, |
| 67 | $contentTransformer, |
| 68 | $commentFormatter, |
| 69 | $tempUserCreator, |
| 70 | $userFactory |
| 71 | ); |
| 72 | $this->revisionStore = $revisionStore; |
| 73 | $this->actorMigration = $actorMigration; |
| 74 | $this->namespaceInfo = $namespaceInfo; |
| 75 | $this->changeTagsStore = $changeTagsStore; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param ApiPageSet|null $resultPageSet |
| 80 | * @return void |
| 81 | */ |
| 82 | protected function run( ?ApiPageSet $resultPageSet = null ) { |
| 83 | $db = $this->getDB(); |
| 84 | $params = $this->extractRequestParams( false ); |
| 85 | |
| 86 | $result = $this->getResult(); |
| 87 | |
| 88 | $this->requireMaxOneParameter( $params, 'user', 'excludeuser' ); |
| 89 | |
| 90 | $tsField = 'rev_timestamp'; |
| 91 | $idField = 'rev_id'; |
| 92 | $pageField = 'rev_page'; |
| 93 | |
| 94 | // Namespace check is likely to be desired, but can't be done |
| 95 | // efficiently in SQL. |
| 96 | $miser_ns = null; |
| 97 | $needPageTable = false; |
| 98 | if ( $params['namespace'] !== null ) { |
| 99 | $params['namespace'] = array_unique( $params['namespace'] ); |
| 100 | sort( $params['namespace'] ); |
| 101 | if ( $params['namespace'] != $this->namespaceInfo->getValidNamespaces() ) { |
| 102 | $needPageTable = true; |
| 103 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 104 | $miser_ns = $params['namespace']; |
| 105 | } else { |
| 106 | $this->addWhere( [ 'page_namespace' => $params['namespace'] ] ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if ( $resultPageSet === null ) { |
| 112 | $this->parseParameters( $params ); |
| 113 | $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db ) |
| 114 | ->joinComment() |
| 115 | ->joinPage(); |
| 116 | $this->getQueryBuilder()->merge( $queryBuilder ); |
| 117 | } else { |
| 118 | $this->limit = $this->getParameter( 'limit' ) ?: 10; |
| 119 | $this->addTables( [ 'revision' ] ); |
| 120 | $this->addFields( [ 'rev_timestamp', 'rev_id' ] ); |
| 121 | |
| 122 | if ( $params['generatetitles'] ) { |
| 123 | $this->addFields( [ 'rev_page' ] ); |
| 124 | } |
| 125 | |
| 126 | if ( $params['user'] !== null || $params['excludeuser'] !== null ) { |
| 127 | $this->getQueryBuilder()->join( 'actor', 'actor_rev_user', 'actor_rev_user.actor_id = rev_actor' ); |
| 128 | } |
| 129 | |
| 130 | if ( $needPageTable ) { |
| 131 | $this->getQueryBuilder()->join( 'page', null, [ "$pageField = page_id" ] ); |
| 132 | if ( (bool)$miser_ns ) { |
| 133 | $this->addFields( [ 'page_namespace' ] ); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Seems to be needed to avoid a planner bug (T113901) |
| 139 | $this->addOption( 'STRAIGHT_JOIN' ); |
| 140 | |
| 141 | $dir = $params['dir']; |
| 142 | $this->addTimestampWhereRange( $tsField, $dir, $params['start'], $params['end'] ); |
| 143 | |
| 144 | if ( $this->fld_tags ) { |
| 145 | $this->addFields( [ |
| 146 | 'ts_tags' => $this->changeTagsStore->makeTagSummarySubquery( 'revision' ) |
| 147 | ] ); |
| 148 | } |
| 149 | |
| 150 | if ( $params['user'] !== null ) { |
| 151 | $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['user'] ); |
| 152 | $this->addWhere( $actorQuery['conds'] ); |
| 153 | } elseif ( $params['excludeuser'] !== null ) { |
| 154 | $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['excludeuser'] ); |
| 155 | $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' ); |
| 156 | } |
| 157 | |
| 158 | if ( $params['user'] !== null || $params['excludeuser'] !== null ) { |
| 159 | // Paranoia: avoid brute force searches (T19342) |
| 160 | if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) { |
| 161 | $bitmask = RevisionRecord::DELETED_USER; |
| 162 | } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
| 163 | $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED; |
| 164 | } else { |
| 165 | $bitmask = 0; |
| 166 | } |
| 167 | if ( $bitmask ) { |
| 168 | $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if ( $params['continue'] !== null ) { |
| 173 | $op = ( $dir == 'newer' ? '>=' : '<=' ); |
| 174 | $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] ); |
| 175 | $this->addWhere( $db->buildComparison( $op, [ |
| 176 | $tsField => $db->timestamp( $cont[0] ), |
| 177 | $idField => $cont[1], |
| 178 | ] ) ); |
| 179 | } |
| 180 | |
| 181 | $this->addOption( 'LIMIT', $this->limit + 1 ); |
| 182 | |
| 183 | $sort = ( $dir == 'newer' ? '' : ' DESC' ); |
| 184 | $orderby = []; |
| 185 | // Targeting index rev_timestamp, user_timestamp, usertext_timestamp, or actor_timestamp. |
| 186 | // But 'user' is always constant for the latter three, so it doesn't matter here. |
| 187 | $orderby[] = "rev_timestamp $sort"; |
| 188 | $orderby[] = "rev_id $sort"; |
| 189 | $this->addOption( 'ORDER BY', $orderby ); |
| 190 | |
| 191 | $hookData = []; |
| 192 | $res = $this->select( __METHOD__, [], $hookData ); |
| 193 | |
| 194 | if ( $resultPageSet === null ) { |
| 195 | $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ ); |
| 196 | } |
| 197 | |
| 198 | $pageMap = []; // Maps rev_page to array index |
| 199 | $count = 0; |
| 200 | $nextIndex = 0; |
| 201 | $generated = []; |
| 202 | foreach ( $res as $row ) { |
| 203 | if ( $count === 0 && $resultPageSet !== null ) { |
| 204 | // Set the non-continue since the list of all revisions is |
| 205 | // prone to having entries added at the start frequently. |
| 206 | $this->getContinuationManager()->addGeneratorNonContinueParam( |
| 207 | $this, 'continue', "$row->rev_timestamp|$row->rev_id" |
| 208 | ); |
| 209 | } |
| 210 | if ( ++$count > $this->limit ) { |
| 211 | // We've had enough |
| 212 | $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" ); |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | // Miser mode namespace check |
| 217 | if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if ( $resultPageSet !== null ) { |
| 222 | if ( $params['generatetitles'] ) { |
| 223 | $generated[$row->rev_page] = $row->rev_page; |
| 224 | } else { |
| 225 | $generated[] = $row->rev_id; |
| 226 | } |
| 227 | } else { |
| 228 | $revision = $this->revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) ); |
| 229 | $rev = $this->extractRevisionInfo( $revision, $row ); |
| 230 | |
| 231 | if ( !isset( $pageMap[$row->rev_page] ) ) { |
| 232 | $index = $nextIndex++; |
| 233 | $pageMap[$row->rev_page] = $index; |
| 234 | $title = Title::newFromPageIdentity( $revision->getPage() ); |
| 235 | $a = [ |
| 236 | 'pageid' => $title->getArticleID(), |
| 237 | 'revisions' => [ $rev ], |
| 238 | ]; |
| 239 | ApiResult::setIndexedTagName( $a['revisions'], 'rev' ); |
| 240 | ApiQueryBase::addTitleInfo( $a, $title ); |
| 241 | $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) && |
| 242 | $result->addValue( [ 'query', $this->getModuleName() ], $index, $a ); |
| 243 | } else { |
| 244 | $index = $pageMap[$row->rev_page]; |
| 245 | $fit = $this->processRow( $row, $rev, $hookData ) && |
| 246 | $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev ); |
| 247 | } |
| 248 | if ( !$fit ) { |
| 249 | $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" ); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if ( $resultPageSet !== null ) { |
| 256 | if ( $params['generatetitles'] ) { |
| 257 | $resultPageSet->populateFromPageIDs( $generated ); |
| 258 | } else { |
| 259 | $resultPageSet->populateFromRevisionIDs( $generated ); |
| 260 | } |
| 261 | } else { |
| 262 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** @inheritDoc */ |
| 267 | public function getAllowedParams() { |
| 268 | $ret = parent::getAllowedParams() + [ |
| 269 | 'user' => [ |
| 270 | ParamValidator::PARAM_TYPE => 'user', |
| 271 | UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ], |
| 272 | UserDef::PARAM_RETURN_OBJECT => true, |
| 273 | ], |
| 274 | 'namespace' => [ |
| 275 | ParamValidator::PARAM_ISMULTI => true, |
| 276 | ParamValidator::PARAM_TYPE => 'namespace', |
| 277 | ParamValidator::PARAM_DEFAULT => null, |
| 278 | ], |
| 279 | 'start' => [ |
| 280 | ParamValidator::PARAM_TYPE => 'timestamp', |
| 281 | ], |
| 282 | 'end' => [ |
| 283 | ParamValidator::PARAM_TYPE => 'timestamp', |
| 284 | ], |
| 285 | 'dir' => [ |
| 286 | ParamValidator::PARAM_TYPE => [ |
| 287 | 'newer', |
| 288 | 'older' |
| 289 | ], |
| 290 | ParamValidator::PARAM_DEFAULT => 'older', |
| 291 | ApiBase::PARAM_HELP_MSG => 'api-help-param-direction', |
| 292 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [ |
| 293 | 'newer' => 'api-help-paramvalue-direction-newer', |
| 294 | 'older' => 'api-help-paramvalue-direction-older', |
| 295 | ], |
| 296 | ], |
| 297 | 'excludeuser' => [ |
| 298 | ParamValidator::PARAM_TYPE => 'user', |
| 299 | UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ], |
| 300 | UserDef::PARAM_RETURN_OBJECT => true, |
| 301 | ], |
| 302 | 'continue' => [ |
| 303 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 304 | ], |
| 305 | 'generatetitles' => [ |
| 306 | ParamValidator::PARAM_DEFAULT => false, |
| 307 | ], |
| 308 | ]; |
| 309 | |
| 310 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 311 | $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [ |
| 312 | 'api-help-param-limited-in-miser-mode', |
| 313 | ]; |
| 314 | } |
| 315 | |
| 316 | return $ret; |
| 317 | } |
| 318 | |
| 319 | /** @inheritDoc */ |
| 320 | protected function getExamplesMessages() { |
| 321 | return [ |
| 322 | 'action=query&list=allrevisions&arvuser=Example&arvlimit=50' |
| 323 | => 'apihelp-query+allrevisions-example-user', |
| 324 | 'action=query&list=allrevisions&arvdir=newer&arvlimit=50' |
| 325 | => 'apihelp-query+allrevisions-example-ns-any', |
| 326 | ]; |
| 327 | } |
| 328 | |
| 329 | /** @inheritDoc */ |
| 330 | public function getHelpUrls() { |
| 331 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions'; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** @deprecated class alias since 1.43 */ |
| 336 | class_alias( ApiQueryAllRevisions::class, 'ApiQueryAllRevisions' ); |