MediaWiki master
SpecialComparePages.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Specials;
10
19
26
27 private RevisionLookup $revisionLookup;
28 private IContentHandlerFactory $contentHandlerFactory;
29
31 private $differenceEngine;
32
33 public function __construct(
34 RevisionLookup $revisionLookup,
35 IContentHandlerFactory $contentHandlerFactory
36 ) {
37 parent::__construct( 'ComparePages' );
38 $this->revisionLookup = $revisionLookup;
39 $this->contentHandlerFactory = $contentHandlerFactory;
40 }
41
47 public function execute( $par ) {
48 $this->setHeaders();
49 $this->outputHeader();
50 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
51 $this->addHelpLink( 'Help:Diff' );
52
53 $form = HTMLForm::factory( 'ooui', [
54 'Page1' => [
55 'type' => 'title',
56 'exists' => true,
57 'name' => 'page1',
58 'label-message' => 'compare-page1',
59 'size' => '40',
60 'section' => 'page1',
61 'required' => false,
62 ],
63 'Revision1' => [
64 'type' => 'int',
65 'name' => 'rev1',
66 'label-message' => 'compare-rev1',
67 'size' => '8',
68 'section' => 'page1',
69 'validation-callback' => $this->checkExistingRevision( ... ),
70 ],
71 'Page2' => [
72 'type' => 'title',
73 'name' => 'page2',
74 'exists' => true,
75 'label-message' => 'compare-page2',
76 'size' => '40',
77 'section' => 'page2',
78 'required' => false,
79 ],
80 'Revision2' => [
81 'type' => 'int',
82 'name' => 'rev2',
83 'label-message' => 'compare-rev2',
84 'size' => '8',
85 'section' => 'page2',
86 'validation-callback' => $this->checkExistingRevision( ... ),
87 ],
88 'Action' => [
89 'type' => 'hidden',
90 'name' => 'action',
91 ],
92 'Unhide' => [
93 'type' => 'hidden',
94 'name' => 'unhide',
95 ],
96 ], $this->getContext(), 'compare' );
97
98 $form->setMethod( 'get' )
99 ->setSubmitTextMsg( 'compare-submit' )
100 ->setSubmitCallback( $this->showDiff( ... ) )
101 ->show();
102
103 if ( $this->differenceEngine ) {
104 $this->differenceEngine->showDiffPage( true );
105 }
106 }
107
112 private function showDiff( $data, HTMLForm $form ) {
113 $rev1 = $this->revOrTitle( $data['Revision1'], $data['Page1'] );
114 $rev2 = $this->revOrTitle( $data['Revision2'], $data['Page2'] );
115
116 if ( $rev1 && $rev2 ) {
117 // Revision IDs either passed the existence check or were fetched from existing titles.
118 $revisionRecord = $this->revisionLookup->getRevisionById( $rev1 );
119 $contentModel = $revisionRecord->getSlot(
120 SlotRecord::MAIN,
121 RevisionRecord::RAW
122 )->getModel();
123 $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
124 $this->differenceEngine = $contentHandler->createDifferenceEngine( $form->getContext(),
125 $rev1,
126 $rev2,
127 0, // rcid
128 ( $data['Action'] == 'purge' ),
129 ( $data['Unhide'] == '1' )
130 );
131 }
132 }
133
134 private function revOrTitle( ?string $revision, ?string $title ): ?int {
135 if ( $revision ) {
136 return (int)$revision;
137 } elseif ( $title ) {
138 return Title::newFromText( $title )->getLatestRevID();
139 }
140
141 return null;
142 }
143
149 private function checkExistingRevision( $value, $alldata ) {
150 if ( $value === '' || $value === null ) {
151 return true;
152 }
153 $revisionRecord = $this->revisionLookup->getRevisionById( (int)$value );
154 if ( $revisionRecord === null ) {
155 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
156 }
157
158 return true;
159 }
160
162 protected function getGroupName() {
163 return 'pagetools';
164 }
165}
166
168class_alias( SpecialComparePages::class, 'SpecialComparePages' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
DifferenceEngine is responsible for rendering the difference between two revisions as HTML.
getContext()
Get the base IContextSource object.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
setMethod( $method='post')
Set the method used to submit the form.
Page revision base class.
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getContext()
Gets the context this SpecialPage is executed in.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Implements Special:ComparePages.
execute( $par)
Show a form for filtering namespace and username.
__construct(RevisionLookup $revisionLookup, IContentHandlerFactory $contentHandlerFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Represents a title within MediaWiki.
Definition Title.php:69
Service for looking up page revisions.
msg( $key,... $params)