MediaWiki  master
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 }
getContext()
Gets the context this SpecialPage is executed in.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Implements Special:ComparePages.
static showDiff( $data, HTMLForm $form)
getOutput()
Get the OutputPage being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:307
getContext()
Get the base IContextSource object.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
execute( $par)
Show a form for filtering namespace and username.
checkExistingRevision( $value, $alldata)
checkExistingTitle( $value, $alldata)
static revOrTitle( $revision, $title)
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:119
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319