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