Skip to content

Commit

Permalink
Merge 6b07b39 into 96baa2a
Browse files Browse the repository at this point in the history
  • Loading branch information
s7eph4n committed Oct 9, 2016
2 parents 96baa2a + 6b07b39 commit e05bd0d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
20 changes: 20 additions & 0 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ public function getInMemoryPoolCache() {
return $this->callbackLoader->singleton( 'InMemoryPoolCache' );
}

/**
* @since 2.5
*
* @return \LoadBalancer
*/
public function getLoadBalancer() {
return $this->callbackLoader->singleton( 'DBLoadBalancer' );
}

/**
* @since 2.5
*
* @param \IDatabase $db
* @return string
*/
public function getDefaultSearchEngineTypeForDB( \IDatabase $db ) {
return $this->callbackLoader->create( 'DefaultSearchEngineTypeForDB', $db );

}

/**
* @since 2.4
*
Expand Down
34 changes: 7 additions & 27 deletions src/MediaWiki/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Content;
use DatabaseBase;
use MediaWiki\MediaWikiServices;
use RuntimeException;
use SearchEngine;
use SMW\ApplicationFactory;
Expand Down Expand Up @@ -49,10 +48,6 @@ public function setFallbackSearchEngine( SearchEngine $fallbackSearch = null ) {
*/
private function assertValidFallbackSearchEngineType( $type ) {

if ( $type === null ) {
return;
}

if ( !class_exists( $type ) ) {
throw new RuntimeException( "$type does not exist." );
}
Expand All @@ -62,23 +57,6 @@ private function assertValidFallbackSearchEngineType( $type ) {
}
}

/**
* @param $db
* @return string
*/
private function getDefaultSearchEngineTypeForDB( $db ) {

if ( method_exists( 'SearchEngineFactory', 'getSearchEngineClass' ) ) { // MW > 1.27

return \SearchEngineFactory::getSearchEngineClass( $db );

} else { // MW <= 1.27

return $db->getSearchEngine();

}
}

/**
* @return SearchEngine
*/
Expand All @@ -88,14 +66,14 @@ public function getFallbackSearchEngine() {

$type = ApplicationFactory::getInstance()->getSettings()->get( 'smwgFallbackSearchType' );

$this->assertValidFallbackSearchEngineType( $type );

$dbr = $this->getDB();

if ( $type === null ) {
$type = $this->getDefaultSearchEngineTypeForDB( $dbr );
$type = ApplicationFactory::getInstance()->getDefaultSearchEngineTypeForDB( $dbr );
}

$this->assertValidFallbackSearchEngineType( $type );

$this->fallbackSearch = new $type( $dbr );
}

Expand All @@ -111,12 +89,14 @@ public function setDB( DatabaseBase $connection ) {
}

/**
* @return DatabaseBase
* @return \IDatabase
*/
public function getDB() {

if ( $this->database === null ) {
$this->database = wfGetDB( defined( 'DB_REPLICA' ) ? DB_REPLICA : DB_SLAVE );

$this->database = ApplicationFactory::getInstance()->getLoadBalancer()->getConnection( defined( 'DB_REPLICA' ) ? DB_REPLICA : DB_SLAVE );

}

return $this->database;
Expand Down
23 changes: 23 additions & 0 deletions src/SharedCallbackContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SMW;

use LBFactory;
use MediaWiki\MediaWikiServices;
use Onoi\BlobStore\BlobStore;
use Onoi\CallbackContainer\CallbackContainer;
use Onoi\CallbackContainer\CallbackLoader;
Expand Down Expand Up @@ -100,6 +102,27 @@ private function registerCallbackHandlers( $callbackLoader ) {
return new DeferredCallableUpdate( $callback );
} );

$callbackLoader->registerExpectedReturnType( 'DBLoadBalancer', '\LoadBalancer' );

$callbackLoader->registerCallback( 'DBLoadBalancer', function() {
if ( class_exists( '\MediaWiki\MediaWikiServices' ) && method_exists( '\MediaWiki\MediaWikiServices', 'getDBLoadBalancer' ) ) { // > MW 1.27
return MediaWikiServices::getInstance()->getDBLoadBalancer();
}

return LBFactory::singleton()->getMainLB();
} );

$callbackLoader->registerCallback( 'DefaultSearchEngineTypeForDB', function( \IDatabase $db ) use ( $callbackLoader ) {

if ( class_exists( '\MediaWiki\MediaWikiServices' ) && method_exists( 'SearchEngineFactory', 'getSearchEngineClass' ) ) { // MW > 1.27

return MediaWikiServices::getInstance()->getSearchEngineFactory()->getSearchEngineClass( $db );

}

return $db->getSearchEngine();
} );

/**
* @var InMemoryPoolCache
*/
Expand Down
16 changes: 12 additions & 4 deletions tests/phpunit/Unit/MediaWiki/Search/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,23 @@ public function testSetGetDBConnection() {

public function testGetDefaultFallbackSearchEngineForNullFallbackSearchType() {

// #1531
$searchEngine = class_exists( 'SearchDatabase' ) ? 'SearchDatabase' : 'SearchEngine';
$searchEngine = 'SearchDatabase';

if ( class_exists( 'SearchEngine' ) ) {

$reflection = new \ReflectionClass( 'SearchEngine' );

if ( $reflection->isInstantiable() ) {
$searchEngine = 'SearchEngine';
}
}

$databaseBase = $this->getMockBuilder( '\DatabaseBase' )
->disableOriginalConstructor()
->setMethods( array( 'getSearchEngine' ) )
->getMockForAbstractClass();

$databaseBase->expects( $this->once() )
$databaseBase->expects( $this->any() )
->method( 'getSearchEngine' )
->will( $this->returnValue( $searchEngine ) );

Expand All @@ -73,7 +81,7 @@ public function testGetDefaultFallbackSearchEngineForNullFallbackSearchType() {
$search->setDB( $databaseBase );

$this->assertInstanceOf(
$searchEngine,
'SearchEngine',
$search->getFallbackSearchEngine()
);
}
Expand Down

0 comments on commit e05bd0d

Please sign in to comment.