* (bug 24563) Entries on Special:WhatLinksHere now have a link to their history
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2
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.
8 *
9 * Associative array of LoadBalancer objects, indexed by cluster name.
10 */
11 global $wgExternalLoadBalancers;
12 $wgExternalLoadBalancers = array();
13
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.
19 */
20 global $wgExternalBlobCache;
21 $wgExternalBlobCache = array();
22
23 /**
24 * DB accessable external objects
25 * @ingroup ExternalStorage
26 */
27 class ExternalStoreDB {
28
29 function __construct( $params = array() ) {
30 $this->mParams = $params;
31 }
32
33 /**
34 * Get a LoadBalancer for the specified cluster
35 *
36 * @param $cluster String: cluster name
37 * @return LoadBalancer object
38 */
39 function &getLoadBalancer( $cluster ) {
40 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
41
42 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
43 }
44
45 /**
46 * Get a slave database connection for the specified cluster
47 *
48 * @param $cluster String: cluster name
49 * @return DatabaseBase object
50 */
51 function &getSlave( $cluster ) {
52 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
53 $lb =& $this->getLoadBalancer( $cluster );
54 return $lb->getConnection( DB_SLAVE, array(), $wiki );
55 }
56
57 /**
58 * Get a master database connection for the specified cluster
59 *
60 * @param $cluster String: cluster name
61 * @return DatabaseBase object
62 */
63 function &getMaster( $cluster ) {
64 $wiki = isset($this->mParams['wiki']) ? $this->mParams['wiki'] : false;
65 $lb =& $this->getLoadBalancer( $cluster );
66 return $lb->getConnection( DB_MASTER, array(), $wiki );
67 }
68
69 /**
70 * Get the 'blobs' table name for this database
71 *
72 * @param $db DatabaseBase
73 * @return String: table name ('blobs' by default)
74 */
75 function getTable( &$db ) {
76 $table = $db->getLBInfo( 'blobs table' );
77 if ( is_null( $table ) ) {
78 $table = 'blobs';
79 }
80 return $table;
81 }
82
83 /**
84 * Fetch data from given URL
85 * @param $url String: an url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
86 */
87 function fetchFromURL( $url ) {
88 $path = explode( '/', $url );
89 $cluster = $path[2];
90 $id = $path[3];
91 if ( isset( $path[4] ) ) {
92 $itemID = $path[4];
93 } else {
94 $itemID = false;
95 }
96
97 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
98
99 if ( $itemID !== false && $ret !== false ) {
100 return $ret->getItem( $itemID );
101 }
102 return $ret;
103 }
104
105 /**
106 * Fetch a blob item out of the database; a cache of the last-loaded
107 * blob will be kept so that multiple loads out of a multi-item blob
108 * can avoid redundant database access and decompression.
109 * @param $cluster
110 * @param $id
111 * @param $itemID
112 * @return mixed
113 * @private
114 */
115 function &fetchBlob( $cluster, $id, $itemID ) {
116 global $wgExternalBlobCache;
117 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
118 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
119 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
120 return $wgExternalBlobCache[$cacheID];
121 }
122
123 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
124
125 $dbr =& $this->getSlave( $cluster );
126 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
127 if ( $ret === false ) {
128 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
129 // Try the master
130 $dbw =& $this->getMaster( $cluster );
131 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
132 if( $ret === false) {
133 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
134 }
135 }
136 if( $itemID !== false && $ret !== false ) {
137 // Unserialise object; caller extracts item
138 $ret = unserialize( $ret );
139 }
140
141 $wgExternalBlobCache = array( $cacheID => &$ret );
142 return $ret;
143 }
144
145 /**
146 * Insert a data item into a given cluster
147 *
148 * @param $cluster String: the cluster name
149 * @param $data String: the data item
150 * @return string URL
151 */
152 function store( $cluster, $data ) {
153 $dbw = $this->getMaster( $cluster );
154 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
155 $dbw->insert( $this->getTable( $dbw ),
156 array( 'blob_id' => $id, 'blob_text' => $data ),
157 __METHOD__ );
158 $id = $dbw->insertId();
159 if ( !$id ) {
160 throw new MWException( __METHOD__.': no insert ID' );
161 }
162 if ( $dbw->getFlag( DBO_TRX ) ) {
163 $dbw->commit();
164 }
165 return "DB://$cluster/$id";
166 }
167 }