From: jerome Date: Fri, 16 Jan 2009 08:00:18 +0000 (+0000) Subject: web service MDL-12886 add SOAP wsdl generator X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=61cb09e5181d23fc6b678df27bb2855793feedbe;p=moodle.git web service MDL-12886 add SOAP wsdl generator --- diff --git a/user/wsapi.php b/user/wsapi.php index b8a547b805..f7b7c07398 100644 --- a/user/wsapi.php +++ b/user/wsapi.php @@ -33,24 +33,24 @@ final class user_ws_api extends moodle_ws_api { /// ///TODO: manage object->object parameter $this->descriptions['tmp_get_users'] = array( 'wsparams' => array('search'=> PARAM_ALPHANUM), - 'return' => array('user', array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, + 'return' => array('user'=> array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, 'firstname' => PARAM_RAW, 'lastname' => PARAM_RAW, 'email' => PARAM_RAW, 'emailstop' => PARAM_RAW, 'lang' => PARAM_RAW, 'theme' => PARAM_RAW, 'timezone' => PARAM_RAW, 'mailformat' => PARAM_RAW))); $this->descriptions['tmp_create_user'] = array( 'wsparams' => array('username'=> PARAM_RAW, 'firstname'=> PARAM_RAW, 'lastname'=> PARAM_RAW, 'email'=> PARAM_RAW, 'password'=> PARAM_RAW), - 'return' => array('userid', PARAM_RAW)); + 'return' => array('userid' => PARAM_RAW)); $this->descriptions['tmp_namedparams_get_users'] = array( 'wsparams' => array('search'=> PARAM_RAW), - 'return' => array('user', array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, + 'return' => array('user' => array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, 'firstname' => PARAM_RAW, 'lastname' => PARAM_RAW, 'email' => PARAM_RAW, 'emailstop' => PARAM_RAW, 'lang' => PARAM_RAW, 'theme' => PARAM_RAW, 'timezone' => PARAM_RAW, 'mailformat' => PARAM_RAW))); $this->descriptions['tmp_delete_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER), - 'return' => array('result', PARAM_BOOL)); + 'return' => array('result' => PARAM_BOOL)); $this->descriptions['tmp_update_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER, 'newusername' => PARAM_ALPHANUM, 'firstname' => PARAM_ALPHANUM), - 'return' => array('result', PARAM_BOOL)); + 'return' => array('result' => PARAM_BOOL)); } /** diff --git a/webservice/rest/lib.php b/webservice/rest/lib.php index e81035cfa2..661ffefe4f 100644 --- a/webservice/rest/lib.php +++ b/webservice/rest/lib.php @@ -41,7 +41,7 @@ function call_moodle_function ($rest_arguments) { $res = call_user_func_array ( $classname.'::'.$functionname, $params); ///Transform result into xml in order to send the REST response - $return = mdl_conn_rest_object_to_xml ($res,$description['return'][0]); + $return = mdl_conn_rest_object_to_xml ($res,key($description['return'])); return "$return"; } diff --git a/webservice/soap/generatewsdl.php b/webservice/soap/generatewsdl.php new file mode 100644 index 0000000000..2667c8f938 --- /dev/null +++ b/webservice/soap/generatewsdl.php @@ -0,0 +1,266 @@ +generate_wsdl(); +echo $wsdl; + +/** + * WORK IN PROGRESS - Generator not working yet + */ +class wsdl_generator { + + private $exceptionlist; + + //private $wsdl; + + function __construct () { + // The exception list + // if ever there is some wsapi.php file that are not a web service class, they need to be declared here + // example: $this->exceptionlist['/home/jerome/Projects/Moodle_HEAD/moodle/mod/scorm/wsapi.php'] = true; + $this->exceptionlist = array(); + } + + /** + * Generate the WSDL for Moodle API + * @global $CFG + * @return string wsdl xml + */ + public function generate_wsdl () { + global $CFG; + + ///initialize different wsdl part + $wsdlmessage = ""; + $wsdlporttype = ""; + $wsdlbinding = ""; + $wsdlservice = ""; + + ///retrieve al api.php file + $listfiles = array(); + $this->setListApiFiles( $listfiles, $CFG->dirroot); + + ///WSDL header + $wsdl = << + + +EOF; + + $wsdltypes = << + + + + + + + +EOF; + + foreach ($listfiles as $fileapipath) { + require_once($fileapipath); + + ///load the class + $classpath = substr($fileapipath,strlen($CFG->dirroot)+1); //remove the dir root + / from the file path + $classpath = substr($classpath,0,strlen($classpath) - 10); //remove /wsapi.php from the classpath + $classpath = str_replace('/','_',$classpath); //convert all / into _ + $classname = $classpath."_ws_api"; + $api = new $classname(); + + $wsdlporttype .= << +EOF; + $wsdlbinding .= << + + +EOF; + $wsdlservice .= << + + + + + +EOF; + foreach($api->get_descriptions() as $functionname => $description) { + + + $wsdlmessage .= << + +EOF; + foreach ($description['wsparams'] as $param => $paramtype) { + $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype); + $wsdlmessage .= << + +EOF; + } + $wsdlmessage .= << + + +EOF; + foreach ($description['return'] as $param => $paramtype) { + $wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype); + $wsdlmessage .= << + +EOF; + } + $wsdlmessage .= << + +EOF; + + $wsdlporttype .= << + + + + +EOF; + $wsdlbinding .= << + + + + + + + + + +EOF; + } + $wsdlporttype .= << + + +EOF; + $wsdlbinding .= << + + +EOF; + } + + ///write WSDL + $wsdl .= $wsdltypes; + $wsdl .= $wsdlmessage; + $wsdl .= $wsdlporttype; + $wsdl .= $wsdlbinding; + $wsdl .= $wsdlservice; + + ///WSDL footer + $wsdl .= << + +EOF; + + $this->wsdl = $wsdl; + return $wsdl; + //$this->writewsdl(); + } + + + /** + * Retrieve all api.php from Moodle (except the one of the exception list) + * @param $ + * @param $directorypath + * @return boolean true if n + */ + private function setListApiFiles( &$files, $directorypath ) + { + $generatewsdl = true; + if(is_dir($directorypath)){ //check that we are browsing a folder not a file + + if( $dh = opendir($directorypath)) + { + while( false !== ($file = readdir($dh))) + { + + if( $file == '.' || $file == '..') { // Skip '.' and '..' + continue; + } + $path = $directorypath . '/' . $file; + ///browse the subfolder + if( is_dir($path) ) { + $this->setListApiFiles($files, $path); + } + ///retrieve api.php file + else if ($file == "wsapi.php" && ! $this->inExceptionList($path)) { + $files[] = $path; + } + } + closedir($dh); + + } + } + + return $generatewsdl; + + } + + /** + * Hacky function + * We need to define if we remove all wsapi.php file from Moodle when they do not really + * are ws api file for Moodle ws API + * @param string $path + * @return boolean true if the path if in the exception list + */ + private function inExceptionList($path) { + return (!empty( $this->exceptionlist[$path])); + } + + /** + * Convert a Moodle type (PARAM_ALPHA, PARAM_NUMBER,...) as a SOAP type (string, interger,...) + * @param integer $moodleparam + * @return string SOAP type + */ + private function converterMoodleParamIntoWsParam($moodleparam) { + switch ($moodleparam) { + case PARAM_NUMBER: + return "integer"; + break; + case PARAM_ALPHANUM: + return "string"; + break; + case PARAM_RAW: + return "string"; + break; + default: + return "object"; + break; + } + } +/* + private function writewsdl() { + $fp = fopen('moodle.wsdl', 'w'); + fwrite($fp, $this->wsdl); + fclose($fp); + } +*/ + +} + +?>