From: daniel Date: Mon, 19 Nov 2012 09:32:51 +0000 (+0100) Subject: Base class for objects accessign databases. X-Git-Tag: 1.31.0-rc.0~21497^2~1 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=1c4cc095b55984a9ca97c9e99632472114c8d4d8;p=lhc%2Fweb%2Fwiklou.git Base class for objects accessign databases. DBAccessBase provides utility methods for DB access, including access to other wiki's databases. Change-Id: I3c23a5c6e49e4921d48fddd72f2cf28ad1d13a58 --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 8e2447d719..4d728d76d0 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -456,6 +456,7 @@ $wgAutoloadLocalClasses = array( # includes/dao 'IDBAccessObject' => 'includes/dao/IDBAccessObject.php', + 'DBAccessBase' => 'includes/dao/DBAccessBase.php', # includes/db 'Blob' => 'includes/db/DatabaseUtility.php', diff --git a/includes/dao/DBAccessBase.php b/includes/dao/DBAccessBase.php new file mode 100644 index 0000000000..72e54fc901 --- /dev/null +++ b/includes/dao/DBAccessBase.php @@ -0,0 +1,87 @@ +wiki = $wiki; + } + + /** + * Returns a database connection. + * + * @see wfGetDB() + * @see LoadBalancer::getConnection() + * + * @param int $id Which connection to use + * + * @return \DatabaseBase + */ + protected function getConnection( $id ) { + $loadBalancer = wfGetLB( $this->wiki ); + return $loadBalancer->getConnection( $id ); + } + + /** + * Releases a database connection and makes it available for recycling. + * + * @see LoadBalancer::reuseConnection() + * + * @param \DatabaseBase $db the database connection to release. + */ + protected function releaseConnection( \DatabaseBase $db ) { + if ( $this->wiki !== false ) { + $loadBalancer = $this->getLoadBalancer(); + $loadBalancer->reuseConnection( $db ); + } + } + + /** + * Get the database type used for read operations. + * + * @see wfGetLB + * + * @since 1.20 + * + * @return LoadBalancer The database load balancer object + */ + public function getLoadBalancer() { + return wfGetLB( $this->wiki ); + } +} diff --git a/includes/db/ORMTable.php b/includes/db/ORMTable.php index 0756ce83f4..13c2d9ff2c 100644 --- a/includes/db/ORMTable.php +++ b/includes/db/ORMTable.php @@ -27,7 +27,7 @@ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -abstract class ORMTable implements IORMTable { +abstract class ORMTable extends DBAccessBase implements IORMTable { /** * Gets the db field prefix. @@ -55,15 +55,6 @@ abstract class ORMTable implements IORMTable { */ protected $readDb = DB_SLAVE; - /** - * The ID of any foreign wiki to use as a target for database operations, - * or false to use the local wiki. - * - * @since 1.20 - * @var String|bool - */ - protected $wiki = false; - /** * Returns a list of default field values. * field name => field value @@ -489,19 +480,6 @@ abstract class ORMTable implements IORMTable { return $this->getLoadBalancer()->getConnection( DB_MASTER, array(), $this->getTargetWiki() ); } - /** - * Get the database type used for read operations. - * - * @see wfGetLB - * - * @since 1.20 - * - * @return LoadBalancer The database load balancer object - */ - public function getLoadBalancer() { - return wfGetLB( $this->getTargetWiki() ); - } - /** * Releases the lease on the given database connection. This is useful mainly * for connections to a foreign wiki. It does nothing for connections to the local wiki. @@ -513,10 +491,7 @@ abstract class ORMTable implements IORMTable { * @since 1.20 */ public function releaseConnection( DatabaseBase $db ) { - if ( $this->wiki !== false ) { - // recycle connection to foreign wiki - $this->getLoadBalancer()->reuseConnection( $db ); - } + parent::releaseConnection( $db ); // just make it public } /**