* (bug 4385) Separate parser cache entries for non-editing users, so section
[mediawiki.git] / includes / ExternalStoreDB.php
blob96237783501d7cff4cd06e18b6232312d40a6481
1 <?php
2 /**
3 *
4 * @package MediaWiki
6 * DB accessable external objects
8 */
9 require_once( 'LoadBalancer.php' );
12 /** @package MediaWiki */
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.
20 * Associative array of LoadBalancer objects, indexed by cluster name.
22 global $wgExternalLoadBalancers;
23 $wgExternalLoadBalancers = array();
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.
31 global $wgExternalBlobCache;
32 $wgExternalBlobCache = array();
34 class ExternalStoreDB {
35 /**
36 * Fetch data from given URL
37 * @param string $url An url
40 function &getLoadBalancer( $cluster ) {
41 global $wgExternalServers, $wgExternalLoadBalancers;
42 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
43 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
45 return $wgExternalLoadBalancers[$cluster];
48 function &getSlave( $cluster ) {
49 $lb =& $this->getLoadBalancer( $cluster );
50 return $lb->getConnection( DB_SLAVE );
53 function &getMaster( $cluster ) {
54 $lb =& $this->getLoadBalancer( $cluster );
55 return $lb->getConnection( DB_MASTER );
58 function getTable( &$db ) {
59 $table = $db->getLBInfo( 'blobs table' );
60 if ( is_null( $table ) ) {
61 $table = 'blobs';
63 return $table;
66 function fetchFromURL($url) {
67 global $wgExternalServers;
69 # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage
71 $path = explode( '/', $url );
72 $cluster = $path[2];
73 $id = $path[3];
74 if ( isset( $path[4] ) ) {
75 $itemID = $path[4];
76 } else {
77 $itemID = false;
80 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
82 if ( $itemID !== false ) {
83 return $ret->getItem( $itemID );
85 return $ret;
88 /**
89 * Fetch a blob item out of the database; a cache of the last-loaded
90 * blob will be kept so that multiple loads out of a multi-item blob
91 * can avoid redundant database access and decompression.
92 * @return mixed
93 * @access 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( $itemID !== false ) {
108 // Unserialise object; caller extracts item
109 $ret = unserialize( $ret );
112 $wgExternalBlobCache = array( $cacheID => &$ret );
113 return $ret;
117 * Insert a data item into a given cluster
119 * @param string $cluster The cluster name
120 * @param string $data The data item
121 * @return string URL
123 function store( $cluster, $data ) {
124 global $wgExternalServers;
125 $fname = 'ExternalStoreDB::store';
127 $dbw =& $this->getMaster( $cluster );
129 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
130 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
131 return "DB://$cluster/" . $dbw->insertId();