SpecialBlockList.php 7.4 KB

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