This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathdb.php
More file actions
500 lines (438 loc) · 14.5 KB
/
db.php
File metadata and controls
500 lines (438 loc) · 14.5 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
import("classes.BaseController");
class DbController extends BaseController {
/** database **/
public function doIndex() {
$this->db = trim(xn("db"));
$dbs = $this->_server->listDbs();
$ret = array();
foreach ($dbs["databases"] as $db) {
if ($db["name"] == $this->db) {
$ret = $db;
}
}
//collections
$db = $this->_mongo->selectDB($this->db);
$collections = MDb::listCollections($db);
$ret = array_merge($ret, $db->command(array("dbstats" => 1)));
$ret["diskSize"] = "-";
if (isset($ret["sizeOnDisk"])) {
$ret["diskSize"] = $this->_formatBytes($ret["sizeOnDisk"]);
}
if(isset($ret["dataSize"])) {
$ret["dataSize"] = $this->_formatBytes($ret["dataSize"]);
}
if(isset($ret["storageSize"])) {
$ret["storageSize"] = $this->_formatBytes($ret["storageSize"]);
}
if(isset($ret["indexSize"])) {
$ret["indexSize"] = $this->_formatBytes($ret["indexSize"]);
}
$this->stats = array();
$this->stats["Size"] = $ret["diskSize"];
$this->stats["Is Empty?"] = $ret["empty"] ? "Yes" : "No";
if (empty($collections)) {
$this->stats["Collections"] = count($collections) . " collections:";
$this->stats["Collections"] .= "<br/>No collections yet";
}
else {
$key = "Collections<br/>[<a href=\"" . $this->path("db.dropDbCollections", array( "db" => $this->db )) . "\" onclick=\"return window.confirm('Are you sure to drop all collections in the db?')\"><u>Drop All</u></a>]<br/>[<a href=\"" . $this->path("clearDbCollections", array( "db" => $this->db )) . "\" onclick=\"return window.confirm('Are you sure to clear all records in all collections?')\"><u>Clear All</u></a>]";
$this->stats[$key] = count($collections) . " collections:";
foreach ($collections as $collection) {
$this->stats[$key] .= "<br/><a href=\""
. $this->path("collection.index", array( "db" => $this->db, "collection" => $collection->getName())) . "\">" . $collection->getName() . "</a>";
}
}
if(isset($ret["objects"])) {
$this->stats["Objects"] = $ret["objects"];
}
if(isset($ret["dataSize"])) {
$this->stats["Data Size"] = $ret["dataSize"];
}
if(isset($ret["storageSize"])) {
$this->stats["Storage Size"] = $ret["storageSize"];
}
if(isset($ret["numEvents"])) {
$this->stats["Extents"] = $ret["numExtents"];
}
if(isset($ret["indexes"])) {
$this->stats["Indexes"] = $ret["indexes"];
}
if(isset($ret["indexSize"])) {
$this->stats["Index Size"] = $ret["indexSize"];
}
$this->display();
}
/** transfer db collections from one server to another **/
public function doDbTransfer() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$this->collections = $db->listCollections();
$this->servers = $this->_admin->servers();
$this->selectedCollections = array();
if (!$this->isPost()) {
$this->selectedCollections[] = xn("collection");
x("copy_indexes", 1);
$this->target_host = "";
$this->target_port = 27017;
$this->target_auth = 0;
$this->target_username = "";
$this->target_password = "";
}
else {
$this->target_host = trim(xn("target_host"));
$this->target_port = xi("target_port");
$this->target_auth = xi("target_auth");
$this->target_username = trim(xn("target_username"));
$this->target_password = trim(xn("target_password"));
$checkeds = xn("checked");
if (is_array($checkeds)) {
$this->selectedCollections = array_keys($checkeds);
}
if (empty($checkeds)) {
$this->error = "Please select collections which you want to transfer.";
$this->display();
return;
}
if (empty($this->target_host)) {
$this->error = "Target host must not be empty.";
$this->display();
return;
}
$copyIndexes = xi("copy_indexes");
/**if ($target === "") {
$this->error = "Please enter a valid database name.";
$this->display();
return;
}**/
//start to transfer
$targetOptions = array();
if ($this->target_auth) {
$targetOptions["username"] = $this->target_username;
$targetOptions["password"] = $this->target_password;
}
$targetConnection = new RMongo("mongodb://" . $this->target_host . ":" . $this->target_port, $targetOptions);
$targetDb = $targetConnection->selectDB($this->db);
if ($this->target_auth) {
// "authenticate" can only be used between 1.0.1 - 1.2.11
if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
$targetDb->authenticate($this->target_username, $this->target_password);
}
}
$errors = array();
foreach ($this->selectedCollections as $collectionName) {
$ret = $targetDb->command(array(
"cloneCollection" => $this->db . "." . $collectionName,
"from" => $this->_server->uri(),
"copyIndexes" => (bool)$copyIndexes
));
if (!$ret["ok"]) {
$errors[] = MMongo::readException($ret);
break;
}
}
if (!empty($errors)) {
$this->error = implode("<br/>", $errors);
$this->display();
return;
}
$this->message = "All data were transfered to '{$this->target_host}' successfully.";
}
$this->display();
}
/** export db **/
public function doDbExport() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$this->collections = MDb::listCollections($db);
$this->selectedCollections = array();
if (!$this->isPost()) {
$this->selectedCollections[] = xn("collection");
}
else {
$checkeds = xn("checked");
$canDownload = xn("can_download");
if (is_array($checkeds)) {
$this->selectedCollections = array_keys($checkeds);
}
sort($this->selectedCollections);
import("classes.VarExportor");
$this->contents = "";
$this->countRows = 0;
//indexes
foreach ($this->selectedCollections as $collection) {
$collObj = $db->selectCollection($collection);
$infos = $collObj->getIndexInfo();
foreach ($infos as $info) {
$options = array();
if (isset($info["unique"])) {
$options["unique"] = $info["unique"];
}
$exportor = new VarExportor($db, $info["key"]);
$exportor2 = new VarExportor($db, $options);
$this->contents .= "\n/** {$collection} indexes **/\ndb.getCollection(\"" . addslashes($collection) . "\").ensureIndex(" . $exportor->export(MONGO_EXPORT_JSON) . "," . $exportor2->export(MONGO_EXPORT_JSON) . ");\n";
}
}
//data
foreach ($this->selectedCollections as $collection) {
$cursor = $db->selectCollection($collection)->find();
$this->contents .= "\n/** " . $collection . " records **/\n";
foreach ($cursor as $one) {
$this->countRows ++;
$exportor = new VarExportor($db, $one);
$this->contents .= "db.getCollection(\"" . addslashes($collection) . "\").insert(" . $exportor->export(MONGO_EXPORT_JSON) . ");\n";
unset($exportor);
}
unset($cursor);
}
if (x("can_download")) {
$prefix = "mongo-" . urlencode($this->db) . "-" . date("Ymd-His");
//gzip
if (x("gzip")) {
ob_end_clean();
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=\"{$prefix}.gz\"");
echo gzcompress($this->contents, 9);
exit();
}
else {
ob_end_clean();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$prefix}.js\"");
echo $this->contents;
exit();
}
}
}
$this->display();
}
/** import db **/
public function doDbImport() {
$this->db = xn("db");
if ($this->isPost()) {
$format = x("format");
if (!empty($_FILES["json"]["tmp_name"])) {
$tmp = $_FILES["json"]["tmp_name"];
//read file by it's format
$body = "";
if (preg_match("/\\.gz$/", $_FILES["json"]["name"])) {
$body = gzuncompress(file_get_contents($tmp));
}
else {
$body = file_get_contents($tmp);
}
//check format
$ret = array("ok" => 0);
if ($format == "js") {
$ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $body . ' }');
if (!$ret["ok"]) {
$this->error = $ret["errmsg"];
}
else {
$this->message = "All data import successfully.";
}
}
else {
$collection = trim(xn("collection"));
if ($collection === "") {
$this->error2 = "Please enter the collection name";
}
else {
$lines = explode("\n", $body);
foreach ($lines as $line) {
$line = trim($line);
if ($line) {
$ret = $this->_mongo->selectDB($this->db)->execute('function (c, o){ o=eval("(" + o + ")"); db.getCollection(c).insert(o); }', array( $collection, $line ));
}
}
if (!$ret["ok"]) {
$this->error2 = $ret["errmsg"];
}
else {
$this->message2 = "All data import successfully.";
}
}
}
}
else {
if ($format == "js") {
$this->error = "Either no file input or file is too large to upload.";
}
else {
$this->error2 = "Either no file input or file is too large to upload.";
}
}
}
$this->display();
}
/** db profiling **/
public function doProfile() {
$this->db = xn("db");
import("lib.mongo.RQuery");
import("lib.page.RPageStyle1");
$query = new RQuery($this->_mongo, $this->db, "system.profile");
$page = new RPageStyle1();
$page->setTotal($query->count());
$page->setSize(10);
$page->setAutoQuery();
$this->page = $page;
$this->rows = $query
->offset($page->offset())
->limit($page->size())
->desc("ts")
->findAll();
foreach ($this->rows as $index => $row) {
$this->rows[$index]["text"] = $this->_highlight($row, "json");
}
$this->display();
}
/** change db profiling level **/
public function doProfileLevel() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$query1 = $db->execute("function (){ return db.getProfilingLevel(); }");
$this->level = $query1["retval"];
if (x("go") == "save_level") {
$level = xi("level");
$slowms = xi("slowms");
$db->execute("function(level,slowms) { db.setProfilingLevel(level,slowms); }", array($level, $slowms));
$this->level = $level;
}
else {
x("slowms", 50);
}
$this->display();
}
/** clear profiling data **/
public function doClearProfile() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$query1 = $db->execute("function (){ return db.getProfilingLevel(); }");
$oldLevel = $query1["retval"];
$db->execute("function(level) { db.setProfilingLevel(level); }", array(0));
$ret = $db->selectCollection("system.profile")->drop();
$db->execute("function(level) { db.setProfilingLevel(level); }", array($oldLevel));
$this->redirect("db.profile", array(
"db" => $this->db
));
}
/** authentication **/
public function doAuth() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
//users
$collection = $db->selectCollection("system.users");
$cursor = $collection->find();
$this->users= array();
while($cursor->hasNext()) {
$this->users[] = $cursor->getNext();
}
$this->display();
}
/** delete user **/
public function doDeleteUser() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$db->execute("function (username){ db.removeUser(username); }", array(xn("user")));
$this->redirect("db.auth", array(
"db" => $this->db
));
}
/** add user **/
public function doAddUser() {
$this->db = xn("db");
if (!$this->isPost()) {
$this->display();
return;
}
$username = trim(xn("username"));
$password = trim(xn("password"));
$password2 = trim(xn("password2"));
if ($username == "") {
$this->error = "You must supply a username for user.";
$this->display();
return;
}
if ($password == "") {
$this->error = "You must supply a password for user.";
$this->display();
return;
}
if ($password != $password2) {
$this->error = "Passwords you typed twice is not same.";
$this->display();
return;
}
$db = $this->_mongo->selectDB($this->db);
$db->execute("function (username, pass, readonly){ db.addUser(username, pass, readonly); }", array(
$username,
$password,
x("readonly") ? true : false
));
$this->redirect("auth", array(
"db" => $this->db
));
}
/** create new collection **/
public function doNewCollection() {
$this->db = xn("db");
$this->name = trim(xn("name"));
$this->isCapped = xi("is_capped");
$this->size = xi("size");
$this->max = xi("max");
if ($this->isPost()) {
$db = $this->_mongo->selectDB($this->db);
$db->createCollection($this->name, $this->isCapped, $this->size, $this->max);
$this->message = "New collection is created.";
//add index
if (!$this->isCapped) {
$db->selectCollection($this->name)->ensureIndex(array( "_id" => 1 ));
}
}
$this->display();
}
/** drop all collections in a db **/
public function doDropDbCollections() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
foreach ($db->listCollections() as $collection) {
$collection->drop();
}
echo '<script language="javascript">
window.parent.frames["left"].location.reload();
</script>';
$this->redirect("db.index", array( "db" => $this->db ), true);
}
/** clear all records in all collections **/
public function doClearDbCollections() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
foreach ($db->listCollections() as $collection) {
$collection->remove();
}
echo '<script language="javascript">
window.parent.frames["left"].location.reload();
</script>';
$this->redirect("db.index", array( "db" => $this->db ), true);
}
/** repair dataase **/
public function doRepairDatabase() {
$this->db = xn("db");
$db = $this->_mongo->selectDB($this->db);
$ret = $db->command(array( "repairDatabase" => 1 ));
//$ret = $db->execute('function (){ return db.repairDatabase(); }'); //occure error in current version, we did not know why?
$this->ret = $this->_highlight($ret, "json");
$this->display();
}
/** drop database **/
public function doDropDatabase() {
$this->db = xn("db");
if (!x("confirm")) {
$this->display();
return;
}
$ret = $this->_mongo->dropDB($this->db);
$this->ret = $this->_highlight($ret, "json");
$this->display("dropDatabaseResult");
}
}