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