Special:ProtectedPages: Use HTMLForm
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedpages.php
1 <?php
2 /**
3 * Implements Special:Protectedpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that lists protected pages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialProtectedpages extends SpecialPage {
30 protected $IdLevel = 'level';
31 protected $IdType = 'type';
32
33 public function __construct() {
34 parent::__construct( 'Protectedpages' );
35 }
36
37 public function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
41
42 $request = $this->getRequest();
43 $type = $request->getVal( $this->IdType );
44 $level = $request->getVal( $this->IdLevel );
45 $sizetype = $request->getVal( 'size-mode' );
46 $size = $request->getIntOrNull( 'size' );
47 $ns = $request->getIntOrNull( 'namespace' );
48 $indefOnly = $request->getBool( 'indefonly' ) ? 1 : 0;
49 $cascadeOnly = $request->getBool( 'cascadeonly' ) ? 1 : 0;
50 $noRedirect = $request->getBool( 'noredirect' ) ? 1 : 0;
51
52 $pager = new ProtectedPagesPager(
53 $this,
54 [],
55 $type,
56 $level,
57 $ns,
58 $sizetype,
59 $size,
60 $indefOnly,
61 $cascadeOnly,
62 $noRedirect,
63 $this->getLinkRenderer()
64 );
65
66 $this->getOutput()->addHTML( $this->showOptions(
67 $ns,
68 $type,
69 $level,
70 $sizetype,
71 $size,
72 $indefOnly,
73 $cascadeOnly,
74 $noRedirect
75 ) );
76
77 if ( $pager->getNumRows() ) {
78 $this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
79 } else {
80 $this->getOutput()->addWikiMsg( 'protectedpagesempty' );
81 }
82 }
83
84 /**
85 * @param int $namespace
86 * @param string $type Restriction type
87 * @param string $level Restriction level
88 * @param string $sizetype "min" or "max"
89 * @param int $size
90 * @param bool $indefOnly Only indefinite protection
91 * @param bool $cascadeOnly Only cascading protection
92 * @param bool $noRedirect Don't show redirects
93 * @return string Input form
94 */
95 protected function showOptions( $namespace, $type = 'edit', $level, $sizetype,
96 $size, $indefOnly, $cascadeOnly, $noRedirect
97 ) {
98 $formDescriptor = [
99 'namespace' => $this->getNamespaceMenu( $namespace ),
100 'typemenu' => $this->getTypeMenu( $type ),
101 'levelmenu' => $this->getLevelMenu( $level ),
102
103 'expirycheck' => $this->getExpiryCheck( $indefOnly ),
104 'cascadecheck' => $this->getCascadeCheck( $cascadeOnly ),
105 'redirectcheck' => $this->getRedirectCheck( $noRedirect ),
106
107 'sizelimit' => $this->getSizeLimit( $sizetype, $size ),
108 ];
109 $htmlForm = new HTMLForm( $formDescriptor, $this->getContext() );
110 $htmlForm
111 ->setMethod( 'get' )
112 ->setWrapperLegendMsg( 'protectedpages' )
113 ->setSubmitText( $this->msg( 'protectedpages-submit' )->text() );
114
115 return $htmlForm->prepareForm()->getHTML( false );
116 }
117
118 /**
119 * Prepare the namespace filter drop-down; standard namespace
120 * selector, sans the MediaWiki namespace
121 *
122 * @param string|null $namespace Pre-select namespace
123 * @return array
124 */
125 protected function getNamespaceMenu( $namespace = null ) {
126 return [
127 'class' => 'HTMLSelectNamespace',
128 'name' => 'namespace',
129 'id' => 'namespace',
130 'cssclass' => 'namespaceselector',
131 'selected' => $namespace,
132 'all' => '',
133 'label' => $this->msg( 'namespace' )->text(),
134 ];
135 }
136
137 /**
138 * @param bool $indefOnly
139 * @return array
140 */
141 protected function getExpiryCheck( $indefOnly ) {
142 return [
143 'type' => 'check',
144 'label' => $this->msg( 'protectedpages-indef' )->text(),
145 'name' => 'indefonly',
146 'id' => 'indefonly',
147 'value' => $indefOnly
148 ];
149 }
150
151 /**
152 * @param bool $cascadeOnly
153 * @return array
154 */
155 protected function getCascadeCheck( $cascadeOnly ) {
156 return [
157 'type' => 'check',
158 'label' => $this->msg( 'protectedpages-cascade' )->text(),
159 'name' => 'cascadeonly',
160 'id' => 'cascadeonly',
161 'value' => $cascadeOnly
162 ];
163 }
164
165 /**
166 * @param bool $noRedirect
167 * @return array
168 */
169 protected function getRedirectCheck( $noRedirect ) {
170 return [
171 'type' => 'check',
172 'label' => $this->msg( 'protectedpages-noredirect' )->text(),
173 'name' => 'noredirect',
174 'id' => 'noredirect',
175 'value' => $noRedirect,
176 ];
177 }
178
179 /**
180 * @param string $sizetype "min" or "max"
181 * @param mixed $size
182 * @return array
183 */
184 protected function getSizeLimit( $sizetype, $size ) {
185 $max = $sizetype === 'max';
186
187 return [
188 'class' => 'HTMLSizeFilterField',
189 'name' => 'size',
190 ];
191 }
192
193 /**
194 * Creates the input label of the restriction type
195 * @param string $pr_type Protection type
196 * @return array
197 */
198 protected function getTypeMenu( $pr_type ) {
199 $m = []; // Temporary array
200 $options = [];
201
202 // First pass to load the log names
203 foreach ( Title::getFilteredRestrictionTypes( true ) as $type ) {
204 // Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
205 $text = $this->msg( "restriction-$type" )->text();
206 $m[$text] = $type;
207 }
208
209 // Third pass generates sorted XHTML content
210 foreach ( $m as $text => $type ) {
211 $options[$text] = $type;
212 }
213
214 return [
215 'type' => 'select',
216 'options' => $options,
217 'value' => $pr_type,
218 'label' => $this->msg( 'restriction-type' )->text(),
219 'name' => $this->IdType,
220 'id' => $this->IdType,
221 ];
222 }
223
224 /**
225 * Creates the input label of the restriction level
226 * @param string $pr_level Protection level
227 * @return array
228 */
229 protected function getLevelMenu( $pr_level ) {
230 // Temporary array
231 $m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
232 $options = [];
233
234 // First pass to load the log names
235 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
236 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
237 if ( $type != '' && $type != '*' ) {
238 $text = $this->msg( "restriction-level-$type" )->text();
239 $m[$text] = $type;
240 }
241 }
242
243 // Third pass generates sorted XHTML content
244 foreach ( $m as $text => $type ) {
245 $options[$text] = $type;
246 }
247
248 return [
249 'type' => 'select',
250 'options' => $options,
251 'value' => $pr_level,
252 'label' => $this->msg( 'restriction-level' )->text(),
253 'name' => $this->IdLevel,
254 'id' => $this->IdLevel
255 ];
256 }
257
258 protected function getGroupName() {
259 return 'maintenance';
260 }
261 }