From e865b57cbe12985a858e5728570b1fc035fc138e Mon Sep 17 00:00:00 2001 From: David Barratt Date: Fri, 23 Nov 2018 00:08:27 -0500 Subject: [PATCH] Add Namespace Restrictions to Special:BlockList The editing restrictions will be split by type and add a heading for each type. The namespace will be linked to Special:AllPages with the namespace set so the user can see what pages the user is blocked from. Bug: T204990 Change-Id: Idb1de20c1a780562b072ea350e5ba7dd1518d177 --- includes/specials/pagers/BlockListPager.php | 62 ++++++++++++++----- languages/i18n/en.json | 2 + languages/i18n/qqq.json | 2 + .../specials/pagers/BlockListPagerTest.php | 24 +++++-- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/includes/specials/pagers/BlockListPager.php b/includes/specials/pagers/BlockListPager.php index 205ffbf8b0..8fc586bedd 100644 --- a/includes/specials/pagers/BlockListPager.php +++ b/includes/specials/pagers/BlockListPager.php @@ -24,6 +24,8 @@ */ use MediaWiki\Block\BlockRestriction; use MediaWiki\Block\Restriction\Restriction; +use MediaWiki\Block\Restriction\PageRestriction; +use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\MediaWikiServices; use Wikimedia\Rdbms\IResultWrapper; @@ -198,7 +200,7 @@ class BlockListPager extends TablePager { } if ( !$row->ipb_sitewide && $this->restrictions ) { - $list = $this->getRestrictionListHTML( $this->restrictions, $row ); + $list = $this->getRestrictionListHTML( $row ); if ( $list ) { $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list; } @@ -246,41 +248,69 @@ class BlockListPager extends TablePager { /** * Get Restriction List HTML * - * @param Restriction[] $restrictions * @param stdClass $row * * @return string */ - private static function getRestrictionListHTML( - array $restrictions, - stdClass $row - ) { + private function getRestrictionListHTML( stdClass $row ) { $items = []; - foreach ( $restrictions as $restriction ) { + foreach ( $this->restrictions as $restriction ) { if ( $restriction->getBlockId() !== (int)$row->ipb_id ) { continue; } - if ( $restriction->getType() !== 'page' ) { - continue; + switch ( $restriction->getType() ) { + case PageRestriction::TYPE: + $items[$restriction->getType()][] = HTML::rawElement( + 'li', + [], + Linker::link( $restriction->getTitle() ) + ); + break; + case NamespaceRestriction::TYPE: + $text = $restriction->getValue() === NS_MAIN + ? $this->msg( 'blanknamespace' ) + : $this->getLanguage()->getFormattedNsText( + $restriction->getValue() + ); + $items[$restriction->getType()][] = HTML::rawElement( + 'li', + [], + Linker::link( + SpecialPage::getTitleValueFor( 'Allpages' ), + $text, + [], + [ + 'namespace' => $restriction->getValue() + ] + ) + ); + break; } - - $items[] = Html::rawElement( - 'li', - [], - Linker::link( $restriction->getTitle() ) - ); } if ( empty( $items ) ) { return ''; } + $sets = []; + foreach ( $items as $key => $value ) { + $sets[] = Html::rawElement( + 'li', + [], + $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement( + 'ul', + [], + implode( '', $value ) + ) + ); + } + return Html::rawElement( 'ul', [], - implode( '', $items ) + implode( '', $sets ) ); } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 86da5d98d2..e86c6fa125 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -2665,6 +2665,8 @@ "blocklist-nousertalk": "cannot edit own talk page", "blocklist-editing": "editing", "blocklist-editing-sitewide": "editing (sitewide)", + "blocklist-editing-page": "pages", + "blocklist-editing-ns": "namespaces", "ipblocklist-empty": "The block list is empty.", "ipblocklist-no-results": "The requested IP address or username is not blocked.", "blocklink": "block", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 58c2b9739d..2fc5086735 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -2870,6 +2870,8 @@ "blocklist-nousertalk": "Used in [[Special:IPBlockList]] when \"Allow this user to edit own talk page while blocked\" option hasn't been flagged.\n\nSee also {{msg-mw|Block-log-flags-nousertalk}}.\n\nPart of the log entry of user block in [[Special:BlockList]].\n\n{{Related|Blocklist}}", "blocklist-editing": "Used in [[Special:IPBlockList]] when a block is not a sitewide block.", "blocklist-editing-sitewide": "Used in [[Special:IPBlockList]] when a block is a sitewide block.", + "blocklist-editing-page": "Used in [[Special:IPBlockList]] when a partial block has page restrictions.", + "blocklist-editing-ns": "Used in [[Special:IPBlockList]] when a partial block has namespace restrictions.", "ipblocklist-empty": "Used in [[Special:BlockList]], if the target is not specified.\n\nSee also:\n* {{msg-mw|Ipblocklist-no-results}}", "ipblocklist-no-results": "Used in [[Special:BlockList]], if the target is specified.\n\nSee also:\n* {{msg-mw|Ipblocklist-empty}}", "blocklink": "Display name for a link that, when selected, leads to a form where a user can be blocked. Used in page history and recent changes pages. Example: \"''UserName (Talk | contribs | '''block''')''\".\n\nUsed as link title in [[Special:Contributions]] and in [[Special:DeletedContributions]].\n\nSee also:\n* {{msg-mw|Sp-contributions-talk}}\n* {{msg-mw|Change-blocklink}}\n* {{msg-mw|Unblocklink}}\n* {{msg-mw|Sp-contributions-blocklog}}\n* {{msg-mw|Sp-contributions-uploads}}\n* {{msg-mw|Sp-contributions-logs}}\n* {{msg-mw|Sp-contributions-deleted}}\n* {{msg-mw|Sp-contributions-userrights}}\n{{Identical|Block}}", diff --git a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php index 80df1d0866..570291cd6e 100644 --- a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php +++ b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php @@ -1,6 +1,7 @@ setMwGlobals( 'wgArticlePath', '/wiki/$1' ); + $this->setMwGlobals( [ + 'wgArticlePath' => '/wiki/$1', + 'wgScript' => '/w/index.php', + ] ); $pager = new BlockListPager( new SpecialPage(), [] ); @@ -134,7 +139,8 @@ class BlockListPagerTest extends MediaWikiTestCase { $pageId = $page['id']; $restrictions = [ - ( new PageRestriction( 0, $pageId ) )->setTitle( $title ) + ( new PageRestriction( 0, $pageId ) )->setTitle( $title ), + new NamespaceRestriction( 0, NS_MAIN ) ]; $wrappedPager = TestingAccessWrapper::newFromObject( $pager ); @@ -146,11 +152,21 @@ class BlockListPagerTest extends MediaWikiTestCase { // and must not depend on a localisation message. // TODO: Mock the message or consider using qqx. . wfMessage( 'blocklist-editing' )->text() - . '', + . '
  • ' + . wfMessage( 'blocklist-editing-ns' )->text() + . '
  • ', $formatted ); } -- 2.20.1