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