cf5ce6768fa96c342140bcbfec2247cee67b987c
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29
30 function __construct( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle =& $article;
34 $this->mTitle =& $article->mTitle;
35
36 if( $this->mTitle ) {
37 foreach( $wgRestrictionTypes as $action ) {
38 // Fixme: this form currently requires individual selections,
39 // but the db allows multiples separated by commas.
40 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
41 }
42
43 $this->mCascade = $this->mTitle->areRestrictionsCascading();
44 }
45
46 // The form will be available in read-only to show levels.
47 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
48 $this->disabledAttrib = $this->disabled
49 ? array( 'disabled' => 'disabled' )
50 : array();
51
52 if( $wgRequest->wasPosted() ) {
53 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
54 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
55 foreach( $wgRestrictionTypes as $action ) {
56 $val = $wgRequest->getVal( "mwProtect-level-$action" );
57 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
58 $this->mRestrictions[$action] = $val;
59 }
60 }
61 }
62 }
63
64 function show() {
65 global $wgOut;
66
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68
69 if( is_null( $this->mTitle ) ||
70 !$this->mTitle->exists() ||
71 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
72 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
73 return;
74 }
75
76 $cascadeSources = $this->mTitle->getCascadeProtectionSources();
77
78 if ( $cascadeSources && count($cascadeSources) > 0 ) {
79 $titles = '';
80
81 foreach ( $cascadeSources as $title ) {
82 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
83 }
84
85 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
86
87 $wgOut->addWikiText( $notice );
88 }
89
90 if( $this->save() ) {
91 $wgOut->redirect( $this->mTitle->getFullUrl() );
92 return;
93 }
94
95 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
96 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
97
98 $wgOut->addWikiText(
99 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
100 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
101
102 $wgOut->addHTML( $this->buildForm() );
103
104 $this->showLogExtract( $wgOut );
105 }
106
107 function save() {
108 global $wgRequest, $wgUser, $wgOut;
109 if( !$wgRequest->wasPosted() ) {
110 return false;
111 }
112
113 if( $this->disabled ) {
114 return false;
115 }
116
117 $token = $wgRequest->getVal( 'wpEditToken' );
118 if( !$wgUser->matchEditToken( $token ) ) {
119 throw new FatalError( wfMsg( 'sessionfailure' ) );
120 }
121
122 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade );
123 if( !$ok ) {
124 throw new FatalError( "Unknown error at restriction save time." );
125 }
126 return $ok;
127 }
128
129 function buildForm() {
130 global $wgUser;
131
132 $out = '';
133 if( !$this->disabled ) {
134 $out .= $this->buildScript();
135 // The submission needs to reenable the move permission selector
136 // if it's in locked mode, or some browsers won't submit the data.
137 $out .= wfOpenElement( 'form', array(
138 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
139 'method' => 'post',
140 'onsubmit' => 'protectEnable(true)' ) );
141
142 $out .= wfElement( 'input', array(
143 'type' => 'hidden',
144 'name' => 'wpEditToken',
145 'value' => $wgUser->editToken() ) );
146 }
147
148 $out .= "<table id='mwProtectSet'>";
149 $out .= "<tbody>";
150 $out .= "<tr>\n";
151 foreach( $this->mRestrictions as $action => $required ) {
152 /* Not all languages have V_x <-> N_x relation */
153 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
154 }
155 $out .= "</tr>\n";
156 $out .= "<tr>\n";
157 foreach( $this->mRestrictions as $action => $selected ) {
158 $out .= "<td>\n";
159 $out .= $this->buildSelector( $action, $selected );
160 $out .= "</td>\n";
161 }
162 $out .= "</tr>\n";
163
164 // JavaScript will add another row with a value-chaining checkbox
165
166 $out .= "</tbody>\n";
167 $out .= "</table>\n";
168
169 global $wgEnableCascadingProtection;
170
171 if ($wgEnableCascadingProtection)
172 $out .= $this->buildCascadeInput();
173
174 if( !$this->disabled ) {
175 $out .= "<table>\n";
176 $out .= "<tbody>\n";
177 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
178 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
179 $out .= "</tbody>\n";
180 $out .= "</table>\n";
181 $out .= "</form>\n";
182 $out .= $this->buildCleanupScript();
183 }
184
185 return $out;
186 }
187
188 function buildSelector( $action, $selected ) {
189 global $wgRestrictionLevels;
190 $id = 'mwProtect-level-' . $action;
191 $attribs = array(
192 'id' => $id,
193 'name' => $id,
194 'size' => count( $wgRestrictionLevels ),
195 'onchange' => 'protectLevelsUpdate(this)',
196 ) + $this->disabledAttrib;
197
198 $out = wfOpenElement( 'select', $attribs );
199 foreach( $wgRestrictionLevels as $key ) {
200 $out .= $this->buildOption( $key, $selected );
201 }
202 $out .= "</select>\n";
203 return $out;
204 }
205
206 function buildOption( $key, $selected ) {
207 $text = ( $key == '' )
208 ? wfMsg( 'protect-default' )
209 : wfMsg( "protect-level-$key" );
210 $selectedAttrib = ($selected == $key)
211 ? array( 'selected' => 'selected' )
212 : array();
213 return wfElement( 'option',
214 array( 'value' => $key ) + $selectedAttrib,
215 $text );
216 }
217
218 function buildReasonInput() {
219 $id = 'mwProtect-reason';
220 return wfElement( 'label', array(
221 'id' => "$id-label",
222 'for' => $id ),
223 wfMsg( 'protectcomment' ) ) .
224 '</td><td>' .
225 wfElement( 'input', array(
226 'size' => 60,
227 'name' => $id,
228 'id' => $id ) );
229 }
230
231 function buildCascadeInput() {
232 $id = 'mwProtect-cascade';
233 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
234
235 return $ci;
236 }
237
238 function buildSubmit() {
239 return wfElement( 'input', array(
240 'type' => 'submit',
241 'value' => wfMsg( 'confirm' ) ) );
242 }
243
244 function buildScript() {
245 global $wgStylePath, $wgStyleVersion;
246 return '<script type="text/javascript" src="' .
247 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
248 '"></script>';
249 }
250
251 function buildCleanupScript() {
252 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
253 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
254 }
255
256 /**
257 * @param OutputPage $out
258 * @access private
259 */
260 function showLogExtract( &$out ) {
261 # Show relevant lines from the deletion log:
262 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
263 $logViewer = new LogViewer(
264 new LogReader(
265 new FauxRequest(
266 array( 'page' => $this->mTitle->getPrefixedText(),
267 'type' => 'protect' ) ) ) );
268 $logViewer->showList( $out );
269 }
270 }
271
272 ?>