Fix typo reported at twn
[mediawiki.git] / includes / ExternalStoreDB.php
blobc37e706242e74f8a0014889ea5af73338a198d9c
1 <?php
3 /**
4 * External database storage will use one (or more) separate connection pools
5 * from what the main wiki uses. If we load many revisions, such as when doing
6 * bulk backups or maintenance, we want to keep them around over the lifetime
7 * of the script.
9 * Associative array of LoadBalancer objects, indexed by cluster name.
11 global $wgExternalLoadBalancers;
12 $wgExternalLoadBalancers = array();
14 /**
15 * One-step cache variable to hold base blobs; operations that
16 * pull multiple revisions may often pull multiple times from
17 * the same blob. By keeping the last-used one open, we avoid
18 * redundant unserialization and decompression overhead.
20 global $wgExternalBlobCache;
21 $wgExternalBlobCache = array();
23 /**
24 * DB accessable external objects
25 * @ingroup ExternalStorage
27 class ExternalStoreDB {
29 function __construct( $params = array() ) {
30 $this->mParams = $params;
33 /** @todo Document.*/
34 function &getLoadBalancer( $cluster ) {
35 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
37 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
40 /** @todo Document.*/
41 function &getSlave( $cluster ) {
42 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
43 $lb =& $this->getLoadBalancer( $cluster );
44 return $lb->getConnection( DB_SLAVE, array(), $wiki );
47 /** @todo Document.*/
48 function &getMaster( $cluster ) {
49 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
50 $lb =& $this->getLoadBalancer( $cluster );
51 return $lb->getConnection( DB_MASTER, array(), $wiki );
54 /** @todo Document.*/
55 function getTable( &$db ) {
56 $table = $db->getLBInfo( 'blobs table' );
57 if ( is_null( $table ) ) {
58 $table = 'blobs';
60 return $table;
63 /**
64 * Fetch data from given URL
65 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
67 function fetchFromURL( $url ) {
68 $path = explode( '/', $url );
69 $cluster = $path[2];
70 $id = $path[3];
71 if ( isset( $path[4] ) ) {
72 $itemID = $path[4];
73 } else {
74 $itemID = false;
77 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
79 if ( $itemID !== false && $ret !== false ) {
80 return $ret->getItem( $itemID );
82 return $ret;
85 /**
86 * Fetch a blob item out of the database; a cache of the last-loaded
87 * blob will be kept so that multiple loads out of a multi-item blob
88 * can avoid redundant database access and decompression.
89 * @param $cluster
90 * @param $id
91 * @param $itemID
92 * @return mixed
93 * @private
95 function &fetchBlob( $cluster, $id, $itemID ) {
96 global $wgExternalBlobCache;
97 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
98 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
99 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
100 return $wgExternalBlobCache[$cacheID];
103 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
105 $dbr =& $this->getSlave( $cluster );
106 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
107 if ( $ret === false ) {
108 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
109 // Try the master
110 $dbw =& $this->getMaster( $cluster );
111 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
112 if( $ret === false) {
113 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
116 if( $itemID !== false && $ret !== false ) {
117 // Unserialise object; caller extracts item
118 $ret = unserialize( $ret );
121 $wgExternalBlobCache = array( $cacheID => &$ret );
122 return $ret;
126 * Insert a data item into a given cluster
128 * @param $cluster String: the cluster name
129 * @param $data String: the data item
130 * @return string URL
132 function store( $cluster, $data ) {
133 $dbw = $this->getMaster( $cluster );
134 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
135 $dbw->insert( $this->getTable( $dbw ),
136 array( 'blob_id' => $id, 'blob_text' => $data ),
137 __METHOD__ );
138 $id = $dbw->insertId();
139 if ( !$id ) {
140 throw new MWException( __METHOD__.': no insert ID' );
142 if ( $dbw->getFlag( DBO_TRX ) ) {
143 $dbw->immediateCommit();
145 return "DB://$cluster/$id";