|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Provides drush integration for MongoDB. |
|
6
|
|
|
*/ |
|
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Implements hook_drush_command(). |
|
9
|
|
|
*/ |
|
10
|
|
|
function mongodb_drush_command() { |
|
11
|
|
|
$items['mongodb-find'] = array( |
|
|
|
|
|
|
12
|
|
|
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE, |
|
13
|
|
|
'description' => 'Execute a query against a collection.', |
|
14
|
|
|
'examples' => array( |
|
15
|
|
|
'drush mongodb-find "{}" logger' => 'Get the logger/watchdog entries.', |
|
16
|
|
|
), |
|
17
|
|
|
'arguments' => array( |
|
18
|
|
|
'collection' => 'The collection name in the database', |
|
19
|
|
|
'alias' => 'The database alias', |
|
20
|
|
|
'selector' => 'A MongoDB find() selector in JSON format. Defaults to {}', |
|
21
|
|
|
), |
|
22
|
|
|
'aliases' => ['mdbf', 'mdbq'], |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
$items['mongodb-settings'] = array( |
|
26
|
|
|
'description' => 'Print MongoDB settings using print_r().', |
|
27
|
|
|
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, |
|
28
|
|
|
'aliases' => ['mdbs'], |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
// TODO : to be ported to D8. |
|
32
|
|
|
/* |
|
|
|
|
|
|
33
|
|
|
$items['mongodb-connect'] = array( |
|
34
|
|
|
'description' => 'A string for connecting to the mongodb.', |
|
|
|
|
|
|
35
|
|
|
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, |
|
|
|
|
|
|
36
|
|
|
// 'options' => $options, |
|
|
|
|
|
|
37
|
|
|
'arguments' => array( |
|
|
|
|
|
|
38
|
|
|
'alias' => 'The connection', |
|
|
|
|
|
|
39
|
|
|
), |
|
|
|
|
|
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$items['mongodb-cli'] = array( |
|
43
|
|
|
'description' => "Open a mongodb command-line interface using Drupal's credentials.", |
|
|
|
|
|
|
44
|
|
|
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION, |
|
|
|
|
|
|
45
|
|
|
// 'options' => $options, |
|
|
|
|
|
|
46
|
|
|
'examples' => array( |
|
|
|
|
|
|
47
|
|
|
'`drush mongodb-connect`' => 'Connect to the mongodb.', |
|
|
|
|
|
|
48
|
|
|
), |
|
|
|
|
|
|
49
|
|
|
'arguments' => array( |
|
|
|
|
|
|
50
|
|
|
'alias' => 'The connection', |
|
|
|
|
|
|
51
|
|
|
), |
|
|
|
|
|
|
52
|
|
|
'aliases' => array('mdbc'), |
|
|
|
|
|
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
*/ |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
return $items; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Implementation of hook_drush_help(). |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
function mongodb_drush_help($section) { |
|
|
|
|
|
|
64
|
|
|
switch ($section) { |
|
65
|
|
|
case 'drush:mongodb-settings': |
|
66
|
|
|
return dt('Show MongoDB settings.'); |
|
|
|
|
|
|
67
|
|
|
case 'drush:mongodb-find': |
|
68
|
|
|
return dt("Usage: drush [options] mongodb-find <query>...\n<query> is a single JSON selector."); |
|
|
|
|
|
|
69
|
|
|
// case 'drush:mongodb-connect': |
|
|
|
|
|
|
70
|
|
|
// return dt('A string which connects to the current database.'); |
|
|
|
|
|
|
71
|
|
|
// case 'drush:mongodb-cli': |
|
|
|
|
|
|
72
|
|
|
// return dt('Quickly enter the mongodb shell.'); |
|
|
|
|
|
|
73
|
|
|
// // TODO |
|
|
|
|
|
|
74
|
|
|
// case 'drush:mongodb-dump': |
|
|
|
|
|
|
75
|
|
|
// return dt('Prints the whole database to STDOUT or save to a file.'); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the basic shell command string. |
|
81
|
|
|
*/ |
|
82
|
|
|
function _drush_mongodb_connect($alias) { |
|
|
|
|
|
|
83
|
|
|
$connections = variable_get('mongodb_connections', array()); |
|
|
|
|
|
|
84
|
|
|
$connections += array('default' => array('host' => 'localhost', 'db' => 'drupal')); |
|
85
|
|
|
if (!isset($connections[$alias])) { |
|
86
|
|
|
$alias = 'default'; |
|
87
|
|
|
} |
|
88
|
|
|
$connection = $connections[$alias]; |
|
89
|
|
|
$host = $connection['host']; |
|
|
|
|
|
|
90
|
|
|
$db = $connection['db']; |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$query = $host; |
|
|
|
|
|
|
93
|
|
|
$query .= "/$db"; |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$command = "mongo ${query}"; |
|
96
|
|
|
return $command; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Drush callback; Start the mongodb shell. |
|
101
|
|
|
*/ |
|
102
|
|
|
function drush_mongodb_cli($alias = 'default') { |
|
103
|
|
|
$command = _drush_mongodb_connect($alias); |
|
104
|
|
|
drush_print(proc_close(proc_open(escapeshellcmd($command), array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes))); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Drush callback; Return the connect string. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $alias |
|
111
|
|
|
* The alias of a database to connect to. |
|
112
|
|
|
*/ |
|
113
|
|
|
function drush_mongodb_connect($alias = 'default') { |
|
114
|
|
|
$command = _drush_mongodb_connect($alias); |
|
115
|
|
|
drush_print($command); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Drush callback; Execute a find against a Mongodb database. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $collection |
|
|
|
|
|
|
122
|
|
|
* @param string $selector |
|
123
|
|
|
* JSON |
|
|
|
|
|
|
124
|
|
|
* @param string $alias |
|
|
|
|
|
|
125
|
|
|
*/ |
|
126
|
|
|
function drush_mongodb_find($collection, $selector = '{}', $alias = 'default') { |
|
127
|
|
|
/** @var \MongoDB\Database $db */ |
|
128
|
|
|
$db = \Drupal::service('mongodb.database_factory')->get($alias); |
|
|
|
|
|
|
129
|
|
|
$docs = $db->selectCollection($collection) |
|
130
|
|
|
->find(json_decode($selector), [ |
|
131
|
|
|
'typeMap' => [ |
|
132
|
|
|
'root' => 'array', |
|
133
|
|
|
'document' => 'array', |
|
134
|
|
|
'array' => 'array' |
|
135
|
|
|
]]) |
|
136
|
|
|
->toArray(); |
|
137
|
|
|
drush_print_r($docs); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Drush callback; Print the config of the mongodb. |
|
142
|
|
|
*/ |
|
143
|
|
|
function drush_mongodb_settings() { |
|
144
|
|
|
$settings = \Drupal::service('settings')->get('mongodb'); |
|
145
|
|
|
drush_print_r($settings); |
|
146
|
|
|
} |
|
|
|
|
|
|
147
|
|
|
|