Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
79 / 79 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| ArchivedRevisionLookup | |
100.00% |
79 / 79 |
|
100.00% |
9 / 9 |
20 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| listArchivedRevisions | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| listRevisions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getRevisionRecordByTimestamp | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getArchivedRevisionRecord | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRevisionByConditions | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| getPreviousRevisionRecord | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
8 | |||
| getLastRevisionId | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| hasArchivedRevisions | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Revision; |
| 8 | |
| 9 | use MediaWiki\MediaWikiServices; |
| 10 | use MediaWiki\Page\PageIdentity; |
| 11 | use MediaWiki\Permissions\Authority; |
| 12 | use MediaWiki\Permissions\SimpleAuthority; |
| 13 | use MediaWiki\User\UserIdentityValue; |
| 14 | use Wikimedia\Rdbms\IConnectionProvider; |
| 15 | use Wikimedia\Rdbms\IResultWrapper; |
| 16 | use Wikimedia\Rdbms\SelectQueryBuilder; |
| 17 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 18 | |
| 19 | /** |
| 20 | * @since 1.38 |
| 21 | */ |
| 22 | class ArchivedRevisionLookup { |
| 23 | |
| 24 | /** @var IConnectionProvider */ |
| 25 | private $dbProvider; |
| 26 | |
| 27 | /** @var RevisionStore */ |
| 28 | private $revisionStore; |
| 29 | |
| 30 | public function __construct( |
| 31 | IConnectionProvider $dbProvider, |
| 32 | RevisionStore $revisionStore |
| 33 | ) { |
| 34 | $this->dbProvider = $dbProvider; |
| 35 | $this->revisionStore = $revisionStore; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * List the revisions of the given page. Returns result wrapper with |
| 40 | * various archive table fields. |
| 41 | * |
| 42 | * @since 1.47 |
| 43 | * |
| 44 | * @param PageIdentity $page |
| 45 | * @param Authority $performer Viewer, for restricted-tag access checks |
| 46 | * @param array $extraConds Extra conditions to be added to the query |
| 47 | * @param ?int $limit The limit to be applied to the query, or null for no limit |
| 48 | * @return IResultWrapper |
| 49 | */ |
| 50 | public function listArchivedRevisions( |
| 51 | PageIdentity $page, |
| 52 | Authority $performer, |
| 53 | array $extraConds = [], |
| 54 | ?int $limit = null |
| 55 | ): IResultWrapper { |
| 56 | $queryBuilder = $this->revisionStore->newArchiveSelectQueryBuilder( $this->dbProvider->getReplicaDatabase() ) |
| 57 | ->joinComment() |
| 58 | ->where( $extraConds ) |
| 59 | ->andWhere( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] ); |
| 60 | |
| 61 | // NOTE: ordering by ar_timestamp and ar_id, to remove ambiguity. |
| 62 | // XXX: Ideally, we would be ordering by ar_timestamp and ar_rev_id, but since we |
| 63 | // don't have an index on ar_rev_id, that causes a file sort. |
| 64 | $queryBuilder->orderBy( [ 'ar_timestamp', 'ar_id' ], SelectQueryBuilder::SORT_DESC ); |
| 65 | if ( $limit !== null ) { |
| 66 | $queryBuilder->limit( $limit ); |
| 67 | } |
| 68 | |
| 69 | MediaWikiServices::getInstance()->getChangeTagsStore() |
| 70 | ->addTagsToDisplayQuery( $queryBuilder, 'archive', $performer ); |
| 71 | |
| 72 | return $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * List the revisions of the given page. Returns result wrapper with |
| 77 | * various archive table fields. |
| 78 | * |
| 79 | * @deprecated since 1.47, use listArchivedRevisions() instead, passing an Authority |
| 80 | * |
| 81 | * @param PageIdentity $page |
| 82 | * @param array $extraConds Extra conditions to be added to the query |
| 83 | * @param ?int $limit The limit to be applied to the query, or null for no limit |
| 84 | * @param Authority|null $performer Viewer, for restricted-tag access checks. Null hides all |
| 85 | * restricted tags. |
| 86 | * @return IResultWrapper |
| 87 | */ |
| 88 | public function listRevisions( |
| 89 | PageIdentity $page, |
| 90 | array $extraConds = [], |
| 91 | ?int $limit = null, |
| 92 | ?Authority $performer = null |
| 93 | ): IResultWrapper { |
| 94 | wfDeprecated( __METHOD__, '1.47' ); |
| 95 | |
| 96 | $viewer = $performer ?? new SimpleAuthority( UserIdentityValue::newAnonymous( '127.0.0.1' ), [] ); |
| 97 | |
| 98 | return $this->listArchivedRevisions( $page, $viewer, $extraConds, $limit ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return a RevisionRecord object containing data for the deleted revision. |
| 103 | * |
| 104 | * @internal only for use in SpecialUndelete |
| 105 | * |
| 106 | * @param PageIdentity $page |
| 107 | * @param string $timestamp |
| 108 | * @return RevisionRecord|null |
| 109 | */ |
| 110 | public function getRevisionRecordByTimestamp( PageIdentity $page, $timestamp ): ?RevisionRecord { |
| 111 | return $this->getRevisionByConditions( |
| 112 | $page, |
| 113 | [ 'ar_timestamp' => $this->dbProvider->getReplicaDatabase()->timestamp( $timestamp ) ] |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return the archived revision with the given ID. |
| 119 | * |
| 120 | * @param PageIdentity|null $page |
| 121 | * @param int $revId |
| 122 | * @return RevisionRecord|null |
| 123 | */ |
| 124 | public function getArchivedRevisionRecord( ?PageIdentity $page, int $revId ): ?RevisionRecord { |
| 125 | return $this->getRevisionByConditions( $page, [ 'ar_rev_id' => $revId ] ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param PageIdentity|null $page |
| 130 | * @param array $conditions |
| 131 | * @param array $options |
| 132 | * |
| 133 | * @return RevisionRecord|null |
| 134 | */ |
| 135 | private function getRevisionByConditions( |
| 136 | ?PageIdentity $page, |
| 137 | array $conditions, |
| 138 | array $options = [] |
| 139 | ): ?RevisionRecord { |
| 140 | $queryBuilder = $this->revisionStore->newArchiveSelectQueryBuilder( $this->dbProvider->getReplicaDatabase() ) |
| 141 | ->joinComment() |
| 142 | ->where( $conditions ) |
| 143 | ->options( $options ); |
| 144 | |
| 145 | if ( $page ) { |
| 146 | $queryBuilder->andWhere( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] ); |
| 147 | } |
| 148 | |
| 149 | $row = $queryBuilder->caller( __METHOD__ )->fetchRow(); |
| 150 | |
| 151 | if ( $row ) { |
| 152 | return $this->revisionStore->newRevisionFromArchiveRow( $row, 0, $page ); |
| 153 | } |
| 154 | |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Return the most-previous revision, either live or deleted, against |
| 160 | * the deleted revision given by timestamp. |
| 161 | * |
| 162 | * May produce unexpected results in case of history merges or other |
| 163 | * unusual time issues. |
| 164 | * |
| 165 | * @param PageIdentity $page |
| 166 | * @param string $timestamp |
| 167 | * @return RevisionRecord|null Null when there is no previous revision |
| 168 | */ |
| 169 | public function getPreviousRevisionRecord( PageIdentity $page, string $timestamp ): ?RevisionRecord { |
| 170 | $dbr = $this->dbProvider->getReplicaDatabase(); |
| 171 | |
| 172 | // Check the previous deleted revision... |
| 173 | $row = $dbr->newSelectQueryBuilder() |
| 174 | ->select( [ 'ar_rev_id', 'ar_timestamp' ] ) |
| 175 | ->from( 'archive' ) |
| 176 | ->where( [ |
| 177 | 'ar_namespace' => $page->getNamespace(), |
| 178 | 'ar_title' => $page->getDBkey(), |
| 179 | $dbr->expr( 'ar_timestamp', '<', $dbr->timestamp( $timestamp ) ), |
| 180 | ] ) |
| 181 | ->orderBy( 'ar_timestamp DESC' ) |
| 182 | ->caller( __METHOD__ )->fetchRow(); |
| 183 | $prevDeleted = $row ? wfTimestamp( TS::MW, $row->ar_timestamp ) : false; |
| 184 | $prevDeletedId = $row ? intval( $row->ar_rev_id ) : null; |
| 185 | |
| 186 | $row = $dbr->newSelectQueryBuilder() |
| 187 | ->select( [ 'rev_id', 'rev_timestamp' ] ) |
| 188 | ->from( 'page' ) |
| 189 | ->join( 'revision', null, 'page_id = rev_page' ) |
| 190 | ->where( [ |
| 191 | 'page_namespace' => $page->getNamespace(), |
| 192 | 'page_title' => $page->getDBkey(), |
| 193 | $dbr->expr( 'rev_timestamp', '<', $dbr->timestamp( $timestamp ) ) |
| 194 | ] ) |
| 195 | ->orderBy( 'rev_timestamp DESC' ) |
| 196 | ->caller( __METHOD__ )->fetchRow(); |
| 197 | $prevLive = $row ? wfTimestamp( TS::MW, $row->rev_timestamp ) : false; |
| 198 | $prevLiveId = $row ? intval( $row->rev_id ) : null; |
| 199 | |
| 200 | if ( $prevLive && $prevLive > $prevDeleted ) { |
| 201 | // Most prior revision was live |
| 202 | $rec = $this->revisionStore->getRevisionById( $prevLiveId ); |
| 203 | } elseif ( $prevDeleted ) { |
| 204 | // Most prior revision was deleted |
| 205 | $rec = $this->getArchivedRevisionRecord( $page, $prevDeletedId ); |
| 206 | } else { |
| 207 | $rec = null; |
| 208 | } |
| 209 | |
| 210 | return $rec; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns the ID of the latest deleted revision. |
| 215 | * |
| 216 | * @param PageIdentity $page |
| 217 | * |
| 218 | * @return int|false The revision's ID, or false if there is no deleted revision. |
| 219 | */ |
| 220 | public function getLastRevisionId( PageIdentity $page ) { |
| 221 | $revId = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 222 | ->select( 'ar_rev_id' ) |
| 223 | ->from( 'archive' ) |
| 224 | ->where( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] ) |
| 225 | ->orderBy( [ 'ar_timestamp', 'ar_id' ], SelectQueryBuilder::SORT_DESC ) |
| 226 | ->caller( __METHOD__ )->fetchField(); |
| 227 | |
| 228 | return $revId ? intval( $revId ) : false; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Quick check if any archived revisions are present for the page. |
| 233 | * This says nothing about whether the page currently exists in the page table or not. |
| 234 | * |
| 235 | * @param PageIdentity $page |
| 236 | * |
| 237 | * @return bool |
| 238 | */ |
| 239 | public function hasArchivedRevisions( PageIdentity $page ): bool { |
| 240 | $row = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder() |
| 241 | ->select( '1' ) // We don't care about the value. Allow the database to optimize. |
| 242 | ->from( 'archive' ) |
| 243 | ->where( [ |
| 244 | 'ar_namespace' => $page->getNamespace(), |
| 245 | 'ar_title' => $page->getDBkey() |
| 246 | ] ) |
| 247 | ->caller( __METHOD__ ) |
| 248 | ->fetchRow(); |
| 249 | |
| 250 | return (bool)$row; |
| 251 | } |
| 252 | |
| 253 | } |