* (bug 9769) Provide "watch this page" toggle on protection form
[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
22 /**
23 * @todo document, briefly.
24 * @addtogroup SpecialPage
25 */
26 class ProtectionForm {
27 var $mRestrictions = array();
28 var $mReason = '';
29 var $mCascade = false;
30 var $mExpiry = null;
31
32 function __construct( &$article ) {
33 global $wgRequest, $wgUser;
34 global $wgRestrictionTypes, $wgRestrictionLevels;
35 $this->mArticle =& $article;
36 $this->mTitle =& $article->mTitle;
37
38 if( $this->mTitle ) {
39 $this->mTitle->loadRestrictions();
40
41 foreach( $wgRestrictionTypes as $action ) {
42 // Fixme: this form currently requires individual selections,
43 // but the db allows multiples separated by commas.
44 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
45 }
46
47 $this->mCascade = $this->mTitle->areRestrictionsCascading();
48
49 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
50 $this->mExpiry = 'infinite';
51 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
52 $this->mExpiry = '';
53 } else {
54 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
55 }
56 }
57
58 // The form will be available in read-only to show levels.
59 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
60 $this->disabledAttrib = $this->disabled
61 ? array( 'disabled' => 'disabled' )
62 : array();
63
64 if( $wgRequest->wasPosted() ) {
65 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
66 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
67 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
68
69 foreach( $wgRestrictionTypes as $action ) {
70 $val = $wgRequest->getVal( "mwProtect-level-$action" );
71 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
72 $this->mRestrictions[$action] = $val;
73 }
74 }
75 }
76 }
77
78 function execute() {
79 global $wgRequest;
80 if( $wgRequest->wasPosted() ) {
81 if( $this->save() ) {
82 global $wgOut;
83 $wgOut->redirect( $this->mTitle->getFullUrl() );
84 }
85 } else {
86 $this->show();
87 }
88 }
89
90 function show( $err = null ) {
91 global $wgOut, $wgUser;
92
93 $wgOut->setRobotpolicy( 'noindex,nofollow' );
94
95 if( is_null( $this->mTitle ) ||
96 !$this->mTitle->exists() ||
97 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
98 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
99 return;
100 }
101
102 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
103
104 if ( "" != $err ) {
105 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
106 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
107 }
108
109 if ( $cascadeSources && count($cascadeSources) > 0 ) {
110 $titles = '';
111
112 foreach ( $cascadeSources as $title ) {
113 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
114 }
115
116 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
117
118 $wgOut->addWikiText( $notice );
119 }
120
121 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
122 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
123
124 # Show an appropriate message if the user isn't allowed or able to change
125 # the protection settings at this time
126 if( $this->disabled ) {
127 if( $wgUser->isAllowed( 'protect' ) ) {
128 if( $wgUser->isBlocked() ) {
129 # Blocked
130 $message = 'protect-locked-blocked';
131 } else {
132 # Database lock
133 $message = 'protect-locked-dblock';
134 }
135 } else {
136 # Permission error
137 $message = 'protect-locked-access';
138 }
139 } else {
140 $message = 'protect-text';
141 }
142 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
143
144 $wgOut->addHTML( $this->buildForm() );
145
146 $this->showLogExtract( $wgOut );
147 }
148
149 function save() {
150 global $wgRequest, $wgUser, $wgOut;
151
152 if( $this->disabled ) {
153 $this->show();
154 return false;
155 }
156
157 $token = $wgRequest->getVal( 'wpEditToken' );
158 if( !$wgUser->matchEditToken( $token ) ) {
159 $this->show( wfMsg( 'sessionfailure' ) );
160 return false;
161 }
162
163 if ( strlen( $this->mExpiry ) == 0 ) {
164 $this->mExpiry = 'infinite';
165 }
166
167 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
168 $expiry = Block::infinity();
169 } else {
170 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
171 $expiry = strtotime( $this->mExpiry );
172
173 if ( $expiry < 0 || $expiry === false ) {
174 $this->show( wfMsg( 'protect_expiry_invalid' ) );
175 return false;
176 }
177
178 $expiry = wfTimestamp( TS_MW, $expiry );
179
180 if ( $expiry < wfTimestampNow() ) {
181 $this->show( wfMsg( 'protect_expiry_old' ) );
182 return false;
183 }
184
185 }
186
187 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
188 if( !$ok ) {
189 throw new FatalError( "Unknown error at restriction save time." );
190 }
191
192 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
193 $this->mArticle->doWatch();
194 } elseif( $this->mTitle->userIsWatching() ) {
195 $this->mArticle->doUnwatch();
196 }
197
198 return $ok;
199 }
200
201 function buildForm() {
202 global $wgUser;
203
204 $out = '';
205 if( !$this->disabled ) {
206 $out .= $this->buildScript();
207 // The submission needs to reenable the move permission selector
208 // if it's in locked mode, or some browsers won't submit the data.
209 $out .= wfOpenElement( 'form', array(
210 'id' => 'mw-Protect-Form',
211 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
212 'method' => 'post',
213 'onsubmit' => 'protectEnable(true)' ) );
214
215 $out .= wfElement( 'input', array(
216 'type' => 'hidden',
217 'name' => 'wpEditToken',
218 'value' => $wgUser->editToken() ) );
219 }
220
221 $out .= "<table id='mwProtectSet'>";
222 $out .= "<tbody>";
223 $out .= "<tr>\n";
224 foreach( $this->mRestrictions as $action => $required ) {
225 /* Not all languages have V_x <-> N_x relation */
226 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
227 }
228 $out .= "</tr>\n";
229 $out .= "<tr>\n";
230 foreach( $this->mRestrictions as $action => $selected ) {
231 $out .= "<td>\n";
232 $out .= $this->buildSelector( $action, $selected );
233 $out .= "</td>\n";
234 }
235 $out .= "</tr>\n";
236
237 // JavaScript will add another row with a value-chaining checkbox
238
239 $out .= "</tbody>\n";
240 $out .= "</table>\n";
241
242 $out .= "<table>\n";
243 $out .= "<tbody>\n";
244
245 global $wgEnableCascadingProtection;
246 if( $wgEnableCascadingProtection )
247 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
248
249 if( !$this->disabled )
250 $out .= '<tr><td></td><td>' . $this->buildWatchInput() . "</td></tr>\n";
251
252 $out .= $this->buildExpiryInput();
253
254 if( !$this->disabled ) {
255 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
256 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
257 }
258
259 $out .= "</tbody>\n";
260 $out .= "</table>\n";
261
262 if ( !$this->disabled ) {
263 $out .= "</form>\n";
264 $out .= $this->buildCleanupScript();
265 }
266
267 return $out;
268 }
269
270 function buildSelector( $action, $selected ) {
271 global $wgRestrictionLevels;
272 $id = 'mwProtect-level-' . $action;
273 $attribs = array(
274 'id' => $id,
275 'name' => $id,
276 'size' => count( $wgRestrictionLevels ),
277 'onchange' => 'protectLevelsUpdate(this)',
278 ) + $this->disabledAttrib;
279
280 $out = wfOpenElement( 'select', $attribs );
281 foreach( $wgRestrictionLevels as $key ) {
282 $out .= $this->buildOption( $key, $selected );
283 }
284 $out .= "</select>\n";
285 return $out;
286 }
287
288 function buildOption( $key, $selected ) {
289 $text = ( $key == '' )
290 ? wfMsg( 'protect-default' )
291 : wfMsg( "protect-level-$key" );
292 $selectedAttrib = ($selected == $key)
293 ? array( 'selected' => 'selected' )
294 : array();
295 return wfElement( 'option',
296 array( 'value' => $key ) + $selectedAttrib,
297 $text );
298 }
299
300 function buildReasonInput() {
301 $id = 'mwProtect-reason';
302 return wfElement( 'label', array(
303 'id' => "$id-label",
304 'for' => $id ),
305 wfMsg( 'protectcomment' ) ) .
306 '</td><td>' .
307 wfElement( 'input', array(
308 'size' => 60,
309 'name' => $id,
310 'id' => $id,
311 'value' => $this->mReason ) );
312 }
313
314 function buildCascadeInput() {
315 $id = 'mwProtect-cascade';
316 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
317 return $ci;
318 }
319
320 function buildExpiryInput() {
321 $id = 'mwProtect-expiry';
322
323 $ci = "<tr><td align=\"right\">";
324 $ci .= wfElement( 'label', array (
325 'id' => "$id-label",
326 'for' => $id ),
327 wfMsg( 'protectexpiry' ) );
328 $ci .= "</td> <td align=\"left\">";
329 $ci .= wfElement( 'input', array(
330 'size' => 60,
331 'name' => $id,
332 'id' => $id,
333 'value' => $this->mExpiry ) + $this->disabledAttrib );
334 $ci .= "</td></tr>";
335
336 return $ci;
337 }
338
339 function buildWatchInput() {
340 global $wgUser;
341 return Xml::checkLabel(
342 wfMsg( 'watchthis' ),
343 'mwProtectWatch',
344 'mwProtectWatch',
345 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' )
346 );
347 }
348
349 function buildSubmit() {
350 return wfElement( 'input', array(
351 'id' => 'mw-Protect-submit',
352 'type' => 'submit',
353 'value' => wfMsg( 'confirm' ) ) );
354 }
355
356 function buildScript() {
357 global $wgStylePath, $wgStyleVersion;
358 return '<script type="text/javascript" src="' .
359 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
360 '"></script>';
361 }
362
363 function buildCleanupScript() {
364 global $wgRestrictionLevels, $wgGroupPermissions;
365 $script = 'var wgCascadeableLevels=';
366 $CascadeableLevels = array();
367 foreach( $wgRestrictionLevels as $key ) {
368 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
369 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
370 }
371 }
372 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
373 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
374 return '<script type="text/javascript">' . $script . '</script>';
375 }
376
377 /**
378 * @param OutputPage $out
379 * @access private
380 */
381 function showLogExtract( &$out ) {
382 # Show relevant lines from the protection log:
383 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
384 $logViewer = new LogViewer(
385 new LogReader(
386 new FauxRequest(
387 array( 'page' => $this->mTitle->getPrefixedText(),
388 'type' => 'protect' ) ) ) );
389 $logViewer->showList( $out );
390 }
391 }
392
393 ?>