Merge "Simplify code to avoid interpreting "$" characters in string replacement"
[mediawiki.git] / includes / specials / SpecialDiff.php
blob0a14275564e89c67ff0b3c419d7fce239e91118a
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use MediaWiki\HTMLForm\HTMLForm;
24 use MediaWiki\SpecialPage\RedirectSpecialPage;
25 use MediaWiki\Title\Title;
27 /**
28 * Redirect from Special:Diff/### to index.php?diff=### and
29 * from Special:Diff/###/### to index.php?oldid=###&diff=###.
31 * All of the following are valid usages:
32 * - [[Special:Diff/12345]] (diff of a revision with the previous one)
33 * - [[Special:Diff/12345/prev]] (diff of a revision with the previous one as well)
34 * - [[Special:Diff/12345/next]] (diff of a revision with the next one)
35 * - [[Special:Diff/12345/cur]] (diff of a revision with the latest one of that page)
36 * - [[Special:Diff/12345/98765]] (diff between arbitrary two revisions)
38 * @ingroup SpecialPage
39 * @since 1.23
41 class SpecialDiff extends RedirectSpecialPage {
42 public function __construct() {
43 parent::__construct( 'Diff' );
44 $this->mAllowedRedirectParams = [];
47 /**
48 * @param string|null $subpage
49 * @return Title|bool
51 public function getRedirect( $subpage ) {
52 $parts = $subpage !== null ? explode( '/', $subpage ) : [];
54 // Try to parse the values given, generating somewhat pretty URLs if possible
55 if ( count( $parts ) === 1 && $parts[0] !== '' ) {
56 $this->mAddedRedirectParams['diff'] = $parts[0];
57 } elseif ( count( $parts ) === 2 ) {
58 $this->mAddedRedirectParams['oldid'] = $parts[0];
59 $this->mAddedRedirectParams['diff'] = $parts[1];
60 } else {
61 return false;
64 return true;
67 protected function showNoRedirectPage() {
68 $this->addHelpLink( 'Help:Diff' );
69 $this->setHeaders();
70 $this->outputHeader();
71 $this->showForm();
74 private function showForm() {
75 $form = HTMLForm::factory( 'ooui', [
76 'oldid' => [
77 'name' => 'oldid',
78 'type' => 'int',
79 'label-message' => 'diff-form-oldid',
81 'diff' => [
82 'name' => 'diff',
83 // FIXME Set the type for the other field to int - T256425
84 'type' => 'selectorother',
85 'options-messages' => [
86 'diff-form-other-revid' => 'other',
87 'last' => 'prev',
88 'cur' => 'cur',
89 'next' => 'next',
91 'label-message' => 'diff-form-revid',
92 // Remove validation callback when using int type - T256425
93 'validation-callback' => function ( $value ) {
94 $value = trim( $value ?? '' );
95 if ( preg_match( '/^\d*$/', $value )
96 || in_array( $value, [ 'prev', 'cur', 'next' ], true )
97 ) {
98 return true;
100 return $this->msg( 'diff-form-error-revid' );
103 ], $this->getContext(), 'diff-form' );
104 $form->setSubmitTextMsg( 'diff-form-submit' );
105 $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
106 $form->show();
109 public function onFormSubmit( $formData ) {
110 $params = [];
111 if ( $formData['oldid'] ) {
112 $params[] = $formData['oldid'];
114 if ( $formData['diff'] ) {
115 // Remove trim when using int type - T256425
116 $params[] = trim( $formData['diff'] );
118 $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
119 $url = $title->getFullUrlForRedirect();
120 $this->getOutput()->redirect( $url );
123 public function getDescription() {
124 // 'diff' message is in lowercase, using own message
125 return $this->msg( 'diff-form' );
128 public function getName() {
129 return 'diff-form';
132 public function isListed() {
133 return true;
136 protected function getGroupName() {
137 return 'redirects';
141 /** @deprecated class alias since 1.41 */
142 class_alias( SpecialDiff::class, 'SpecialDiff' );