3 * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
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.
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.
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
23 namespace MediaWiki\Specials
;
26 use MediaWiki\Content\IContentHandlerFactory
;
27 use MediaWiki\HTMLForm\HTMLForm
;
28 use MediaWiki\Revision\RevisionLookup
;
29 use MediaWiki\Revision\RevisionRecord
;
30 use MediaWiki\Revision\SlotRecord
;
31 use MediaWiki\SpecialPage\SpecialPage
;
32 use MediaWiki\Title\Title
;
35 * Implements Special:ComparePages
37 * @ingroup SpecialPage
39 class SpecialComparePages
extends SpecialPage
{
41 private RevisionLookup
$revisionLookup;
42 private IContentHandlerFactory
$contentHandlerFactory;
44 /** @var DifferenceEngine */
45 private $differenceEngine;
48 * @param RevisionLookup $revisionLookup
49 * @param IContentHandlerFactory $contentHandlerFactory
51 public function __construct(
52 RevisionLookup
$revisionLookup,
53 IContentHandlerFactory
$contentHandlerFactory
55 parent
::__construct( 'ComparePages' );
56 $this->revisionLookup
= $revisionLookup;
57 $this->contentHandlerFactory
= $contentHandlerFactory;
61 * Show a form for filtering namespace and username
63 * @param string|null $par
65 public function execute( $par ) {
67 $this->outputHeader();
68 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
69 $this->addHelpLink( 'Help:Diff' );
71 $form = HTMLForm
::factory( 'ooui', [
76 'label-message' => 'compare-page1',
84 'label-message' => 'compare-rev1',
87 'validation-callback' => [ $this, 'checkExistingRevision' ],
93 'label-message' => 'compare-page2',
101 'label-message' => 'compare-rev2',
103 'section' => 'page2',
104 'validation-callback' => [ $this, 'checkExistingRevision' ],
114 ], $this->getContext(), 'compare' );
116 $form->setMethod( 'get' )
117 ->setSubmitTextMsg( 'compare-submit' )
118 ->setSubmitCallback( [ $this, 'showDiff' ] )
121 if ( $this->differenceEngine
) {
122 $this->differenceEngine
->showDiffPage( true );
127 * @internal Callback for HTMLForm
129 * @param HTMLForm $form
131 public function showDiff( $data, HTMLForm
$form ) {
132 $rev1 = $this->revOrTitle( $data['Revision1'], $data['Page1'] );
133 $rev2 = $this->revOrTitle( $data['Revision2'], $data['Page2'] );
135 if ( $rev1 && $rev2 ) {
136 // Revision IDs either passed the existence check or were fetched from existing titles.
137 $revisionRecord = $this->revisionLookup
->getRevisionById( $rev1 );
138 $contentModel = $revisionRecord->getSlot(
142 $contentHandler = $this->contentHandlerFactory
->getContentHandler( $contentModel );
143 $this->differenceEngine
= $contentHandler->createDifferenceEngine( $form->getContext(),
147 ( $data['Action'] == 'purge' ),
148 ( $data['Unhide'] == '1' )
153 private function revOrTitle( $revision, $title ) {
156 } elseif ( $title ) {
157 return Title
::newFromText( $title )->getLatestRevID();
164 * @internal Callback for HTMLForm
165 * @param string|null $value
166 * @param array $alldata
167 * @return string|bool
169 public function checkExistingRevision( $value, $alldata ) {
170 if ( $value === '' ||
$value === null ) {
173 $revisionRecord = $this->revisionLookup
->getRevisionById( (int)$value );
174 if ( $revisionRecord === null ) {
175 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
181 protected function getGroupName() {
186 /** @deprecated class alias since 1.41 */
187 class_alias( SpecialComparePages
::class, 'SpecialComparePages' );