MediaWiki  master
SpecialComparePages.php
Go to the documentation of this file.
1 <?php
26 namespace MediaWiki\Specials;
27 
29 use HTMLForm;
36 
43 
44  private RevisionLookup $revisionLookup;
45  private IContentHandlerFactory $contentHandlerFactory;
46 
48  private $differenceEngine;
49 
54  public function __construct(
55  RevisionLookup $revisionLookup,
56  IContentHandlerFactory $contentHandlerFactory
57  ) {
58  parent::__construct( 'ComparePages' );
59  $this->revisionLookup = $revisionLookup;
60  $this->contentHandlerFactory = $contentHandlerFactory;
61  }
62 
68  public function execute( $par ) {
69  $this->setHeaders();
70  $this->outputHeader();
71  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
72  $this->addHelpLink( 'Help:Diff' );
73 
74  $form = HTMLForm::factory( 'ooui', [
75  'Page1' => [
76  'type' => 'title',
77  'exists' => true,
78  'name' => 'page1',
79  'label-message' => 'compare-page1',
80  'size' => '40',
81  'section' => 'page1',
82  'required' => false,
83  ],
84  'Revision1' => [
85  'type' => 'int',
86  'name' => 'rev1',
87  'label-message' => 'compare-rev1',
88  'size' => '8',
89  'section' => 'page1',
90  'validation-callback' => [ $this, 'checkExistingRevision' ],
91  ],
92  'Page2' => [
93  'type' => 'title',
94  'name' => 'page2',
95  'exists' => true,
96  'label-message' => 'compare-page2',
97  'size' => '40',
98  'section' => 'page2',
99  'required' => false,
100  ],
101  'Revision2' => [
102  'type' => 'int',
103  'name' => 'rev2',
104  'label-message' => 'compare-rev2',
105  'size' => '8',
106  'section' => 'page2',
107  'validation-callback' => [ $this, 'checkExistingRevision' ],
108  ],
109  'Action' => [
110  'type' => 'hidden',
111  'name' => 'action',
112  ],
113  'Unhide' => [
114  'type' => 'hidden',
115  'name' => 'unhide',
116  ],
117  ], $this->getContext(), 'compare' );
118 
119  $form->setMethod( 'get' )
120  ->setSubmitTextMsg( 'compare-submit' )
121  ->setSubmitCallback( [ $this, 'showDiff' ] )
122  ->show();
123 
124  if ( $this->differenceEngine ) {
125  $this->differenceEngine->showDiffPage( true );
126  }
127  }
128 
134  public function showDiff( $data, HTMLForm $form ) {
135  $rev1 = $this->revOrTitle( $data['Revision1'], $data['Page1'] );
136  $rev2 = $this->revOrTitle( $data['Revision2'], $data['Page2'] );
137 
138  if ( $rev1 && $rev2 ) {
139  // Revision IDs either passed the existence check or were fetched from existing titles.
140  $revisionRecord = $this->revisionLookup->getRevisionById( $rev1 );
141  $contentModel = $revisionRecord->getSlot(
144  )->getModel();
145  $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
146  $this->differenceEngine = $contentHandler->createDifferenceEngine( $form->getContext(),
147  $rev1,
148  $rev2,
149  0, // rcid
150  ( $data['Action'] == 'purge' ),
151  ( $data['Unhide'] == '1' )
152  );
153  }
154  }
155 
156  private function revOrTitle( $revision, $title ) {
157  if ( $revision ) {
158  return $revision;
159  } elseif ( $title ) {
160  return Title::newFromText( $title )->getLatestRevID();
161  }
162 
163  return null;
164  }
165 
172  public function checkExistingRevision( $value, $alldata ) {
173  if ( $value === '' || $value === null ) {
174  return true;
175  }
176  $revisionRecord = $this->revisionLookup->getRevisionById( (int)$value );
177  if ( $revisionRecord === null ) {
178  return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
179  }
180 
181  return true;
182  }
183 
184  protected function getGroupName() {
185  return 'pagetools';
186  }
187 }
188 
192 class_alias( SpecialComparePages::class, 'SpecialComparePages' );
getContext()
Get the base IContextSource object.
DifferenceEngine is responsible for rendering the difference between two revisions as HTML.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:158
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:360
Page revision base class.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Parent class for all special pages.
Definition: SpecialPage.php:65
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
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:76
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:400
Service for looking up page revisions.