From: jerome Date: Wed, 25 Feb 2009 04:52:17 +0000 (+0000) Subject: web service MDL-12886 return value fix for REST server X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=7c9152bc7fe7f81a607aac224ae7620ffb7664b9;p=moodle.git web service MDL-12886 return value fix for REST server --- diff --git a/webservice/lib.php b/webservice/lib.php index 3e1153701a..dc213799c7 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -138,186 +138,179 @@ final class webservice_lib { foreach($reflection->getMethods() as $method){ - if ($method->getName()!="get_function_webservice_description" - && $method->getName()!="get_descriptions" ) { - $docBlock = $method->getDocComment(); + $docBlock = $method->getDocComment(); + //retrieve the return and add it into the description if not array|object + preg_match_all('/@return\s+(\w+)\s+((?:\$)?\w+)/', $docBlock, $returnmatches); - //retrieve the return and add it into the description if not array|object - preg_match_all('/@return\s+(\w+)\s+((?:\$)?\w+)/', $docBlock, $returnmatches); + //retrieve the subparam and subreturn + preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches); - //retrieve the subparam and subreturn - preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches); + for($i=0;$igetName()])) { + $description[$method->getName()] = array(); + } + + if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) { + $description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]); + } + + if ($matches[1][$i] == "subparam" || $matches[1][$i] == "subreturn") { - if (empty($description[$method->getName()])) { - $description[$method->getName()] = array(); + + ///algorythm parts + ///1. We compare the string to the description array + /// When we find a attribut that is not in the description, we retrieve all the rest of the string + ///2. We create the missing part of the description array, starting from the end of the rest of the string + ///3. We add the missing part to the description array + + ///Part 1. + + + ///construct the description array + if (strpos($matches[3][$i], "->")===false && strpos($matches[3][$i], ":")===false) { + //no separator + $otherparam = $matches[3][$i]; } + else if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) { + $separator = ":"; + $separatorsize=1; - if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) { - $description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]); + } else { + $separator = "->"; + $separatorsize=2; } - if ($matches[1][$i] == "subparam" || $matches[1][$i] == "subreturn") { + $param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1); + $otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize); + $parsingdesc = $description[$method->getName()]; - ///algorythm parts - ///1. We compare the string to the description array - /// When we find a attribut that is not in the description, we retrieve all the rest of the string - ///2. We create the missing part of the description array, starting from the end of the rest of the string - ///3. We add the missing part to the description array + if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){ + $parsingdesc = $parsingdesc[$descriptiontype]; + } + $descriptionpath=array(); - ///Part 1. + $creationfinished = false; + unset($type); + while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) { + if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) { + $type = $separator; - ///construct the description array - if (strpos($matches[3][$i], "->")===false && strpos($matches[3][$i], ":")===false) { - //no separator - $otherparam = $matches[3][$i]; - } - else if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) { $separator = ":"; $separatorsize=1; - } else { + $type = $separator; $separator = "->"; $separatorsize=2; } - $param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1); + $param = substr($otherparam,0,strpos($otherparam, $separator)); - $otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize); - $parsingdesc = $description[$method->getName()]; - - if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){ - $parsingdesc = $parsingdesc[$descriptiontype]; - } - $descriptionpath=array(); + $otherparam = substr($otherparam,strpos($otherparam, $separator)+$separatorsize); - $creationfinished = false; - unset($type); - while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) { - if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) { - $type = $separator; - - $separator = ":"; - $separatorsize=1; - } else { - $type = $separator; - $separator = "->"; - $separatorsize=2; - } - - $param = substr($otherparam,0,strpos($otherparam, $separator)); - - $otherparam = substr($otherparam,strpos($otherparam, $separator)+$separatorsize); + if ($type==":") { + if (!array_key_exists('multiple:'.$param, $parsingdesc)){ + $desctoadd = webservice_lib::create_end_of_description(":".$param.$separator.$otherparam, $matches[2][$i]); - if ($type==":") { - if (!array_key_exists('multiple:'.$param, $parsingdesc)){ - - $desctoadd = webservice_lib::create_end_of_description(":".$param.$separator.$otherparam, $matches[2][$i]); - - if(empty($descriptionpath) ) { - if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { - $desctoadd = array($descriptiontype => $desctoadd); - } - $paramtoadd = $descriptiontype; - } else { - $paramtoadd = 'multiple:'.$param; + if(empty($descriptionpath) ) { + if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { + $desctoadd = array($descriptiontype => $desctoadd); } - webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); - $creationfinished = true; + $paramtoadd = $descriptiontype; } else { - if(empty($descriptionpath)) { - $descriptionpath[] = $descriptiontype; - } - $descriptionpath[] = 'multiple:'.$param; - $parsingdesc = $parsingdesc['multiple:'.$param]; + $paramtoadd = 'multiple:'.$param; } + webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); + $creationfinished = true; } else { - if (!array_key_exists($param, $parsingdesc)){ - - $desctoadd = webservice_lib::create_end_of_description("->".$param.$separator.$otherparam, $matches[2][$i]); - - if(empty($descriptionpath)) { - - if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { - $desctoadd = array($descriptiontype => $desctoadd); - } - $paramtoadd = $descriptiontype; - - } else { - $paramtoadd = $param; - } - webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); - - $creationfinished = true; - } else { - if(empty($descriptionpath)) { - $descriptionpath[] = $descriptiontype; - } - $descriptionpath[] = $param; - $parsingdesc = $parsingdesc[$param]; + if(empty($descriptionpath)) { + $descriptionpath[] = $descriptiontype; } + $descriptionpath[] = 'multiple:'.$param; + $parsingdesc = $parsingdesc['multiple:'.$param]; } + } else { + if (!array_key_exists($param, $parsingdesc)){ - } - - if (!$creationfinished) { - - if (!empty($type) && $type==":") { - - $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]); + $desctoadd = webservice_lib::create_end_of_description("->".$param.$separator.$otherparam, $matches[2][$i]); if(empty($descriptionpath)) { + if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { $desctoadd = array($descriptiontype => $desctoadd); } $paramtoadd = $descriptiontype; + } else { - $paramtoadd = 'multiple:'.$param; + $paramtoadd = $param; } - webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); + $creationfinished = true; } else { - $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]); if(empty($descriptionpath)) { + $descriptionpath[] = $descriptiontype; + } + $descriptionpath[] = $param; + $parsingdesc = $parsingdesc[$param]; + } + } - if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { - $desctoadd = array($descriptiontype => $desctoadd); - } - $paramtoadd = $descriptiontype; + } - } else { - $paramtoadd = $param; + if (!$creationfinished) { + + if (!empty($type) && $type==":") { + $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]); + + if(empty($descriptionpath)) { + if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { + $desctoadd = array($descriptiontype => $desctoadd); } - webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); + $paramtoadd = $descriptiontype; + } else { + $paramtoadd = 'multiple:'.$param; + } + + webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); + + } else { + $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]); + if(empty($descriptionpath)) { + if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) { + $desctoadd = array($descriptiontype => $desctoadd); + } + $paramtoadd = $descriptiontype; + + } else { + $paramtoadd = $param; } + webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); } } } } } - return $description; } diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index 27125aa7f1..8c17ffeb24 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -93,7 +93,14 @@ function call_moodle_function ($rest_arguments) { } ///Transform result into xml in order to send the REST response - $return = mdl_conn_rest_object_to_xml ($res,key($description[$functionname]['return'])); + $key = key($description[$functionname]['return']); + + if (strpos($key,":")!==false) { + $key = substr($key, strpos($key,":")+1); + } else { + $key = 'return'; + } + $return = mdl_conn_rest_object_to_xml ($res,$key); return "$return"; } diff --git a/webservice/rest/testclient/getgroup.php b/webservice/rest/testclient/getgroup.php index 1578202c2f..3a3a9003ad 100644 --- a/webservice/rest/testclient/getgroup.php +++ b/webservice/rest/testclient/getgroup.php @@ -53,7 +53,7 @@ if ($groupid) { $res = basicxml_xml_to_object($out); - show_object($res->group); + show_object($res->return); show_xml ($out); } else { diff --git a/webservice/rest/testclient/getusers.php b/webservice/rest/testclient/getusers.php index cb4c35e4d5..4e62b9a6dc 100644 --- a/webservice/rest/testclient/getusers.php +++ b/webservice/rest/testclient/getusers.php @@ -32,9 +32,6 @@ start_interface("List of Users"); if ($search) { $data['search'] = $search; - var_dump($CFG->serverurl.'/user/tmp_get_users'); - - //we are asking for a token $connectiondata['username'] = 'wsuser'; $connectiondata['password'] = 'wspassword'; diff --git a/webservice/rest/testclient/php_curl_rest_client.php b/webservice/rest/testclient/index.php similarity index 93% rename from webservice/rest/testclient/php_curl_rest_client.php rename to webservice/rest/testclient/index.php index 29c024a536..be0dca44d2 100644 --- a/webservice/rest/testclient/php_curl_rest_client.php +++ b/webservice/rest/testclient/index.php @@ -21,7 +21,8 @@ $links = array( array('USERS'), array('updateuser.php','update_user()'), array('GROUPS'), array('creategroup.php','create_group()'), - array('addgroupmember.php', 'add_groupmember()')); + array('addgroupmember.php', 'add_groupmember()'), + array('getgroup.php', 'get_group()')); echo '