MediaWiki  master
SpecialBlockList.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\IPUtils;
33 
40  protected $target;
41 
42  protected $options;
43 
44  protected $blockType;
45 
47  private $linkBatchFactory;
48 
50  private $blockRestrictionStore;
51 
53  private $loadBalancer;
54 
56  private $commentStore;
57 
59  private $blockUtils;
60 
62  private $blockActionInfo;
63 
65  private $rowCommentFormatter;
66 
67  public function __construct(
68  LinkBatchFactory $linkBatchFactory,
69  BlockRestrictionStore $blockRestrictionStore,
70  ILoadBalancer $loadBalancer,
71  CommentStore $commentStore,
72  BlockUtils $blockUtils,
73  BlockActionInfo $blockActionInfo,
74  RowCommentFormatter $rowCommentFormatter
75  ) {
76  parent::__construct( 'BlockList' );
77 
78  $this->linkBatchFactory = $linkBatchFactory;
79  $this->blockRestrictionStore = $blockRestrictionStore;
80  $this->loadBalancer = $loadBalancer;
81  $this->commentStore = $commentStore;
82  $this->blockUtils = $blockUtils;
83  $this->blockActionInfo = $blockActionInfo;
84  $this->rowCommentFormatter = $rowCommentFormatter;
85  }
86 
90  public function execute( $par ) {
91  $this->setHeaders();
92  $this->outputHeader();
93  $this->addHelpLink( 'Help:Blocking_users' );
94  $out = $this->getOutput();
95  $out->setPageTitle( $this->msg( 'ipblocklist' ) );
96  $out->addModuleStyles( [ 'mediawiki.special' ] );
97 
98  $request = $this->getRequest();
99  $par = $request->getVal( 'ip', $par ?? '' );
100  $this->target = trim( $request->getVal( 'wpTarget', $par ) );
101 
102  $this->options = $request->getArray( 'wpOptions', [] );
103  $this->blockType = $request->getVal( 'blockType' );
104 
105  $action = $request->getText( 'action' );
106 
107  if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
108  # B/C @since 1.18: Unblock interface is now at Special:Unblock
109  $title = $this->getSpecialPageFactory()->getTitleForAlias( 'Unblock/' . $this->target );
110  $out->redirect( $title->getFullURL() );
111 
112  return;
113  }
114 
115  # setup BlockListPager here to get the actual default Limit
116  $pager = $this->getBlockListPager();
117 
118  # Just show the block list
119  $fields = [
120  'Target' => [
121  'type' => 'user',
122  'label-message' => 'ipaddressorusername',
123  'tabindex' => '1',
124  'size' => '45',
125  'default' => $this->target,
126  ],
127  'Options' => [
128  'type' => 'multiselect',
129  'options-messages' => [
130  'blocklist-tempblocks' => 'tempblocks',
131  'blocklist-indefblocks' => 'indefblocks',
132  'blocklist-autoblocks' => 'autoblocks',
133  'blocklist-userblocks' => 'userblocks',
134  'blocklist-addressblocks' => 'addressblocks',
135  'blocklist-rangeblocks' => 'rangeblocks',
136  ],
137  'flatlist' => true,
138  ],
139  ];
140 
141  $fields['BlockType'] = [
142  'type' => 'select',
143  'label-message' => 'blocklist-type',
144  'options' => [
145  $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
146  $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
147  $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
148  ],
149  'name' => 'blockType',
150  'cssclass' => 'mw-field-block-type',
151  ];
152 
153  $fields['Limit'] = [
154  'type' => 'limitselect',
155  'label-message' => 'table_pager_limit_label',
156  'options' => $pager->getLimitSelectList(),
157  'name' => 'limit',
158  'default' => $pager->getLimit(),
159  'cssclass' => 'mw-field-limit mw-has-field-block-type',
160  ];
161 
162  $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
163  $form
164  ->setMethod( 'get' )
165  ->setTitle( $this->getPageTitle() ) // Remove subpage
166  ->setFormIdentifier( 'blocklist' )
167  ->setWrapperLegendMsg( 'ipblocklist-legend' )
168  ->setSubmitTextMsg( 'ipblocklist-submit' )
169  ->prepareForm()
170  ->displayForm( false );
171 
172  $this->showList( $pager );
173  }
174 
179  protected function getBlockListPager() {
180  $conds = [];
181  $db = $this->getDB();
182  # Is the user allowed to see hidden blocks?
183  if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) {
184  $conds['ipb_deleted'] = 0;
185  }
186 
187  if ( $this->target !== '' ) {
188  [ $target, $type ] = $this->blockUtils->parseBlockTarget( $this->target );
189 
190  switch ( $type ) {
191  case DatabaseBlock::TYPE_ID:
192  case DatabaseBlock::TYPE_AUTO:
193  $conds['ipb_id'] = $target;
194  break;
195 
196  case DatabaseBlock::TYPE_IP:
197  case DatabaseBlock::TYPE_RANGE:
198  [ $start, $end ] = IPUtils::parseRange( $target );
199  $conds[] = $db->makeList(
200  [
201  'ipb_address' => $target,
202  DatabaseBlock::getRangeCond( $start, $end )
203  ],
204  LIST_OR
205  );
206  $conds['ipb_auto'] = 0;
207  break;
208 
209  case DatabaseBlock::TYPE_USER:
210  $conds['ipb_address'] = $target->getName();
211  $conds['ipb_auto'] = 0;
212  break;
213  }
214  }
215 
216  # Apply filters
217  if ( in_array( 'userblocks', $this->options ) ) {
218  $conds['ipb_user'] = 0;
219  }
220  if ( in_array( 'autoblocks', $this->options ) ) {
221  // ipb_parent_block_id = 0 because of T282890
222  $conds[] = "ipb_parent_block_id IS NULL OR ipb_parent_block_id = 0";
223  }
224  if ( in_array( 'addressblocks', $this->options ) ) {
225  $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
226  }
227  if ( in_array( 'rangeblocks', $this->options ) ) {
228  $conds[] = "ipb_range_end = ipb_range_start";
229  }
230 
231  $hideTemp = in_array( 'tempblocks', $this->options );
232  $hideIndef = in_array( 'indefblocks', $this->options );
233  if ( $hideTemp && $hideIndef ) {
234  // If both types are hidden, ensure query doesn't produce any results
235  $conds[] = '1=0';
236  } elseif ( $hideTemp ) {
237  $conds['ipb_expiry'] = $db->getInfinity();
238  } elseif ( $hideIndef ) {
239  $conds[] = "ipb_expiry != " . $db->addQuotes( $db->getInfinity() );
240  }
241 
242  if ( $this->blockType === 'sitewide' ) {
243  $conds['ipb_sitewide'] = 1;
244  } elseif ( $this->blockType === 'partial' ) {
245  $conds['ipb_sitewide'] = 0;
246  }
247 
248  return new BlockListPager(
249  $this->getContext(),
250  $this->blockActionInfo,
251  $this->blockRestrictionStore,
252  $this->blockUtils,
253  $this->commentStore,
254  $this->linkBatchFactory,
255  $this->getLinkRenderer(),
256  $this->loadBalancer,
257  $this->rowCommentFormatter,
258  $this->getSpecialPageFactory(),
259  $conds
260  );
261  }
262 
267  protected function showList( BlockListPager $pager ) {
268  $out = $this->getOutput();
269 
270  # Check for other blocks, i.e. global/tor blocks
271  $otherBlockLink = [];
272  $this->getHookRunner()->onOtherBlockLogLink( $otherBlockLink, $this->target );
273 
274  # Show additional header for the local block only when other blocks exists.
275  # Not necessary in a standard installation without such extensions enabled
276  if ( count( $otherBlockLink ) ) {
277  $out->addHTML(
278  Html::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
279  );
280  }
281 
282  if ( $pager->getNumRows() ) {
283  $out->addParserOutputContent( $pager->getFullOutput() );
284  } elseif ( $this->target ) {
285  $out->addWikiMsg( 'ipblocklist-no-results' );
286  } else {
287  $out->addWikiMsg( 'ipblocklist-empty' );
288  }
289 
290  if ( count( $otherBlockLink ) ) {
291  $out->addHTML(
293  'h2',
294  [],
295  $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
296  ) . "\n"
297  );
298  $list = '';
299  foreach ( $otherBlockLink as $link ) {
300  $list .= Html::rawElement( 'li', [], $link ) . "\n";
301  }
302  $out->addHTML( Html::rawElement(
303  'ul',
304  [ 'class' => 'mw-ipblocklist-otherblocks' ],
305  $list
306  ) . "\n" );
307  }
308  }
309 
310  protected function getGroupName() {
311  return 'users';
312  }
313 
319  protected function getDB() {
320  return $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
321  }
322 }
const LIST_OR
Definition: Defines.php:46
Handle database storage of comments such as edit summaries and log reasons.
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:349
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
getNumRows()
Get the number of rows in the result set.
Definition: IndexPager.php:735
Defines the actions that can be blocked by a partial block.
Backend class for blocking utils.
Definition: BlockUtils.php:46
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
This is basically a CommentFormatter with a CommentStore dependency, allowing it to retrieve comment ...
A special page that lists existing blocks.
getDB()
Return a IDatabase object for reading.
showList(BlockListPager $pager)
Show the list of blocked accounts matching the actual filter.
__construct(LinkBatchFactory $linkBatchFactory, BlockRestrictionStore $blockRestrictionStore, ILoadBalancer $loadBalancer, CommentStore $commentStore, BlockUtils $blockUtils, BlockActionInfo $blockActionInfo, RowCommentFormatter $rowCommentFormatter)
getBlockListPager()
Setup a new BlockListPager instance.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Parent class for all special pages.
Definition: SpecialPage.php:44
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
getFullOutput()
Get the formatted result list, with navigation bars.
Definition: TablePager.php:107
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
Create and track the database connections and transactions for a given database cluster.
const DB_REPLICA
Definition: defines.php:26