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