You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
1.7 KiB

  1. <?php
  2. class MDb {
  3. /**
  4. * Execute a piece of javascript code
  5. *
  6. * @param MongoDB $db DB
  7. * @param string $code javascript code
  8. * @param array $params javascript function parameters
  9. * @return array
  10. */
  11. static function exec(MongoDB $db, $code, array $params = array()) {
  12. $query = $db->execute($code, $params);
  13. if (!$query["ok"]) {
  14. exit("Execute failed:<font color=\"red\">" . $query["errmsg"] . "</font><br/>\n<pre>" . $code . "</pre>");
  15. }
  16. return $query["retval"];
  17. }
  18. /**
  19. * List collections in a DB
  20. *
  21. * @param MongoDB $db DB
  22. * @return array<MongoCollection>
  23. */
  24. static function listCollections(MongoDB $db) {
  25. $server = MServer::currentServer();
  26. $names = array();
  27. $query = $db->execute("function (){ return db.getCollectionNames(); }", array());
  28. if ($query["ok"]) {
  29. $names= $query["retval"];
  30. }
  31. else{
  32. $colls = $db->listCollections(true);
  33. foreach($colls as $coll){
  34. $names[] = $coll->getName();
  35. }
  36. }
  37. $ret = array();
  38. foreach ($names as $name) {
  39. if ($server->shouldHideCollection($name)) {
  40. continue;
  41. }
  42. if (preg_match("/^system\\./", $name)) {
  43. continue;
  44. }
  45. $ret[] = $name;
  46. }
  47. sort($ret);
  48. //system collections
  49. if (!$server->uiHideSystemCollections()) {
  50. foreach ($names as $name) {
  51. if ($server->shouldHideCollection($name)) {
  52. continue;
  53. }
  54. if (preg_match("/^system\\./", $name)) {
  55. $ret[] = $name;
  56. }
  57. }
  58. }
  59. $collections = array();
  60. foreach ($ret as $v) {
  61. if ($v === "") {//older MongoDB version (maybe before 1.7) allow empty collection name
  62. continue;
  63. }
  64. $collections[] = $db->selectCollection($v);
  65. }
  66. return $collections;
  67. }
  68. }
  69. ?>