From 34b16b92b07651b1aed6441ed341ddb17f36df8b Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sat, 9 Nov 2013 18:46:50 +0100 Subject: [PATCH 1/4] Improve autoloading code * Removed custom autoloaders * Use Composer classmap (psr-0 would require making the dir structure deeper) * Updated TravisCI config --- .travis.yml | 1 + WikibaseDatabase.mw.php | 8 +++---- WikibaseDatabase.php | 52 +++++++---------------------------------- composer.json | 6 ++--- tests/bootstrap.php | 8 ++++--- tests/testLoader.php | 41 -------------------------------- 6 files changed, 21 insertions(+), 95 deletions(-) delete mode 100644 tests/testLoader.php diff --git a/.travis.yml b/.travis.yml index fc94937..3453e14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,7 @@ script: - if [ "$TESTSUITE" == "MediaWiki" ] ; then phpunit --coverage-clover build/logs/clover.xml ; fi after_script: + - composer require satooshi/php-coveralls dev-master - php vendor/bin/coveralls -v notifications: diff --git a/WikibaseDatabase.mw.php b/WikibaseDatabase.mw.php index 3ef22d1..5fe2bb1 100644 --- a/WikibaseDatabase.mw.php +++ b/WikibaseDatabase.mw.php @@ -12,6 +12,10 @@ die( 'Not an entry point.' ); } +if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) { + require_once( __DIR__ . '/vendor/autoload.php' ); +} + $GLOBALS['wgExtensionCredits']['wikibase'][] = array( 'path' => __DIR__, 'name' => 'Wikibase Database', @@ -26,10 +30,6 @@ $GLOBALS['wgExtensionMessagesFiles']['WikibaseDatabase'] = __DIR__ . '/WikibaseDatabase.i18n.php'; -if ( defined( 'MW_PHPUNIT_TEST' ) ) { - require_once __DIR__ . '/tests/testLoader.php'; -} - /** * Hook to add PHPUnit test cases. * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList diff --git a/WikibaseDatabase.php b/WikibaseDatabase.php index 01ea1ee..8680c93 100644 --- a/WikibaseDatabase.php +++ b/WikibaseDatabase.php @@ -3,54 +3,18 @@ /** * Entry point of the Wikibase Database component. * + * @codeCoverageIgnore + * * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -if ( defined( 'WIKIBASE_DATABASE_VERSION' ) ) { - // Do not initialize more then once. - return; -} - -define( 'WIKIBASE_DATABASE_VERSION', '0.1' ); - -spl_autoload_register( function ( $className ) { - $className = ltrim( $className, '\\' ); - $fileName = ''; - $namespace = ''; - - if ( $lastNsPos = strripos( $className, '\\') ) { - $namespace = substr( $className, 0, $lastNsPos ); - $className = substr( $className, $lastNsPos + 1 ); - $fileName = str_replace( '\\', '/', $namespace ) . '/'; - } - - $fileName .= str_replace( '_', '/', $className ) . '.php'; - - $namespaceSegments = explode( '\\', $namespace ); - - $inQueryEngineNamespace = count( $namespaceSegments ) > 1 - && $namespaceSegments[0] === 'Wikibase' - && $namespaceSegments[1] === 'Database'; +if ( !defined( 'WIKIBASE_DATABASE_VERSION' ) ) { + define( 'WIKIBASE_DATABASE_VERSION', '0.2 alpha' ); - if ( $inQueryEngineNamespace ) { - $inTestNamespace = count( $namespaceSegments ) > 2 && $namespaceSegments[2] === 'Tests'; - - if ( !$inTestNamespace ) { - $pathParts = explode( '/', $fileName ); - array_shift( $pathParts ); - array_shift( $pathParts ); - $fileName = implode( '/', $pathParts ); - - require_once __DIR__ . '/src/' . $fileName; - } + if ( defined( 'MEDIAWIKI' ) ) { + call_user_func( function() { + require_once __DIR__ . '/WikibaseDatabase.mw.php'; + } ); } -} ); - -// @codeCoverageIgnoreStart -if ( defined( 'MEDIAWIKI' ) ) { - call_user_func( function() { - require_once __DIR__ . '/WikibaseDatabase.mw.php'; - } ); } -// @codeCoverageIgnoreEnd diff --git a/composer.json b/composer.json index b039519..c671071 100644 --- a/composer.json +++ b/composer.json @@ -28,10 +28,10 @@ "require": { "php": ">=5.3.2" }, - "require-dev": { - "satooshi/php-coveralls": "dev-master" - }, "autoload": { + "classmap": [ + "src" + ], "files" : [ "WikibaseDatabase.php" ] diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 29bd168..bc75cc7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,10 +9,12 @@ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ +if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { + die( 'You need to install this package with Composer before you can run the tests' ); +} + if ( !in_array( '--testsuite=WikibaseDatabaseStandalone', $GLOBALS['argv'] ) ) { require_once( __DIR__ . '/evilMediaWikiBootstrap.php' ); } -require_once( __DIR__ . '/../WikibaseDatabase.php' ); - -require_once( __DIR__ . '/testLoader.php' ); +require_once( __DIR__ . '/../vendor/autoload.php' ); diff --git a/tests/testLoader.php b/tests/testLoader.php deleted file mode 100644 index 61fd544..0000000 --- a/tests/testLoader.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ - -spl_autoload_register( function ( $className ) { - $className = ltrim( $className, '\\' ); - $fileName = ''; - $namespace = ''; - - if ( $lastNsPos = strripos( $className, '\\') ) { - $namespace = substr( $className, 0, $lastNsPos ); - $className = substr( $className, $lastNsPos + 1 ); - $fileName = str_replace( '\\', '/', $namespace ) . '/'; - } - - $fileName .= str_replace( '_', '/', $className ) . '.php'; - - $namespaceSegments = explode( '\\', $namespace ); - - $inTestNamespace = count( $namespaceSegments ) > 2 - && $namespaceSegments[0] === 'Wikibase' - && $namespaceSegments[1] === 'Database' - && $namespaceSegments[2] === 'Tests'; - - if ( $inTestNamespace ) { - $pathParts = explode( '/', $fileName ); - array_shift( $pathParts ); - array_shift( $pathParts ); - array_shift( $pathParts ); - $fileName = implode( '/', $pathParts ); - - require_once __DIR__ . '/phpunit/' . $fileName; - } -} ); From 8442ff977fb60b51f3e39404f04b7c70d412012c Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sat, 9 Nov 2013 19:16:22 +0100 Subject: [PATCH 2/4] Fix compatibility with MW 1.22 Change-Id: Ief6c9845d76c583d5d818a89f43dd6b6f3f040f6 From b5fdbfc9a3e6d5a2b09f96b814ce10417d4fec7a Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sat, 9 Nov 2013 22:52:14 +0100 Subject: [PATCH 3/4] Split Escaper interface and remove unused UnEscaper --- RELEASE-NOTES.md | 2 ++ src/Escaper.php | 16 +---------- src/IdentifierEscaper.php | 20 +++++++++++++ src/SQLite/SQLiteUnEscaper.php | 7 ++--- src/Standalone/DatabaseEscaper.php | 45 ++++++++++++++++++++++++++++++ src/UnEscaper.php | 23 --------------- src/ValueEscaper.php | 20 +++++++++++++ 7 files changed, 91 insertions(+), 42 deletions(-) create mode 100644 src/IdentifierEscaper.php create mode 100644 src/Standalone/DatabaseEscaper.php delete mode 100644 src/UnEscaper.php create mode 100644 src/ValueEscaper.php diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 3582bad..76a5771 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -4,6 +4,8 @@ These are the release notes for the [Wikibase Database library](README.md). * Removed custom autoloaders is favour of using the Composer autoloader. * MediaWiki plugin: added compatibility with changes in MediaWiki 1.22. +* Removed unused UnEscaper interface. +* Split Escaper interface into ValueEscaper and IdentifierEscaper, keeping Escaper itself. ## Version 0.1 (2013-11-01) diff --git a/src/Escaper.php b/src/Escaper.php index b7dff43..cd92cc3 100644 --- a/src/Escaper.php +++ b/src/Escaper.php @@ -12,20 +12,6 @@ * @author Jeroen De Dauw < jeroendedauw@gmail.com > * @author Adam Shorland */ -interface Escaper { - - /** - * @param mixed $value - * - * @return string The escaped value - */ - public function getEscapedValue( $value ); - - /** - * @param mixed $identifier - * - * @return string The escaped identifier - */ - public function getEscapedIdentifier( $identifier ); +interface Escaper extends ValueEscaper, IdentifierEscaper { } diff --git a/src/IdentifierEscaper.php b/src/IdentifierEscaper.php new file mode 100644 index 0000000..7777c6b --- /dev/null +++ b/src/IdentifierEscaper.php @@ -0,0 +1,20 @@ + + */ +interface IdentifierEscaper { + + /** + * @param mixed $identifier + * + * @return string The escaped identifier + */ + public function getEscapedIdentifier( $identifier ); + +} diff --git a/src/SQLite/SQLiteUnEscaper.php b/src/SQLite/SQLiteUnEscaper.php index c9812d1..8ed2e4e 100644 --- a/src/SQLite/SQLiteUnEscaper.php +++ b/src/SQLite/SQLiteUnEscaper.php @@ -2,15 +2,14 @@ namespace Wikibase\Database\SQLite; -use Wikibase\Database\UnEscaper; - /** - * UnEscaper to remove the Escaping from SQLLite escaped SQL strings + * UnEscaper to remove the Escaping from SQLLite escaped SQL strings. + * * @since 0.1 * @licence GNU GPL v2+ * @author Adam Shorland */ -class SQLiteUnEscaper implements UnEscaper { +class SQLiteUnEscaper { /** * @param string $identifier diff --git a/src/Standalone/DatabaseEscaper.php b/src/Standalone/DatabaseEscaper.php new file mode 100644 index 0000000..a59c64a --- /dev/null +++ b/src/Standalone/DatabaseEscaper.php @@ -0,0 +1,45 @@ + + */ +class DatabaseEscaper implements Escaper { + + protected $dbConnection; + + public function __construct( DatabaseBase $dbConnection ) { + $this->dbConnection = $dbConnection; + } + + /** + * @see Escaper::getEscapedValue + * + * @param mixed $value + * + * @return string The escaped value + */ + public function getEscapedValue( $value ) { + return $this->dbConnection->addQuotes( $value ); + } + + /** + * @see Escaper::getEscapedIdentifier + * + * @param mixed $identifier + * + * @return string The escaped identifier + */ + public function getEscapedIdentifier( $identifier ) { + return $this->dbConnection->addIdentifierQuotes( $identifier ); + } + +} diff --git a/src/UnEscaper.php b/src/UnEscaper.php deleted file mode 100644 index da4cd63..0000000 --- a/src/UnEscaper.php +++ /dev/null @@ -1,23 +0,0 @@ - + */ +interface ValueEscaper { + + /** + * @param mixed $value + * + * @return string The escaped value + */ + public function getEscapedValue( $value ); + +} From bf800a1b20de110760c0bb424fd19d27ca589f56 Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Sun, 1 Dec 2013 07:25:45 +0100 Subject: [PATCH 4/4] Remove MW test runner support The MW test runner does not make use of the PHPUnit config file and bootstrap unless it is specified, in which case one can just as well not use this special runner. (The bootstarp file needs to be included at this point.) Change-Id: I7629dfa02afed5dfc0c7beed858ae4c7d8f60d1d --- WikibaseDatabase.mw.php | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/WikibaseDatabase.mw.php b/WikibaseDatabase.mw.php index 5fe2bb1..6624d67 100644 --- a/WikibaseDatabase.mw.php +++ b/WikibaseDatabase.mw.php @@ -29,30 +29,3 @@ ); $GLOBALS['wgExtensionMessagesFiles']['WikibaseDatabase'] = __DIR__ . '/WikibaseDatabase.i18n.php'; - -/** - * Hook to add PHPUnit test cases. - * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList - * - * @since 0.1 - * - * @param array $files - * - * @return boolean - */ -$GLOBALS['wgHooks']['UnitTestsList'][] = function( array &$files ) { - // @codeCoverageIgnoreStart - $directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests' ); - - /** - * @var SplFileInfo $fileInfo - */ - foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) { - if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) { - $files[] = $fileInfo->getPathname(); - } - } - - return true; - // @codeCoverageIgnoreEnd -};