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