Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / specials / SpecialDiff.php
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
25 /**
26 * Redirect from Special:Diff/### to index.php?diff=### and
27 * from Special:Diff/###/### to index.php?oldid=###&diff=###.
28 *
29 * All of the following are valid usages:
30 * - [[Special:Diff/12345]] (diff of a revision with the previous one)
31 * - [[Special:Diff/12345/prev]] (diff of a revision with the previous one as well)
32 * - [[Special:Diff/12345/next]] (diff of a revision with the next one)
33 * - [[Special:Diff/12345/cur]] (diff of a revision with the latest one of that page)
34 * - [[Special:Diff/12345/98765]] (diff between arbitrary two revisions)
35 *
36 * @ingroup SpecialPage
37 * @since 1.23
38 */
39 class SpecialDiff extends RedirectSpecialPage {
40 public function __construct() {
41 parent::__construct( 'Diff' );
42 $this->mAllowedRedirectParams = [];
43 }
44
45 /**
46 * @param string|null $subpage
47 * @return Title|bool
48 */
49 public function getRedirect( $subpage ) {
50 $parts = explode( '/', $subpage );
51
52 // Try to parse the values given, generating somewhat pretty URLs if possible
53 if ( count( $parts ) === 1 && $parts[0] !== '' ) {
54 $this->mAddedRedirectParams['diff'] = $parts[0];
55 } elseif ( count( $parts ) === 2 ) {
56 $this->mAddedRedirectParams['oldid'] = $parts[0];
57 $this->mAddedRedirectParams['diff'] = $parts[1];
58 } else {
59 return false;
60 }
61
62 return true;
63 }
64
65 protected function showNoRedirectPage() {
66 $this->addHelpLink( 'Help:Diff' );
67 $this->setHeaders();
68 $this->outputHeader();
69 $this->showForm();
70 }
71
72 private function showForm() {
73 $form = HTMLForm::factory( 'ooui', [
74 'oldid' => [
75 'name' => 'oldid',
76 'type' => 'int',
77 'label-message' => 'diff-form-oldid',
78 ],
79 'diff' => [
80 'name' => 'diff',
81 'class' => 'HTMLTextField',
82 'label-message' => 'diff-form-revid',
83 ],
84 ], $this->getContext(), 'diff-form' );
85 $form->setSubmitTextMsg( 'diff-form-submit' );
86 $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
87 $form->show();
88 }
89
90 public function onFormSubmit( $formData ) {
91 $params = [];
92 if ( $formData['oldid'] ) {
93 $params[] = $formData['oldid'];
94 }
95 if ( $formData['diff'] ) {
96 $params[] = $formData['diff'];
97 }
98 $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
99 $url = $title->getFullUrlForRedirect();
100 $this->getOutput()->redirect( $url );
101 }
102
103 public function getDescription() {
104 // 'diff' message is in lowercase, using own message
105 return $this->msg( 'diff-form' )->text();
106 }
107
108 public function getName() {
109 return 'diff-form';
110 }
111
112 public function isListed() {
113 return true;
114 }
115
116 protected function getGroupName() {
117 return 'redirects';
118 }
119 }