Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

95 rader
2.4 KiB

  1. <?php
  2. class MCollection {
  3. public static function fields (MongoDB $db, $collection) {
  4. $one = $db->selectCollection($collection)->findOne();
  5. if (empty($one)) {
  6. return array();
  7. }
  8. $fields = array();
  9. self::_fieldsFromRow($fields, $one);
  10. return $fields;
  11. }
  12. private static function _fieldsFromRow(&$fields, $row, $prefix = null) {
  13. foreach ($row as $field => $value) {
  14. if (is_integer($field) || is_float($field)) {
  15. continue;
  16. }
  17. $namespace = (is_null($prefix)) ? $field : $prefix . "." . $field;
  18. $fields[] = $namespace;
  19. if (is_array($value)) {
  20. self::_fieldsFromRow($fields, $value, $namespace);
  21. }
  22. }
  23. }
  24. /**
  25. * If a row is GridFS row
  26. *
  27. * @param array $row record data
  28. * @return boolean
  29. */
  30. public static function isFile(array $row) {
  31. return isset($row["filename"]) && isset($row["chunkSize"]);
  32. }
  33. /**
  34. * get .chunks collection name from .files collection name
  35. *
  36. * @param string $filesCollection
  37. * @return string
  38. */
  39. public static function chunksCollection($filesCollection) {
  40. return preg_replace("/\\.files$/", ".chunks", $filesCollection);
  41. }
  42. /**
  43. * read collection information
  44. *
  45. * @param MongoDB $db database
  46. * @param string $collection collection name
  47. */
  48. public static function info(MongoDB $db, $collection) {
  49. $ret = $db->command(array( "collStats" => $collection ));
  50. if (!$ret["ok"]) {
  51. exit("There is something wrong:<font color=\"red\">{$ret['errmsg']}</font>, please refresh the page to try again.");
  52. }
  53. if (!isset($ret["retval"]["options"])) {
  54. $ret["retval"]["options"] = array();
  55. }
  56. $isCapped = 0;
  57. $size = 0;
  58. $max = 0;
  59. $options = $ret["retval"]["options"];
  60. if (isset($options["capped"])) {
  61. $isCapped = $options["capped"];
  62. }
  63. if (isset($options["size"])) {
  64. $size = $options["size"];
  65. }
  66. if (isset($options["max"])) {
  67. $max = $options["max"];
  68. }
  69. return array( "capped" => $isCapped, "size" => $size, "max" => $max );
  70. }
  71. /**
  72. * Create collection
  73. *
  74. * @param MongoDB $db MongoDB
  75. * @param string $name Collection name
  76. * @param array $options Options, capped, size, max
  77. */
  78. public static function createCollection(MongoDB $db, $name, array $options) {
  79. if (RMongo::compareVersion("1.4.0") >= 0) {
  80. $db->createCollection($name, $options);
  81. }
  82. else {
  83. $db->createCollection($name, isset($options["capped"]) ? $options["capped"] : false, isset($options["size"]) ? $options["size"] : 0, isset($options["max"]) ? $options["max"] : 0);
  84. }
  85. }
  86. }
  87. ?>