Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / specials / SpecialBlockList.php
1 <?php
2 /**
3 * Implements Special:BlockList
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 use MediaWiki\Block\DatabaseBlock;
25
26 /**
27 * A special page that lists existing blocks
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialBlockList extends SpecialPage {
32 protected $target;
33
34 protected $options;
35
36 protected $blockType;
37
38 function __construct() {
39 parent::__construct( 'BlockList' );
40 }
41
42 /**
43 * @param string|null $par Title fragment
44 */
45 public function execute( $par ) {
46 $this->setHeaders();
47 $this->outputHeader();
48 $out = $this->getOutput();
49 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
50 $out->addModuleStyles( [ 'mediawiki.special' ] );
51
52 $request = $this->getRequest();
53 $par = $request->getVal( 'ip', $par );
54 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
55
56 $this->options = $request->getArray( 'wpOptions', [] );
57 $this->blockType = $request->getVal( 'blockType' );
58
59 $action = $request->getText( 'action' );
60
61 if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
62 # B/C @since 1.18: Unblock interface is now at Special:Unblock
63 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
64 $out->redirect( $title->getFullURL() );
65
66 return;
67 }
68
69 # setup BlockListPager here to get the actual default Limit
70 $pager = $this->getBlockListPager();
71
72 # Just show the block list
73 $fields = [
74 'Target' => [
75 'type' => 'user',
76 'label-message' => 'ipaddressorusername',
77 'tabindex' => '1',
78 'size' => '45',
79 'default' => $this->target,
80 ],
81 'Options' => [
82 'type' => 'multiselect',
83 'options-messages' => [
84 'blocklist-userblocks' => 'userblocks',
85 'blocklist-tempblocks' => 'tempblocks',
86 'blocklist-addressblocks' => 'addressblocks',
87 'blocklist-rangeblocks' => 'rangeblocks',
88 ],
89 'flatlist' => true,
90 ],
91 ];
92
93 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
94 $fields['BlockType'] = [
95 'type' => 'select',
96 'label-message' => 'blocklist-type',
97 'options' => [
98 $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
99 $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
100 $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
101 ],
102 'name' => 'blockType',
103 'cssclass' => 'mw-field-block-type',
104 ];
105 }
106
107 $fields['Limit'] = [
108 'type' => 'limitselect',
109 'label-message' => 'table_pager_limit_label',
110 'options' => $pager->getLimitSelectList(),
111 'name' => 'limit',
112 'default' => $pager->getLimit(),
113 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
114 'mw-field-limit mw-has-field-block-type' :
115 'mw-field-limit',
116 ];
117
118 $context = new DerivativeContext( $this->getContext() );
119 $context->setTitle( $this->getPageTitle() ); // Remove subpage
120 $form = HTMLForm::factory( 'ooui', $fields, $context );
121 $form
122 ->setMethod( 'get' )
123 ->setFormIdentifier( 'blocklist' )
124 ->setWrapperLegendMsg( 'ipblocklist-legend' )
125 ->setSubmitTextMsg( 'ipblocklist-submit' )
126 ->prepareForm()
127 ->displayForm( false );
128
129 $this->showList( $pager );
130 }
131
132 /**
133 * Setup a new BlockListPager instance.
134 * @return BlockListPager
135 */
136 protected function getBlockListPager() {
137 $conds = [];
138 # Is the user allowed to see hidden blocks?
139 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
140 $conds['ipb_deleted'] = 0;
141 }
142
143 if ( $this->target !== '' ) {
144 list( $target, $type ) = DatabaseBlock::parseTarget( $this->target );
145
146 switch ( $type ) {
147 case DatabaseBlock::TYPE_ID:
148 case DatabaseBlock::TYPE_AUTO:
149 $conds['ipb_id'] = $target;
150 break;
151
152 case DatabaseBlock::TYPE_IP:
153 case DatabaseBlock::TYPE_RANGE:
154 list( $start, $end ) = IP::parseRange( $target );
155 $conds[] = wfGetDB( DB_REPLICA )->makeList(
156 [
157 'ipb_address' => $target,
158 DatabaseBlock::getRangeCond( $start, $end )
159 ],
160 LIST_OR
161 );
162 $conds['ipb_auto'] = 0;
163 break;
164
165 case DatabaseBlock::TYPE_USER:
166 $conds['ipb_address'] = $target->getName();
167 $conds['ipb_auto'] = 0;
168 break;
169 }
170 }
171
172 # Apply filters
173 if ( in_array( 'userblocks', $this->options ) ) {
174 $conds['ipb_user'] = 0;
175 }
176 if ( in_array( 'tempblocks', $this->options ) ) {
177 $conds['ipb_expiry'] = 'infinity';
178 }
179 if ( in_array( 'addressblocks', $this->options ) ) {
180 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
181 }
182 if ( in_array( 'rangeblocks', $this->options ) ) {
183 $conds[] = "ipb_range_end = ipb_range_start";
184 }
185
186 if ( $this->blockType === 'sitewide' ) {
187 $conds[] = 'ipb_sitewide = 1';
188 } elseif ( $this->blockType === 'partial' ) {
189 $conds[] = 'ipb_sitewide = 0';
190 }
191
192 return new BlockListPager( $this, $conds );
193 }
194
195 /**
196 * Show the list of blocked accounts matching the actual filter.
197 * @param BlockListPager $pager The BlockListPager instance for this page
198 */
199 protected function showList( BlockListPager $pager ) {
200 $out = $this->getOutput();
201
202 # Check for other blocks, i.e. global/tor blocks
203 $otherBlockLink = [];
204 Hooks::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target ] );
205
206 # Show additional header for the local block only when other blocks exists.
207 # Not necessary in a standard installation without such extensions enabled
208 if ( count( $otherBlockLink ) ) {
209 $out->addHTML(
210 Html::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
211 );
212 }
213
214 if ( $pager->getNumRows() ) {
215 $out->addParserOutputContent( $pager->getFullOutput() );
216 } elseif ( $this->target ) {
217 $out->addWikiMsg( 'ipblocklist-no-results' );
218 } else {
219 $out->addWikiMsg( 'ipblocklist-empty' );
220 }
221
222 if ( count( $otherBlockLink ) ) {
223 $out->addHTML(
224 Html::rawElement(
225 'h2',
226 [],
227 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
228 ) . "\n"
229 );
230 $list = '';
231 foreach ( $otherBlockLink as $link ) {
232 $list .= Html::rawElement( 'li', [], $link ) . "\n";
233 }
234 $out->addHTML( Html::rawElement(
235 'ul',
236 [ 'class' => 'mw-ipblocklist-otherblocks' ],
237 $list
238 ) . "\n" );
239 }
240 }
241
242 protected function getGroupName() {
243 return 'users';
244 }
245 }