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