Robert Vogel has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/364445 )
Change subject: Adding PSR-4 Autoloader and new NamespaceList
......................................................................
Adding PSR-4 Autoloader and new NamespaceList
Change-Id: I1931e2b355cacabbcdbe3af29d3e9a513e4a316f
---
A composer.json
M extension.json
M includes/NSFileRepoHooks.php
A src/Config.php
A src/Hooks/UnitTestsList.php
A src/Hooks/UploadFormBeforeProcessing.php
A src/Hooks/UploadFormInitDescriptor.php
A src/Hooks/UserCan.php
A src/MWNamespace.php
A src/NamespaceList.php
A tests/phpunit/NamespaceListTest.php
11 files changed, 637 insertions(+), 164 deletions(-)
Approvals:
Robert Vogel: Verified; Looks good to me, approved
ItSpiderman: Looks good to me, but someone else must approve
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..b183c89
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,17 @@
+{
+ "require-dev": {
+ "jakub-onderka/php-parallel-lint": "0.9.2",
+ "jakub-onderka/php-console-highlighter": "0.3.2"
+ },
+ "autoload" : {
+ "psr-4": {
+ "NSFileRepo\\Tests\\" : "tests/phpunit",
+ "NSFileRepo\\" : "src"
+ }
+ },
+ "scripts": {
+ "test": [
+ "parallel-lint . --exclude vendor"
+ ]
+ }
+}
diff --git a/extension.json b/extension.json
index b7e5f46..846989f 100644
--- a/extension.json
+++ b/extension.json
@@ -42,14 +42,24 @@
"remoteExtPath": "NSFileRepo/resources"
},
"Hooks": {
- "UploadForm:BeforeProcessing":
"NSFileRepoHooks::onUploadFormBeforeProcessing",
+ "UploadForm:BeforeProcessing":
"NSFileRepo\\Hooks\\UploadFormBeforeProcessing::handle",
"@note": "Note, this must be AFTER Extension:Lockdown has been
included - thus assuming that the user has access to files in general + files
at this particular namespace.",
- "userCan": "NSFileRepoHooks::onUserCan",
+ "userCan": "NSFileRepo\\Hooks\\UserCan::handle",
"BeforePageDisplay": "NSFileRepoHooks::onBeforePageDisplay",
- "UploadFormInitDescriptor":
"NSFileRepoHooks::onUploadFormInitDescriptor",
+ "UploadFormInitDescriptor":
"NSFileRepo\\Hooks\\UploadFormInitDescriptor::handle",
"ImgAuthBeforeCheckFileExists":
"NSFileRepoHooks::onImgAuthBeforeCheckFileExists",
"ImgAuthBeforeStream": "NSFileRepoHooks::onImgAuthBeforeStream",
- "UploadVerification": "NSFileRepoHooks::onUploadVerification"
+ "UploadVerification": "NSFileRepoHooks::onUploadVerification",
+ "UnitTestsList": "NSFileRepo\\Hooks\\UnitTestsList::handle"
},
- "manifest_version": 1
+ "config": {
+ "_prefix": "egNSFileRepo",
+ "@note": "This should probably be 3000 as recommended by
MediaWiki. But for backwards compatibility we'll stick with this default",
+ "NamespaceThreshold": 100,
+ "@note": "There are some widespread extensions that create
namespaces. E.g. SemanticMediaWiki, PageForms, Widgets, Scribunto, ...",
+ "NamespaceBlacklist": [ 102, 104, 106, 108, 274, 828 ],
+ "SkipTalk": true
+ },
+ "manifest_version": 1,
+ "load_composer_autoloader": true
}
\ No newline at end of file
diff --git a/includes/NSFileRepoHooks.php b/includes/NSFileRepoHooks.php
index 116a3bc..573f8f3 100644
--- a/includes/NSFileRepoHooks.php
+++ b/includes/NSFileRepoHooks.php
@@ -70,165 +70,6 @@
}
/**
- * Check for Namespace in Title line
- * @param UploadForm $uploadForm
- * @return boolean
- */
- public static function onUploadFormBeforeProcessing( &$uploadForm ) {
- $title = Title::newFromText( $uploadForm->mDesiredDestName );
- if( $title === null ) {
- return true;
- }
- if ( $title->getNamespace() < 100 ) {
- $uploadForm->mDesiredDestName = preg_replace( "/:/",
'-', $uploadForm->mDesiredDestName );
- } else {
- $bits = explode( ':', $uploadForm->mDesiredDestName );
- $ns = array_shift( $bits );
- $uploadForm->mDesiredDestName = $ns.":" . implode( "-",
$bits );
- }
- return true;
- }
-
- /**
- * Check individual namespace protection using Extension:Lockdown
- * @global array $wgWhitelistRead
- * @param Title $title
- * @param user $user
- * @param string $action
- * @param mixed $result
- * @return boolean
- */
- public static function onUserCan( &$title, &$user, $action, &$result) {
- global $wgWhitelistRead;
- if ( $wgWhitelistRead !== false && in_array(
$title->getPrefixedText(), $wgWhitelistRead ) ) {
- return true;
- }
-
- if( $title->getNamespace() !== NS_FILE ) {
- return true;
- }
-
- $ntitle = Title::newFromText( $title->getDBkey() );
- $ret_val = true;
-
- //Additional check for NS_MAIN: If a user is not allowed to
read NS_MAIN he should also be not allowed
- //to view files with no namespace-prefix as they are logically
assigned to namespace NS_MAIN
- if( $ntitle->getNamespace() < 100 || $ntitle->getNamespace()
=== NS_MAIN ) {
- $ret_val = lockdownUserPermissionsErrors( $ntitle,
$user, $action, $result );
- }
-
- $result = null;
- return $ret_val;
- }
-
- /**
- * Add fields to Special:Upload
- * @param array $descriptor
- * @return boolean
- */
- public static function onUploadFormInitDescriptor( &$descriptor ) {
- $sSelectedNamespace = '';
- //wpDestFile is set on query string. e.g after click on redlink
or on re-upload
- if( !empty( $descriptor['DestFile']['default'] ) ) {
- $oTarget = Title::newFromText(
$descriptor['DestFile']['default'] );
- $descriptor['DestFile']['default'] =
$oTarget->getText();
- $sSelectedNamespace = $oTarget->getNsText();
- }
-
- $aNamespaces = self::getPossibleNamespaces();
- $aOptions = array(
- wfMessage('nsfilerepo-nsmain')->plain() => ''
- );
- foreach($aNamespaces as $iNsId => $sNsText ) {
- if( $iNsId === NS_MAIN ) {
- continue;
- }
- $aOptions[$sNsText] = $sNsText;
- }
-
- $aFieldDef = array(
- 'NSFR_Namespace' => array (
- 'label' => wfMessage('namespace')->plain(),
- 'section' => 'description',
- 'class' => 'HTMLSelectField',
- 'options' => $aOptions,
- 'required' => true,
- 'default' => str_replace( ' ', '_',
$sSelectedNamespace )
- ),
- 'NSFR_DestFile' =>
- array (
- 'type' => 'text',
- 'section' => 'description',
- 'label-message' =>
'nsfilerepo-upload-target',
- 'size' => 60,
- 'default' => '',
- 'readonly' => true,
- 'nodata' => false,
- ),
- );
-
- $sPostion = array_search(
- 'UploadDescription',
- array_keys( $descriptor )
- );
-
- $descriptor =
- array_slice($descriptor, 0, $sPostion, true) +
- $aFieldDef +
- array_slice($descriptor, $sPostion, count($descriptor)
- 1, true) ;
-
- return true;
- }
-
-
- /**
- * Returns an Array of Namespaces, that can be used for NSFileRepo
- * @param Boolean $filterByPermissions
- * @param User $user
- * @return Array (NsIdx => NsLocalizedName)
- */
- protected static function getPossibleNamespaces( $filterByPermissions =
true, $user = null ) {
- $availableNamespaces = RequestContext::getMain()
- ->getLanguage()
- ->getNamespaces();
-
- foreach( $availableNamespaces as $nsIdx => $nsText ) {
- if( $nsIdx % 2 !== 0 || $nsIdx < 100 ) {
- unset( $availableNamespaces[$nsIdx] );
- }
- }
-
- if( !$filterByPermissions ) {
- return $availableNamespaces;
- }
-
- return static::filterNamespacesByPermission(
- $availableNamespaces,
- 'read',
- $user
- );
- }
-
- /**
- * Filter array of namespaces based on the user's permission
- * @param array $namespaces
- * @param string $permission
- * @param User $user
- * @return array
- */
- protected static function filterNamespacesByPermission( $namespaces,
$permission = 'read', $user = null ) {
- $aReturn = array();
- foreach($namespaces as $iNsId => $sNsText ) {
- $oTitle = Title::makeTitle( $iNsId, 'X' );
- if( $oTitle->userCan( $permission, $user ) === false ) {
- continue;
- }
- $aReturn[$iNsId] = $sNsText;
- }
- return $aReturn;
- }
-
- /**
* Checks if the destination file name contains a valid namespace prefix
* @param string $destName
* @param string $tempPath
diff --git a/src/Config.php b/src/Config.php
new file mode 100644
index 0000000..7de20fa
--- /dev/null
+++ b/src/Config.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace NSFileRepo;
+
+class Config extends \GlobalVarConfig {
+ const CONFIG_SKIP_TALK = 'SkipTalk';
+ const CONFIG_THRESHOLD = 'NamespaceThreshold';
+ const CONFIG_BLACKLIST = 'NamespaceBlacklist';
+
+ public function __construct() {
+ parent::__construct( 'egNSFileRepo' );
+ }
+}
diff --git a/src/Hooks/UnitTestsList.php b/src/Hooks/UnitTestsList.php
new file mode 100644
index 0000000..090b2f4
--- /dev/null
+++ b/src/Hooks/UnitTestsList.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace NSFileRepo\Hooks;
+
+class UnitTestsList {
+
+ /**
+ * Register PHP Unit Tests with MediaWiki framework
+ * @param array $paths
+ * @return boolean
+ */
+ public static function handle( &$paths ) {
+ $paths[] = dirname( dirname( __DIR__ ) ) . '/tests/phpunit/';
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/src/Hooks/UploadFormBeforeProcessing.php
b/src/Hooks/UploadFormBeforeProcessing.php
new file mode 100644
index 0000000..6f91816
--- /dev/null
+++ b/src/Hooks/UploadFormBeforeProcessing.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace NSFileRepo\Hooks;
+
+class UploadFormBeforeProcessing {
+
+ /**
+ *
+ * @var \Config
+ */
+ protected $config = null;
+
+ /**
+ *
+ * @var \IContextSource
+ */
+ protected $context = null;
+
+ /**
+ *
+ * @var \SpecialUpload
+ */
+ protected $uploadForm = null;
+
+ /**
+ * Check for Namespace in Title line
+ * @param SpecialUpload $uploadForm
+ * @return boolean
+ */
+ public static function handle( &$uploadForm ) {
+ $instance = new self(
+ \RequestContext::getMain(),
+ new \NSFileRepo\Config(),
+ $uploadForm
+ );
+
+ return $instance->process();
+ }
+
+ /**
+ * See static method "handle"
+ * @param \IContextSource $context
+ * @param \Config $config
+ * @param \SpecialUpload $uploadForm
+ */
+ public function __construct( \IContextSource $context, \Config $config,
\SpecialUpload $uploadForm ) {
+ $this->context = $context;
+ $this->config = $config;
+ $this->uploadForm = $uploadForm;
+ }
+
+ public function process() {
+ $title = \Title::newFromText(
$this->uploadForm->mDesiredDestName );
+ if( $title === null ) {
+ return true;
+ }
+ if ( $title->getNamespace() < $this->config->get(
'NamespaceThreshold' ) ) {
+ $this->uploadForm->mDesiredDestName = preg_replace(
"/:/", '-', $this->uploadForm->mDesiredDestName );
+ } else {
+ $bits = explode( ':',
$this->uploadForm->mDesiredDestName );
+ $ns = array_shift( $bits );
+ $this->uploadForm->mDesiredDestName = $ns.":" .
implode( "-", $bits );
+ }
+ return true;
+ }
+}
diff --git a/src/Hooks/UploadFormInitDescriptor.php
b/src/Hooks/UploadFormInitDescriptor.php
new file mode 100644
index 0000000..f0b2740
--- /dev/null
+++ b/src/Hooks/UploadFormInitDescriptor.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace NSFileRepo\Hooks;
+
+class UploadFormInitDescriptor {
+
+ /**
+ *
+ * @var \Config
+ */
+ protected $config = null;
+
+ /**
+ *
+ * @var \IContextSource
+ */
+ protected $context = null;
+
+ /**
+ *
+ * @var array
+ */
+ protected $descriptor = [];
+
+ /**
+ *
+ * @param \IContextSource $context
+ * @param \Config $config
+ * @param array $descriptor
+ */
+ public function __construct( \IContextSource $context, \Config $config,
&$descriptor ) {
+ $this->context = $context;
+ $this->config = $config;
+ $this->descriptor =& $descriptor;
+ }
+
+ /**
+ * Add fields to Special:Upload
+ * @param array $descriptor
+ * @return boolean
+ */
+ public static function handle( &$descriptor ) {
+ $instance = new self(
+ \RequestContext::getMain(),
+ new \NSFileRepo\Config(),
+ $descriptor
+ );
+
+ return $instance->process();
+ }
+
+ public function process() {
+ $this->setDefaultNamespace();
+ $this->setNamespaceSelectOptions();
+ $this->setFieldDefinitions();
+ $this->modifyDescriptor();
+
+ return true;
+ }
+
+ protected $selectedNamespace = '';
+
+ protected function setDefaultNamespace() {
+ $this->selectedNamespace = '';
+ //"wpDestFile" is set on query string. e.g after click on
redlink or on re-upload
+ if( !empty( $this->descriptor['DestFile']['default'] ) ) {
+ $target = \Title::newFromText(
$this->descriptor['DestFile']['default'] );
+ $this->descriptor['DestFile']['default'] =
$target->getText();
+ $this->selectedNamespace = str_replace( ' ', '_',
$target->getNsText() );
+ }
+ }
+
+ protected $namespaceSelectOptions = [];
+
+ protected function setNamespaceSelectOptions() {
+ $namespaceList = new \NSFileRepo\NamespaceList(
+ $this->context->getUser(),
+ $this->config,
+ $this->context->getLanguage()
+ );
+
+ foreach( $namespaceList->getEditable() as $nsId => $namespace )
{
+
$this->namespaceSelectOptions[$namespace->getDisplayName()]
+ = $namespace->getCanonicalName();
+ }
+ }
+
+ protected $fieldDef = [];
+
+ protected function setFieldDefinitions() {
+ $this->fieldDef = [
+ 'NSFR_Namespace' => [
+ 'label' => wfMessage('namespace')->plain(),
+ 'section' => 'description',
+ 'class' => 'HTMLSelectField',
+ 'options' => $this->namespaceSelectOptions,
+ 'required' => true,
+ 'default' => $this->selectedNamespace
+ ],
+ 'NSFR_DestFile' => [
+ 'type' => 'text',
+ 'section' => 'description',
+ 'label-message' => 'nsfilerepo-upload-target',
+ 'size' => 60,
+ 'default' => '',
+ 'readonly' => true,
+ 'nodata' => false,
+ ],
+ ];
+ }
+
+ protected function modifyDescriptor() {
+ $pos = array_search( 'UploadDescription', array_keys(
$this->descriptor ) );
+
+ $this->descriptor =
+ array_slice( $this->descriptor, 0, $pos, true ) +
+ $this->fieldDef +
+ array_slice( $this->descriptor, $pos, count(
$this->descriptor ) - 1, true ) ;
+ }
+}
\ No newline at end of file
diff --git a/src/Hooks/UserCan.php b/src/Hooks/UserCan.php
new file mode 100644
index 0000000..c8a3d08
--- /dev/null
+++ b/src/Hooks/UserCan.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace NSFileRepo\Hooks;
+
+class UserCan {
+
+ /**
+ *
+ * @var \Config
+ */
+ protected $config = null;
+
+ /**
+ *
+ * @var \IContextSource
+ */
+ protected $context = null;
+
+ /**
+ * @var \Title
+ */
+ protected $title = null;
+
+ /**
+ * @var \User
+ */
+ protected $user = null;
+
+ /**
+ * @var string
+ */
+ protected $action = '';
+
+ /**
+ * @var boolean
+ */
+ protected $result = true;
+
+ /**
+ * Check individual namespace protection using Extension:Lockdown
+ * @param Title $title
+ * @param user $user
+ * @param string $action
+ * @param mixed $result
+ * @return boolean
+ */
+ public static function handle( &$title, &$user, $action, &$result ) {
+ $instance = new self(
+ \RequestContext::getMain(),
+ new \MultiConfig( [
+ new \NSFileRepo\Config(),
+
\MediaWiki\MediaWikiServices::getInstance()->getMainConfig()
+ ] ),
+ $title,
+ $user,
+ $action,
+ $result
+ );
+
+ return $instance->process();
+ }
+
+ /**
+ * See static method "handle"
+ * @param \IContextSource $context
+ * @param \Config $config
+ * @param \Title $title
+ * @param \User $user
+ * @param string $action
+ * @param boolean $result
+ */
+ public function __construct( \IContextSource $context, \Config $config,
\Title &$title, \User &$user, $action, &$result ) {
+ $this->context = $context;
+ $this->config = $config;
+ $this->title = $title;
+ $this->user = $user;
+ $this->action = $action;
+ $this->result =& $result;
+ }
+
+ /**
+ *
+ * @return boolean
+ */
+ public function process() {
+ $whitelistRead = $this->config->get( 'WhitelistRead' );
+ if ( $whitelistRead !== false && in_array(
$this->title->getPrefixedText(), $whitelistRead ) ) {
+ return true;
+ }
+
+ if( $this->title->getNamespace() !== NS_FILE ) {
+ return true;
+ }
+
+ $ntitle = \Title::newFromText( $this->title->getDBkey() );
+ $ret_val = true;
+
+ //Additional check for NS_MAIN: If a user is not allowed to
read NS_MAIN he should also be not allowed
+ //to view files with no namespace-prefix as they are logically
assigned to namespace NS_MAIN
+ $titleIsNSMAIN = $ntitle->getNamespace() === NS_MAIN;
+ $titleNSaboveThreshold = $ntitle->getNamespace() >
$this->config->get( 'NamespaceThreshold' );
+ if( $titleIsNSMAIN || $titleNSaboveThreshold ) {
+ $ret_val = lockdownUserPermissionsErrors( $ntitle,
$this->user, $this->action, $this->result );
+ }
+
+ $this->result = null;
+ return $ret_val;
+ }
+}
diff --git a/src/MWNamespace.php b/src/MWNamespace.php
new file mode 100644
index 0000000..31f1293
--- /dev/null
+++ b/src/MWNamespace.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace NSFileRepo;
+
+class MWNamespace implements \JsonSerializable {
+
+ /**
+ *
+ * @var int
+ */
+ protected $id = '';
+
+ /**
+ *
+ * @var string
+ */
+ protected $canonicalName = '';
+
+ /**
+ *
+ * @var string
+ */
+ protected $displayName = '';
+
+ /**
+ *
+ * @param int $id
+ * @param string $canonicalName
+ * @param string $displayName
+ */
+ public function __construct( $id, $canonicalName, $displayName ) {
+ $this->id = (int)$id;
+ $this->canonicalName = $canonicalName;
+ $this->displayName = $displayName;
+ }
+
+ /**
+ *
+ * @return int
+ */
+ public function getId() {
+ return $this->id;
+ }
+
+ /**
+ *
+ * @return string
+ */
+ public function getCanonicalName() {
+ return $this->canonicalName;
+ }
+
+ /**
+ *
+ * @return string
+ */
+ public function getDisplayName() {
+ return $this->displayName;
+ }
+
+ /**
+ *
+ * @return array
+ */
+ public function jsonSerialize() {
+ return [
+ 'id' => $this->getId(),
+ 'canonicalName' => $this->getCanonicalName(),
+ 'displayName' => $this->getDisplayName()
+ ];
+ }
+
+}
\ No newline at end of file
diff --git a/src/NamespaceList.php b/src/NamespaceList.php
new file mode 100644
index 0000000..6093e42
--- /dev/null
+++ b/src/NamespaceList.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace NSFileRepo;
+
+class NamespaceList {
+
+ /**
+ *
+ * @var \User
+ */
+ protected $user = null;
+
+ /**
+ *
+ * @var \Config
+ */
+ protected $config = null;
+
+ /**
+ *
+ * @var \Language
+ */
+ protected $lang = null;
+
+ /**
+ *
+ * @param \User $user
+ * @param \Config $config
+ * @param \Language $lang
+ */
+ public function __construct( \User $user, \Config $config, \Language
$lang ) {
+ $this->user = $user;
+ $this->config = new \MultiConfig([
+ $config,
+ new \HashConfig( [
+ Config::CONFIG_SKIP_TALK => true,
+ Config::CONFIG_THRESHOLD => 0,
+ Config::CONFIG_BLACKLIST => []
+ ] )
+ ]);
+ $this->lang = $lang;
+ }
+
+ /**
+ * @return MWNamespace[] With namespace id as an index
+ */
+ public function getReadable() {
+ return $this->getNamespacesByPermission( 'read' );
+ }
+
+ /**
+ * @return MWNamespace[] With namespace id as an index
+ */
+ public function getEditable() {
+ return $this->getNamespacesByPermission( 'edit' );
+ }
+
+ protected function getNamespacesByPermission( $permission ) {
+ $availableNamespaces = $this->lang->getNamespaces();
+
+ $namespaces = [];
+ foreach( $availableNamespaces as $nsId => $nsText ) {
+ #var_dump( $this->skip( $nsIdx, $permission ) );
+ if( $this->skip( $nsId, $permission ) ) {
+ continue;
+ }
+
+ if( $nsId === NS_MAIN ) {
+ $nsText =
wfMessage('nsfilerepo-nsmain')->plain();
+ }
+
+ $canonicalName = \MWNamespace::getCanonicalName( $nsId
);
+ $namespaces[$nsId] = new MWNamespace( $nsId,
$canonicalName , $nsText );
+ }
+
+ return $namespaces;
+ }
+
+ protected function skip( $nsId, $permission = '' ) {
+
+ if( $nsId < $this->config->get( Config::CONFIG_THRESHOLD ) &&
$nsId !== NS_MAIN ) {
+ return true;
+ }
+
+ if( in_array( $nsId, $this->config->get(
Config::CONFIG_BLACKLIST ) ) ) {
+ return true;
+ }
+
+ if( $this->config->get( Config::CONFIG_SKIP_TALK )
+ && \MWNamespace::isTalk( $nsId ) ) {
+ return true;
+ }
+
+ if( !empty( $permission ) ) {
+ $title = \Title::makeTitle( $nsId, 'Dummy' );
+ return !$title->userCan( $permission, $this->user );
+ }
+
+ return false;
+ }
+
+}
diff --git a/tests/phpunit/NamespaceListTest.php
b/tests/phpunit/NamespaceListTest.php
new file mode 100644
index 0000000..752f30e
--- /dev/null
+++ b/tests/phpunit/NamespaceListTest.php
@@ -0,0 +1,106 @@
+<?php
+
+namespace NSFileRepo\Tests;
+
+class NamespaceListTest extends \MediaWikiLangTestCase {
+
+ const DUMMY_NS_A_ID = 12412;
+ const DUMMY_NS_B_ID = 12512;
+ const DUMMY_NS_C_ID = 12612;
+
+ protected function setUp() {
+ global $wgExtraNamespaces, $wgNamespaceContentModels,
$wgContentHandlers, $wgContLang;
+
+ parent::setUp();
+
+ $this->setMwGlobals( [
+ 'wgExtraNamespaces' => $wgExtraNamespaces,
+ 'wgNamespaceContentModels' => $wgNamespaceContentModels,
+ 'wgContentHandlers' => $wgContentHandlers,
+ 'wgContLang' => $wgContLang,
+ ] );
+
+ $wgExtraNamespaces[self::DUMMY_NS_A_ID] = 'NSFRDummyA';
+ $wgExtraNamespaces[self::DUMMY_NS_A_ID + 1] = 'NSFRDummyA_talk';
+
+ $wgExtraNamespaces[self::DUMMY_NS_B_ID] = 'NSFRDummyB';
+ $wgExtraNamespaces[self::DUMMY_NS_B_ID + 1] = 'NSFRDummyB_talk';
+
+ $wgExtraNamespaces[self::DUMMY_NS_C_ID] = 'NSFRDummyC';
+ $wgExtraNamespaces[self::DUMMY_NS_C_ID + 1] = 'NSFRDummyC_talk';
+
+ \MWNamespace::getCanonicalNamespaces( true ); # reset namespace
cache
+ $wgContLang->resetNamespaces(); # reset namespace cache
+
+ /**
+ * Test hook handler that mimics Extension:Lockdown and revokes
read
+ * permissions on 'NSFRDummyA' and edit permission on
'NSFRDummyB'
+ */
+ \Hooks::register( 'userCan', function( &$title, &$user,
$action, &$result ) {
+ if( $action === 'read'
+ && $title instanceof \Title
+ && $title->getNamespace() ===
self::DUMMY_NS_A_ID ) {
+ return false;
+ }
+
+ if( $action === 'edit'
+ && $title instanceof \Title
+ && $title->getNamespace() ===
self::DUMMY_NS_B_ID ) {
+ return false;
+ }
+
+ return true;
+ } );
+ }
+
+ public function textInstance() {
+ $namespacelist = $this->makeInstance();
+ $this->assertInstanceOf( 'NSFileRepo\NamespaceList',
$namespacelist );
+ }
+
+ public function testGetReadableNoTalks() {
+ $instance = $this->makeInstance( new \HashConfig([
+ \NSFileRepo\Config::CONFIG_SKIP_TALK => true
+ ]) );
+
+ $readables = $instance->getReadable();
+ $hasTalk = false;
+ foreach( $readables as $namsepace ) {
+ if( \MWNamespace::isTalk( $namsepace->getId() ) ) {
+ $hasTalk = true;
+ break;
+ }
+ }
+
+ $this->assertFalse( $hasTalk, 'List should not contain any Talk
namespaces' );
+ }
+
+ public function testGetReadableNoUnreadables() {
+ $instance = $this->makeInstance( new \HashConfig([
+ \NSFileRepo\Config::CONFIG_SKIP_TALK => true
+ ]) );
+
+ $readables = $instance->getReadable();
+ $hasUnreadables = false;
+ foreach( $readables as $namsepace ) {
+ if( $namsepace->getId() === self::DUMMY_NS_A_ID ) {
+ $hasUnreadables = true;
+ break;
+ }
+ }
+
+ $this->assertFalse( $hasUnreadables, 'List should not contain
an unreadable namespace' );
+ }
+
+ protected function makeInstance( $config = null ) {
+ if( $config === null ) {
+ $config = new \HashConfig( [] );
+ }
+
+ $user = \RequestContext::getMain()->getUser();
+ $lang = \RequestContext::getMain()->getLanguage();
+
+ return new \NSFileRepo\NamespaceList( $user, $config, $lang );
+ }
+
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/364445
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1931e2b355cacabbcdbe3af29d3e9a513e4a316f
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/NSFileRepo
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>
Gerrit-Reviewer: ItSpiderman <[email protected]>
Gerrit-Reviewer: Robert Vogel <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits