-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodb.module
More file actions
executable file
·377 lines (310 loc) · 8.78 KB
/
mongodb.module
File metadata and controls
executable file
·377 lines (310 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
// $Id$
/**
* @file
* Base API for MongoDB integration
*/
define('_MONGODB_LOG', 'mongodb');
define('_MONGODB_ENABLED', variable_get('mongodb_enabled', FALSE));
/**
* Implementation of hook_menu()
*/
function mongodb_menu() {
$items = array();
$items['admin/settings/mongodb'] = array(
'title' => 'MongoDB',
'page callback' => 'drupal_get_form',
'page arguments' => array('mongodb_settings_form'),
'access arguments' => array('administer configuration'),
'file' => 'mongodb.admin.inc',
);
return $items;
}
/**
* Returns MongoDB instance
*/
function mongodb_instance() {
static $mongo_db;
static $no_connection = FALSE;
if ($no_connection) {
return NULL;
}
if (isset($mongo_db)) {
return $mongo_db;
}
if (!class_exists('Mongo') || !_MONGODB_ENABLED) {
$no_connection = TRUE;
return NULL;
}
$conf = variable_get('mongo_url', '');
$url = parse_url($conf);
$replica = variable_get('mongo_replica', NULL);
if (empty($url['host'])) {
return NULL;
}
try {
$mongo_server = ($replica) ? new Mongo($url['host'], array("replicaSet" => $replica)) : new Mongo($url['host']);
$mongo_db = $mongo_server->selectDB(ltrim($url['path'], '/'));
if ($url['user']) {
if (!isset($url['pass'])) {
$url['pass'] = '';
}
$mongo_db->authenticate($url['user'], $url['pass']);
}
}
catch (Exception $ex) {
watchdog(_MONGODB_LOG, 'Exception @ex', array('@ex' => $ex->getMessage()));
$no_connection = TRUE;
return NULL;
}
return $mongo_db;
}
/**
* Returns MongoDB collection
*/
function mongodb_collection($collection_name) {
global $db_prefix;
static $connections, $simpletest_prefix, $collections;
if (!_MONGODB_ENABLED) {
return NULL;
}
// We call this function earlier than the database is initalized so we would
// read the parent collection without this.
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 = '';
}
}
// However, once $db_prefix is filled by the database, the simpletest_prefix
// is no longer needed.
if ($simpletest_prefix && $db_prefix && strpos($db_prefix, $simpletest_prefix) !== FALSE) {
$simpletest_prefix = '';
}
$collection_name = $db_prefix . $simpletest_prefix . $collection_name;
$mongo = mongodb_instance();
if (!$mongo) {
return NULL;
}
if (!isset($collections[$collection_name])) {
$collections[$collection_name] = $mongo->selectCollection($collection_name);
}
return $collections[$collection_name];
}
/**
* MongoDB query wrapper
*/
function mongodb_query($collection_name, $query = array(), $sort = array(), $limit = 0, $group = '') {
if (!_MONGODB_ENABLED) {
return array();
}
$result = array();
// Select collection
$collection = mongodb_collection($collection_name);
if (!$collection) {
return $result;
}
// Set query
$mongo_result = $collection->find($query);
// Set sorting criteria
$mongo_result->sort($sort);
// Set limit if defined
if ($limit > 0) {
$mongo_result->limit($limit);
}
while ($item = $mongo_result->getNext()) {
$result[] = $item;
}
// Empty memory
unset($collection, $mongo_result);
return $result;
}
/**
* MongoDB count query wrapper
*/
function mongodb_query_count($collection_name, $query = array()) {
if (!_MONGODB_ENABLED) {
return 0;
}
// Select collection
$collection = mongodb_collection($collection_name);
// Get count
$count = $collection->count($query);
// Empty memory
unset($collection);
return $count;
}
/**
* Implementation of hook_views_api
*
*/
function mongodb_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'mongodb'),
'template path' => 'mongodb.views.inc'
);
}
/**
* Implementation of hook_theme()
*/
function mongodb_theme() {
$theme = array();
// @TODO refactor this, row style should work without this definition
$theme['mongodb_views_view_row_node'] = array(
'arguments' => array(
'results' => NULL,
'options' => NULL,
'row' => NULL,
'alias' => NULL,
),
'path' => drupal_get_path('module', 'mongodb') . '/templates/plugins',
'template' => 'mongodb-views-view-row-node',
);
return $theme;
}
/**
* Implementation of hook_ctools_plugin_directory
*/
function mongodb_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
/**
* Pull node objects from mongo with fallback to MySQL
* @param array $nids
* @return array
*/
function mongodb_nodes_get_from_mongo($nids) {
$nodes = array();
if (!empty($nids) && is_array($nids)) {
$nodes_to_load = array();
if (mongodb_node_mongo_available()) {
$collection = mongodb_collection(_mongodb_node_collection_name());
if ($collection) {
foreach($nids as &$nid) {
$nid = (int) $nid;
}
$result = $collection->find(array('nid' => array('$in' => $nids)));
while ($record = $result->getNext()) {
$nodes[$record['nid']] = (object) $record;
}
$nodes_to_load = array_diff($nids, array_keys($nodes));
}
else {
watchdog(_MONGODB_LOG, 'There was an exception while fetch noderefs from MongoDB.');
//Load all nodes from MySQL
$nodes_to_load = $nids;
}
}
else {
$nodes_to_load = $nids;
}
//Pull all missed nodes from MySQL
foreach ($nodes_to_load as $node_to_load) {
$nodes[$node_to_load] = node_load($node_to_load);
}
}
$result = array();
foreach ($nids as $nid) {
$result[$nid] = $nodes[$nid];
}
return $result;
}
/**
* Template helper for theme_views_view_row_node
*/
function mongodb_preprocess_views_view_row_node_mongodb(&$vars) {
$options = $vars['options'];
$nid = $vars['row']->{$vars['field_alias']};
$vars['row']->nid = $vars['row']->{$vars['field_alias']};
if (!is_numeric($nid)) {
return;
}
// Make sure the variables are defined.
$vars['node'] = '';
$vars['comments'] = '';
// Look up node in cache.
if ($node = mongodb_node_static_storage_set($nid)) {
_mongodb_node_render($vars, $node, $options);
return;
}
// If not include variable mongodb.
if ($options['load_node'] == FALSE || !mongodb_node_mongo_available()) {
$node = node_load($nid);
mongodb_node_static_storage_set($node);
_mongodb_node_render($vars, drupal_clone($node), $options);
}
else {
$nids = array();
foreach ($vars['view']->result as &$value) {
// If node already cached, don't pull it.
$nid_to_check = $value->{$vars['field_alias']};
$value->nid = $value->{$vars['field_alias']};
if (!mongodb_node_static_storage_get($nid_to_check)) {
$nids[$nid_to_check] = (int) $nid_to_check;
}
}
$nodes = mongodb_nodes_get_from_mongo($nids);
mongodb_views_static_result($nodes);
// Put all to cache.
foreach ($nodes as $node) {
mongodb_node_static_storage_set($node);
}
if (is_object($nodes[$nid])) {
_mongodb_node_render($vars, drupal_clone($nodes[$nid]), $options);
}
}
}
/**
* Node view
*
* @param $vars
* @param object $node
*
* @see mongodb_preprocess_views_view_row_node_mongodb()
*/
function _mongodb_node_render(&$vars, $node, $options) {
$node->view = $vars['view'];
$node->build_mode = ($options['build_mode'] == 'teaser' || $options['build_mode'] == 'full') ? NODE_BUILD_NORMAL : $options['build_mode'];
$vars['origin_node'] = $node;
$vars['node'] = node_view($node, $options['build_mode'] == 'teaser', FALSE, $options['links']);
// $vars['node'] = mongodb_node_view($node, $options['build_mode'] == 'teaser', FALSE, $options['links']);
if (!empty($options['comments']) && function_exists('comment_render')) {
$vars['comments'] = comment_render($node);
}
}
/**
* Invoke hook_ensure_index().
*/
function mongodb_ensure_index() {
module_invoke_all('mongodb_ensure_index');
}
/**
* Fork of node_view().
*/
function mongodb_node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) {
$node = (object)$node;
$node = node_build_content($node, $teaser, $page);
if ($links) {
$node->links = module_invoke_all('link', 'node', $node, $teaser);
drupal_alter('link', $node->links, $node);
}
// Allow modules to modify the fully-built node.
node_invoke_nodeapi($node, 'alter', $teaser, $page);
return theme('node', $node, $teaser, $page);
}
function mongodb_views_static_result($nodes = NULL) {
static $current_nodes = array();
if ($nodes) {
foreach ($nodes as $nid => $node) {
$current_nodes[$nid] = drupal_clone($node);
}
}
elseif ($current_nodes) {
return $current_nodes;
}
}