Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialDiff
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 9
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRedirect
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 showNoRedirectPage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 showForm
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
12
 onFormSubmit
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Redirect from Special:Diff/### to index.php?diff=### and
4 * from Special:Diff/###/### to index.php?oldid=###&diff=###.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup SpecialPage
23 */
24
25namespace MediaWiki\Specials;
26
27use MediaWiki\HTMLForm\HTMLForm;
28use MediaWiki\SpecialPage\RedirectSpecialPage;
29use MediaWiki\Title\Title;
30
31/**
32 * Redirect from Special:Diff/### to index.php?diff=### and
33 * from Special:Diff/###/### to index.php?oldid=###&diff=###.
34 *
35 * All of the following are valid usages:
36 * - [[Special:Diff/12345]] (diff of a revision with the previous one)
37 * - [[Special:Diff/12345/prev]] (diff of a revision with the previous one as well)
38 * - [[Special:Diff/12345/next]] (diff of a revision with the next one)
39 * - [[Special:Diff/12345/cur]] (diff of a revision with the latest one of that page)
40 * - [[Special:Diff/12345/98765]] (diff between arbitrary two revisions)
41 *
42 * @ingroup SpecialPage
43 * @since 1.23
44 */
45class SpecialDiff extends RedirectSpecialPage {
46    public function __construct() {
47        parent::__construct( 'Diff' );
48        $this->mAllowedRedirectParams = [];
49    }
50
51    /**
52     * @param string|null $subpage
53     * @return Title|bool
54     */
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' );
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
145/** @deprecated class alias since 1.41 */
146class_alias( SpecialDiff::class, 'SpecialDiff' );