* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[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 * Fetch data from given URL
37 * @param string $url An url
38 */
39
40 function &getLoadBalancer( $cluster ) {
41 global $wgExternalServers, $wgExternalLoadBalancers;
42 if ( !array_key_exists( $cluster, $wgExternalLoadBalancers ) ) {
43 $wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
44 }
45 $wgExternalLoadBalancers[$cluster]->allowLagged(true);
46 return $wgExternalLoadBalancers[$cluster];
47 }
48
49 function &getSlave( $cluster ) {
50 $lb =& $this->getLoadBalancer( $cluster );
51 return $lb->getConnection( DB_SLAVE );
52 }
53
54 function &getMaster( $cluster ) {
55 $lb =& $this->getLoadBalancer( $cluster );
56 return $lb->getConnection( DB_MASTER );
57 }
58
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 function fetchFromURL($url) {
68 global $wgExternalServers;
69 #
70 # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage
71 #
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 * @return mixed
94 * @access private
95 */
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];
102 }
103
104 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
105
106 $dbr =& $this->getSlave( $cluster );
107 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
108 if ( $ret === false ) {
109 // Try the master
110 $dbw =& $this->getMaster( $cluster );
111 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
112 }
113 if( $itemID !== false && $ret !== false ) {
114 // Unserialise object; caller extracts item
115 $ret = unserialize( $ret );
116 }
117
118 $wgExternalBlobCache = array( $cacheID => &$ret );
119 return $ret;
120 }
121
122 /**
123 * Insert a data item into a given cluster
124 *
125 * @param string $cluster The cluster name
126 * @param string $data The data item
127 * @return string URL
128 */
129 function store( $cluster, $data ) {
130 global $wgExternalServers;
131 $fname = 'ExternalStoreDB::store';
132
133 $dbw =& $this->getMaster( $cluster );
134
135 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
136 $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
137 $id = $dbw->insertId();
138 if ( $dbw->getFlag( DBO_TRX ) ) {
139 $dbw->immediateCommit();
140 }
141 return "DB://$cluster/$id";
142 }
143 }
144 ?>