1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22use MediaWiki\MediaWikiServices;
23
24/**
25 * @ingroup Pager
26 */
27class MergeHistoryPager extends ReverseChronologicalPager {
28
29	/** @var SpecialMergeHistory */
30	public $mForm;
31
32	/** @var array */
33	public $mConds;
34
35	/** @var int */
36	private $articleID;
37
38	/** @var int */
39	private $maxTimestamp;
40
41	public function __construct( SpecialMergeHistory $form, $conds, Title $source, Title $dest ) {
42		$this->mForm = $form;
43		$this->mConds = $conds;
44		$this->articleID = $source->getArticleID();
45
46		$dbr = wfGetDB( DB_REPLICA );
47		$maxtimestamp = $dbr->selectField(
48			'revision',
49			'MIN(rev_timestamp)',
50			[ 'rev_page' => $dest->getArticleID() ],
51			__METHOD__
52		);
53		$this->maxTimestamp = $maxtimestamp;
54
55		parent::__construct( $form->getContext() );
56	}
57
58	protected function getStartBody() {
59		# Do a link batch query
60		$this->mResult->seek( 0 );
61		$batch = new LinkBatch();
62		# Give some pointers to make (last) links
63		$this->mForm->prevId = [];
64		$rev_id = null;
65		foreach ( $this->mResult as $row ) {
66			$batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
67			$batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
68
69			if ( isset( $rev_id ) ) {
70				if ( $rev_id > $row->rev_id ) {
71					$this->mForm->prevId[$rev_id] = $row->rev_id;
72				} elseif ( $rev_id < $row->rev_id ) {
73					$this->mForm->prevId[$row->rev_id] = $rev_id;
74				}
75			}
76
77			$rev_id = $row->rev_id;
78		}
79
80		$batch->execute();
81		$this->mResult->seek( 0 );
82
83		return '';
84	}
85
86	public function formatRow( $row ) {
87		return $this->mForm->formatRevisionRow( $row );
88	}
89
90	public function getQueryInfo() {
91		$conds = $this->mConds;
92		$conds['rev_page'] = $this->articleID;
93		$conds[] = "rev_timestamp < " . $this->mDb->addQuotes( $this->maxTimestamp );
94
95		// TODO inject a RevisionStore into SpecialMergeHistory and pass it to
96		// the MergeHistoryPager constructor
97		$revQuery = MediaWikiServices::getInstance()
98			->getRevisionStore()
99			->getQueryInfo( [ 'page', 'user' ] );
100		return [
101			'tables' => $revQuery['tables'],
102			'fields' => $revQuery['fields'],
103			'conds' => $conds,
104			'join_conds' => $revQuery['joins']
105		];
106	}
107
108	public function getIndexField() {
109		return 'rev_timestamp';
110	}
111}
112