155e428b8e5bb25116d06b316a9d2dd809cb3546
[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 class ExternalStoreDB {
15 var $loadBalancers = array();
16
17 /**
18 * Fetch data from given URL
19 * @param string $url An url
20 */
21
22 function &getLoadBalancer( $cluster ) {
23 global $wgExternalServers;
24 if ( !array_key_exists( $cluster, $this->loadBalancers ) ) {
25 $this->loadBalancers[$cluster] = LoadBalancer::newFromParams( $wgExternalServers[$cluster] );
26 }
27 return $this->loadBalancers[$cluster];
28 }
29
30 function &getSlave( $cluster ) {
31 $lb =& $this->getLoadBalancer( $cluster );
32 return $lb->getConnection( DB_SLAVE );
33 }
34
35 function &getMaster( $cluster ) {
36 $lb =& $this->getLoadBalancer( $cluster );
37 return $lb->getConnection( DB_MASTER );
38 }
39
40 function ExternalStoreDB() {
41 $this->mLoadBalancer = LoadBalancer::NewFromParams( $wgExternalServers[$cluster] );
42 }
43
44 function fetchFromURL($url) {
45 global $wgExternalServers;
46 #
47 # URLs have the form DB://cluster/id or DB://cluster/id/itemid for concatenated storage
48 #
49 $path = explode( '/', $url );
50 $cluster = $path[2];
51 $id = $path[3];
52 if ( isset( $path[4] ) ) {
53 $itemID = $path[4];
54 } else {
55 $itemID = false;
56 }
57
58 $dbr =& $this->getSlave( $cluster );
59 $ret = $dbr->selectField( 'blobs', 'blob_text', array( 'blob_id' => $id ) );
60
61 if ( $itemID !== false ) {
62 # Unserialise object and get item
63 $obj = unserialize( $ret );
64 $ret = $obj->getItem( $itemID );
65 }
66
67 return $ret;
68 }
69
70 /**
71 * Insert a data item into a given cluster
72 *
73 * @param string $cluster The cluster name
74 * @param string $data The data item
75 * @return string URL
76 */
77 function store( $cluster, $data ) {
78 global $wgExternalServers;
79 $fname = 'ExternalStoreDB::store';
80
81 $dbw =& $this->getMaster( $cluster );
82
83 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
84 $dbw->insert( 'blobs', array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
85 return "DB://$cluster/" . $dbw->insertId();
86 }
87 }
88 ?>