17
17
* @author Jonathan H. Wage <jonwage@gmail.com>
18
18
*/
19
19
20
+ header ('Pragma: no-cache ' );
21
+
20
22
$ server = 'mongodb://localhost:27017 ' ;
21
23
$ options = array (
22
24
'connect ' => true
@@ -71,12 +73,12 @@ function linkDocumentReferences($mongo, $document)
71
73
$ ref = findMongoDbDocument ($ value ['$id ' ], $ _REQUEST ['db ' ], $ value ['$ref ' ], true );
72
74
}
73
75
74
- $ document [$ key ]['$ref ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ _REQUEST [ ' db ' ].'&collection= ' .$ value ['$ref ' ].'"> ' .$ document [$ key ]['$ref ' ].'</a> ' ;
76
+ $ document [$ key ]['$ref ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ value [ ' $ db ' ].'&collection= ' .$ value ['$ref ' ].'"> ' .$ document [$ key ]['$ref ' ].'</a> ' ;
75
77
76
78
if ($ ref ['_id ' ] instanceof MongoId) {
77
- $ document [$ key ]['$id ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ _REQUEST [ ' db ' ].'&collection= ' .$ value ['$ref ' ].'&id= ' .$ value ['$id ' ].'"> ' .$ document [$ key ]['$id ' ].'</a> ' ;
79
+ $ document [$ key ]['$id ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ value [ ' $ db ' ].'&collection= ' .$ value ['$ref ' ].'&id= ' .$ value ['$id ' ].'"> ' .$ document [$ key ]['$id ' ].'</a> ' ;
78
80
} else {
79
- $ document [$ key ]['$id ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ _REQUEST [ ' db ' ].'&collection= ' .$ value ['$ref ' ].'&id= ' .$ value ['$id ' ].'&custom_id=1"> ' .$ document [$ key ]['$id ' ].'</a> ' ;
81
+ $ document [$ key ]['$id ' ] = '<a href=" ' .$ _SERVER ['PHP_SELF ' ].'?db= ' .$ value [ ' $ db ' ].'&collection= ' .$ value ['$ref ' ].'&id= ' .$ value ['$id ' ].'&custom_id=1"> ' .$ document [$ key ]['$id ' ].'</a> ' ;
80
82
}
81
83
} else {
82
84
$ document [$ key ] = linkDocumentReferences ($ mongo , $ value );
@@ -98,20 +100,26 @@ function prepareValueForMongoDB($value)
98
100
$ customId = isset ($ _REQUEST ['custom_id ' ]);
99
101
100
102
if (is_string ($ value )) {
103
+ $ value = preg_replace ('/ \'_id \' => \s*MongoId::__set_state\(array\(\s*\)\)/ ' , '\'_id \' => new MongoId(" ' . (isset ($ _REQUEST ['id ' ]) ? $ _REQUEST ['id ' ] : '' ) . '") ' , $ value );
104
+ $ value = preg_replace ('/MongoId::__set_state\(array\(\s*\)\)/ ' , 'new MongoId() ' , $ value );
105
+ $ value = preg_replace ('/MongoDate::__set_state\(array\(\s* \'sec \' => (\d+),\s* \'usec \' => \d+,\s*\)\)/m ' , 'new MongoDate($1) ' , $ value );
106
+ $ value = preg_replace ('/MongoBinData::__set_state\(array\(\s* \'bin \' => \'(.*?) \',\s* \'type \' => ([1,2,3,5,128]),\s*\)\)/m ' , 'new MongoBinData( \'$1 \', $2) ' , $ value );
107
+
101
108
eval ('$value = ' . $ value . '; ' );
109
+
102
110
if (!$ value ) {
103
- header ('location: ' . $ _SERVER ['HTTP_REFERER ' ]);
111
+ header ('location: ' . $ _SERVER ['HTTP_REFERER ' ] . ( $ customId ? ' &custom_id=1 ' : null ) );
104
112
exit ;
105
113
}
106
114
}
107
115
108
116
$ prepared = array ();
109
117
foreach ($ value as $ k => $ v ) {
110
- if ($ k === '_id ' && $ customId ) {
118
+ if ($ k === '_id ' && ! $ customId ) {
111
119
$ v = new MongoId ($ v );
112
- }
120
+ }
113
121
114
- if ($ k === '$id ' && $ customId ) {
122
+ if ($ k === '$id ' && ! $ customId ) {
115
123
$ v = new MongoId ($ v );
116
124
}
117
125
@@ -165,7 +173,7 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
165
173
166
174
$ collection = $ mongo ->selectDB ($ db )->selectCollection ($ collection );
167
175
168
- if (isset ($ _REQUEST ['custom_id ' ]) && ! $ forceCustomId ) {
176
+ if (isset ($ _REQUEST ['custom_id ' ]) || $ forceCustomId ) {
169
177
$ document =$ collection ->findOne (array ('_id ' => $ id ));
170
178
} else {
171
179
$ document = $ collection ->findOne (array ('_id ' => new MongoId ($ id )));
@@ -176,6 +184,27 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
176
184
177
185
// Actions
178
186
try {
187
+ // SEARCH
188
+ if (isset ($ _REQUEST ['search ' ])) {
189
+ $ customId = false ;
190
+ $ document = findMongoDbDocument ($ _REQUEST ['search ' ], $ _REQUEST ['db ' ], $ _REQUEST ['collection ' ]);
191
+
192
+ if (!$ document ) {
193
+ $ document = findMongoDbDocument ($ _REQUEST ['search ' ], $ _REQUEST ['db ' ], $ _REQUEST ['collection ' ], true );
194
+ $ customId = true ;
195
+ }
196
+
197
+ if (isset ($ document ['_id ' ])) {
198
+ $ url = $ _SERVER ['PHP_SELF ' ] . '?db= ' . $ _REQUEST ['db ' ] . '&collection= ' . $ _REQUEST ['collection ' ] . '&id= ' . (string ) $ document ['_id ' ];
199
+
200
+ if ($ customId ) {
201
+ header ('location: ' . $ url . '&custom_id=true ' );
202
+ } else {
203
+ header ('location: ' . $ url );
204
+ }
205
+ }
206
+ }
207
+
179
208
// DELETE DB
180
209
if (isset ($ _REQUEST ['delete_db ' ])) {
181
210
$ mongo
@@ -248,13 +277,14 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
248
277
249
278
// INSERT OR UPDATE A DB COLLECTION DOCUMENT
250
279
if (isset ($ _POST ['save ' ])) {
280
+ $ customId = isset ($ _REQUEST ['custom_id ' ]);
251
281
$ collection = $ mongo ->selectDB ($ _REQUEST ['db ' ])->selectCollection ($ _REQUEST ['collection ' ]);
252
282
253
283
$ document = prepareValueForMongoDB ($ _REQUEST ['value ' ]);
254
284
$ collection ->save ($ document );
255
285
256
286
$ url = $ _SERVER ['PHP_SELF ' ] . '?db= ' . $ _REQUEST ['db ' ] . '&collection= ' . $ _REQUEST ['collection ' ] . '&id= ' . (string ) $ document ['_id ' ];
257
- header ('location: ' . $ url );
287
+ header ('location: ' . $ url . ( $ customId ? ' &custom_id=1 ' : null ) );
258
288
exit ;
259
289
}
260
290
@@ -344,6 +374,18 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
344
374
padding: 8px;
345
375
margin-bottom: 15px;
346
376
width: 350px;
377
+ float: left;
378
+ }
379
+ #search {
380
+ -moz-border-radius: 10px;
381
+ -webkit-border-radius: 10px;
382
+ border-radius: 10px;
383
+ background: #f5f5f5;
384
+ border: 1px solid #ccc;
385
+ padding: 8px;
386
+ margin-bottom: 15px;
387
+ width: 350px;
388
+ float: right;
347
389
}
348
390
table {
349
391
background: #333;
@@ -507,6 +549,14 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
507
549
</div>
508
550
<?php endif ; ?>
509
551
552
+ <div id="search">
553
+ <form action="<?php echo $ _SERVER ['PHP_SELF ' ] ?> ?db=<?php echo $ _REQUEST ['db ' ] ?> &collection=<?php echo $ _REQUEST ['collection ' ] ?> " method="POST">
554
+ <label for="search_input">Search by ID</label>
555
+ <input type="text" id="search_input" name="search" size="20" />
556
+ <input type="submit" name="submit_search" value="Search" />
557
+ </form>
558
+ </div>
559
+
510
560
<table>
511
561
<thead>
512
562
<th colspan="3">ID</th>
@@ -583,4 +633,3 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
583
633
</div>
584
634
</body>
585
635
</html>
586
-
0 commit comments