MediaWiki  1.34.0
SpecialComparePages.php
Go to the documentation of this file.
1 <?php
32 
33  // Stored objects
34  protected $opts, $skin;
35 
36  // Some internal settings
37  protected $showNavigation = false;
38 
39  public function __construct() {
40  parent::__construct( 'ComparePages' );
41  }
42 
48  public function execute( $par ) {
49  $this->setHeaders();
50  $this->outputHeader();
51  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
52  $this->addHelpLink( 'Help:Diff' );
53 
54  $form = HTMLForm::factory( 'ooui', [
55  'Page1' => [
56  'type' => 'title',
57  'name' => 'page1',
58  'label-message' => 'compare-page1',
59  'size' => '40',
60  'section' => 'page1',
61  'validation-callback' => [ $this, 'checkExistingTitle' ],
62  'required' => false,
63  ],
64  'Revision1' => [
65  'type' => 'int',
66  'name' => 'rev1',
67  'label-message' => 'compare-rev1',
68  'size' => '8',
69  'section' => 'page1',
70  'validation-callback' => [ $this, 'checkExistingRevision' ],
71  ],
72  'Page2' => [
73  'type' => 'title',
74  'name' => 'page2',
75  'label-message' => 'compare-page2',
76  'size' => '40',
77  'section' => 'page2',
78  'validation-callback' => [ $this, 'checkExistingTitle' ],
79  'required' => false,
80  ],
81  'Revision2' => [
82  'type' => 'int',
83  'name' => 'rev2',
84  'label-message' => 'compare-rev2',
85  'size' => '8',
86  'section' => 'page2',
87  'validation-callback' => [ $this, 'checkExistingRevision' ],
88  ],
89  'Action' => [
90  'type' => 'hidden',
91  'name' => 'action',
92  ],
93  'Diffonly' => [
94  'type' => 'hidden',
95  'name' => 'diffonly',
96  ],
97  'Unhide' => [
98  'type' => 'hidden',
99  'name' => 'unhide',
100  ],
101  ], $this->getContext(), 'compare' );
102  $form->setSubmitTextMsg( 'compare-submit' );
103  $form->suppressReset();
104  $form->setMethod( 'get' );
105  $form->setSubmitCallback( [ __CLASS__, 'showDiff' ] );
106 
107  $form->loadData();
108  $form->displayForm( '' );
109  $form->trySubmit();
110  }
111 
112  public static function showDiff( $data, HTMLForm $form ) {
113  $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
114  $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
115 
116  if ( $rev1 && $rev2 ) {
117  $revision = Revision::newFromId( $rev1 );
118 
119  if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
120  $contentHandler = $revision->getContentHandler();
121  $de = $contentHandler->createDifferenceEngine( $form->getContext(),
122  $rev1,
123  $rev2,
124  null, // rcid
125  ( $data['Action'] == 'purge' ),
126  ( $data['Unhide'] == '1' )
127  );
128  $de->showDiffPage( true );
129  }
130  }
131  }
132 
133  public static function revOrTitle( $revision, $title ) {
134  if ( $revision ) {
135  return $revision;
136  } elseif ( $title ) {
138  if ( $title instanceof Title ) {
139  return $title->getLatestRevID();
140  }
141  }
142 
143  return null;
144  }
145 
146  public function checkExistingTitle( $value, $alldata ) {
147  if ( $value === '' || $value === null ) {
148  return true;
149  }
150  $title = Title::newFromText( $value );
151  if ( !$title instanceof Title ) {
152  return $this->msg( 'compare-invalid-title' )->parseAsBlock();
153  }
154  if ( !$title->exists() ) {
155  return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
156  }
157 
158  return true;
159  }
160 
161  public function checkExistingRevision( $value, $alldata ) {
162  if ( $value === '' || $value === null ) {
163  return true;
164  }
165  $revision = Revision::newFromId( $value );
166  if ( $revision === null ) {
167  return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
168  }
169 
170  return true;
171  }
172 
173  protected function getGroupName() {
174  return 'pagetools';
175  }
176 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
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:316
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:119
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialComparePages\checkExistingRevision
checkExistingRevision( $value, $alldata)
Definition: SpecialComparePages.php:161
SpecialComparePages\$opts
$opts
Definition: SpecialComparePages.php:34
SpecialComparePages\$skin
$skin
Definition: SpecialComparePages.php:34
SpecialComparePages\checkExistingTitle
checkExistingTitle( $value, $alldata)
Definition: SpecialComparePages.php:146
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
SpecialComparePages
Implements Special:ComparePages.
Definition: SpecialComparePages.php:31
$title
$title
Definition: testCompression.php:34
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
SpecialComparePages\showDiff
static showDiff( $data, HTMLForm $form)
Definition: SpecialComparePages.php:112
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
SpecialComparePages\__construct
__construct()
Definition: SpecialComparePages.php:39
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:37
SpecialComparePages\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialComparePages.php:173
SpecialComparePages\$showNavigation
$showNavigation
Definition: SpecialComparePages.php:37
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:48
SpecialComparePages\revOrTitle
static revOrTitle( $revision, $title)
Definition: SpecialComparePages.php:133
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
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:639
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131