MediaWiki  master
SpecialComparePages.php
Go to the documentation of this file.
1 <?php
29 
36 
37  // Stored objects
38  protected $opts, $skin;
39 
40  // Some internal settings
41  protected $showNavigation = false;
42 
43  public function __construct() {
44  parent::__construct( 'ComparePages' );
45  }
46 
52  public function execute( $par ) {
53  $this->setHeaders();
54  $this->outputHeader();
55  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
56  $this->addHelpLink( 'Help:Diff' );
57 
58  $form = HTMLForm::factory( 'ooui', [
59  'Page1' => [
60  'type' => 'title',
61  'name' => 'page1',
62  'label-message' => 'compare-page1',
63  'size' => '40',
64  'section' => 'page1',
65  'validation-callback' => [ $this, 'checkExistingTitle' ],
66  'required' => false,
67  ],
68  'Revision1' => [
69  'type' => 'int',
70  'name' => 'rev1',
71  'label-message' => 'compare-rev1',
72  'size' => '8',
73  'section' => 'page1',
74  'validation-callback' => [ $this, 'checkExistingRevision' ],
75  ],
76  'Page2' => [
77  'type' => 'title',
78  'name' => 'page2',
79  'label-message' => 'compare-page2',
80  'size' => '40',
81  'section' => 'page2',
82  'validation-callback' => [ $this, 'checkExistingTitle' ],
83  'required' => false,
84  ],
85  'Revision2' => [
86  'type' => 'int',
87  'name' => 'rev2',
88  'label-message' => 'compare-rev2',
89  'size' => '8',
90  'section' => 'page2',
91  'validation-callback' => [ $this, 'checkExistingRevision' ],
92  ],
93  'Action' => [
94  'type' => 'hidden',
95  'name' => 'action',
96  ],
97  'Diffonly' => [
98  'type' => 'hidden',
99  'name' => 'diffonly',
100  ],
101  'Unhide' => [
102  'type' => 'hidden',
103  'name' => 'unhide',
104  ],
105  ], $this->getContext(), 'compare' );
106  $form->setSubmitTextMsg( 'compare-submit' );
107  $form->suppressReset();
108  $form->setMethod( 'get' );
109  $form->setSubmitCallback( [ __CLASS__, 'showDiff' ] );
110 
111  $form->loadData();
112  $form->displayForm( '' );
113  $form->trySubmit();
114  }
115 
116  public static function showDiff( $data, HTMLForm $form ) {
117  $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
118  $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
119 
120  if ( $rev1 && $rev2 ) {
121  $revisionRecord = MediaWikiServices::getInstance()
122  ->getRevisionLookup()
123  ->getRevisionById( $rev1 );
124 
125  if ( $revisionRecord ) { // NOTE: $rev1 was already checked, should exist.
126  $contentModel = $revisionRecord->getSlot(
127  SlotRecord::MAIN,
128  RevisionRecord::RAW
129  )->getModel();
130  $contentHandler = MediaWikiServices::getInstance()
131  ->getContentHandlerFactory()
132  ->getContentHandler( $contentModel );
133  $de = $contentHandler->createDifferenceEngine( $form->getContext(),
134  $rev1,
135  $rev2,
136  null, // rcid
137  ( $data['Action'] == 'purge' ),
138  ( $data['Unhide'] == '1' )
139  );
140  $de->showDiffPage( true );
141  }
142  }
143  }
144 
145  public static function revOrTitle( $revision, $title ) {
146  if ( $revision ) {
147  return $revision;
148  } elseif ( $title ) {
150  if ( $title instanceof Title ) {
151  return $title->getLatestRevID();
152  }
153  }
154 
155  return null;
156  }
157 
158  public function checkExistingTitle( $value, $alldata ) {
159  if ( $value === '' || $value === null ) {
160  return true;
161  }
162  $title = Title::newFromText( $value );
163  if ( !$title instanceof Title ) {
164  return $this->msg( 'compare-invalid-title' )->parseAsBlock();
165  }
166  if ( !$title->exists() ) {
167  return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
168  }
169 
170  return true;
171  }
172 
173  public function checkExistingRevision( $value, $alldata ) {
174  if ( $value === '' || $value === null ) {
175  return true;
176  }
177  $revisionRecord = MediaWikiServices::getInstance()
178  ->getRevisionLookup()
179  ->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:828
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:329
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:42
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
SpecialComparePages\checkExistingRevision
checkExistingRevision( $value, $alldata)
Definition: SpecialComparePages.php:173
SpecialComparePages\$opts
$opts
Definition: SpecialComparePages.php:38
SpecialComparePages\$skin
$skin
Definition: SpecialComparePages.php:38
SpecialComparePages\checkExistingTitle
checkExistingTitle( $value, $alldata)
Definition: SpecialComparePages.php:158
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:864
SpecialComparePages
Implements Special:ComparePages.
Definition: SpecialComparePages.php:35
$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:571
SpecialComparePages\showDiff
static showDiff( $data, HTMLForm $form)
Definition: SpecialComparePages.php:116
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
SpecialComparePages\__construct
__construct()
Definition: SpecialComparePages.php:43
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:41
SpecialComparePages\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialComparePages.php:187
SpecialComparePages\$showNavigation
$showNavigation
Definition: SpecialComparePages.php:41
Title
Represents a title within MediaWiki.
Definition: Title.php:42
SpecialComparePages\execute
execute( $par)
Show a form for filtering namespace and username.
Definition: SpecialComparePages.php:52
SpecialComparePages\revOrTitle
static revOrTitle( $revision, $title)
Definition: SpecialComparePages.php:145
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:311
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:662
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:133