MediaWiki  master
SpecialDiff.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\Specials;
26 
27 use HTMLForm;
30 
46  public function __construct() {
47  parent::__construct( 'Diff' );
48  $this->mAllowedRedirectParams = [];
49  }
50 
55  public function getRedirect( $subpage ) {
56  $parts = $subpage !== null ? explode( '/', $subpage ) : [];
57 
58  // Try to parse the values given, generating somewhat pretty URLs if possible
59  if ( count( $parts ) === 1 && $parts[0] !== '' ) {
60  $this->mAddedRedirectParams['diff'] = $parts[0];
61  } elseif ( count( $parts ) === 2 ) {
62  $this->mAddedRedirectParams['oldid'] = $parts[0];
63  $this->mAddedRedirectParams['diff'] = $parts[1];
64  } else {
65  return false;
66  }
67 
68  return true;
69  }
70 
71  protected function showNoRedirectPage() {
72  $this->addHelpLink( 'Help:Diff' );
73  $this->setHeaders();
74  $this->outputHeader();
75  $this->showForm();
76  }
77 
78  private function showForm() {
79  $form = HTMLForm::factory( 'ooui', [
80  'oldid' => [
81  'name' => 'oldid',
82  'type' => 'int',
83  'label-message' => 'diff-form-oldid',
84  ],
85  'diff' => [
86  'name' => 'diff',
87  // FIXME Set the type for the other field to int - T256425
88  'type' => 'selectorother',
89  'options-messages' => [
90  'diff-form-other-revid' => 'other',
91  'last' => 'prev',
92  'cur' => 'cur',
93  'next' => 'next',
94  ],
95  'label-message' => 'diff-form-revid',
96  // Remove validation callback when using int type - T256425
97  'validation-callback' => function ( $value ) {
98  $value = trim( $value ?? '' );
99  if ( preg_match( '/^\d*$/', $value )
100  || in_array( $value, [ 'prev', 'cur', 'next' ], true )
101  ) {
102  return true;
103  }
104  return $this->msg( 'diff-form-error-revid' );
105  },
106  ],
107  ], $this->getContext(), 'diff-form' );
108  $form->setSubmitTextMsg( 'diff-form-submit' );
109  $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
110  $form->show();
111  }
112 
113  public function onFormSubmit( $formData ) {
114  $params = [];
115  if ( $formData['oldid'] ) {
116  $params[] = $formData['oldid'];
117  }
118  if ( $formData['diff'] ) {
119  // Remove trim when using int type - T256425
120  $params[] = trim( $formData['diff'] );
121  }
122  $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
123  $url = $title->getFullUrlForRedirect();
124  $this->getOutput()->redirect( $url );
125  }
126 
127  public function getDescription() {
128  // 'diff' message is in lowercase, using own message
129  return $this->msg( 'diff-form' )->text();
130  }
131 
132  public function getName() {
133  return 'diff-form';
134  }
135 
136  public function isListed() {
137  return true;
138  }
139 
140  protected function getGroupName() {
141  return 'redirects';
142  }
143 }
144 
148 class_alias( SpecialDiff::class, 'SpecialDiff' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:155
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:354
Redirect from Special:Diff/### to index.php?diff=### and from Special:Diff/###/### to index....
Definition: SpecialDiff.php:45
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
isListed()
Whether this special page is listed in Special:SpecialPages.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getName()
Get the name of this Special Page.
Represents a title within MediaWiki.
Definition: Title.php:82
Shortcut to construct a special page alias.
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.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.