[MediaWiki-commits] [Gerrit] Lua func fixes - change (mediawiki...ZeroPortal)
Yurik has uploaded a new change for review. https://gerrit.wikimedia.org/r/177133 Change subject: Lua func fixes .. Lua func fixes * isSiteAdmin() - proper handling of non-admin users * isSiteAdmin() - return proper array-wrapped results * setRawResult() - handle empty string as a valid value, allow null to turn rawResult mode off Change-Id: I108576635e08f3d278bd810fb44bd1727d14f52d --- M includes/LuaLibrary.php 1 file changed, 36 insertions(+), 17 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroPortal refs/changes/33/177133/1 diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php index 0746080..c666888 100644 --- a/includes/LuaLibrary.php +++ b/includes/LuaLibrary.php @@ -70,7 +70,7 @@ $resp-header( 'Cache-Control: ' . $cacheMode . ', must-revalidate, max-age=0' ); } - if ( self::$result ) { + if ( self::$result !== null ) { $out-disable(); $resp-header( 'Content-Type: application/json; charset=UTF-8' ); echo self::$result; @@ -128,7 +128,7 @@ */ public function getAllowedAccountIds() { $configs = array(); - list( $name ) = $this-getUsername(); + $name = $this-getUsernameInt(); if ( $name ) { ApiZeroPortal::iterateAllConfigs( false, function ( ZeroConfig $content, $title ) use ( $name, $configs ) { @@ -147,12 +147,7 @@ * @throws Scribunto_LuaError */ public function getUsername() { - $user = self::getContext()-getUser(); - if ( $user-isAnon() ) { - return array( false ); - } else { - return array( $user-getName() ); - } + return array( $this-getUsernameInt() ); } /** @@ -184,16 +179,17 @@ // First validate parameter, than analyze $this-checkType( self::luaNamespace . __FUNCTION__, 1, $xcs, 'string' ); $title = new TitleValue( NS_ZERO, $xcs ); - if ( $this-isSiteAdmin() ) { - return true; + + if ( $this-isSiteAdminInt() ) { + return array( true ); } - list( $name ) = $this-getUsername(); + $name = $this-getUsernameInt(); if ( !$name ) { - return false; + return array( false ); } /** @var ZeroConfig $content */ $content = JCSingleton::getContent( $title ); - return in_array( $name, $content-admins() ); + return array( in_array( $name, $content-admins() ) ); } /** @@ -202,7 +198,7 @@ * @throws Scribunto_LuaError */ public function isSiteAdmin() { - return array( self::getContext()-getUser()-isAllowed( 'zero-edit' ) ); + return array( $this-isSiteAdminInt() ); } /** @@ -245,12 +241,12 @@ } /** -* Set raw result of the special page -* @param mixed $result +* Set raw result of the special page, or null to disable raw mode. An empty string is a valid result. +* @param string|null $result */ public function setRawResult( $result = null ) { self::getContext(); // ensure special page is enabled - $this-checkType( self::luaNamespace . __FUNCTION__, 1, $result, 'string' ); + $this-checkTypeOptional( self::luaNamespace . __FUNCTION__, 1, $result, 'string', null ); self::$result = $result; } @@ -263,4 +259,27 @@ return array( self::getContext()-getRequest()-wasPosted() ); } + /** +* Return true if this is an admin user (anyone belonging to the zero-edit group) +* @return bool +* @throws Scribunto_LuaError +*/ + private function isSiteAdminInt() { + return self::getContext()-getUser()-isAllowed( 'zero-edit' ); + } + + /** +* Return name of the logged in user, or false if anonymous +* @return string|false +* @throws Scribunto_LuaError +*/ + public function getUsernameInt() { + $user = self::getContext()-getUser(); + if ( $user-isAnon() ) { + return false; + } else { + return $user-getName(); + } + } + } -- To view, visit https://gerrit.wikimedia.org/r/177133 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I108576635e08f3d278bd810fb44bd1727d14f52d
[MediaWiki-commits] [Gerrit] Lua func fixes - change (mediawiki...ZeroPortal)
jenkins-bot has submitted this change and it was merged. Change subject: Lua func fixes .. Lua func fixes * isSiteAdmin() - proper handling of non-admin users * isSiteAdmin() - return proper array-wrapped results * setRawResult() - handle empty string as a valid value, allow null to turn rawResult mode off Change-Id: I108576635e08f3d278bd810fb44bd1727d14f52d --- M includes/LuaLibrary.php 1 file changed, 36 insertions(+), 17 deletions(-) Approvals: MaxSem: Looks good to me, but someone else must approve Yurik: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php index 0746080..c666888 100644 --- a/includes/LuaLibrary.php +++ b/includes/LuaLibrary.php @@ -70,7 +70,7 @@ $resp-header( 'Cache-Control: ' . $cacheMode . ', must-revalidate, max-age=0' ); } - if ( self::$result ) { + if ( self::$result !== null ) { $out-disable(); $resp-header( 'Content-Type: application/json; charset=UTF-8' ); echo self::$result; @@ -128,7 +128,7 @@ */ public function getAllowedAccountIds() { $configs = array(); - list( $name ) = $this-getUsername(); + $name = $this-getUsernameInt(); if ( $name ) { ApiZeroPortal::iterateAllConfigs( false, function ( ZeroConfig $content, $title ) use ( $name, $configs ) { @@ -147,12 +147,7 @@ * @throws Scribunto_LuaError */ public function getUsername() { - $user = self::getContext()-getUser(); - if ( $user-isAnon() ) { - return array( false ); - } else { - return array( $user-getName() ); - } + return array( $this-getUsernameInt() ); } /** @@ -184,16 +179,17 @@ // First validate parameter, than analyze $this-checkType( self::luaNamespace . __FUNCTION__, 1, $xcs, 'string' ); $title = new TitleValue( NS_ZERO, $xcs ); - if ( $this-isSiteAdmin() ) { - return true; + + if ( $this-isSiteAdminInt() ) { + return array( true ); } - list( $name ) = $this-getUsername(); + $name = $this-getUsernameInt(); if ( !$name ) { - return false; + return array( false ); } /** @var ZeroConfig $content */ $content = JCSingleton::getContent( $title ); - return in_array( $name, $content-admins() ); + return array( in_array( $name, $content-admins() ) ); } /** @@ -202,7 +198,7 @@ * @throws Scribunto_LuaError */ public function isSiteAdmin() { - return array( self::getContext()-getUser()-isAllowed( 'zero-edit' ) ); + return array( $this-isSiteAdminInt() ); } /** @@ -245,12 +241,12 @@ } /** -* Set raw result of the special page -* @param mixed $result +* Set raw result of the special page, or null to disable raw mode. An empty string is a valid result. +* @param string|null $result */ public function setRawResult( $result = null ) { self::getContext(); // ensure special page is enabled - $this-checkType( self::luaNamespace . __FUNCTION__, 1, $result, 'string' ); + $this-checkTypeOptional( self::luaNamespace . __FUNCTION__, 1, $result, 'string', null ); self::$result = $result; } @@ -263,4 +259,27 @@ return array( self::getContext()-getRequest()-wasPosted() ); } + /** +* Return true if this is an admin user (anyone belonging to the zero-edit group) +* @return bool +* @throws Scribunto_LuaError +*/ + private function isSiteAdminInt() { + return self::getContext()-getUser()-isAllowed( 'zero-edit' ); + } + + /** +* Return name of the logged in user, or false if anonymous +* @return string|false +* @throws Scribunto_LuaError +*/ + public function getUsernameInt() { + $user = self::getContext()-getUser(); + if ( $user-isAnon() ) { + return false; + } else { + return $user-getName(); + } + } + } -- To view, visit https://gerrit.wikimedia.org/r/177133 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I108576635e08f3d278bd810fb44bd1727d14f52d