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