File: ArchivedRevisionLookup.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (232 lines) | stat: -rw-r--r-- 7,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Revision;

use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\SelectQueryBuilder;

/**
 * @since 1.38
 */
class ArchivedRevisionLookup {

	/** @var IConnectionProvider */
	private $dbProvider;

	/** @var RevisionStore */
	private $revisionStore;

	/**
	 * @param IConnectionProvider $dbProvider
	 * @param RevisionStore $revisionStore
	 */
	public function __construct(
		IConnectionProvider $dbProvider,
		RevisionStore $revisionStore
	) {
		$this->dbProvider = $dbProvider;
		$this->revisionStore = $revisionStore;
	}

	/**
	 * List the revisions of the given page. Returns result wrapper with
	 * various archive table fields.
	 *
	 * @param PageIdentity $page
	 * @param array $extraConds Extra conditions to be added to the query
	 * @param ?int $limit The limit to be applied to the query, or null for no limit
	 * @return IResultWrapper
	 */
	public function listRevisions( PageIdentity $page, array $extraConds = [], ?int $limit = null ) {
		$queryBuilder = $this->revisionStore->newArchiveSelectQueryBuilder( $this->dbProvider->getReplicaDatabase() )
			->joinComment()
			->where( $extraConds )
			->andWhere( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] );

		// NOTE: ordering by ar_timestamp and ar_id, to remove ambiguity.
		// XXX: Ideally, we would be ordering by ar_timestamp and ar_rev_id, but since we
		// don't have an index on ar_rev_id, that causes a file sort.
		$queryBuilder->orderBy( [ 'ar_timestamp', 'ar_id' ], SelectQueryBuilder::SORT_DESC );
		if ( $limit !== null ) {
			$queryBuilder->limit( $limit );
		}

		MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQueryBuilder( $queryBuilder, 'archive' );

		return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
	}

	/**
	 * Return a RevisionRecord object containing data for the deleted revision.
	 *
	 * @internal only for use in SpecialUndelete
	 *
	 * @param PageIdentity $page
	 * @param string $timestamp
	 * @return RevisionRecord|null
	 */
	public function getRevisionRecordByTimestamp( PageIdentity $page, $timestamp ): ?RevisionRecord {
		return $this->getRevisionByConditions(
			$page,
			[ 'ar_timestamp' => $this->dbProvider->getReplicaDatabase()->timestamp( $timestamp ) ]
		);
	}

	/**
	 * Return the archived revision with the given ID.
	 *
	 * @param PageIdentity|null $page
	 * @param int $revId
	 * @return RevisionRecord|null
	 */
	public function getArchivedRevisionRecord( ?PageIdentity $page, int $revId ): ?RevisionRecord {
		return $this->getRevisionByConditions( $page, [ 'ar_rev_id' => $revId ] );
	}

	/**
	 * @param PageIdentity|null $page
	 * @param array $conditions
	 * @param array $options
	 *
	 * @return RevisionRecord|null
	 */
	private function getRevisionByConditions(
		?PageIdentity $page,
		array $conditions,
		array $options = []
	): ?RevisionRecord {
		$queryBuilder = $this->revisionStore->newArchiveSelectQueryBuilder( $this->dbProvider->getReplicaDatabase() )
			->joinComment()
			->where( $conditions )
			->options( $options );

		if ( $page ) {
			$queryBuilder->andWhere( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] );
		}

		$row = $queryBuilder->caller( __METHOD__ )->fetchRow();

		if ( $row ) {
			return $this->revisionStore->newRevisionFromArchiveRow( $row, 0, $page );
		}

		return null;
	}

	/**
	 * Return the most-previous revision, either live or deleted, against
	 * the deleted revision given by timestamp.
	 *
	 * May produce unexpected results in case of history merges or other
	 * unusual time issues.
	 *
	 * @param PageIdentity $page
	 * @param string $timestamp
	 * @return RevisionRecord|null Null when there is no previous revision
	 */
	public function getPreviousRevisionRecord( PageIdentity $page, string $timestamp ): ?RevisionRecord {
		$dbr = $this->dbProvider->getReplicaDatabase();

		// Check the previous deleted revision...
		$row = $dbr->newSelectQueryBuilder()
			->select( [ 'ar_rev_id', 'ar_timestamp' ] )
			->from( 'archive' )
			->where( [
				'ar_namespace' => $page->getNamespace(),
				'ar_title' => $page->getDBkey(),
				$dbr->expr( 'ar_timestamp', '<', $dbr->timestamp( $timestamp ) ),
			] )
			->orderBy( 'ar_timestamp DESC' )
			->caller( __METHOD__ )->fetchRow();
		$prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
		$prevDeletedId = $row ? intval( $row->ar_rev_id ) : null;

		$row = $dbr->newSelectQueryBuilder()
			->select( [ 'rev_id', 'rev_timestamp' ] )
			->from( 'page' )
			->join( 'revision', null, 'page_id = rev_page' )
			->where( [
				'page_namespace' => $page->getNamespace(),
				'page_title' => $page->getDBkey(),
				$dbr->expr( 'rev_timestamp', '<', $dbr->timestamp( $timestamp ) )
			] )
			->orderBy( 'rev_timestamp DESC' )
			->caller( __METHOD__ )->fetchRow();
		$prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
		$prevLiveId = $row ? intval( $row->rev_id ) : null;

		if ( $prevLive && $prevLive > $prevDeleted ) {
			// Most prior revision was live
			$rec = $this->revisionStore->getRevisionById( $prevLiveId );
		} elseif ( $prevDeleted ) {
			// Most prior revision was deleted
			$rec = $this->getArchivedRevisionRecord( $page, $prevDeletedId );
		} else {
			$rec = null;
		}

		return $rec;
	}

	/**
	 * Returns the ID of the latest deleted revision.
	 *
	 * @param PageIdentity $page
	 *
	 * @return int|false The revision's ID, or false if there is no deleted revision.
	 */
	public function getLastRevisionId( PageIdentity $page ) {
		$revId = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
			->select( 'ar_rev_id' )
			->from( 'archive' )
			->where( [ 'ar_namespace' => $page->getNamespace(), 'ar_title' => $page->getDBkey() ] )
			->orderBy( [ 'ar_timestamp', 'ar_id' ], SelectQueryBuilder::SORT_DESC )
			->caller( __METHOD__ )->fetchField();

		return $revId ? intval( $revId ) : false;
	}

	/**
	 * Quick check if any archived revisions are present for the page.
	 * This says nothing about whether the page currently exists in the page table or not.
	 *
	 * @param PageIdentity $page
	 *
	 * @return bool
	 */
	public function hasArchivedRevisions( PageIdentity $page ): bool {
		$row = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
			->select( '1' ) // We don't care about the value. Allow the database to optimize.
			->from( 'archive' )
			->where( [
				'ar_namespace' => $page->getNamespace(),
				'ar_title' => $page->getDBkey()
			] )
			->caller( __METHOD__ )
			->fetchRow();

		return (bool)$row;
	}

}