Skip to content

Commit 456d1b4

Browse files
committed
added "only user's logs" option to geocache retrieval methods
1 parent b8e236c commit 456d1b4

File tree

7 files changed

+63
-16
lines changed

7 files changed

+63
-16
lines changed

okapi/services/caches/formatters/gpx/WebService.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,19 @@ public static function create_gpx(OkapiRequest $request, $flags = null)
9595
$langpref = $request->get_parameter('langpref');
9696
if (!$langpref) $langpref = "en";
9797
$langprefs = explode("|", $langpref);
98-
foreach (array('ns_ground', 'ns_gsak', 'ns_ox', 'ns_oc', 'latest_logs', 'alt_wpts', 'mark_found') as $param)
98+
foreach (array('ns_ground', 'ns_gsak', 'ns_ox', 'ns_oc', 'alt_wpts', 'mark_found') as $param)
9999
{
100100
$val = $request->get_parameter($param);
101-
if (!$val) $val = "false";
102-
elseif (!in_array($val, array("true", "false")))
101+
if (!$val) $val = 'false';
102+
elseif (!in_array($val, array('true', 'false')))
103103
throw new InvalidParam($param);
104-
$vars[$param] = ($val == "true");
104+
$vars[$param] = ($val == 'true');
105105
}
106-
if ($vars['latest_logs'] && (!$vars['ns_ground']))
106+
107+
$vars['latest_logs'] = $request->get_parameter('latest_logs');
108+
if (!in_array($vars['latest_logs'], array('true', 'user', 'false')))
109+
throw new InvalidParam('latest_logs');
110+
if ($vars['latest_logs'] !== 'false' && (!$vars['ns_ground']))
107111
throw new BadRequest("In order for 'latest_logs' to work you have to also include 'ns_ground' extensions.");
108112

109113
$tmp = $request->get_parameter('my_notes');
@@ -220,7 +224,7 @@ public static function create_gpx(OkapiRequest $request, $flags = null)
220224
$fields .= "|recommendations|founds";
221225
if (count($vars['my_notes']) > 0)
222226
$fields .= "|my_notes";
223-
if ($vars['latest_logs'])
227+
if ($vars['latest_logs'] != 'false')
224228
$fields .= "|latest_logs";
225229
if ($vars['mark_found'])
226230
$fields .= "|is_found";
@@ -232,6 +236,7 @@ public static function create_gpx(OkapiRequest $request, $flags = null)
232236
'langpref' => $langpref,
233237
'fields' => $fields,
234238
'lpc' => $lpc,
239+
'user_logs_only' => ($vars['latest_logs'] == 'user' ? 'true' : 'false'),
235240
'user_uuid' => $user_uuid,
236241
'log_fields' => 'uuid|date|user|type|comment|oc_team_entry|internal_id|was_recommended'
237242
)

okapi/services/caches/formatters/gpx/docs.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,17 @@
181181
<p>Note: You need to use Level 3 Authentication in order to set it to anything else than "none".</p>
182182
</opt>
183183
<opt name='latest_logs' default='false'>
184-
<p>Boolean. If <b>true</b> then the response will include a couple of
185-
latest log entries for this cache (see also the <b>lpc</b> argument).</p>
184+
<p>This argument controls if log entries are included. There are three options:</p>
185+
<ul>
186+
<li><b>true</b> - include the latest log entries for each cache; the <b>lpc</b>
187+
argument controls the number of included entries,</li>
188+
<li><b>user</b> - with Level 3 Authentication, include (only) the latest
189+
log entries of your user for each cache. With Level 1 or 2 Authentication,
190+
include (only) the latest log entries of the user given in the
191+
<b>user_uuid</b> argument. The <b>lpc</b> argument controls the number of
192+
included entries,</li>
193+
<li><b>false</b> - do not include any log entries.</li>
194+
</ul>
186195
<p>You <b>must</b> set <b>ns_ground</b> argument to <b>true</b> if you want to use this.</p>
187196
</opt>
188197
<opt name='lpc' default='10'>

