MediaWiki  master
SpecialComparePages.php
Go to the documentation of this file.
1 <?php
30 
37 
39  private $revisionLookup;
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 ) {
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 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
SpecialComparePages\showDiff
showDiff( $data, HTMLForm $form)
Definition: SpecialComparePages.php:131
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
SpecialComparePages\__construct
__construct(RevisionLookup $revisionLookup, IContentHandlerFactory $contentHandlerFactory)
Definition: SpecialComparePages.php:48
SpecialComparePages\checkExistingRevision
checkExistingRevision( $value, $alldata)
Definition: SpecialComparePages.php:175
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:948
SpecialComparePages\$revisionLookup
RevisionLookup $revisionLookup
Definition: SpecialComparePages.php:39
SpecialComparePages
Implements Special:ComparePages.
Definition: SpecialComparePages.php:36
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialComparePages\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: SpecialComparePages.php:42
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
SpecialComparePages\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialComparePages.php:187
Title
Represents a title within MediaWiki.
Definition: Title.php:47
SpecialComparePages\execute
execute( $par)
Show a form for filtering namespace and username.
Definition: SpecialComparePages.php:62
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:143
SpecialComparePages\revOrTitle
revOrTitle( $revision, $title)
Definition: SpecialComparePages.php:156