|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Contains the main module connecting Drupal to MongoDB. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Implements hook_help(). |
|
12
|
|
|
*/ |
|
13
|
|
|
function mongodb_help($route_name, RouteMatchInterface $route_match) { |
|
|
|
|
|
|
14
|
|
|
switch ($route_name) { |
|
15
|
|
|
case 'help.page.mongodb': |
|
16
|
|
|
return '<p>' . t('The Drupal <a href=":project">MongoDB</a> project implements a generic interface to the <a href=":mongo">MongoDB</a> database server.', [ |
|
17
|
|
|
':project' => 'http://drupal.org/project/mongodb', |
|
18
|
|
|
':mongo' => 'http://www.mongodb.org/', |
|
19
|
|
|
]); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/* ==== Highly suspicious below this line =================================== */ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Implements hook_test_group_finished(). |
|
27
|
|
|
* |
|
28
|
|
|
* @throws \InvalidArgumentException |
|
29
|
|
|
* If the database cannot be selected. |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \MongoDB\Driver\Exception\ConnectionException |
|
32
|
|
|
* If the connection cannot be established. |
|
33
|
|
|
*/ |
|
34
|
|
|
function mongodb_test_group_finished() { |
|
35
|
|
|
$aliases = variable_get('mongodb_connections', array()); |
|
36
|
|
|
$aliases['default'] = TRUE; |
|
37
|
|
|
foreach (array_keys($aliases) as $alias) { |
|
38
|
|
|
$db = mongodb($alias); |
|
39
|
|
|
foreach ($db->listCollections() as $collection) { |
|
40
|
|
|
if (preg_match('/\.simpletest\d+/', $collection)) { |
|
41
|
|
|
$db->dropCollection($collection); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return the next id in a sequence. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* The name of the sequence. |
|
52
|
|
|
* @param int $existing_id |
|
53
|
|
|
* An existing id. |
|
54
|
|
|
* |
|
55
|
|
|
* @return int |
|
56
|
|
|
* The next id in the sequence. |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \MongoConnectionException |
|
59
|
|
|
* If the connection cannot be established. |
|
60
|
|
|
*/ |
|
61
|
|
|
function mongodb_next_id($name, $existing_id = 0) { |
|
62
|
|
|
// Atomically get the next id in the sequence. |
|
63
|
|
|
$mongo = mongodb(); |
|
64
|
|
|
$cmd = array( |
|
65
|
|
|
'findandmodify' => mongodb_collection_name('sequence'), |
|
66
|
|
|
'query' => array('_id' => $name), |
|
67
|
|
|
'update' => array('$inc' => array('value' => 1)), |
|
68
|
|
|
'new' => TRUE, |
|
69
|
|
|
); |
|
70
|
|
|
// It's very likely that this is not necessary as command returns an array |
|
71
|
|
|
// not an exception. The increment will, however, will fix the problem of |
|
72
|
|
|
// the sequence not existing. Still, better safe than sorry. |
|
73
|
|
|
try { |
|
74
|
|
|
$sequence = $mongo->command($cmd); |
|
75
|
|
|
$value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0; |
|
76
|
|
|
} |
|
77
|
|
|
catch (Exception $e) { |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
if (0 < $existing_id - $value + 1) { |
|
80
|
|
|
$cmd = array( |
|
81
|
|
|
'findandmodify' => mongodb_collection_name('sequence'), |
|
82
|
|
|
'query' => array('_id' => $name), |
|
83
|
|
|
'update' => array('$inc' => array('value' => $existing_id - $value + 1)), |
|
|
|
|
|
|
84
|
|
|
'upsert' => TRUE, |
|
85
|
|
|
'new' => TRUE, |
|
86
|
|
|
); |
|
87
|
|
|
$sequence = $mongo->command($cmd); |
|
88
|
|
|
$value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0; |
|
89
|
|
|
} |
|
90
|
|
|
return $value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns default options for MongoDB write operations. |
|
95
|
|
|
* |
|
96
|
|
|
* @param bool $safe |
|
97
|
|
|
* Set it to FALSE for "fire and forget" write operation. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
* Default options for Mongo write operations. |
|
101
|
|
|
*/ |
|
102
|
|
|
function mongodb_default_write_options($safe = TRUE) { |
|
103
|
|
|
if ($safe) { |
|
104
|
|
View Code Duplication |
if (version_compare(phpversion('mongo'), '1.5.0') == -1) { |
|
|
|
|
|
|
105
|
|
|
return array('safe' => TRUE); |
|
106
|
|
|
} |
|
107
|
|
|
else { |
|
108
|
|
|
return variable_get('mongodb_write_safe_options', array('w' => 1)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
112
|
|
|
if (version_compare(phpversion('mongo'), '1.3.0') == -1) { |
|
113
|
|
|
return array(); |
|
114
|
|
|
} |
|
115
|
|
|
else { |
|
116
|
|
|
return variable_get('mongodb_write_nonsafe_options', array('w' => 0)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
|
|
|
|
|
120
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.