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