@@ -23,13 +23,18 @@ public static function call(OkapiRequest $request)
23
23
{
24
24
$ user_uuid = $ request ->get_parameter ('user_uuid ' );
25
25
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
+
26
30
$ limit = $ request ->get_parameter ('limit ' );
27
31
if (!$ limit ) $ limit = "20 " ;
28
32
if (!is_numeric ($ limit ))
29
33
throw new InvalidParam ('limit ' , "' $ limit' " );
30
34
$ limit = intval ($ limit );
31
35
if (($ limit < 1 ) || ($ limit > 1000 ))
32
36
throw new InvalidParam ('limit ' , "Has to be in range 1..1000. " );
37
+
33
38
$ offset = $ request ->get_parameter ('offset ' );
34
39
if (!$ offset ) $ offset = "0 " ;
35
40
if (!is_numeric ($ offset ))
@@ -45,16 +50,35 @@ public static function call(OkapiRequest $request)
45
50
46
51
# User exists. Retrieving logs.
47
52
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
+
48
73
# See caches/geocaches/WebService.php for explanation.
49
74
if (Settings::get ('OC_BRANCH ' ) == 'oc.de ' ) {
50
75
$ logs_order_field_SQL = 'order_date ' ;
51
76
} else {
52
77
$ logs_order_field_SQL = 'date ' ;
53
78
}
54
79
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
58
82
from cache_logs cl, caches c
59
83
where
60
84
cl.user_id = ' " .Db::escape_string ($ user ['internal_id ' ])."'
@@ -63,17 +87,46 @@ public static function call(OkapiRequest $request)
63
87
and cl.cache_id = c.cache_id
64
88
order by cl. $ logs_order_field_SQL desc, cl.date_created desc, cl.id desc
65
89
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
69
117
{
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
+ )
76
126
);
127
+ $ logsRequest ->skip_limits = true ;
128
+ $ logsResponse = OkapiServiceRunner::call ("services/logs/entries " , $ logsRequest );
129
+ $ results = array_values ($ logsResponse );
77
130
}
78
131
79
132
return Okapi::formatted_response ($ request , $ results );
0 commit comments