public static function call(OkapiRequest $request) { $user_uuid = $request->get_parameter('user_uuid'); if (!$user_uuid) { throw new ParamMissing('user_uuid'); } $limit = $request->get_parameter('limit'); if (!$limit) { $limit = "20"; } if (!is_numeric($limit)) { throw new InvalidParam('limit', "'{$limit}'"); } $limit = intval($limit); if ($limit < 1 || $limit > 1000) { throw new InvalidParam('limit', "Has to be in range 1..1000."); } $offset = $request->get_parameter('offset'); if (!$offset) { $offset = "0"; } if (!is_numeric($offset)) { throw new InvalidParam('offset', "'{$offset}'"); } $offset = intval($offset); if ($offset < 0) { throw new InvalidParam('offset', "'{$offset}'"); } # Check if user exists and retrieve user's ID (this will throw # a proper exception on invalid UUID). $user = OkapiServiceRunner::call('services/users/user', new OkapiInternalRequest($request->consumer, null, array('user_uuid' => $user_uuid, 'fields' => 'internal_id'))); # User exists. Retrieving logs. $rs = Db::query("\n select cl.id, cl.uuid, cl.type, unix_timestamp(cl.date) as date, cl.text,\n c.wp_oc as cache_code\n from cache_logs cl, caches c\n where\n cl.user_id = '" . Db::escape_string($user['internal_id']) . "'\n and " . (Settings::get('OC_BRANCH') == 'oc.pl' ? "cl.deleted = 0" : "true") . "\n and c.status in (1,2,3)\n and cl.cache_id = c.cache_id\n order by cl.date desc\n limit {$offset}, {$limit}\n "); $results = array(); while ($row = Db::fetch_assoc($rs)) { $results[] = array('uuid' => $row['uuid'], 'date' => date('c', $row['date']), 'cache_code' => $row['cache_code'], 'type' => Okapi::logtypeid2name($row['type']), 'comment' => $row['text']); } return Okapi::formatted_response($request, $results); }
public static function call(OkapiRequest $request) { $log_uuids = $request->get_parameter('log_uuids'); if ($log_uuids === null) { throw new ParamMissing('log_uuids'); } if ($log_uuids === "") { $log_uuids = array(); } else { $log_uuids = explode("|", $log_uuids); } if (count($log_uuids) > 500 && !$request->skip_limits) { throw new InvalidParam('log_uuids', "Maximum allowed number of referenced " . "log entries is 500. You provided " . count($log_uuids) . " UUIDs."); } if (count($log_uuids) != count(array_unique($log_uuids))) { throw new InvalidParam('log_uuids', "Duplicate UUIDs detected (make sure each UUID is referenced only once)."); } $fields = $request->get_parameter('fields'); if (!$fields) { $fields = "date|user|type|comment"; } $fields = explode("|", $fields); foreach ($fields as $field) { if (!in_array($field, self::$valid_field_names)) { throw new InvalidParam('fields', "'{$field}' is not a valid field code."); } } if (Settings::get('OC_BRANCH') == 'oc.de') { $teamentry_field = 'cl.oc_team_comment'; $ratingdate_condition = 'and cr.rating_date=cl.date'; $needs_maintenance_SQL = 'cl.needs_maintenance'; $listing_is_outdated_SQL = 'cl.listing_outdated'; } else { $teamentry_field = '(cl.type=12)'; $ratingdate_condition = ''; $needs_maintenance_SQL = 'IF(cl.type=5, 2, IF(cl.type=6, 1, 0))'; $listing_is_outdated_SQL = '0'; } $rs = Db::query("\n select\n cl.id, c.wp_oc as cache_code, cl.uuid, cl.type,\n " . $teamentry_field . " as oc_team_entry,\n " . $needs_maintenance_SQL . " as needs_maintenance2,\n " . $listing_is_outdated_SQL . " as listing_is_outdated,\n unix_timestamp(cl.date) as date, cl.text,\n u.uuid as user_uuid, u.username, u.user_id,\n if(cr.user_id is null, 0, 1) as was_recommended\n from\n (cache_logs cl,\n user u,\n caches c)\n left join cache_rating cr\n on cr.user_id = u.user_id\n and cr.cache_id = c.cache_id\n " . $ratingdate_condition . "\n and cl.type in (\n " . Okapi::logtypename2id("Found it") . ",\n " . Okapi::logtypename2id("Attended") . "\n )\n where\n cl.uuid in ('" . implode("','", array_map('\\okapi\\Db::escape_string', $log_uuids)) . "')\n and " . (Settings::get('OC_BRANCH') == 'oc.pl' ? "cl.deleted = 0" : "true") . "\n and cl.user_id = u.user_id\n and c.cache_id = cl.cache_id\n and c.status in (1,2,3)\n "); $results = array(); $log_id2uuid = array(); /* Maps logs' internal_ids to uuids */ $flag_options = array('null', 'false', 'true'); while ($row = Db::fetch_assoc($rs)) { $results[$row['uuid']] = array('uuid' => $row['uuid'], 'cache_code' => $row['cache_code'], 'date' => date('c', $row['date']), 'user' => array('uuid' => $row['user_uuid'], 'username' => $row['username'], 'profile_url' => Settings::get('SITE_URL') . "viewprofile.php?userid=" . $row['user_id']), 'type' => Okapi::logtypeid2name($row['type']), 'was_recommended' => $row['was_recommended'] ? true : false, 'needs_maintenance2' => $flag_options[$row['needs_maintenance2']], 'listing_is_outdated' => $flag_options[$row['listing_is_outdated']], 'oc_team_entry' => $row['oc_team_entry'] ? true : false, 'comment' => Okapi::fix_oc_html($row['text'], Okapi::OBJECT_TYPE_CACHE_LOG), 'images' => array(), 'internal_id' => $row['id']); $log_id2uuid[$row['id']] = $row['uuid']; } Db::free_result($rs); # fetch images if (in_array('images', $fields)) { # For OCPL log entry images, pictures.seq currently is always = 1, # while OCDE uses it for ordering the images. $rs = Db::query("\n select object_id, uuid, url, title, spoiler\n from pictures\n where\n object_type = 1\n and object_id in ('" . implode("','", array_map('\\okapi\\Db::escape_string', array_keys($log_id2uuid))) . "')\n and display = 1 /* currently is always 1 for logpix */\n and unknown_format = 0\n order by seq, date_created\n "); if (Settings::get('OC_BRANCH') == 'oc.de') { $object_type_param = 'type=1&'; } else { $object_type_param = ''; } while ($row = Db::fetch_assoc($rs)) { $results[$log_id2uuid[$row['object_id']]]['images'][] = array('uuid' => $row['uuid'], 'url' => $row['url'], 'thumb_url' => Settings::get('SITE_URL') . 'thumbs.php?' . $object_type_param . 'uuid=' . $row['uuid'], 'caption' => $row['title'], 'is_spoiler' => $row['spoiler'] ? true : false); } Db::free_result($rs); } # Check which UUIDs were not found and mark them with null. foreach ($log_uuids as $log_uuid) { if (!isset($results[$log_uuid])) { $results[$log_uuid] = null; } } # Remove unwanted fields. foreach (self::$valid_field_names as $field) { if (!in_array($field, $fields)) { foreach ($results as &$result_ref) { unset($result_ref[$field]); } } } # Order the results in the same order as the input codes were given. $ordered_results = array(); foreach ($log_uuids as $log_uuid) { $ordered_results[$log_uuid] = $results[$log_uuid]; } return Okapi::formatted_response($request, $ordered_results); }