Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
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 / 59
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 / 7
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 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\HTMLForm\HTMLForm;
10use MediaWiki\SpecialPage\RedirectSpecialPage;
11use MediaWiki\Title\Title;
12
13/**
14 * Redirect from Special:Diff/### to index.php?diff=### and
15 * from Special:Diff/###/### to index.php?oldid=###&diff=###.
16 *
17 * All of the following are valid usages:
18 * - [[Special:Diff/12345]] (diff of a revision with the previous one)
19 * - [[Special:Diff/12345/prev]] (diff of a revision with the previous one as well)
20 * - [[Special:Diff/12345/next]] (diff of a revision with the next one)
21 * - [[Special:Diff/12345/cur]] (diff of a revision with the latest one of that page)
22 * - [[Special:Diff/12345/98765]] (diff between arbitrary two revisions)
23 *
24 * @ingroup SpecialPage
25 * @since 1.23
26 */
27class SpecialDiff extends RedirectSpecialPage {
28    public function __construct() {
29        parent::__construct( 'Diff' );
30        $this->mAllowedRedirectParams = [ // T419253
31            'action',
32            'bot',
33            'diffonly',
34            'unhide'
35        ];
36    }
37
38    /**
39     * @param string|null $subpage
40     * @return Title|bool
41     */
42    public function getRedirect( $subpage ) {
43        $parts = $subpage !== null ? explode( '/', $subpage ) : [];
44
45        // Try to parse the values given, generating somewhat pretty URLs if possible
46        if ( count( $parts ) === 1 && $parts[0] !== '' ) {
47            $this->mAddedRedirectParams['diff'] = $parts[0];
48        } elseif ( count( $parts ) === 2 ) {
49            $this->mAddedRedirectParams['oldid'] = $parts[0];
50            $this->mAddedRedirectParams['diff'] = $parts[1];
51        } else {
52            return false;
53        }
54
55        return true;
56    }
57
58    protected function showNoRedirectPage() {
59        $this->addHelpLink( 'Help:Diff' );
60        $this->setHeaders();
61        $this->outputHeader();
62        $this->showForm();
63    }
64
65    private function showForm() {
66        $form = HTMLForm::factory( 'ooui', [
67            'oldid' => [
68                'name' => 'oldid',
69                'type' => 'int',
70                'label-message' => 'diff-form-oldid',
71            ],
72            'diff' => [
73                'name' => 'diff',
74                // FIXME Set the type for the other field to int - T256425
75                'type' => 'selectorother',
76                'options-messages' => [
77                    'diff-form-other-revid' => 'other',
78                    'last' => 'prev',
79                    'cur' => 'cur',
80                    'next' => 'next',
81                ],
82                'label-message' => 'diff-form-revid',
83                // Remove validation callback when using int type - T256425
84                'validation-callback' => function ( $value ) {
85                    $value = trim( $value ?? '' );
86                    if ( preg_match( '/^\d*$/', $value )
87                        || in_array( $value, [ 'prev', 'cur', 'next' ], true )
88                    ) {
89                        return true;
90                    }
91                    return $this->msg( 'diff-form-error-revid' );
92                },
93            ],
94        ], $this->getContext(), 'diff-form' );
95        $form->setSubmitTextMsg( 'diff-form-submit' );
96        $form->setSubmitCallback( $this->onFormSubmit( ... ) );
97        $form->show();
98    }
99
100    /**
101     * @param array $formData
102     */
103    private function onFormSubmit( $formData ) {
104        $params = [];
105        if ( $formData['oldid'] ) {
106            $params[] = $formData['oldid'];
107        }
108        if ( $formData['diff'] ) {
109            // Remove trim when using int type - T256425
110            $params[] = trim( $formData['diff'] );
111        }
112        $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
113        $url = $title->getFullUrlForRedirect();
114        $this->getOutput()->redirect( $url );
115    }
116
117    /** @inheritDoc */
118    public function getDescription() {
119        // 'diff' message is in lowercase, using own message
120        return $this->msg( 'diff-form' );
121    }
122
123    /** @inheritDoc */
124    public function getName() {
125        return 'diff-form';
126    }
127
128    /** @inheritDoc */
129    public function isListed() {
130        return true;
131    }
132
133    /** @inheritDoc */
134    protected function getGroupName() {
135        return 'redirects';
136    }
137}
138
139// @codeCoverageIgnoreStart
140/** @deprecated class alias since 1.41 */
141class_alias( SpecialDiff::class, 'SpecialDiff' );
142// @codeCoverageIgnoreEnd