Localisation updates for core and extension messages from translatewiki.net
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2
3 /**
4 * DB accessable external objects
5 * @ingroup ExternalStorage
6 */
7 class ExternalStoreDB {
8
9 function __construct( $params = array() ) {
10 $this->mParams = $params;
11 }
12
13 /**
14 * Get a LoadBalancer for the specified cluster
15 *
16 * @param $cluster String: cluster name
17 * @return LoadBalancer object
18 */
19 function &getLoadBalancer( $cluster ) {
20 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
21
22 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
23 }
24
25 /**
26 * Get a slave database connection for the specified cluster
27 *
28 * @param $cluster String: cluster name
29 * @return DatabaseBase object
30 */
31 function &getSlave( $cluster ) {
32 global $wgDefaultExternalStore;
33
34 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
35 $lb =& $this->getLoadBalancer( $cluster );
36
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" );
42 }
43
44 return $lb->getConnection( DB_SLAVE, array(), $wiki );
45 }
46
47 /**
48 * Get a master database connection for the specified cluster
49 *
50 * @param $cluster String: cluster name
51 * @return DatabaseBase object
52 */
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 );
57 }
58
59 /**
60 * Get the 'blobs' table name for this database
61 *
62 * @param $db DatabaseBase
63 * @return String: table name ('blobs' by default)
64 */
65 function getTable( &$db ) {
66 $table = $db->getLBInfo( 'blobs table' );
67 if ( is_null( $table ) ) {
68 $table = 'blobs';
69 }
70 return $table;
71 }
72
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.
76 */
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;
85 }
86
87 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
88
89 if ( $itemID !== false && $ret !== false ) {
90 return $ret->getItem( $itemID );
91 }
92 return $ret;
93 }
94
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
104 */
105 function &fetchBlob( $cluster, $id, $itemID ) {
106 /**
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.
111 */
112 static $externalBlobCache = array();
113
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];
118 }
119
120 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
121
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" );
131 }
132 }
133 if( $itemID !== false && $ret !== false ) {
134 // Unserialise object; caller extracts item
135 $ret = unserialize( $ret );
136 }
137
138 $externalBlobCache = array( $cacheID => &$ret );
139 return $ret;
140 }
141
142 /**
143 * Insert a data item into a given cluster
144 *
145 * @param $cluster String: the cluster name
146 * @param $data String: the data item
147 * @return string URL
148 */
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' );
158 }
159 if ( $dbw->getFlag( DBO_TRX ) ) {
160 $dbw->commit();
161 }
162 return "DB://$cluster/$id";
163 }
164 }