Another @group Broken
[mediawiki.git] / includes / ExternalStoreDB.php
blobfee49f26cd34253da9950e0e36ed1946809800a1
1 <?php
3 /**
4 * DB accessable external objects
5 * @ingroup ExternalStorage
6 */
7 class ExternalStoreDB {
9 function __construct( $params = array() ) {
10 $this->mParams = $params;
13 /**
14 * Get a LoadBalancer for the specified cluster
16 * @param $cluster String: cluster name
17 * @return LoadBalancer object
19 function &getLoadBalancer( $cluster ) {
20 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
22 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
25 /**
26 * Get a slave database connection for the specified cluster
28 * @param $cluster String: cluster name
29 * @return DatabaseBase object
31 function &getSlave( $cluster ) {
32 global $wgDefaultExternalStore;
34 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
35 $lb =& $this->getLoadBalancer( $cluster );
37 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
38 wfDebug( "read only external store" );
39 $lb->allowLagged(true);
40 } else {
41 wfDebug( "writable external store" );
44 return $lb->getConnection( DB_SLAVE, array(), $wiki );
47 /**
48 * Get a master database connection for the specified cluster
50 * @param $cluster String: cluster name
51 * @return DatabaseBase object
53 function &getMaster( $cluster ) {
54 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
55 $lb =& $this->getLoadBalancer( $cluster );
56 return $lb->getConnection( DB_MASTER, array(), $wiki );
59 /**
60 * Get the 'blobs' table name for this database
62 * @param $db DatabaseBase
63 * @return String: table name ('blobs' by default)
65 function getTable( &$db ) {
66 $table = $db->getLBInfo( 'blobs table' );
67 if ( is_null( $table ) ) {
68 $table = 'blobs';
70 return $table;
73 /**
74 * Fetch data from given URL
75 * @param $url String: an url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
77 function fetchFromURL( $url ) {
78 $path = explode( '/', $url );
79 $cluster = $path[2];
80 $id = $path[3];
81 if ( isset( $path[4] ) ) {
82 $itemID = $path[4];
83 } else {
84 $itemID = false;
87 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
89 if ( $itemID !== false && $ret !== false ) {
90 return $ret->getItem( $itemID );
92 return $ret;
95 /**
96 * Fetch a blob item out of the database; a cache of the last-loaded
97 * blob will be kept so that multiple loads out of a multi-item blob
98 * can avoid redundant database access and decompression.
99 * @param $cluster
100 * @param $id
101 * @param $itemID
102 * @return mixed
103 * @private
105 function &fetchBlob( $cluster, $id, $itemID ) {
107 * One-step cache variable to hold base blobs; operations that
108 * pull multiple revisions may often pull multiple times from
109 * the same blob. By keeping the last-used one open, we avoid
110 * redundant unserialization and decompression overhead.
112 static $externalBlobCache = array();
114 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
115 if( isset( $externalBlobCache[$cacheID] ) ) {
116 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
117 return $externalBlobCache[$cacheID];
120 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
122 $dbr =& $this->getSlave( $cluster );
123 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
124 if ( $ret === false ) {
125 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
126 // Try the master
127 $dbw =& $this->getMaster( $cluster );
128 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
129 if( $ret === false) {
130 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
133 if( $itemID !== false && $ret !== false ) {
134 // Unserialise object; caller extracts item
135 $ret = unserialize( $ret );
138 $externalBlobCache = array( $cacheID => &$ret );
139 return $ret;
143 * Insert a data item into a given cluster
145 * @param $cluster String: the cluster name
146 * @param $data String: the data item
147 * @return string URL
149 function store( $cluster, $data ) {
150 $dbw = $this->getMaster( $cluster );
151 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
152 $dbw->insert( $this->getTable( $dbw ),
153 array( 'blob_id' => $id, 'blob_text' => $data ),
154 __METHOD__ );
155 $id = $dbw->insertId();
156 if ( !$id ) {
157 throw new MWException( __METHOD__.': no insert ID' );
159 if ( $dbw->getFlag( DBO_TRX ) ) {
160 $dbw->commit();
162 return "DB://$cluster/$id";