protected function createMongoDBConfig($update_from_config_alt)
 {
     require_once "I2CE_MagicDataStorageMongoDB.php";
     $db = MDB2::singleton();
     if (!($db_name = $db->database_name)) {
         I2CE::raiseError("No database to connect to MongoDB");
         return false;
     }
     if (!($db_password = $db->dsn['password'])) {
         I2CE::raiseError("No password to connect to MongoDB");
         return false;
     }
     if (!($db_user = $db->dsn['username'])) {
         I2CE::raiseError("No user to connect to MongoDB");
         return false;
     }
     $conn_string = "mongodb://localhost";
     try {
         $m = new Mongo($conn_string);
     } catch (Exception $e) {
         I2CE::raiseError("Could not connect to mongodb using: " . $conn_string);
         return false;
     }
     if (!is_array($dbs = $m->listDBs()) || !array_key_exists('databases', $dbs) || !is_array($dbs['databases'])) {
         I2CE::raiseError("MongoDB: {$db_name} needs to be created");
         //this is done automatically when we select below
     } else {
         $found = false;
         foreach ($dbs['databases'] as $info) {
             if (!is_array($info) || !array_key_exists('name', $info) || !($info['name'] = $db_name)) {
                 continue;
             }
             $found = true;
         }
         if (!$found) {
             I2CE::raiseError("MongoDB: The database {$db_name} needs to be created:" . print_r($dbs, true));
         }
     }
     $new_coll = false;
     if (!($mdb = $m->selectDB($db_name)) instanceof MongoDB) {
         I2CE::raiseError("Cannont connect to mongo database {$db_name}");
         return false;
     }
     if (!is_array($mcolls = $mdb->listCollections()) || count($mcolls) == 0 || !in_array($db_name . ".config", $mcolls)) {
         I2CE::raiseError("MongoDB: collection {$db_name}.config needs to be created");
         //this is done automatically when we select below
         $new_coll = true;
     }
     if (!($mcoll = $mdb->selectCollection('config')) instanceof MongoCollection) {
         I2CE::raiseError("Cannot connect to config collection of mongo database {$db_name}");
         return false;
     }
     if (!I2CE_MagicDataStorageMongoDB::setupIndices($mcoll)) {
         I2CE::raiseError("Could not setup indices");
         return false;
     }
     $qry = 'SHOW TABLES LIKE "config_alt"';
     $result = $db->query($qry);
     if (I2CE::pearError($result, "Cannot access database")) {
         return false;
     }
     if ($result->numRows() < 1) {
         $update_from_config_alt = false;
     }
     if ($update_from_config_alt && $new_coll) {
         $qry_nodes = "SELECT IF(  LENGTH(p.parent) > 1, concat(p.parent,'/',p.name),IF (LENGTH(p.name) > 1, CONCAT('/',p.name), '')) as path, p.value as value, p.type as type, \nGROUP_CONCAT(c.name SEPARATOR '/') AS children\nfrom config_alt AS p LEFT JOIN\n config_alt c \nON c.parent  =  IF( \n LENGTH(p.parent) > 1,\n  concat(p.parent,'/',p.name),\n IF( LENGTH(p.name) > 1, CONCAT('/',p.name), CONCAT('/',p.name))\n) \nGROUP BY path";
         $res = $db->query($qry_nodes);
         if (PEAR::isError($res)) {
             I2CE::raiseError("Could not query existing config_alt");
             return false;
         }
         I2CE::raiseError("Populating mongodb from config_alt table");
         while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
             if ($row['type'] == '0') {
                 $children = preg_split('/\\//', $row['children'], -1, PREG_SPLIT_NO_EMPTY);
             } else {
                 $children = null;
             }
             $data = array(I2CE_MagicDataStorageMongoDB::PATH => $row['path'], I2CE_MagicDataStorageMongoDB::TYPE => $row['type'], I2CE_MagicDataStorageMongoDB::VALUE => $row['value'], I2CE_MagicDataStorageMongoDB::CHILDREN => $children);
             if (!is_array($r = $mcoll->insert($data, array("safe" => true))) || array_key_exists('err', $r) && $r['err']) {
                 I2CE::raiseError("Could not inset new node:" . print_r($r, true));
                 return false;
             }
         }
     }
     if (!$this->setupMagicDataStorage()) {
         I2CE::raiseError("Could not setup magic data storage");
     }
     return true;
 }
