Jens Ohlig has uploaded a new change for review. https://gerrit.wikimedia.org/r/95598
Change subject: Refactored Wikibase Lua library code to make everything testable. ...................................................................... Refactored Wikibase Lua library code to make everything testable. Change-Id: Ifab855fa635ad096fcdb94d3cad1e0c4784a6f92 --- M client/WikibaseClient.classes.php D client/includes/WikibaseLibrary.php A client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php A client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php R client/includes/scribunto/mw.wikibase.lua A client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php 6 files changed, 310 insertions(+), 156 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/98/95598/1 diff --git a/client/WikibaseClient.classes.php b/client/WikibaseClient.classes.php index ad69ac6..5140f44 100644 --- a/client/WikibaseClient.classes.php +++ b/client/WikibaseClient.classes.php @@ -22,7 +22,6 @@ 'Wikibase\RepoItemLinkGenerator' => 'includes/RepoItemLinkGenerator.php', 'Wikibase\RepoLinker' => 'includes/RepoLinker.php', 'Wikibase\Client\WikibaseClient' => 'includes/WikibaseClient.php', - 'Scribunto_LuaWikibaseLibrary' => 'includes/WikibaseLibrary.php', 'Wikibase\PageUpdater' => 'includes/PageUpdater.php', 'Wikibase\SiteLinkCommentCreator' => 'includes/SiteLinkCommentCreator.php', 'Wikibase\WikiPageUpdater' => 'includes/WikiPageUpdater.php', @@ -61,6 +60,10 @@ 'Wikibase\DirectSqlStore' => 'includes/store/sql/DirectSqlStore.php', 'Wikibase\EntityCacheTable' => 'includes/store/sql/EntityCacheTable.php', + // includes/scribunto + 'Scribunto_LuaWikibaseLibrary' => 'includes/scribunto/Scribunto_LuaWikibaseLibrary.php', + 'Scribunto_LuaWikibaseLibraryImplementation' => 'includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php', + // test 'Wikibase\Test\MockPageUpdater' => 'tests/phpunit/MockPageUpdater.php' diff --git a/client/includes/WikibaseLibrary.php b/client/includes/WikibaseLibrary.php deleted file mode 100644 index 91be67b..0000000 --- a/client/includes/WikibaseLibrary.php +++ /dev/null @@ -1,155 +0,0 @@ -<?php - -/** - * Registers and defines functions to access Wikibase through the Scribunto extension - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - * @since 0.4 - * - * @licence GNU GPL v2+ - * @author Jens Ohlig < jens.oh...@wikimedia.de > - */ - -use ValueParsers\ParseException; -use Wikibase\Client\WikibaseClient; -use Wikibase\Lib\Serializers\SerializationOptions; -use Wikibase\Lib\Serializers\SerializerFactory; -use Wikibase\LanguageFallbackChainFactory; -use Wikibase\Utils; - -class Scribunto_LuaWikibaseLibrary extends Scribunto_LuaLibraryBase { - - /** - * Register mw.wikibase.lua library - * - * @since 0.4 - */ - public function register() { - $lib = array( - 'getEntity' => array( $this, 'getEntity' ), - 'getEntityId' => array( $this, 'getEntityId' ), - 'getGlobalSiteId' => array( $this, 'getGlobalSiteId' ) - ); - $this->getEngine()->registerInterface( dirname( __FILE__ ) . '/../resources/' . 'mw.wikibase.lua', $lib, array() ); - } - - /** - * Get entity from prefixed ID (e.g. "Q23") and return it as serialized array. - * - * @since 0.4 - * - * @param string $prefixedEntityId - * - * @throws ScribuntoException - * @return array $entityArr - */ - public function getEntity( $prefixedEntityId = null ) { - $this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' ); - $prefixedEntityId = trim( $prefixedEntityId ); - - $entityIdParser = WikibaseClient::getDefaultInstance()->getEntityIdParser(); - - try { - $entityId = $entityIdParser->parse( $prefixedEntityId ); - } - catch ( ParseException $parseException ) { - throw $this->getEngine()->newException( 'wikibase-error-invalid-entity-id' ); - } - - $entityObject = WikibaseClient::getDefaultInstance()->getStore()->getEntityLookup()->getEntity( - $entityId - ); - - if ( $entityObject == null ) { - return array( null ); - } - - $opt = new SerializationOptions(); - $serializerFactory = new SerializerFactory( $opt ); - - // Using "ID_KEYS_BOTH" here means that all lists of Snaks or Claims will be listed - // twice, once with a lower case key and once with an upper case key. - // This is a B/C hack to allow existing lua code to use hardcoded IDs - // in both lower (legacy) and upper case. - $opt->setIdKeyMode( SerializationOptions::ID_KEYS_BOTH ); - - // This is $wgContLang, not parser target language or anything else. - // See Scribunto_LuaLanguageLibrary::getContLangCode(). - global $wgContLang; - - // See mw.wikibase.lua. This is the only way to inject values into mw.wikibase.label( ), - // so any customized Lua modules can access labels of another entity written in another variant, - // unless we give them the ability to getEntity() any entity by specifying its ID, not just self. - $chain = WikibaseClient::getDefaultInstance()->getLanguageFallbackChainFactory()->newFromLanguage( - $wgContLang, LanguageFallbackChainFactory::FALLBACK_SELF | LanguageFallbackChainFactory::FALLBACK_VARIANTS - ); - // SerializationOptions accepts mixed types of keys happily. - $opt->setLanguages( Utils::getLanguageCodes() + array( $wgContLang->getCode() => $chain ) ); - - $serializer = $serializerFactory->newSerializerForObject( $entityObject, $opt ); - - try { - $entityArr = $serializer->getSerialized( $entityObject ); - return array( $entityArr ); - } catch ( \Exception $e ) { - throw $this->getEngine()->newException( 'wikibase-error-serialize-error' ); - } - } - - /** - * Get entity id from page title. - * - * @since 0.4 - * - * @param string $pageTitle - * - * @return string $id - */ - public function getEntityId( $pageTitle = null ) { - $this->checkType( 'getEntityByTitle', 1, $pageTitle, 'string' ); - $globalSiteId = \Wikibase\Settings::get( 'siteGlobalID' ); - $table = WikibaseClient::getDefaultInstance()->getStore()->getSiteLinkTable(); - if ( $table == null ) { - return array( null ); - } - - $numericId = $table->getItemIdForLink( $globalSiteId, $pageTitle ); - if ( !is_int( $numericId ) ) { - return array( null ); - } - - $id = new Wikibase\EntityId( \Wikibase\Item::ENTITY_TYPE, $numericId ); - if ( $id == null ) { - return array( null ); - } - - $idFormatter = WikibaseClient::getDefaultInstance()->getEntityIdFormatter(); - - return array( $idFormatter->format( $id ) ); - } - /** - * Get global site ID (e.g. "enwiki") - * This is basically a helper function. - * I can see this becoming part of mw.site in the Scribunto extension. - * - * @since 0.4 - * - */ - public function getGlobalSiteId() { - return array( \Wikibase\Settings::get( 'siteGlobalID' ) ); - } -} diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php new file mode 100644 index 0000000..c05ce8a --- /dev/null +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibrary.php @@ -0,0 +1,109 @@ +<?php + +/** + * Registers and defines functions to access Wikibase through the Scribunto extension + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @licence GNU GPL v2+ + * @author Jens Ohlig < jens.oh...@wikimedia.de > + */ + +use ValueParsers\ParseException; +use Wikibase\Client\WikibaseClient; + + +class Scribunto_LuaWikibaseLibrary extends Scribunto_LuaLibraryBase { + /** + * Constructor for wrapper class, initialize member object holding implementation + * + * @since 0.5 + * + */ + + /* @var Scribunto_LuaWikibaseLibraryImplementation */ + protected $wbLibrary; + + public function __construct( $engine ) { + $this->wbLibrary = new Scribunto_LuaWikibaseLibraryImplementation( + WikibaseClient::getDefaultInstance()->getEntityIdParser(), // EntityIdParser + WikibaseClient::getDefaultInstance()->getStore()->getEntityLookup(), // EntityLookup + WikibaseClient::getDefaultInstance()->getEntityIdFormatter(), // EntityIdFormatter + WikibaseClient::getDefaultInstance()->getStore()->getSiteLinkTable() // SiteLinkLookup + ); + parent::__construct( $engine ); + } + + /** + * Register mw.wikibase.lua library + * + * @since 0.4 + */ + public function register() { + $lib = array( 'getEntity' => array( $this, 'getEntity' ), 'getEntityId' => array( $this, 'getEntityId' ), 'getGlobalSiteId' => array( $this, 'getGlobalSiteId' ) ); + $this->getEngine()->registerInterface( dirname( __FILE__ ) . '/mw.wikibase.lua', $lib, array() ); + } + + /** + * Wrapper for getEntity in Scribunto_LuaWikibaseLibraryImplementation + * + * @since 0.5 + * + * @param string $prefixedEntityId + * + * @throws ScribuntoException + * @return array $entityArr + */ + public function getEntity( $prefixedEntityId = null ) { + $this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' ); + try { + $entityArr = $this->wbLibrary->getEntity( $prefixedEntityId ); + return $entityArr; + } + catch ( ParseException $e ) { + throw new ScribuntoException( 'wikibase-error-invalid-entity-id' ); + } + catch ( \Exception $e ) { + throw new ScribuntoException( 'wikibase-error-serialize-error' ); + } + } + + /** + * Wrapper for getEntityId in Scribunto_LuaWikibaseLibraryImplementation + * + * @since 0.5 + * + * @param string $pageTitle + * + * @return string $id + */ + public function getEntityId( $pageTitle = null ) { + $this->checkType( 'getEntityByTitle', 1, $pageTitle, 'string' ); + return $this->wbLibrary->getEntityId( $pageTitle ); + } + + /** + * Wrapper for getGlobalSiteId in Scribunto_LuaWikibaseLibraryImplementation + * + * @since 0.5 + * + */ + public function getGlobalSiteId() { + return $this->wbLibrary->getGlobalSiteId(); + } +} diff --git a/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php b/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php new file mode 100644 index 0000000..a65675e --- /dev/null +++ b/client/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementation.php @@ -0,0 +1,150 @@ +<?php + +/** + * Actual implementations of the functions to access Wikibase through the Scribunto extension + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @since 0.4 + * + * @licence GNU GPL v2+ + * @author Jens Ohlig < jens.oh...@wikimedia.de > + */ + +use Wikibase\EntityLookup; +use Wikibase\SiteLinkLookup; +use Wikibase\Lib\Serializers\SerializationOptions; +use Wikibase\Lib\Serializers\SerializerFactory; +use Wikibase\Lib\EntityIdFormatter; +use Wikibase\Lib\EntityIdParser; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Utils; + +class Scribunto_LuaWikibaseLibraryImplementation { + + /* @var EntityIdParser */ + protected $entityIdParser; + + /* @var EntityLookup */ + protected $entityLookup; + + /* @var EntityIdFormatter */ + protected $entityIdFormatter; + + /* @var SiteLinkLookup */ + protected $siteLinkTable; + + + /** + * @param EntityIdParser $entityIdParser + * + */ + public function __construct( EntityIdParser $entityIdParser, EntityLookup $entityLookup, EntityIdFormatter $entityIdFormatter, SiteLinkLookup $siteLinkTable ) { + $this->entityIdParser = $entityIdParser; + $this->entityLookup = $entityLookup; + $this->entityIdFormatter = $entityIdFormatter; + $this->siteLinkTable = $siteLinkTable; + } + + + /** + * Get entity from prefixed ID (e.g. "Q23") and return it as serialized array. + * + * @since 0.5 + * + * @param string $prefixedEntityId + * + * @return array $entityArr + */ + public function getEntity( $prefixedEntityId = null ) { + $prefixedEntityId = trim( $prefixedEntityId ); + + $entityId = $this->entityIdParser->parse( $prefixedEntityId ); + + $entityObject = $this->entityLookup->getEntity( $entityId ); + + if ( $entityObject == null ) { + return array( null ); + } + + $opt = new SerializationOptions(); + $serializerFactory = new SerializerFactory( $opt ); + + // Using "ID_KEYS_BOTH" here means that all lists of Snaks or Claims will be listed + // twice, once with a lower case key and once with an upper case key. + // This is a B/C hack to allow existing lua code to use hardcoded IDs + // in both lower (legacy) and upper case. + $opt->setIdKeyMode( SerializationOptions::ID_KEYS_BOTH ); + + // This is $wgContLang, not parser target language or anything else. + // See Scribunto_LuaLanguageLibrary::getContLangCode(). + global $wgContLang; + + // See mw.wikibase.lua. This is the only way to inject values into mw.wikibase.label( ), + // so any customized Lua modules can access labels of another entity written in another variant, + // unless we give them the ability to getEntity() any entity by specifying its ID, not just self. + $fallbackChainFactory = new LanguageFallbackChainFactory(); + $chain = $fallbackChainFactory->newFromLanguage( $wgContLang, LanguageFallbackChainFactory::FALLBACK_SELF | LanguageFallbackChainFactory::FALLBACK_VARIANTS ); + // SerializationOptions accepts mixed types of keys happily. + $opt->setLanguages( Utils::getLanguageCodes() + array( $wgContLang->getCode() => $chain ) ); + + $serializer = $serializerFactory->newSerializerForObject( $entityObject, $opt ); + + $entityArr = $serializer->getSerialized( $entityObject ); + return array( $entityArr ); + } + + /** + * Get entity id from page title. + * + * @since 0.5 + * + * @param string $pageTitle + * + * @return string $id + */ + public function getEntityId( $pageTitle = null ) { + $globalSiteId = $this->getGlobalSiteId(); + $table = $this->siteLinkTable; + if ( $table == null ) { + return array( null ); + } + + $numericId = $table->getItemIdForLink( $globalSiteId, $pageTitle ); + if ( ! is_int( $numericId ) ) { + return array( null ); + } + + $id = new Wikibase\EntityId( \Wikibase\Item::ENTITY_TYPE, $numericId ); + if ( $id == null ) { + return array( null ); + } + + return array( $this->entityIdFormatter->format( $id ) ); + } + + /** + * Get global site ID (e.g. "enwiki") + * This is basically a helper function. + * I can see this becoming part of mw.site in the Scribunto extension. + * + * @since 0.5 + * + */ + public function getGlobalSiteId() { + return array( \Wikibase\Settings::get( 'siteGlobalID' ) ); + } +} diff --git a/client/resources/mw.wikibase.lua b/client/includes/scribunto/mw.wikibase.lua similarity index 100% rename from client/resources/mw.wikibase.lua rename to client/includes/scribunto/mw.wikibase.lua diff --git a/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php new file mode 100644 index 0000000..3740b9c --- /dev/null +++ b/client/tests/phpunit/includes/scribunto/Scribunto_LuaWikibaseLibraryImplementationTest.php @@ -0,0 +1,47 @@ +<?php + +namespace Wikibase\Test; + +use Scribunto_LuaWikibaseLibraryImplementation; +use Wikibase\Client\WikibaseClient; + +/** + * @covers Wikibase\Scribunto_LuaWikibaseLibraryImplementation + * + * @file + * @since 0.4 + * + * @ingroup WikibaseClient + * @ingroup Test + * + * @group Wikibase + * @group WikibaseClient + * @group Scribunto_LuaWikibaseLibraryImplementationTest + * + * @licence GNU GPL v2+ + * @author Jens Ohlig < jens.oh...@wikimedia.de > + */ +class Scribunto_LuaWikibaseLibraryImplementationTest extends \PHPUnit_Framework_TestCase { + + public function getWikibaseLibraryImplementation() { + $entityLookup = new MockRepository(); + return new Scribunto_LuaWikibaseLibraryImplementation( + WikibaseClient::getDefaultInstance()->getEntityIdParser(), // EntityIdParser + $entityLookup, + WikibaseClient::getDefaultInstance()->getEntityIdFormatter(), // EntityIdFormatter + WikibaseClient::getDefaultInstance()->getStore()->getSiteLinkTable() // SiteLinkLookup + ); + } + + /** + * @dataProvider provideEntity + */ + public function testGetEntity( $entity ) { + $entityArr = $this->getWikibaseLibraryImplementation()->getEntity( $entity ); + $this->assertEquals( is_array( $entityArr ), true ); + } + + public function provideEntity() { + return array( array( 'q42' ), array( 'q23' ) ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/95598 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ifab855fa635ad096fcdb94d3cad1e0c4784a6f92 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jens Ohlig <jens.oh...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits