db-config.php 1.32 KB
Newer Older
lechuck's avatar
lechuck committed
1 2
<?php

3
// Common settings
Joe Oblivian's avatar
Joe Oblivian committed
4
$wpdb->persistent = true;
5 6
$wpdb->max_connections = 30;

7
define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends");
8
define("NOBLOGS_MASTER_CONFIG", "/etc/noblogs/master");
lechuck's avatar
lechuck committed
9

10 11
include_once('r2db/db-hash.php');
include_once('r2db/db-backends.php');
lechuck's avatar
lechuck committed
12

13 14
// Add the global database (configured in wp-config.php), stores the global
// blogs and users tables.
Joe's avatar
Joe committed
15
$is_master = __gf_ai_is_master();
16

lechuck's avatar
lechuck committed
17
$wpdb->add_database(array(
18 19 20 21 22
    "host" => DB_HOST,
    "user" => DB_USER,
    "password" => DB_PASSWORD,
    "name" => DB_NAME,
    "dataset" => "global",
Joe's avatar
Joe committed
23
    "write" => $is_master, "read" => 1, "timeout" => 2
24
    ));
lechuck's avatar
lechuck committed
25

26
// This is the write-only master.
Joe's avatar
Joe committed
27 28 29 30
if (!$is_master) {
    $wpdb->add_database(noblogs_load_master(NOBLOGS_MASTER_CONFIG));   
}

31 32

// Add all the sharded blog databases.
33
$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map);
Joe's avatar
Joe committed
34 35 36 37 38 39 40 41 42 43

// Ai patch: allows to understand if the current database is the master server
function __gf_ai_is_master() {
    $master_dsn = trim(file_get_contents('/etc/noblogs/master'));
    preg_match('/@172.16.1.(\d+):/', $master_dsn, $m);
    $master_id = $m[1];
    if (!empty($_SERVER['SERVER_ADDR']))  
        return (int) ('172.16.1.'.$master_id == $_SERVER['SERVER_ADDR']);//works on web
    return (int) ($master_id == $_SERVER['SERVER_PUBLIC_ID']);//works on cli
}