Skip to content

Commit 542352e

Browse files
committed
Merge branch 'userlogs-fields'
2 parents c72e793 + 46400a8 commit 542352e

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

okapi/services/logs/userlogs/WebService.php

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ public static function call(OkapiRequest $request)
2323
{
2424
$user_uuid = $request->get_parameter('user_uuid');
2525
if (!$user_uuid) throw new ParamMissing('user_uuid');
26+
27+
$fields = $request->get_parameter('fields');
28+
if (!$fields) $fields = "uuid|date|cache_code|type|comment"; // validation is done on call
29+
2630
$limit = $request->get_parameter('limit');
2731
if (!$limit) $limit = "20";
2832
if (!is_numeric($limit))
2933
throw new InvalidParam('limit', "'$limit'");
3034
$limit = intval($limit);
3135
if (($limit < 1) || ($limit > 1000))
3236
throw new InvalidParam('limit', "Has to be in range 1..1000.");
37+
3338
$offset = $request->get_parameter('offset');
3439
if (!$offset) $offset = "0";
3540
if (!is_numeric($offset))
@@ -45,16 +50,35 @@ public static function call(OkapiRequest $request)
4550

4651
# User exists. Retrieving logs.
4752

53+
# If the user only requests the default fields or other "basic fields" which
54+
# can easily be handled, we will directly serve the request. Otherwise we call
55+
# the more expensive logs/entries method.
56+
57+
$basic_fields = ['uuid', 'date', 'cache_code', 'type', 'comment', 'internal_id'];
58+
$only_basic_fields = true;
59+
60+
$fields_array = explode('|', $fields);
61+
foreach ($fields_array as $field) {
62+
if (!in_array($field, $basic_fields)) {
63+
$only_basic_fields = false;
64+
break;
65+
}
66+
}
67+
68+
if ($only_basic_fields)
69+
$add_fields_SQL = ", unix_timestamp(cl.date) as date, c.wp_oc as cache_code, cl.type, cl.text, cl.text_html, cl.id";
70+
else
71+
$add_fields_SQL = "";
72+
4873
# See caches/geocaches/WebService.php for explanation.
4974
if (Settings::get('OC_BRANCH') == 'oc.de') {
5075
$logs_order_field_SQL = 'order_date';
5176
} else {
5277
$logs_order_field_SQL = 'date';
5378
}
5479

55-
$rs = Db::query("
56-
select cl.id, cl.uuid, cl.type, unix_timestamp(cl.date) as date, cl.text,
57-
c.wp_oc as cache_code
80+
$query = "
81+
select cl.uuid $add_fields_SQL
5882
from cache_logs cl, caches c
5983
where
6084
cl.user_id = '".Db::escape_string($user['internal_id'])."'
@@ -63,17 +87,46 @@ public static function call(OkapiRequest $request)
6387
and cl.cache_id = c.cache_id
6488
order by cl.$logs_order_field_SQL desc, cl.date_created desc, cl.id desc
6589
limit $offset, $limit
66-
");
67-
$results = array();
68-
while ($row = Db::fetch_assoc($rs))
90+
";
91+
92+
if ($only_basic_fields)
93+
{
94+
$rs = Db::query($query);
95+
$results = [];
96+
while ($row = Db::fetch_assoc($rs))
97+
{
98+
$results[] = array(
99+
'uuid' => $row['uuid'],
100+
'date' => date('c', $row['date']),
101+
'cache_code' => $row['cache_code'],
102+
'type' => Okapi::logtypeid2name($row['type']),
103+
'comment' => Okapi::fix_oc_html($row['text'], $row['text_html']),
104+
'internal_id' => $row['id'],
105+
);
106+
}
107+
Db::free_result($rs);
108+
109+
# Remove unwanted fields.
110+
111+
foreach ($basic_fields as $field)
112+
if (!in_array($field, $fields_array))
113+
foreach ($results as &$result_ref)
114+
unset($result_ref[$field]);
115+
}
116+
else
69117
{
70-
$results[] = array(
71-
'uuid' => $row['uuid'],
72-
'date' => date('c', $row['date']),
73-
'cache_code' => $row['cache_code'],
74-
'type' => Okapi::logtypeid2name($row['type']),
75-
'comment' => $row['text']
118+
$log_uuids = Db::select_column($query);
119+
$logsRequest = new OkapiInternalRequest(
120+
$request->consumer,
121+
$request->token,
122+
array(
123+
'log_uuids' => implode('|', $log_uuids),
124+
'fields' => $fields
125+
)
76126
);
127+
$logsRequest->skip_limits = true;
128+
$logsResponse = OkapiServiceRunner::call("services/logs/entries", $logsRequest);
129+
$results = array_values($logsResponse);
77130
}
78131

79132
return Okapi::formatted_response($request, $results);

okapi/services/logs/userlogs/docs.xml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<req name='user_uuid'>
88
<p>ID of the user. (Use services/users/by_username to get it.)</p>
99
</req>
10+
<opt name='fields' default='uuid|date|cache_code|type|comment'>
11+
Pipe-separated list of log fields to include in the response. For valid
12+
field names, see the <a href='%OKAPI:methodargref:services/logs/entry%'>
13+
services/logs/entry</a> method.
14+
</opt>
1015
<opt name='limit' default='20'>
1116
<p>Integer N. If given, no more than N logs will be returned (the most recent ones).
1217
Maximum allowed value is 1000.</p>
@@ -19,27 +24,8 @@
1924
<common-format-params/>
2025
<returns>
2126
<p>A list of log entries, ordered by descending date. Each entry is a
22-
dictionary of the following format:</p>
23-
24-
<ul>
25-
<li><b>uuid</b> - ID of the log entry,</li>
26-
<li>
27-
<p><b>date</b> - date and time (ISO 8601) when the log entry was submitted.</p>
28-
<p>Please note that log entries often contain dates only (with the times
29-
truncated to midnight, as in the local timezone). In such cases, you may
30-
want to avoid displaying the time. You may assume that if the <b>date</b>
31-
value contains the "00:00:00" string, then it is date-only.</p>
32-
</li>
33-
<li><b>cache_code</b> - code of the geocache,</li>
34-
<li>
35-
<p><b>type</b> - string; log type. This could be <b>pretty much
36-
everything</b>, but there are some primary types (see logs/entry
37-
method for more info).</p>
38-
</li>
39-
<li>
40-
<b>comment</b> - <a href='%OKAPI:docurl:html%'>HTML string</a>, text entered
41-
with the log entry.
42-
</li>
43-
</ul>
27+
dictionary of the requested log fields. For a description of the field
28+
values, see the <a href='%OKAPI:methodargref:services/logs/entry%'>
29+
services/logs/entry</a> method.</p>
4430
</returns>
4531
</xml>

0 commit comments

Comments
 (0)