okapi/services/caches/formatters/gpx/gpxfile.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
<?php if ((in_array('gc:personal_note', $vars['my_notes'])) && ($c['my_notes'] != null)) { /* Does user want us to include personal notes? -> Issue 294 */ ?>
169169
<groundspeak:personal_note><?= Okapi::xmlescape($c['my_notes']) ?></groundspeak:personal_note>
170170
<?php } ?>
171-
<?php if ($vars['latest_logs']) { /* Does user want us to include latest log entries? */ ?>
171+
<?php if ($vars['latest_logs'] !== 'false') { /* Does user want us to include latest log entries? */ ?>
172172
<groundspeak:logs>
173173
<?php foreach ($c['latest_logs'] as $log) { ?>
174174
<groundspeak:log id="<?= $log['internal_id'] ?>">
@@ -221,7 +221,7 @@
221221
<?php if ($c['gc_code']) { ?>
222222
<oc:other_code><?= $c['gc_code'] ?></oc:other_code>
223223
<?php } ?>
224-
<?php if ($vars['latest_logs']) { /* Does user want us to include latest log entries? */ ?>
224+
<?php if ($vars['latest_logs'] !== 'false') { /* Does user want us to include latest log entries? */ ?>
225225
<oc:logs>
226226
<?php foreach ($c['latest_logs'] as $log) { ?>
227227
<oc:log id="<?= $log['internal_id'] ?>" uuid="<?= $log['uuid'] ?>">

okapi/services/caches/geocache/WebService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static function call(OkapiRequest $request)
3232
if (!$log_fields) $log_fields = "uuid|date|user|type|comment";
3333
$lpc = $request->get_parameter('lpc');
3434
if (!$lpc) $lpc = 10;
35+
$user_logs_only = $request->get_parameter('user_logs_only');
36+
if ($user_logs_only === null) $user_logs_only = 'false';
3537
$attribution_append = $request->get_parameter('attribution_append');
3638
if (!$attribution_append) $attribution_append = 'full';
3739
$params = array(
@@ -40,7 +42,8 @@ public static function call(OkapiRequest $request)
4042
'fields' => $fields,
4143
'attribution_append' => $attribution_append,
4244
'lpc' => $lpc,
43-
'log_fields' => $log_fields
45+
'log_fields' => $log_fields,
46+
'user_logs_only' => $user_logs_only,
4447
);
4548
$my_location = $request->get_parameter('my_location');
4649
if ($my_location)

okapi/services/caches/geocache/docs.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
Pipe-separated list of log fields to include in the <b>latest_logs</b> field.
6161
For valid field names, see <b>logs/entry</b> method.
6262
</opt>
63+
<opt name='user_logs_only' default='false'>
64+
<p>Boolean. If <b>true</b> with Level 3 Authentication, only logs
65+
of your user will be included in the <b>latest_logs</b> field. If
66+
<b>true</b> with Level 1 or 2 Authentication, then the parameter
67+
<b>user_uuid</b> must be supplied, and only logs of that user will be
68+
included in the <b>latest_logs</b> field. You must include the
69+
<b>latest_logs</b> field in <b>fields</b> in order to see the log
70+
entries.</p>
71+
</opt>
6372
<opt name='my_location'>
6473
<p>The reference point for cache distance and bearing calculation (typically - the user's location),
6574
in the "lat|lon" format. This parameter is required when accessing <b>distance</b> and/or <b>bearing</b>

okapi/services/caches/geocaches/WebService.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ public static function call(OkapiRequest $request)
9595
if (!in_array($attribution_append, array('none', 'static', 'full')))
9696
throw new InvalidParam('attribution_append');
9797

98-
$log_fields = $request->get_parameter('log_fields');
99-
if (!$log_fields) $log_fields = "uuid|date|user|type|comment"; // validation is done on call
100-
10198
$user_uuid = $request->get_parameter('user_uuid');
10299
if ($user_uuid != null)
103100
{
@@ -112,6 +109,9 @@ public static function call(OkapiRequest $request)
112109
else
113110
$user_id = null;
114111

112+
$log_fields = $request->get_parameter('log_fields');
113+
if (!$log_fields) $log_fields = "uuid|date|user|type|comment"; // validation is done on call
114+
115115
$lpc = $request->get_parameter('lpc');
116116
if ($lpc === null) $lpc = 10;
117117
if ($lpc == 'all')
@@ -125,6 +125,10 @@ public static function call(OkapiRequest $request)
125125
throw new InvalidParam('lpc', "Must be a positive value.");
126126
}
127127

128+
$user_logs_only = $request->get_parameter('user_logs_only');
129+
if (!in_array($user_logs_only, array('true', 'false')))
130+
throw new InvalidParam('user_logs_only', "Unknown option: '$user_logs_only'.");
131+
128132
if (in_array('distance', $fields) || in_array('bearing', $fields) || in_array('bearing2', $fields)
129133
|| in_array('bearing3', $fields))
130134
{
@@ -693,6 +697,19 @@ public static function call(OkapiRequest $request)
693697

694698
if (in_array('latest_logs', $fields))
695699
{
700+
if ($user_logs_only == 'true') {
701+
if ($user_id == null) {
702+
# This error can also be triggered via caches/formatters/gpx, which has
703+
# the "latest_logs=user" option instead of "only_user_logs=true".
704+
# Therefore we avoid to mention "only_user_logs" in the error message.
705+
706+
throw new BadRequest("Either 'user_uuid' parameter OR Level 3 Authentication is required to retrieve the user's logs.");
707+
}
708+
$add_where_sql = " and cache_logs.user_id = '".Db::escape_string($user_id)."'";
709+
} else {
710+
$add_where_sql = "";
711+
}
712+
696713
foreach ($results as &$result_ref)
697714
$result_ref['latest_logs'] = array();
698715

@@ -718,8 +735,9 @@ public static function call(OkapiRequest $request)
718735
from cache_logs
719736
where
720737
cache_id in ('".implode("','", array_map('\okapi\core\Db::escape_string', array_keys($cacheid2wptcode)))."')
721-
and ".((Settings::get('OC_BRANCH') == 'oc.pl') ? "deleted = 0" : "true")."
722-
order by cache_id, ".$logs_order_field_SQL." desc, date_created desc, id desc
738+
and ".((Settings::get('OC_BRANCH') == 'oc.pl') ? "deleted = 0" : "true").
739+
$add_where_sql."
740+
order by cache_id, ".$logs_order_field_SQL." desc, cache_logs.date_created desc, cache_logs.id desc
723741
");
724742

725743
$loguuids = array();

okapi/services/caches/geocaches/docs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<opt name='log_fields' default='uuid|date|user|type|comment'>
3636
Same as in the services/caches/geocache method.
3737
</opt>
38+
<opt name='user_logs_only' default='false'>
39+
Same as in the services/caches/geocache method.
40+
</opt>
3841
<opt name='my_location'>
3942
Same as in the services/caches/geocache method.
4043
</opt>

0 commit comments

Comments
 (0)