Пример #1
0
 public static function call($methodname)
 {
     require_once $GLOBALS['rootpath'] . 'okapi/service_runner.php';
     if (!OkapiServiceRunner::exists($methodname)) {
         throw new BadRequest("Method '{$methodname}' does not exist. " . "See OKAPI docs at " . Settings::get('SITE_URL') . "okapi/");
     }
     $options = OkapiServiceRunner::options($methodname);
     $request = new OkapiHttpRequest($options);
     return OkapiServiceRunner::call($methodname, $request);
 }
Пример #2
0
 public static function call(OkapiRequest $request)
 {
     $methodname = $request->get_parameter('name');
     if (!$methodname) {
         throw new ParamMissing('name');
     }
     if (!preg_match("#^services/[0-9a-z_/]*\$#", $methodname)) {
         throw new InvalidParam('name');
     }
     if (!OkapiServiceRunner::exists($methodname)) {
         throw new InvalidParam('name', "Method does not exist: '{$methodname}'.");
     }
     $options = OkapiServiceRunner::options($methodname);
     if (!isset($options['min_auth_level'])) {
         throw new Exception("Method {$methodname} is missing a required 'min_auth_level' option!");
     }
     $docs = simplexml_load_string(OkapiServiceRunner::docs($methodname));
     $exploded = explode("/", $methodname);
     $result = array('name' => $methodname, 'short_name' => end($exploded), 'ref_url' => Settings::get('SITE_URL') . "okapi/{$methodname}.html", 'auth_options' => array('min_auth_level' => $options['min_auth_level'], 'oauth_consumer' => $options['min_auth_level'] >= 2, 'oauth_token' => $options['min_auth_level'] >= 3));
     if (!$docs->brief) {
         throw new Exception("Missing <brief> element in the {$methodname}.xml file.");
     }
     if ($docs->brief != self::get_inner_xml($docs->brief)) {
         throw new Exception("The <brief> element may not contain HTML markup ({$methodname}.xml).");
     }
     if (strlen($docs->brief) > 80) {
         throw new Exception("The <brief> description may not be longer than 80 characters ({$methodname}.xml).");
     }
     if (strpos($docs->brief, "\n") !== false) {
         throw new Exception("The <brief> element may not contain new-lines ({$methodname}.xml).");
     }
     if (substr(trim($docs->brief), -1) == '.') {
         throw new Exception("The <brief> element should not end with a dot ({$methodname}.xml).");
     }
     $result['brief_description'] = self::get_inner_xml($docs->brief);
     if ($docs->{'issue-id'}) {
         $result['issue_id'] = (string) $docs->{'issue-id'};
     } else {
         $result['issue_id'] = null;
     }
     if (!$docs->desc) {
         throw new Exception("Missing <desc> element in the {$methodname}.xml file.");
     }
     $result['description'] = self::get_inner_xml($docs->desc);
     $result['arguments'] = array();
     foreach ($docs->req as $arg) {
         $result['arguments'][] = self::arg_desc($arg);
     }
     foreach ($docs->opt as $arg) {
         $result['arguments'][] = self::arg_desc($arg);
     }
     foreach ($docs->{'import-params'} as $import_desc) {
         $attrs = $import_desc->attributes();
         $referenced_methodname = $attrs['method'];
         $referenced_method_info = OkapiServiceRunner::call('services/apiref/method', new OkapiInternalRequest(new OkapiInternalConsumer(), null, array('name' => $referenced_methodname)));
         $include_list = isset($attrs['params']) ? explode("|", $attrs['params']) : null;
         $exclude_list = isset($attrs['except']) ? explode("|", $attrs['except']) : array();
         foreach ($referenced_method_info['arguments'] as $arg) {
             if ($arg['class'] == 'common-formatting') {
                 continue;
             }
             if ($include_list === null && count($exclude_list) == 0) {
                 $arg['description'] = "<i>Inherited from <a href='" . $referenced_method_info['ref_url'] . "#arg_" . $arg['name'] . "'>" . $referenced_method_info['name'] . "</a> method.</i>";
             } elseif (($include_list === null || in_array($arg['name'], $include_list)) && !in_array($arg['name'], $exclude_list)) {
                 $arg['description'] = "<i>Same as in the <a href='" . $referenced_method_info['ref_url'] . "#arg_" . $arg['name'] . "'>" . $referenced_method_info['name'] . "</a> method.</i>";
             } else {
                 continue;
             }
             $arg['class'] = 'inherited';
             $result['arguments'][] = $arg;
         }
     }
     if ($docs->{'common-format-params'}) {
         $result['arguments'][] = array('name' => 'format', 'is_required' => false, 'is_deprecated' => false, 'class' => 'common-formatting', 'description' => "<i>Standard <a href='" . Settings::get('SITE_URL') . "okapi/introduction.html#common-formatting'>common formatting</a> argument.</i>");
         $result['arguments'][] = array('name' => 'callback', 'is_required' => false, 'is_deprecated' => false, 'class' => 'common-formatting', 'description' => "<i>Standard <a href='" . Settings::get('SITE_URL') . "okapi/introduction.html#common-formatting'>common formatting</a> argument.</i>");
     }
     foreach ($result['arguments'] as &$arg_ref) {
         if ($arg_ref['is_deprecated']) {
             $arg_ref['class'] .= " deprecated";
         }
     }
     if (!$docs->returns) {
         throw new Exception("Missing <returns> element in the {$methodname}.xml file. " . "If your method does not return anything, you should document in nonetheless.");
     }
     $result['returns'] = self::get_inner_xml($docs->returns);
     return Okapi::formatted_response($request, $result);
 }
 public static function call(OkapiRequest $request)
 {
     # Check search method
     $search_method = $request->get_parameter('search_method');
     if (!$search_method) {
         throw new ParamMissing('search_method');
     }
     if (strpos($search_method, "services/caches/search/") !== 0) {
         throw new InvalidParam('search_method', "Should begin with 'services/caches/search/'.");
     }
     if (!OkapiServiceRunner::exists($search_method)) {
         throw new InvalidParam('search_method', "Method does not exist: '{$search_method}'");
     }
     $search_params = $request->get_parameter('search_params');
     if (!$search_params) {
         throw new ParamMissing('search_params');
     }
     $search_params = json_decode($search_params, true);
     if (!is_array($search_params)) {
         throw new InvalidParam('search_params', "Should be a JSON-encoded dictionary");
     }
     # Check retrieval method
     $retr_method = $request->get_parameter('retr_method');
     if (!$retr_method) {
         throw new ParamMissing('retr_method');
     }
     if (!OkapiServiceRunner::exists($retr_method)) {
         throw new InvalidParam('retr_method', "Method does not exist: '{$retr_method}'");
     }
     $retr_params = $request->get_parameter('retr_params');
     if (!$retr_params) {
         throw new ParamMissing('retr_params');
     }
     $retr_params = json_decode($retr_params, true);
     if (!is_array($retr_params)) {
         throw new InvalidParam('retr_params', "Should be a JSON-encoded dictionary");
     }
     self::map_values_to_strings($search_params);
     self::map_values_to_strings($retr_params);
     # Wrapped?
     $wrap = $request->get_parameter('wrap');
     if ($wrap == null) {
         throw new ParamMissing('wrap');
     }
     if (!in_array($wrap, array('true', 'false'))) {
         throw new InvalidParam('wrap');
     }
     $wrap = $wrap == 'true';
     # Run search method
     try {
         $search_result = OkapiServiceRunner::call($search_method, new OkapiInternalRequest($request->consumer, $request->token, $search_params));
     } catch (BadRequest $e) {
         throw new InvalidParam('search_params', "Search method responded with the " . "following error message: " . $e->getMessage());
     }
     # Run retrieval method
     try {
         $retr_result = OkapiServiceRunner::call($retr_method, new OkapiInternalRequest($request->consumer, $request->token, array_merge($retr_params, array('cache_codes' => implode("|", $search_result['results'])))));
     } catch (BadRequest $e) {
         throw new InvalidParam('retr_params', "Retrieval method responded with the " . "following error message: " . $e->getMessage());
     }
     if ($wrap) {
         # $retr_result might be a PHP object, but also might be a binary response
         # (e.g. a GPX file).
         if ($retr_result instanceof OkapiHttpResponse) {
             $result = array('results' => $retr_result->get_body());
         } else {
             $result = array('results' => $retr_result);
         }
         foreach ($search_result as $key => &$value_ref) {
             if ($key != 'results') {
                 $result[$key] = $value_ref;
             }
         }
         return Okapi::formatted_response($request, $result);
     } else {
         if ($retr_result instanceof OkapiHttpResponse) {
             return $retr_result;
         } else {
             return Okapi::formatted_response($request, $retr_result);
         }
     }
 }