-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiquery.php
233 lines (194 loc) · 6.66 KB
/
apiquery.php
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
<?php
# https://github.com/jakesankey/PHP-RestServer-Class/blob/master/RestServer.php
require_once('RestServer.php');
/**
* Class WFS_Query
*
* allows the caller in possession of a SECRET_ADMIN_KEY to perform
* queries on the users and venues database collections
*
*/
class WFS_Query
{
/**
* method to return a user document corresponding to a unique wfs username
*
* @param $username
* @param $secret
* @return array|bool
*/
public function user($secret, $username)
{
$users = null;
require_once('mongo_setup_users.php');
require_once('../../../wfs_secret.php');
if (strcmp(WFS_SECRET, $secret) != 0)
{
return array('response' => 'fail', 'reason' => 'invalid wfs secret');
}
$user_query = $users->findOne(array('username' => (string) $username),
array('_id' => 0));
if (!is_null($user_query))
{
return array('result' => 'ok', 'user' => $user_query);
}
return array('result' => 'fail', 'reason' => 'bad user');
}
/**
* method to return a venue document corresponding to a unique venue id (same as the 4s id)
*
* @param $id
* @param $secret
* @return array|bool
*/
public function venue($secret, $id)
{
$venues_db = null;
require_once('mongo_setup_venues.php');
require_once('../../../wfs_secret.php');
if (strcmp(WFS_SECRET, $secret) != 0)
{
return array('response' => 'fail', 'reason' => 'invalid wfs secret');
}
$venue_query = $venues_db->findOne(array('id' => (string) $id),
array('_id' => 0));
if (!is_null($venue_query))
{
return array('result' => 'ok', 'venue' => $venue_query);
}
return array('result' => 'fail', 'reason' => 'bad venue');
}
/**
* method to return a list venue documents corresponding to a query of the venue name
*
* @param $name
* @param $secret
* @return array
*
*
* send 'all' as name to get all venues
*
*/
public function venuename($secret, $name)
{
$venues_db = null;
require_once('mongo_setup_venues.php');
require_once('../../../wfs_secret.php');
if (strcmp(WFS_SECRET, $secret) != 0)
{
return array('response' => 'fail', 'reason' => 'invalid wfs secret');
}
# strip spaces
$name = strtolower(trim($name));
if (strlen($name) < 2)
{
return array('response' => 'fail', 'reason' => 'invalid name');
}
# check to see if caller is demanding all venues be returned
if (strcasecmp($name, 'all') == 0)
{
$venue_query = $venues_db->find(array(), array('_id' => 0));
}
else
{
# query will find case insensitive occurences of $name in all venue names
$name_regex = new MongoRegex("/$name/i");
$venue_query = $venues_db->find(array('name' => $name_regex),
array('_id' => 0));
}
if (!is_null($venue_query))
{
return array('result' => 'ok', 'venue' => iterator_to_array($venue_query));
}
return array('result' => 'fail', 'reason' => 'bad venue');
}
/**
* method that returns a list of all venues id's with username of the mayor
* also returns number of soldiers the mayor is defending the venue with and venue name
*
* @param $secret
* @return array
*/
public function mayors($secret)
{
$venues_db = null;
require_once('mongo_setup_venues.php');
require_once('../../../wfs_secret.php');
if (strcmp(WFS_SECRET, $secret) != 0)
{
return array('response' => 'fail', 'reason' => 'invalid wfs secret');
}
# db.venues.find({},{_id:0, mayor:1, id:1, name:1, defenders:1})
$mayors_query = $venues_db->find(array(), #empty array means find all
array('_id' => 0,
'mayor' => 1,
'id' => 1,
'defenders' => 1,
'name' => 1));
if (!is_null($mayors_query))
{
return array('result' => 'ok', 'mayors' => iterator_to_array($mayors_query));
}
return array('result' => 'fail', 'reason' => 'contact DB admin ASAP');
}
/**
* method that returns $how_many venues ranked by most weakly defended
*
* @param $secret
* @param $howmany -1 will return the strongest mayor. any other negative will
* return all the mayors sorted in descending order (strongest at the top)
*
* @return array
*
*/
public function weakest($secret, $howmany)
{
$venues_db = null;
require_once('mongo_setup_venues.php');
require_once('../../../wfs_secret.php');
if (strcmp(WFS_SECRET, $secret) != 0)
{
return array('response' => 'fail', 'reason' => 'invalid wfs secret');
}
$howmany = (int) $howmany;
# short circuit
if ($howmany == 0)
{
return array('response' => 'fail', 'reason' => 'howmany must be greater or less than zero');
}
# set sort order
if ($howmany > 0)
{
$sort_order = 1;
$order_by = 'weakest';
}
elseif ($howmany == -1)
{
$sort_order = -1;
$howmany = 1;
$order_by = 'strongest';
}
else
{
$sort_order = -1;
$howmany = $venues_db->count();
$order_by = 'strongest';
}
# perform this query in php
# db.venues.find({},{_id:0, name:1, id:1, defenders:1}).sort({'defenders' :1}).limit(1)
$query = $venues_db->find(array(), #empty array means find all
array('_id' => 0,
'id' => 1,
'defenders' => 1,
'name' => 1))->sort(array('defenders' => $sort_order))->limit($howmany);
if (!is_null($query))
{
return array('result' => 'ok', $order_by => iterator_to_array($query));
}
return array('result' => 'fail', 'reason' => 'contact DB admin ASAP');
}
}
###################### MAIN
$rest = new RestServer();
$rest->addServiceClass('WFS_Query');
$rest->handle();