View source
<?php
function mongodb_help($path, $arg) {
switch ($path) {
case 'admin/help#mongodb':
return '<p>' . t('<a href="!project">MongoDB</a> implements a generic <a href="!mongo">MongoDB</a> interface.', array(
'!project' => 'http://drupal.org/project/mongodb',
'!mongo' => 'http://www.mongodb.org/',
));
}
}
function mongodb($alias = 'default') {
static $mongo_objects;
$connections = variable_get('mongodb_connections', array());
if (!isset($connections[$alias])) {
$alias = 'default';
}
$connection = isset($connections[$alias]) ? $connections[$alias] : array();
$connection += array(
'host' => 'localhost',
'db' => 'drupal',
'connection_options' => array(),
);
$host = $connection['host'];
$options = $connection['connection_options'] + array(
'connect' => TRUE,
);
$db = $connection['db'];
if (!isset($mongo_objects[$host][$db])) {
try {
if (class_exists('MongoClient')) {
$mongo = new MongoClient($host, $options);
if (!empty($connection['read_preference'])) {
$tags = !empty($connection['read_preference']['tags']) ? $connection['read_preference']['tags'] : array();
$mongo
->setReadPreference($connection['read_preference']['preference'], $tags);
}
}
else {
$mongo = new Mongo($host, $options);
if (!empty($connection['slave_ok'])) {
$mongo
->setSlaveOkay(TRUE);
}
}
$mongo_objects[$host][$db] = $mongo
->selectDB($db);
$mongo_objects[$host][$db]->connection = $mongo;
} catch (MongoConnectionException $e) {
$mongo_objects[$host][$db] = new MongoDummy();
throw $e;
}
}
return $mongo_objects[$host][$db];
}
function mongodb_collection() {
$args = array_filter(func_get_args());
if (is_array($args[0])) {
list($collection_name, $prefixed) = $args[0];
$prefixed .= $collection_name;
}
else {
$collection_name = implode('.', array_filter($args));
$prefixed = mongodb_collection_name($collection_name);
}
$collections = variable_get('mongodb_collections', array());
if (isset($collections[$collection_name])) {
$alias = is_array($collections[$collection_name]) && !empty($collections[$collection_name]['db_connection']) ? $collections[$collection_name]['db_connection'] : $collections[$collection_name];
}
else {
$alias = 'default';
}
$mongodb_object = mongodb($alias);
$collection = $mongodb_object
->selectCollection(mongodb_collection_name($collection_name));
if (!empty($collections[$alias]['read_preference']) && get_class($mongodb_object->connection) == 'MongoClient') {
$tags = !empty($collections[$alias]['read_preference']['tags']) ? $collections[$alias]['read_preference']['tags'] : array();
$collection
->setReadPreference($collections[$alias]['read_preference']['preference'], $tags);
}
$collection->connection = $mongodb_object->connection;
return variable_get('mongodb_debug', FALSE) ? new MongoDebugCollection($collection) : $collection;
}
class MongoDebugCollection {
public function __construct(MongoCollection $collection) {
$this->collection = $collection;
}
public function find($query = array(), $fields = array()) {
debug('find');
debug($query);
debug($fields);
return new MongoDebugCursor($this->collection
->find($query, $fields));
}
public function __call($name, $arguments) {
debug($name);
debug($arguments);
return call_user_func_array(array(
$this->collection,
$name,
), $arguments);
}
}
class MongoDebugCursor {
protected $cursor;
public function __construct(MongoCursor $cursor) {
$this->cursor = $cursor;
}
public function __call($name, $arguments) {
debug($name);
debug($arguments);
return call_user_func_array(array(
$this->cursor,
$name,
), $arguments);
}
}
class MongoDummy {
public $connection;
public function selectCollection() {
return new MongoDummy();
}
public function __call($name, array $arguments) {
}
}
function mongodb_collection_name($name) {
global $db_prefix;
static $simpletest_prefix;
if (!isset($simpletest_prefix)) {
if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\\d+);/", $_SERVER['HTTP_USER_AGENT'], $matches)) {
$simpletest_prefix = $matches[1];
}
else {
$simpletest_prefix = '';
}
}
if (!empty($GLOBALS['drupal_test_info']['test_run_id'])) {
$simpletest_prefix = $GLOBALS['drupal_test_info']['test_run_id'];
}
return $simpletest_prefix . $name;
}
function mongodb_test_group_finished() {
$aliases = variable_get('mongodb_connections', array());
$aliases['default'] = TRUE;
foreach (array_keys($aliases) as $alias) {
$db = mongodb($alias);
foreach ($db
->listCollections() as $collection) {
if (preg_match('/\\.simpletest\\d+/', $collection)) {
$db
->dropCollection($collection);
}
}
}
}
function mongodb_set_active_connection($alias, $connection_name = 'default') {
$alias_exists = isset($GLOBALS['conf']['mongodb_collections'][$alias]) && is_array($GLOBALS['conf']['mongodb_collections'][$alias]);
if ($alias_exists & !empty($GLOBALS['conf']['mongodb_collections'][$alias]['db_connection'])) {
$GLOBALS['conf']['mongodb_collections'][$alias]['db_connection'] = $connection_name;
}
else {
$GLOBALS['conf']['mongodb_collections'][$alias] = $connection_name;
}
}
function mongodb_next_id($name, $existing_id = 0) {
$mongo = mongodb();
$cmd = array(
'findandmodify' => mongodb_collection_name('sequence'),
'query' => array(
'_id' => $name,
),
'update' => array(
'$inc' => array(
'value' => 1,
),
),
'new' => TRUE,
);
try {
$sequence = $mongo
->command($cmd);
$value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0;
} catch (Exception $e) {
}
if (0 < $existing_id - $value + 1) {
$cmd = array(
'findandmodify' => mongodb_collection_name('sequence'),
'query' => array(
'_id' => $name,
),
'update' => array(
'$inc' => array(
'value' => $existing_id - $value + 1,
),
),
'upsert' => TRUE,
'new' => TRUE,
);
$sequence = $mongo
->command($cmd);
$value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0;
}
return $value;
}
function mongodb_default_write_options($safe = TRUE) {
if ($safe) {
if (version_compare(phpversion('mongo'), '1.5.0') == -1) {
return array(
'safe' => TRUE,
);
}
else {
return variable_get('mongodb_write_safe_options', array(
'w' => 1,
));
}
}
else {
if (version_compare(phpversion('mongo'), '1.3.0') == -1) {
return array();
}
else {
return variable_get('mongodb_write_nonsafe_options', array(
'w' => 0,
));
}
}
}