MediaWiki  master
SpecialComparePages.php
Go to the documentation of this file.
1 <?php
30 
37 
39  private $revisionLookup;
40 
42  private $contentHandlerFactory;
43 
45  private $differenceEngine;
46 
51  public function __construct(
52  RevisionLookup $revisionLookup,
53  IContentHandlerFactory $contentHandlerFactory
54  ) {
55  parent::__construct( 'ComparePages' );
56  $this->revisionLookup = $revisionLookup;
57  $this->contentHandlerFactory = $contentHandlerFactory;
58  }
59 
65  public function execute( $par ) {
66  $this->setHeaders();
67  $this->outputHeader();
68  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
69  $this->addHelpLink( 'Help:Diff' );
70 
71  $form = HTMLForm::factory( 'ooui', [
72  'Page1' => [
73  'type' => 'title',
74  'exists' => true,
75  'name' => 'page1',
76  'label-message' => 'compare-page1',
77  'size' => '40',
78  'section' => 'page1',
79  'required' => false,
80  ],
81  'Revision1' => [
82  'type' => 'int',
83  'name' => 'rev1',
84  'label-message' => 'compare-rev1',
85  'size' => '8',
86  'section' => 'page1',
87  'validation-callback' => [ $this, 'checkExistingRevision' ],
88  ],
89  'Page2' => [
90  'type' => 'title',
91  'name' => 'page2',
92  'exists' => true,
93  'label-message' => 'compare-page2',
94  'size' => '40',
95  'section' => 'page2',
96  'required' => false,
97  ],
98  'Revision2' => [
99  'type' => 'int',
100  'name' => 'rev2',
101  'label-message' => 'compare-rev2',
102  'size' => '8',
103  'section' => 'page2',
104  'validation-callback' => [ $this, 'checkExistingRevision' ],
105  ],
106  'Action' => [
107  'type' => 'hidden',
108  'name' => 'action',
109  ],
110  'Unhide' => [
111  'type' => 'hidden',
112  'name' => 'unhide',
113  ],
114  ], $this->getContext(), 'compare' );
115 
116  $form->setMethod( 'get' )
117  ->setSubmitTextMsg( 'compare-submit' )
118  ->setSubmitCallback( [ $this, 'showDiff' ] )
119  ->show();
120 
121  if ( $this->differenceEngine ) {
122  $this->differenceEngine->showDiffPage( true );
123  }
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  // Revision IDs either passed the existence check or were fetched from existing titles.
137  $revisionRecord = $this->revisionLookup->getRevisionById( $rev1 );
138  $contentModel = $revisionRecord->getSlot(
139  SlotRecord::MAIN,
140  RevisionRecord::RAW
141  )->getModel();
142  $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
143  $this->differenceEngine = $contentHandler->createDifferenceEngine( $form->getContext(),
144  $rev1,
145  $rev2,
146  0, // rcid
147  ( $data['Action'] == 'purge' ),
148  ( $data['Unhide'] == '1' )
149  );
150  }
151  }
152 
153  private function revOrTitle( $revision, $title ) {
154  if ( $revision ) {
155  return $revision;
156  } elseif ( $title ) {
157  return Title::newFromText( $title )->getLatestRevID();
158  }
159 
160  return null;
161  }
162 
169  public function checkExistingRevision( $value, $alldata ) {
170  if ( $value === '' || $value === null ) {
171  return true;
172  }
173  $revisionRecord = $this->revisionLookup->getRevisionById( (int)$value );
174  if ( $revisionRecord === null ) {
175  return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
176  }
177 
178  return true;
179  }
180 
181  protected function getGroupName() {
182  return 'pagetools';
183  }
184 }
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:150
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:349
Page revision base class.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Implements Special:ComparePages.
showDiff( $data, HTMLForm $form)
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...
__construct(RevisionLookup $revisionLookup, IContentHandlerFactory $contentHandlerFactory)
Parent class for all special pages.
Definition: SpecialPage.php:44
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.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:373
Service for looking up page revisions.