MediaWiki REL1_37
SpecialComparePages.php
Go to the documentation of this file.
1<?php
30
37
40
43
48 public function __construct(
51 ) {
52 parent::__construct( 'ComparePages' );
53 $this->revisionLookup = $revisionLookup;
54 $this->contentHandlerFactory = $contentHandlerFactory;
55 }
56
62 public function execute( $par ) {
63 $this->setHeaders();
64 $this->outputHeader();
65 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
66 $this->addHelpLink( 'Help:Diff' );
67
68 $form = HTMLForm::factory( 'ooui', [
69 'Page1' => [
70 'type' => 'title',
71 'exists' => true,
72 'name' => 'page1',
73 'label-message' => 'compare-page1',
74 'size' => '40',
75 'section' => 'page1',
76 'required' => false,
77 ],
78 'Revision1' => [
79 'type' => 'int',
80 'name' => 'rev1',
81 'label-message' => 'compare-rev1',
82 'size' => '8',
83 'section' => 'page1',
84 'validation-callback' => [ $this, 'checkExistingRevision' ],
85 ],
86 'Page2' => [
87 'type' => 'title',
88 'name' => 'page2',
89 'exists' => true,
90 'label-message' => 'compare-page2',
91 'size' => '40',
92 'section' => 'page2',
93 'required' => false,
94 ],
95 'Revision2' => [
96 'type' => 'int',
97 'name' => 'rev2',
98 'label-message' => 'compare-rev2',
99 'size' => '8',
100 'section' => 'page2',
101 'validation-callback' => [ $this, 'checkExistingRevision' ],
102 ],
103 'Action' => [
104 'type' => 'hidden',
105 'name' => 'action',
106 ],
107 'Diffonly' => [
108 'type' => 'hidden',
109 'name' => 'diffonly',
110 ],
111 'Unhide' => [
112 'type' => 'hidden',
113 'name' => 'unhide',
114 ],
115 ], $this->getContext(), 'compare' );
116 $form->setSubmitTextMsg( 'compare-submit' );
117 $form->suppressReset();
118 $form->setMethod( 'get' );
119 $form->setSubmitCallback( [ $this, 'showDiff' ] );
120
121 $form->loadData();
122 $form->displayForm( '' );
123 $form->trySubmit();
124 }
125
131 public function showDiff( $data, HTMLForm $form ) {
132 $rev1 = $this->revOrTitle( $data['Revision1'], $data['Page1'] );
133 $rev2 = $this->revOrTitle( $data['Revision2'], $data['Page2'] );
134
135 if ( $rev1 && $rev2 ) {
136 $revisionRecord = $this->revisionLookup->getRevisionById( $rev1 );
137
138 if ( $revisionRecord ) { // NOTE: $rev1 was already checked, should exist.
139 $contentModel = $revisionRecord->getSlot(
140 SlotRecord::MAIN,
141 RevisionRecord::RAW
142 )->getModel();
143 $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
144 $de = $contentHandler->createDifferenceEngine( $form->getContext(),
145 $rev1,
146 $rev2,
147 null, // rcid
148 ( $data['Action'] == 'purge' ),
149 ( $data['Unhide'] == '1' )
150 );
151 $de->showDiffPage( true );
152 }
153 }
154 }
155
156 private function revOrTitle( $revision, $title ) {
157 if ( $revision ) {
158 return $revision;
159 } elseif ( $title ) {
160 $title = Title::newFromText( $title );
161 if ( $title instanceof Title ) {
162 return $title->getLatestRevID();
163 }
164 }
165
166 return null;
167 }
168
175 public function checkExistingRevision( $value, $alldata ) {
176 if ( $value === '' || $value === null ) {
177 return true;
178 }
179 $revisionRecord = $this->revisionLookup->getRevisionById( $value );
180 if ( $revisionRecord === null ) {
181 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
182 }
183
184 return true;
185 }
186
187 protected function getGroupName() {
188 return 'pagetools';
189 }
190}
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:143
Page revision base class.
Value object representing a content slot associated with a page revision.
Implements Special:ComparePages.
IContentHandlerFactory $contentHandlerFactory
showDiff( $data, HTMLForm $form)
RevisionLookup $revisionLookup
execute( $par)
Show a form for filtering namespace and username.
checkExistingRevision( $value, $alldata)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
revOrTitle( $revision, $title)
__construct(RevisionLookup $revisionLookup, IContentHandlerFactory $contentHandlerFactory)
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Represents a title within MediaWiki.
Definition Title.php:48
Service for looking up page revisions.