MediaWiki master
SpecialDiff.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
12
28 public function __construct() {
29 parent::__construct( 'Diff' );
30 $this->mAllowedRedirectParams = [];
31 }
32
37 public function getRedirect( $subpage ) {
38 $parts = $subpage !== null ? explode( '/', $subpage ) : [];
39
40 // Try to parse the values given, generating somewhat pretty URLs if possible
41 if ( count( $parts ) === 1 && $parts[0] !== '' ) {
42 $this->mAddedRedirectParams['diff'] = $parts[0];
43 } elseif ( count( $parts ) === 2 ) {
44 $this->mAddedRedirectParams['oldid'] = $parts[0];
45 $this->mAddedRedirectParams['diff'] = $parts[1];
46 } else {
47 return false;
48 }
49
50 return true;
51 }
52
53 protected function showNoRedirectPage() {
54 $this->addHelpLink( 'Help:Diff' );
55 $this->setHeaders();
56 $this->outputHeader();
57 $this->showForm();
58 }
59
60 private function showForm() {
61 $form = HTMLForm::factory( 'ooui', [
62 'oldid' => [
63 'name' => 'oldid',
64 'type' => 'int',
65 'label-message' => 'diff-form-oldid',
66 ],
67 'diff' => [
68 'name' => 'diff',
69 // FIXME Set the type for the other field to int - T256425
70 'type' => 'selectorother',
71 'options-messages' => [
72 'diff-form-other-revid' => 'other',
73 'last' => 'prev',
74 'cur' => 'cur',
75 'next' => 'next',
76 ],
77 'label-message' => 'diff-form-revid',
78 // Remove validation callback when using int type - T256425
79 'validation-callback' => function ( $value ) {
80 $value = trim( $value ?? '' );
81 if ( preg_match( '/^\d*$/', $value )
82 || in_array( $value, [ 'prev', 'cur', 'next' ], true )
83 ) {
84 return true;
85 }
86 return $this->msg( 'diff-form-error-revid' );
87 },
88 ],
89 ], $this->getContext(), 'diff-form' );
90 $form->setSubmitTextMsg( 'diff-form-submit' );
91 $form->setSubmitCallback( $this->onFormSubmit( ... ) );
92 $form->show();
93 }
94
98 private function onFormSubmit( $formData ) {
99 $params = [];
100 if ( $formData['oldid'] ) {
101 $params[] = $formData['oldid'];
102 }
103 if ( $formData['diff'] ) {
104 // Remove trim when using int type - T256425
105 $params[] = trim( $formData['diff'] );
106 }
107 $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
108 $url = $title->getFullUrlForRedirect();
109 $this->getOutput()->redirect( $url );
110 }
111
113 public function getDescription() {
114 // 'diff' message is in lowercase, using own message
115 return $this->msg( 'diff-form' );
116 }
117
119 public function getName() {
120 return 'diff-form';
121 }
122
124 public function isListed() {
125 return true;
126 }
127
129 protected function getGroupName() {
130 return 'redirects';
131 }
132}
133
135class_alias( SpecialDiff::class, 'SpecialDiff' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
Shortcut to construct a special page alias.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Redirect from Special:Diff/### to index.php?diff=### and from Special:Diff/###/### to index....
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.to override 1.3 (r3583) bool
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getName()
Get the canonical, unlocalized name of this special page without namespace.string
Represents a title within MediaWiki.
Definition Title.php:70
array $params
The job parameters.