示例#2
0
 /**
  * Creates  the magic data instance.
  * Sets the mafic data storage mechanisms to be used.
  * @param boolean $set_config.  Defaults to true meaning we set the magic data storage for I2CE
  * @param boolean $set_config.  Defaults to false meaning we dont replace the magic data instance
  * @returns I2CE_MagicData on success, null on failure
  */
 public static function setupMagicData($set_config = true, $replace = false)
 {
     $config_protocol = self::getRuntimeVariable('I2CE_CONFIG_PROTOCOL', 'config_alt');
     switch (strtolower($config_protocol)) {
         case 'mongodb':
             require_once 'I2CE_MagicDataStorageMongoDB.php';
             $config = I2CE_MagicData::instance("config", $replace);
             $store_mongo = new I2CE_MagicDataStorageMongoDB("config");
             if (!$store_mongo->isAvailable()) {
                 I2CE::raiseError("MongoDB config  not available.  Trying to use original config_alt table");
                 //the alternative config table has not been created.  the config table may be there, so let's try it
                 require_once 'I2CE_MagicDataStorageDBAlt.php';
                 $store_db = new I2CE_MagicDataStorageDBAlt("config");
                 if (!$store_db || !$store_db->isAvailable()) {
                     I2CE::raiseError("No persistent caching storage mechanism (apc or memchached) is available. Adding simple memory storage");
                     require_once 'I2CE_MagicDataStorageMem.php';
                     $config->addStorage(new I2CE_MagicDataStorageMem());
                 } else {
                     I2CE::raiseError("Added persistent caching storage mechanism config_alt");
                     $config->addStorage($store_db);
                 }
             } else {
                 require_once 'I2CE_MagicDataStorageAPC.php';
                 $db = MDB2::singleton();
                 $store_mem = new I2CE_MagicDataStorageAPC($db->database_name . "_config");
                 if ($store_mem->isAvailable()) {
                     $config->addStorage($store_mem);
                 }
             }
             $config->addStorage($store_mongo);
             //store_mongo may not be available.  that's OK on initialization
             if ($set_config) {
                 self::setConfig($config);
             }
             return $config;
         default:
             $db = MDB2::singleton();
             require_once 'I2CE_MagicDataStorageAPC.php';
             require_once 'I2CE_MagicDataStorageDBAlt.php';
             $store_db = new I2CE_MagicDataStorageDBAlt("config");
             if (!$store_db->isAvailable()) {
                 I2CE::raiseError("Alt config table not available.  Trying to use original config table");
                 //the alternative config table has not been created.  the config table may be there, so let's try it
                 require_once 'I2CE_MagicDataStorageDB.php';
                 $store_db = new I2CE_MagicDataStorageDB("config");
             }
             $config = I2CE_MagicData::instance("config", $replace);
             $store_mem = new I2CE_MagicDataStorageAPC($db->database_name . "_config");
             if ($store_mem->isAvailable()) {
                 $config->addStorage($store_mem);
             }
             require_once 'I2CE_MagicDataStorageMemcached.php';
             $store_memcached = new I2CE_MagicDataStorageMemcached($db->database_name . '_config');
             if ($store_memcached->isAvailable()) {
                 $config->addStorage($store_memcached);
             } else {
                 I2CE::raiseError("No memcached");
             }
             if (!$store_mem->isAvailable() && !$store_memcached->isAvailable()) {
                 I2CE::raiseError("No persistent caching storage mechanism (apc or memchached) is available. Adding simple memory storage");
                 require_once 'I2CE_MagicDataStorageMem.php';
                 $config->addStorage(new I2CE_MagicDataStorageMem());
             }
             $config->addStorage($store_db);
             //store_dn may not be available.  that's OK on initialization
             if ($set_config) {
                 self::setConfig($config);
             }
             return $config;
     }
 }