Add rate limiter to Special:ConfirmEmail
[lhc/web/wiklou.git] / includes / specials / SpecialComparePages.php
1 <?php
2 /**
3 * Implements Special:ComparePages
4 *
5 * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * Implements Special:ComparePages
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialComparePages extends SpecialPage {
32
33 // Stored objects
34 protected $opts, $skin;
35
36 // Some internal settings
37 protected $showNavigation = false;
38
39 public function __construct() {
40 parent::__construct( 'ComparePages' );
41 }
42
43 /**
44 * Show a form for filtering namespace and username
45 *
46 * @param string|null $par
47 */
48 public function execute( $par ) {
49 $this->setHeaders();
50 $this->outputHeader();
51 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
52 $this->addHelpLink( 'Help:Diff' );
53
54 $form = HTMLForm::factory( 'ooui', [
55 'Page1' => [
56 'type' => 'title',
57 'name' => 'page1',
58 'label-message' => 'compare-page1',
59 'size' => '40',
60 'section' => 'page1',
61 'validation-callback' => [ $this, 'checkExistingTitle' ],
62 'required' => false,
63 ],
64 'Revision1' => [
65 'type' => 'int',
66 'name' => 'rev1',
67 'label-message' => 'compare-rev1',
68 'size' => '8',
69 'section' => 'page1',
70 'validation-callback' => [ $this, 'checkExistingRevision' ],
71 ],
72 'Page2' => [
73 'type' => 'title',
74 'name' => 'page2',
75 'label-message' => 'compare-page2',
76 'size' => '40',
77 'section' => 'page2',
78 'validation-callback' => [ $this, 'checkExistingTitle' ],
79 'required' => false,
80 ],
81 'Revision2' => [
82 'type' => 'int',
83 'name' => 'rev2',
84 'label-message' => 'compare-rev2',
85 'size' => '8',
86 'section' => 'page2',
87 'validation-callback' => [ $this, 'checkExistingRevision' ],
88 ],
89 'Action' => [
90 'type' => 'hidden',
91 'name' => 'action',
92 ],
93 'Diffonly' => [
94 'type' => 'hidden',
95 'name' => 'diffonly',
96 ],
97 'Unhide' => [
98 'type' => 'hidden',
99 'name' => 'unhide',
100 ],
101 ], $this->getContext(), 'compare' );
102 $form->setSubmitTextMsg( 'compare-submit' );
103 $form->suppressReset();
104 $form->setMethod( 'get' );
105 $form->setSubmitCallback( [ __CLASS__, 'showDiff' ] );
106
107 $form->loadData();
108 $form->displayForm( '' );
109 $form->trySubmit();
110 }
111
112 public static function showDiff( $data, HTMLForm $form ) {
113 $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
114 $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
115
116 if ( $rev1 && $rev2 ) {
117 $revision = Revision::newFromId( $rev1 );
118
119 if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
120 $contentHandler = $revision->getContentHandler();
121 $de = $contentHandler->createDifferenceEngine( $form->getContext(),
122 $rev1,
123 $rev2,
124 null, // rcid
125 ( $data['Action'] == 'purge' ),
126 ( $data['Unhide'] == '1' )
127 );
128 $de->showDiffPage( true );
129 }
130 }
131 }
132
133 public static function revOrTitle( $revision, $title ) {
134 if ( $revision ) {
135 return $revision;
136 } elseif ( $title ) {
137 $title = Title::newFromText( $title );
138 if ( $title instanceof Title ) {
139 return $title->getLatestRevID();
140 }
141 }
142
143 return null;
144 }
145
146 public function checkExistingTitle( $value, $alldata ) {
147 if ( $value === '' || $value === null ) {
148 return true;
149 }
150 $title = Title::newFromText( $value );
151 if ( !$title instanceof Title ) {
152 return $this->msg( 'compare-invalid-title' )->parseAsBlock();
153 }
154 if ( !$title->exists() ) {
155 return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
156 }
157
158 return true;
159 }
160
161 public function checkExistingRevision( $value, $alldata ) {
162 if ( $value === '' || $value === null ) {
163 return true;
164 }
165 $revision = Revision::newFromId( $value );
166 if ( $revision === null ) {
167 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
168 }
169
170 return true;
171 }
172
173 protected function getGroupName() {
174 return 'pagetools';
175 }
176 }