*cough* Er, make it work? :) [dropped the action=submit] part
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 *
6 * DB accessable external objects
7 *
8 */
9 require_once( 'LoadBalancer.php' );
10
11
12 /** @package MediaWiki */
13
14 /**
15 * External database storage will use one (or more) separate connection pools
16 * from what the main wiki uses. If we load many revisions, such as when doing
17 * bulk backups or maintenance, we want to keep them around over the lifetime
18 * of the script.
19 *
20 * Associative array of LoadBalancer objects, indexed by cluster name.
21 */
22 global $wgExternalLoadBalancers;
23 $wgExternalLoadBalancers = array();
24
25 /**
26 * One-step cache variable to hold base blobs; operations that
27 * pull multiple revisions may often pull multiple times from
28 * the same blob. By keeping the last-used one open, we avoid
29 * redundant unserialization and decompression overhead.
30 */
31 global $wgExternalBlobCache;
32 $wgExternalBlobCache = array();
33
34 class ExternalStoreDB {
35
36 /** @todo Document.*/
37 function &getLoadBalancer( $cluster ) {
38 global $wgExternalServers, $wgExternalLoadBalancers;
39 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
40 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
41 }
42 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
43 return $wgExternalLoadBalancers[$cluster];
44 }
45
46 /** @todo Document.*/
47 function &getSlave( $cluster ) {
48 $lb =& $this->getLoadBalancer( $cluster );
49 return $lb->getConnection( DB_SLAVE );
50 }
51
52 /** @todo Document.*/
53 function &getMaster( $cluster ) {
54 $lb =& $this->getLoadBalancer( $cluster );
55 return $lb->getConnection( DB_MASTER );
56 }
57
58 /** @todo Document.*/
59 function getTable( &$db ) {
60 $table = $db->getLBInfo( 'blobs table' );
61 if ( is_null( $table ) ) {
62 $table = 'blobs';
63 }
64 return $table;
65 }
66
67 /**
68 * Fetch data from given URL
69 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
70 */
71 function fetchFromURL($url) {
72 $path = explode( '/', $url );
73 $cluster = $path[2];
74 $id = $path[3];
75 if ( isset( $path[4] ) ) {
76 $itemID = $path[4];
77 } else {
78 $itemID = false;
79 }
80
81 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
82
83 if ( $itemID !== false && $ret !== false ) {
84 return $ret->getItem( $itemID );
85 }
86 return $ret;
87 }
88
89 /**
90 * Fetch a blob item out of the database; a cache of the last-loaded
91 * blob will be kept so that multiple loads out of a multi-item blob
92 * can avoid redundant database access and decompression.
93 * @param $cluster
94 * @param $id
95 * @param $itemID
96 * @return mixed
97 * @private
98 */
99 function &fetchBlob( $cluster, $id, $itemID ) {
100 global $wgExternalBlobCache;
101 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
102 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
103 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
104 return $wgExternalBlobCache[$cacheID];
105 }
106
107 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
108
109 $dbr =& $this->getSlave( $cluster );
110 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
111 if ( $ret === false ) {
112 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
113 // Try the master
114 $dbw =& $this->getMaster( $cluster );
115 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
116 if( $ret === false) {
117 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
118 }
119 }
120 if( $itemID !== false && $ret !== false ) {
121 // Unserialise object; caller extracts item
122 $ret = unserialize( $ret );
123 }
124
125 $wgExternalBlobCache = array( $cacheID => &$ret );
126 return $ret;
127 }
128
129 /**
130 * Insert a data item into a given cluster
131 *
132 * @param $cluster String: the cluster name
133 * @param $data String: the data item
134 * @return string URL
135 */
136 function store( $cluster, $data ) {
137 $fname = 'ExternalStoreDB::store';
138
139 $dbw =& $this->getMaster( $cluster );
140
141 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
142 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
143 $id = $dbw->insertId();
144 if ( $dbw->getFlag( DBO_TRX ) ) {
145 $dbw->immediateCommit();
146 }
147 return "DB://$cluster/$id";
148 }
149 }
150 ?>