Merge "debug: Use __CLASS__ to get the name of the class"
[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
53 $form = HTMLForm::factory( 'ooui', [
54 'Page1' => [
55 'type' => 'title',
56 'name' => 'page1',
57 'label-message' => 'compare-page1',
58 'size' => '40',
59 'section' => 'page1',
60 'validation-callback' => [ $this, 'checkExistingTitle' ],
61 'required' => false,
62 ],
63 'Revision1' => [
64 'type' => 'int',
65 'name' => 'rev1',
66 'label-message' => 'compare-rev1',
67 'size' => '8',
68 'section' => 'page1',
69 'validation-callback' => [ $this, 'checkExistingRevision' ],
70 ],
71 'Page2' => [
72 'type' => 'title',
73 'name' => 'page2',
74 'label-message' => 'compare-page2',
75 'size' => '40',
76 'section' => 'page2',
77 'validation-callback' => [ $this, 'checkExistingTitle' ],
78 'required' => false,
79 ],
80 'Revision2' => [
81 'type' => 'int',
82 'name' => 'rev2',
83 'label-message' => 'compare-rev2',
84 'size' => '8',
85 'section' => 'page2',
86 'validation-callback' => [ $this, 'checkExistingRevision' ],
87 ],
88 'Action' => [
89 'type' => 'hidden',
90 'name' => 'action',
91 ],
92 'Diffonly' => [
93 'type' => 'hidden',
94 'name' => 'diffonly',
95 ],
96 'Unhide' => [
97 'type' => 'hidden',
98 'name' => 'unhide',
99 ],
100 ], $this->getContext(), 'compare' );
101 $form->setSubmitTextMsg( 'compare-submit' );
102 $form->suppressReset();
103 $form->setMethod( 'get' );
104 $form->setSubmitCallback( [ __CLASS__, 'showDiff' ] );
105
106 $form->loadData();
107 $form->displayForm( '' );
108 $form->trySubmit();
109 }
110
111 public static function showDiff( $data, HTMLForm $form ) {
112 $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
113 $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
114
115 if ( $rev1 && $rev2 ) {
116 $revision = Revision::newFromId( $rev1 );
117
118 if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
119 $contentHandler = $revision->getContentHandler();
120 $de = $contentHandler->createDifferenceEngine( $form->getContext(),
121 $rev1,
122 $rev2,
123 null, // rcid
124 ( $data['Action'] == 'purge' ),
125 ( $data['Unhide'] == '1' )
126 );
127 $de->showDiffPage( true );
128 }
129 }
130 }
131
132 public static function revOrTitle( $revision, $title ) {
133 if ( $revision ) {
134 return $revision;
135 } elseif ( $title ) {
136 $title = Title::newFromText( $title );
137 if ( $title instanceof Title ) {
138 return $title->getLatestRevID();
139 }
140 }
141
142 return null;
143 }
144
145 public function checkExistingTitle( $value, $alldata ) {
146 if ( $value === '' || $value === null ) {
147 return true;
148 }
149 $title = Title::newFromText( $value );
150 if ( !$title instanceof Title ) {
151 return $this->msg( 'compare-invalid-title' )->parseAsBlock();
152 }
153 if ( !$title->exists() ) {
154 return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
155 }
156
157 return true;
158 }
159
160 public function checkExistingRevision( $value, $alldata ) {
161 if ( $value === '' || $value === null ) {
162 return true;
163 }
164 $revision = Revision::newFromId( $value );
165 if ( $revision === null ) {
166 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
167 }
168
169 return true;
170 }
171
172 protected function getGroupName() {
173 return 'pagetools';
174 }
175 }