Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| SpecialDiff | |
0.00% |
0 / 54 |
|
0.00% |
0 / 9 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getRedirect | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| showNoRedirectPage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| showForm | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
12 | |||
| onFormSubmit | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isListed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\HTMLForm\HTMLForm; |
| 10 | use MediaWiki\SpecialPage\RedirectSpecialPage; |
| 11 | use 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 | */ |
| 27 | class SpecialDiff extends RedirectSpecialPage { |
| 28 | public function __construct() { |
| 29 | parent::__construct( 'Diff' ); |
| 30 | $this->mAllowedRedirectParams = []; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param string|null $subpage |
| 35 | * @return Title|bool |
| 36 | */ |
| 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 | |
| 95 | /** |
| 96 | * @param array $formData |
| 97 | */ |
| 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 | |
| 112 | /** @inheritDoc */ |
| 113 | public function getDescription() { |
| 114 | // 'diff' message is in lowercase, using own message |
| 115 | return $this->msg( 'diff-form' ); |
| 116 | } |
| 117 | |
| 118 | /** @inheritDoc */ |
| 119 | public function getName() { |
| 120 | return 'diff-form'; |
| 121 | } |
| 122 | |
| 123 | /** @inheritDoc */ |
| 124 | public function isListed() { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | /** @inheritDoc */ |
| 129 | protected function getGroupName() { |
| 130 | return 'redirects'; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** @deprecated class alias since 1.41 */ |
| 135 | class_alias( SpecialDiff::class, 'SpecialDiff' ); |