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.
 
 
 

434 rader
11 KiB

  1. <?php
  2. import("classes.BaseController");
  3. class FieldController extends BaseController {
  4. public $db;
  5. public $collection;
  6. /**
  7. * Enter description here...
  8. *
  9. * @var MongoDB
  10. */
  11. protected $_mongodb;
  12. function onBefore() {
  13. parent::onBefore();
  14. $this->db = xn("db");
  15. $this->collection = xn("collection");
  16. $this->_mongodb = $this->_mongo->selectDB($this->db);
  17. }
  18. /**
  19. * remove a field
  20. */
  21. function doRemove() {
  22. $db = $this->_mongo->selectDb($this->db);
  23. $field = xn("field");
  24. $id = xn("id");
  25. if ($id) {
  26. $db->execute('function (collection, id, field) {
  27. var col = db.getCollection(collection);
  28. var obj = {
  29. "$unset": {}
  30. };
  31. obj["$unset"][field] = 1;
  32. col.update({"_id":id}, obj, false, false);
  33. }', array( $this->collection, rock_real_id($id), $field ));
  34. }
  35. else {
  36. $db->execute('function (collection, field) {
  37. var col = db.getCollection(collection);
  38. var obj = {
  39. "$unset": {}
  40. };
  41. obj["$unset"][field] = 1;
  42. col.update({}, obj, false, true);
  43. }', array( $this->collection, $field ));
  44. }
  45. exit();
  46. }
  47. /**
  48. * set field value to NULL
  49. */
  50. function doClear() {
  51. $db = $this->_mongo->selectDb($this->db);
  52. $field = xn("field");
  53. $id = xn("id");
  54. if ($id) {
  55. $db->execute('function (collection, id, field) {
  56. var col = db.getCollection(collection);
  57. var obj = {
  58. "$set": {}
  59. };
  60. obj["$set"][field] = null;
  61. col.update({"_id":id}, obj, false, false);
  62. }', array( $this->collection, rock_real_id($id), $field ));
  63. }
  64. else {
  65. $db->execute('function (collection, field) {
  66. var col = db.getCollection(collection);
  67. var obj = {
  68. "$set": {}
  69. };
  70. obj["$set"][field] = null;
  71. col.update({}, obj, false, true);
  72. }', array( $this->collection, $field ));
  73. }
  74. exit();
  75. }
  76. /**
  77. * rename a field
  78. */
  79. function doRename() {
  80. $db = $this->_mongo->selectDB($this->db);
  81. $field = xn("field");
  82. $id = xn("id");
  83. $newname = trim(xn("newname"));
  84. $keep = xn("keep");
  85. if ($newname === "") {
  86. $this->_outputJson(array( "code" => 300, "message" => "New field name must not be empty"));
  87. }
  88. $ret = $db->execute('function (coll, field, newname, id, keep){
  89. var cursor;
  90. if (id) {
  91. cursor = db.getCollection(coll).find({_id:id});
  92. }
  93. else {
  94. cursor = db.getCollection(coll).find();
  95. }
  96. while(cursor.hasNext()) {
  97. var row = cursor.next();
  98. var newobj = { $rename: {} };
  99. if (typeof(row[newname]) == "undefined" || !keep) {
  100. newobj["$rename"][field] = newname;
  101. }
  102. if (typeof(row["_id"]) != "undefined") {
  103. db.getCollection(coll).update({ _id:row["_id"] }, newobj);
  104. }
  105. else {
  106. db.getCollection(coll).update(row, newobj);
  107. }
  108. }
  109. }', array($this->collection, $field, $newname, rock_real_id($id), $keep ? true:false));
  110. $this->_outputJson(array( "code" => 200 ));
  111. }
  112. /**
  113. * create new field
  114. */
  115. function doNew() {
  116. $db = $this->_mongo->selectDB($this->db);
  117. $id = xn("id");
  118. $newname = trim(xn("newname"));
  119. $keep = xn("keep");
  120. $dataType = xn("data_type");
  121. $value = xn("value");
  122. $boolValue = xn("bool_value");
  123. $integerValue = xn("integer_value");
  124. $longValue = xn("long_value");
  125. $doubleValue = xn("double_value");
  126. $mixedValue = xn("mixed_value");
  127. $format = x("format");
  128. $this->_rememberFormat($format);
  129. if ($newname === "") {
  130. $this->_outputJson(array( "code" => 300, "message" => "New field name must not be empty"));
  131. }
  132. $realValue = null;
  133. try {
  134. $realValue = $this->_convertValue($this->_mongodb, $dataType, $format, $value, $integerValue, $longValue, $doubleValue, $boolValue, $mixedValue);
  135. } catch (Exception $e) {
  136. $this->_outputJson(array( "code" => 400, "message" => $e->getMessage()));
  137. }
  138. $fieldType = "";
  139. if ($dataType == "integer") {
  140. $fieldType = "integer";
  141. } else if ($dataType == "long") {
  142. $fieldType = "long";
  143. }
  144. if (!$keep) {
  145. if ($id) {
  146. $db->selectCollection($this->collection)->update(array(
  147. "_id" => rock_real_id($id)
  148. ), array( '$set' => array( $newname => $realValue ) ));
  149. }
  150. else {
  151. $db->selectCollection($this->collection)->update(array(), array( '$set' => array( $newname => $realValue ) ), array( "multiple" => 1 ));
  152. }
  153. $this->_outputJson(array( "code" => 200 ));
  154. }
  155. $ret = $db->execute('function (coll, newname, fieldType, value, id, keep){
  156. if (typeof(value) != "object") {
  157. if (fieldType == "integer") {
  158. if (typeof(NumberInt) != "undefined") {
  159. value = NumberInt(value);
  160. }
  161. } else if (fieldType == "long") {
  162. value = NumberLong(value);
  163. }
  164. }
  165. var cursor;
  166. if (id) {
  167. cursor = db.getCollection(coll).find({_id:id});
  168. }
  169. else {
  170. cursor = db.getCollection(coll).find();
  171. }
  172. while(cursor.hasNext()) {
  173. var row = cursor.next();
  174. var newobj = { $set:{} };
  175. if (typeof(row[newname]) == "undefined" || !keep) {
  176. newobj["$set"][newname] = value;
  177. }
  178. if (typeof(row["_id"]) != "undefined") {
  179. db.getCollection(coll).update({ _id:row["_id"] }, newobj);
  180. }
  181. else {
  182. db.getCollection(coll).update(row, newobj);
  183. }
  184. }
  185. }', array($this->collection, $newname, $fieldType, $realValue, rock_real_id($id), $keep ? true:false));
  186. if ($ret["ok"]) {
  187. $this->_outputJson(array( "code" => 200 ));
  188. }
  189. else {
  190. $this->_outputJson(array( "code" => 500, "message" => $ret["errmsg"] ));
  191. }
  192. }
  193. /**
  194. * load field data
  195. *
  196. */
  197. function doLoad() {
  198. $collection = $this->_mongodb->selectCollection($this->collection);
  199. $id = xn("id");
  200. $field = xn("field");
  201. $type = "integer";
  202. $data = null;
  203. if ($id) {
  204. $one = $collection->findOne(array( "_id" => rock_real_id($id) ));//not select field, because there is a bug in list, such as "list.0"
  205. $data = rock_array_get($one, $field);
  206. switch (gettype($data)) {
  207. case "boolean":
  208. $type = "boolean";
  209. break;
  210. case "integer":
  211. $type = "integer";
  212. break;
  213. case "long":
  214. $type = "long";
  215. break;
  216. case "float":
  217. case "double":
  218. $type = "double";
  219. break;
  220. case "string":
  221. $type = "string";
  222. break;
  223. case "array":
  224. $type = "mixed";
  225. break;
  226. case "object":
  227. // int64 is returned as object (Kyryl Bilokurov <kyryl.bilokurov@gmail.com>)
  228. if (get_class($data) == "MongoInt64") {
  229. $type = "long";
  230. } else {
  231. $type = "mixed";
  232. }
  233. break;
  234. case "resource":
  235. $type = "mixed";
  236. break;
  237. case "NULL":
  238. $type = "null";
  239. break;
  240. }
  241. }
  242. $exporter = new VarExportor($this->_mongodb, $data);
  243. $format = rock_cookie("rock_format", "json");
  244. $represent = $exporter->export($format);
  245. if ($format == "json") {
  246. $represent = json_unicode_to_utf8($represent);
  247. }
  248. $this->_outputJson(array(
  249. "code" => 200,
  250. "type" => $type,
  251. // long requires special handling (Kyryl Bilokurov <kyryl.bilokurov@gmail.com>)
  252. "value" => ($type=="long") ? $data->__toString() : $data,
  253. "represent" => $represent,
  254. "format" => $format
  255. ));
  256. }
  257. /**
  258. * update value for a field
  259. */
  260. function doUpdate() {
  261. $db = $this->_mongo->selectDB($this->db);
  262. $id = xn("id");
  263. $newname = trim(xn("newname"));
  264. $dataType = xn("data_type");
  265. $value = xn("value");
  266. $boolValue = xn("bool_value");
  267. $integerValue = xn("integer_value");
  268. $longValue = xn("long_value");
  269. $doubleValue = xn("double_value");
  270. $mixedValue = xn("mixed_value");
  271. $format = xn("format");
  272. $this->_rememberFormat($format);
  273. if ($newname === "") {
  274. $this->_outputJson(array( "code" => 300, "message" => "New field name must not be empty"));
  275. }
  276. $realValue = null;
  277. try {
  278. $realValue = $this->_convertValue($this->_mongodb, $dataType, $format, $value, $integerValue, $longValue, $doubleValue, $boolValue, $mixedValue);
  279. } catch (Exception $e) {
  280. $this->_outputJson(array( "code" => 400, "message" => $e->getMessage()));
  281. }
  282. $fieldType = "";
  283. if ($dataType=="integer") {
  284. $fieldType = "integer";
  285. } else if ($dataType == "long") {
  286. $fieldType = "long";
  287. }
  288. $ret = array();
  289. if ($id) {
  290. $ret = $db->execute('function (collection, id, field, fieldType, value) {
  291. var col = db.getCollection(collection);
  292. var obj = {
  293. "$set": {}
  294. };
  295. if (typeof(value) != "object") {
  296. if (fieldType == "integer") {
  297. if (typeof(NumberInt) != "undefined") {
  298. value = NumberInt(value);
  299. }
  300. } else if (fieldType=="long") {
  301. value = NumberLong(value);
  302. }
  303. }
  304. obj["$set"][field] = value;
  305. col.update({ "_id": id }, obj, false, false);
  306. }', array($this->collection, rock_real_id($id), $newname, $fieldType, $realValue));
  307. }
  308. else {
  309. $ret = $db->execute('function (collection, field, fieldType, value) {
  310. var col = db.getCollection(collection);
  311. var obj = {
  312. "$set": {}
  313. };
  314. if (typeof(value) != "object") {
  315. if (fieldType=="integer") {
  316. if (typeof(NumberInt) != "undefined") {
  317. value = NumberInt(value);
  318. }
  319. } else if (fieldType=="long") {
  320. value = NumberLong(value);
  321. }
  322. }
  323. obj["$set"][field] = value;
  324. col.update({}, obj, false, true);
  325. }', array($this->collection, $newname, $fieldType, $realValue));
  326. }
  327. if ($ret["ok"]) {
  328. $this->_outputJson(array( "code" => 200 ));
  329. }
  330. else {
  331. $this->_outputJson(array( "code" => 500, "message" => $ret["errmsg"] ));
  332. }
  333. }
  334. function doIndexes() {
  335. $field = xn("field");
  336. $indexes = $this->_mongodb->selectCollection($this->collection)->getIndexInfo();
  337. $ret = array();
  338. foreach ($indexes as $index) {
  339. if (isset($index["key"][$field])) {
  340. $ret[] = array( "name" => $index["name"], "key" => $this->_highlight($index["key"], MONGO_EXPORT_JSON));
  341. }
  342. }
  343. $this->_outputJson(array( "code" => 200, "indexes" => $ret ));
  344. }
  345. function doCreateIndex() {
  346. $fields = xn("field");
  347. if (!is_array($fields)) {
  348. $this->_outputJson(array( "code" => 300, "message" => "Index contains one field at least."));
  349. }
  350. $orders = xn("order");
  351. $attrs = array();
  352. foreach ($fields as $index => $field) {
  353. $field = trim($field);
  354. if (!empty($field)) {
  355. $attrs[$field] = ($orders[$index] == "asc") ? 1 : -1;
  356. }
  357. }
  358. if (empty($attrs)) {
  359. $this->_outputJson(array( "code" => 300, "message" => "Index contains one field at least."));
  360. }
  361. //if is unique
  362. $options = array();
  363. if (x("is_unique")) {
  364. $options["unique"] = 1;
  365. if (x("drop_duplicate")) {
  366. $options["dropDups"] = 1;
  367. }
  368. }
  369. $options["background"] = 1;
  370. $options["safe"] = 1;
  371. //name
  372. $name = trim(xn("name"));
  373. if (!empty($name)) {
  374. $options["name"] = $name;
  375. }
  376. //check name
  377. $collection = $this->_mongodb->selectCollection($this->collection);
  378. $indexes = $collection->getIndexInfo();
  379. foreach ($indexes as $index) {
  380. if ($index["name"] == $name) {
  381. $this->_outputJson(array( "code" => 300, "message" => "The name \"{$name}\" is token by other index."));
  382. break;
  383. }
  384. if ($attrs === $index["key"]) {
  385. $this->_outputJson(array( "code" => 300, "message" => "The key on same fields already exists."));
  386. break;
  387. }
  388. }
  389. $ret = null;
  390. try {
  391. $ret = $collection->ensureIndex($attrs, $options);
  392. } catch (Exception $e) {
  393. $this->_outputJson(array( "code" => 300, "message" => $e->getMessage()));
  394. }
  395. if ($ret["ok"]) {
  396. $this->_outputJson(array( "code" => 200));
  397. }
  398. else {
  399. $this->_outputJson(array( "code" => 300, "message" => $ret["err"]));
  400. }
  401. }
  402. }
  403. ?>