BlockListPager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Pager
  20. */
  21. /**
  22. * @ingroup Pager
  23. */
  24. use MediaWiki\Block\DatabaseBlock;
  25. use MediaWiki\Block\Restriction\Restriction;
  26. use MediaWiki\Block\Restriction\PageRestriction;
  27. use MediaWiki\Block\Restriction\NamespaceRestriction;
  28. use MediaWiki\MediaWikiServices;
  29. use Wikimedia\Rdbms\IResultWrapper;
  30. class BlockListPager extends TablePager {
  31. protected $conds;
  32. /**
  33. * Array of restrictions.
  34. *
  35. * @var Restriction[]
  36. */
  37. protected $restrictions = [];
  38. /**
  39. * @param SpecialPage $page
  40. * @param array $conds
  41. */
  42. public function __construct( $page, $conds ) {
  43. parent::__construct( $page->getContext(), $page->getLinkRenderer() );
  44. $this->conds = $conds;
  45. $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
  46. }
  47. function getFieldNames() {
  48. static $headers = null;
  49. if ( $headers === null ) {
  50. $headers = [
  51. 'ipb_timestamp' => 'blocklist-timestamp',
  52. 'ipb_target' => 'blocklist-target',
  53. 'ipb_expiry' => 'blocklist-expiry',
  54. 'ipb_by' => 'blocklist-by',
  55. 'ipb_params' => 'blocklist-params',
  56. 'ipb_reason' => 'blocklist-reason',
  57. ];
  58. foreach ( $headers as $key => $val ) {
  59. $headers[$key] = $this->msg( $val )->text();
  60. }
  61. }
  62. return $headers;
  63. }
  64. /**
  65. * @param string $name
  66. * @param string $value
  67. * @return string
  68. * @suppress PhanTypeArraySuspicious
  69. */
  70. function formatValue( $name, $value ) {
  71. static $msg = null;
  72. if ( $msg === null ) {
  73. $keys = [
  74. 'anononlyblock',
  75. 'createaccountblock',
  76. 'noautoblockblock',
  77. 'emailblock',
  78. 'blocklist-nousertalk',
  79. 'unblocklink',
  80. 'change-blocklink',
  81. 'blocklist-editing',
  82. 'blocklist-editing-sitewide',
  83. ];
  84. foreach ( $keys as $key ) {
  85. $msg[$key] = $this->msg( $key )->text();
  86. }
  87. }
  88. /** @var object $row */
  89. $row = $this->mCurrentRow;
  90. $language = $this->getLanguage();
  91. $formatted = '';
  92. $linkRenderer = $this->getLinkRenderer();
  93. switch ( $name ) {
  94. case 'ipb_timestamp':
  95. $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
  96. break;
  97. case 'ipb_target':
  98. if ( $row->ipb_auto ) {
  99. $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
  100. } else {
  101. list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
  102. switch ( $type ) {
  103. case DatabaseBlock::TYPE_USER:
  104. case DatabaseBlock::TYPE_IP:
  105. $formatted = Linker::userLink( $target->getId(), $target );
  106. $formatted .= Linker::userToolLinks(
  107. $target->getId(),
  108. $target,
  109. false,
  110. Linker::TOOL_LINKS_NOBLOCK
  111. );
  112. break;
  113. case DatabaseBlock::TYPE_RANGE:
  114. $formatted = htmlspecialchars( $target );
  115. }
  116. }
  117. break;
  118. case 'ipb_expiry':
  119. $formatted = htmlspecialchars( $language->formatExpiry(
  120. $value,
  121. /* User preference timezone */true
  122. ) );
  123. if ( MediaWikiServices::getInstance()
  124. ->getPermissionManager()
  125. ->userHasRight( $this->getUser(), 'block' )
  126. ) {
  127. $links = [];
  128. if ( $row->ipb_auto ) {
  129. $links[] = $linkRenderer->makeKnownLink(
  130. SpecialPage::getTitleFor( 'Unblock' ),
  131. $msg['unblocklink'],
  132. [],
  133. [ 'wpTarget' => "#{$row->ipb_id}" ]
  134. );
  135. } else {
  136. $links[] = $linkRenderer->makeKnownLink(
  137. SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
  138. $msg['unblocklink']
  139. );
  140. $links[] = $linkRenderer->makeKnownLink(
  141. SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
  142. $msg['change-blocklink']
  143. );
  144. }
  145. $formatted .= ' ' . Html::rawElement(
  146. 'span',
  147. [ 'class' => 'mw-blocklist-actions' ],
  148. $this->msg( 'parentheses' )->rawParams(
  149. $language->pipeList( $links ) )->escaped()
  150. );
  151. }
  152. if ( $value !== 'infinity' ) {
  153. $timestamp = new MWTimestamp( $value );
  154. $formatted .= '<br />' . $this->msg(
  155. 'ipb-blocklist-duration-left',
  156. $language->formatDuration(
  157. $timestamp->getTimestamp() - MWTimestamp::time(),
  158. // reasonable output
  159. [
  160. 'minutes',
  161. 'hours',
  162. 'days',
  163. 'years',
  164. ]
  165. )
  166. )->escaped();
  167. }
  168. break;
  169. case 'ipb_by':
  170. if ( isset( $row->by_user_name ) ) {
  171. $formatted = Linker::userLink( $value, $row->by_user_name );
  172. $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
  173. } else {
  174. $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
  175. }
  176. break;
  177. case 'ipb_reason':
  178. $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
  179. $formatted = Linker::formatComment( $value );
  180. break;
  181. case 'ipb_params':
  182. $properties = [];
  183. if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
  184. $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
  185. }
  186. if ( !$row->ipb_sitewide && $this->restrictions ) {
  187. $list = $this->getRestrictionListHTML( $row );
  188. if ( $list ) {
  189. $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
  190. }
  191. }
  192. if ( $row->ipb_anon_only ) {
  193. $properties[] = htmlspecialchars( $msg['anononlyblock'] );
  194. }
  195. if ( $row->ipb_create_account ) {
  196. $properties[] = htmlspecialchars( $msg['createaccountblock'] );
  197. }
  198. if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
  199. $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
  200. }
  201. if ( $row->ipb_block_email ) {
  202. $properties[] = htmlspecialchars( $msg['emailblock'] );
  203. }
  204. if ( !$row->ipb_allow_usertalk ) {
  205. $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
  206. }
  207. $formatted = Html::rawElement(
  208. 'ul',
  209. [],
  210. implode( '', array_map( function ( $prop ) {
  211. return Html::rawElement(
  212. 'li',
  213. [],
  214. $prop
  215. );
  216. }, $properties ) )
  217. );
  218. break;
  219. default:
  220. $formatted = "Unable to format $name";
  221. break;
  222. }
  223. return $formatted;
  224. }
  225. /**
  226. * Get Restriction List HTML
  227. *
  228. * @param stdClass $row
  229. *
  230. * @return string
  231. */
  232. private function getRestrictionListHTML( stdClass $row ) {
  233. $items = [];
  234. $linkRenderer = $this->getLinkRenderer();
  235. foreach ( $this->restrictions as $restriction ) {
  236. if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
  237. continue;
  238. }
  239. switch ( $restriction->getType() ) {
  240. case PageRestriction::TYPE:
  241. '@phan-var PageRestriction $restriction';
  242. if ( $restriction->getTitle() ) {
  243. $items[$restriction->getType()][] = Html::rawElement(
  244. 'li',
  245. [],
  246. $linkRenderer->makeLink( $restriction->getTitle() )
  247. );
  248. }
  249. break;
  250. case NamespaceRestriction::TYPE:
  251. $text = $restriction->getValue() === NS_MAIN
  252. ? $this->msg( 'blanknamespace' )->text()
  253. : $this->getLanguage()->getFormattedNsText(
  254. $restriction->getValue()
  255. );
  256. $items[$restriction->getType()][] = Html::rawElement(
  257. 'li',
  258. [],
  259. $linkRenderer->makeLink(
  260. SpecialPage::getTitleValueFor( 'Allpages' ),
  261. $text,
  262. [],
  263. [
  264. 'namespace' => $restriction->getValue()
  265. ]
  266. )
  267. );
  268. break;
  269. }
  270. }
  271. if ( empty( $items ) ) {
  272. return '';
  273. }
  274. $sets = [];
  275. foreach ( $items as $key => $value ) {
  276. $sets[] = Html::rawElement(
  277. 'li',
  278. [],
  279. $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
  280. 'ul',
  281. [],
  282. implode( '', $value )
  283. )
  284. );
  285. }
  286. return Html::rawElement(
  287. 'ul',
  288. [],
  289. implode( '', $sets )
  290. );
  291. }
  292. function getQueryInfo() {
  293. $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
  294. $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
  295. $info = [
  296. 'tables' => array_merge(
  297. [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
  298. ),
  299. 'fields' => [
  300. 'ipb_id',
  301. 'ipb_address',
  302. 'ipb_user',
  303. 'by_user_name' => 'user_name',
  304. 'ipb_timestamp',
  305. 'ipb_auto',
  306. 'ipb_anon_only',
  307. 'ipb_create_account',
  308. 'ipb_enable_autoblock',
  309. 'ipb_expiry',
  310. 'ipb_range_start',
  311. 'ipb_range_end',
  312. 'ipb_deleted',
  313. 'ipb_block_email',
  314. 'ipb_allow_usertalk',
  315. 'ipb_sitewide',
  316. ] + $commentQuery['fields'] + $actorQuery['fields'],
  317. 'conds' => $this->conds,
  318. 'join_conds' => [
  319. 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
  320. ] + $commentQuery['joins'] + $actorQuery['joins']
  321. ];
  322. # Filter out any expired blocks
  323. $db = $this->getDatabase();
  324. $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
  325. # Is the user allowed to see hidden blocks?
  326. if ( !MediaWikiServices::getInstance()
  327. ->getPermissionManager()
  328. ->userHasRight( $this->getUser(), 'hideuser' )
  329. ) {
  330. $info['conds']['ipb_deleted'] = 0;
  331. }
  332. return $info;
  333. }
  334. /**
  335. * Get total number of autoblocks at any given time
  336. *
  337. * @return int Total number of unexpired active autoblocks
  338. */
  339. function getTotalAutoblocks() {
  340. $dbr = $this->getDatabase();
  341. $res = $dbr->selectField( 'ipblocks',
  342. 'COUNT(*)',
  343. [
  344. 'ipb_auto' => '1',
  345. 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
  346. ]
  347. );
  348. if ( $res ) {
  349. return $res;
  350. }
  351. return 0; // We found nothing
  352. }
  353. protected function getTableClass() {
  354. return parent::getTableClass() . ' mw-blocklist';
  355. }
  356. function getIndexField() {
  357. return 'ipb_timestamp';
  358. }
  359. function getDefaultSort() {
  360. return 'ipb_timestamp';
  361. }
  362. function isFieldSortable( $name ) {
  363. return false;
  364. }
  365. /**
  366. * Do a LinkBatch query to minimise database load when generating all these links
  367. * @param IResultWrapper $result
  368. */
  369. function preprocessResults( $result ) {
  370. # Do a link batch query
  371. $lb = new LinkBatch;
  372. $lb->setCaller( __METHOD__ );
  373. $partialBlocks = [];
  374. foreach ( $result as $row ) {
  375. $lb->add( NS_USER, $row->ipb_address );
  376. $lb->add( NS_USER_TALK, $row->ipb_address );
  377. if ( isset( $row->by_user_name ) ) {
  378. $lb->add( NS_USER, $row->by_user_name );
  379. $lb->add( NS_USER_TALK, $row->by_user_name );
  380. }
  381. if ( !$row->ipb_sitewide ) {
  382. $partialBlocks[] = $row->ipb_id;
  383. }
  384. }
  385. if ( $partialBlocks ) {
  386. // Mutations to the $row object are not persisted. The restrictions will
  387. // need be stored in a separate store.
  388. $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
  389. $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
  390. }
  391. $lb->execute();
  392. }
  393. }