web service MDL-12886

* Remove description array => all these information are now into the phpdoc. Remove all call/reference to moodleexternal.php
* Adapt our own REST server to these changes
* Remove Zend REST server as it's going to be deprecated in Zend Framework 1.8
* Remove our own SOAP server as we use the Zend SOAP server
This commit is contained in:
jerome 2009-02-24 05:11:04 +00:00
parent 60cd5b5a34
commit 40f024c9f3
17 changed files with 457 additions and 835 deletions

View file

@ -1,371 +0,0 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Generate the Moodle WSDL file
* This file is not finish but will give you a base to start
*/
require_once('../../config.php');
$token = optional_param('token',null,PARAM_ALPHANUM);
$wsdl_generator = new wsdl_generator();
$wsdl = $wsdl_generator->generate_wsdl($token);
echo $wsdl;
/**
* WORK IN PROGRESS
*/
class wsdl_generator {
private $exceptionlist;
//private $wsdl;
function __construct () {
// The exception list
// if ever there is some external.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/external.php'] = true;
$this->exceptionlist = array();
}
/**
* Generate the WSDL for Moodle API
* @global <type> $CFG
* @param <type> $token
* @return string wsdl xml
*/
public function generate_wsdl ($token = null) {
global $CFG;
if (empty($token)) {
return $this->generate_authentication_wsdl();
}
///initialize different wsdl part
$wsdlmessage = "";
$wsdlporttype = "";
$wsdlbinding = "";
$wsdlservice = "";
///retrieve al api.php file
$listfiles = array();
$this->setListApiFiles( $listfiles, $CFG->dirroot);
///WSDL header
$wsdl = <<<EOF
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='User'
targetNamespace='http://example.org/User'
xmlns:tns=' http://example.org/User '
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
EOF;
$wsdltypes = <<<EOF
<types>
<xsd:schema targetNamespace="http://example.org/User"
xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="object">
</xsd:complexType>
</xsd:schema>
</types>
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) - 13); //remove /external.php from the classpath
$classpath = str_replace('/','_',$classpath); //convert all / into _
$classname = $classpath."_external";
$api = new $classname();
$wsdlporttype .= <<<EOF
<portType name='{$classpath}PortType'>
EOF;
$wsdlbinding .= <<<EOF
<binding name='{$classpath}Binding' type='tns:{$classpath}PortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
EOF;
$wsdlservice .= <<<EOF
<service name='{$classpath}Service'>
<port name='{$classpath}Port' binding='{$classpath}Binding'>
<soap:address location='{$CFG->wwwroot}/webservice/soap/server.php?classpath={$classpath}&amp;token={$token}'/>
</port>
</service>
EOF;
foreach($api->get_descriptions() as $functionname => $description) {
$wsdlmessage .= <<<EOF
<message name="{$functionname}Request">
EOF;
/*
foreach ($description['params'] as $param => $paramtype) {
$wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype);
$wsdlmessage .= <<<EOF
<part name="{$param}" type="xsd:{$wsparamtype}"/>
EOF;
}
foreach ($description['optionalparams'] as $param => $paramtype) {
$wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype);
$wsdlmessage .= <<<EOF
<part name="{$param}" type="xsd:{$wsparamtype}"/>
EOF;
} * */
$wsdlmessage .= <<<EOF
<part name="params" type="xsd:object"/>
EOF;
$wsdlmessage .= <<<EOF
</message>
<message name="{$functionname}Response">
EOF;
foreach ($description['return'] as $param => $paramtype) {
$wsparamtype = $this->converterMoodleParamIntoWsParam($paramtype);
$wsdlmessage .= <<<EOF
<part name="{$param}" type="xsd:{$wsparamtype}"/>
EOF;
}
$wsdlmessage .= <<<EOF
</message>
EOF;
$wsdlporttype .= <<<EOF
<operation name='{$functionname}'>
<input message='tns:{$functionname}Request'/>
<output message='tns:{$functionname}Response'/>
</operation>
EOF;
$wsdlbinding .= <<<EOF
<operation name='{$functionname}'>
<soap:operation soapAction='urn:xmethods-delayed-quotes#{$functionname}'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
EOF;
}
$wsdlporttype .= <<<EOF
</portType>
EOF;
$wsdlbinding .= <<<EOF
</binding>
EOF;
}
///write WSDL
$wsdl .= $wsdltypes;
$wsdl .= $wsdlmessage;
$wsdl .= $wsdlporttype;
$wsdl .= $wsdlbinding;
$wsdl .= $wsdlservice;
///WSDL footer
$wsdl .= <<<EOF
</definitions>
EOF;
return $wsdl;
}
private function generate_authentication_wsdl() {
global $CFG;
$wsdl = <<<EOF
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='User'
targetNamespace='http://example.org/User'
xmlns:tns=' http://example.org/User '
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<types>
<xsd:schema targetNamespace="http://example.org/User"
xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="object">
</xsd:complexType>
</xsd:schema>
</types>
<message name="tmp_get_tokenRequest">
<part name="params" type="xsd:object"/>
</message>
<message name="tmp_get_tokenResponse">
<part name="user" type="xsd:object"/>
</message>
<portType name='userPortType'>
<operation name='tmp_get_token'>
<input message='tns:tmp_get_tokenRequest'/>
<output message='tns:tmp_get_tokenResponse'/>
</operation>
</portType>
<binding name='userBinding' type='tns:userPortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='tmp_get_token'>
<soap:operation soapAction='urn:xmethods-delayed-quotes#tmp_get_token'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<service name='userService'>
<port name='userPort' binding='userBinding'>
<soap:address location='{$CFG->wwwroot}/webservice/soap/server.php'/>
</port>
</service>
</definitions>
EOF;
return $wsdl;
}
/**
* Retrieve all api.php from Moodle (except the one of the exception list)
* @param <type> $
* @param <type> $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 == "external.php" && ! $this->inExceptionList($path)) {
$files[] = $path;
}
}
closedir($dh);
}
}
return $generatewsdl;
}
/**
* Hacky function
* We need to define if we remove all external.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_INT:
return "integer";
break;
case PARAM_BOOL:
return "boolean";
break;
case PARAM_ALPHANUM:
return "string";
break;
case PARAM_RAW:
return "string";
break;
default:
return "object";
break;
}
}
}
?>

View file

@ -36,54 +36,14 @@ final class soap_server extends webservice_server {
$this->set_protocolname("Soap");
}
/**
* Run SOAP server
* @global <type> $CFG
* @global <type> $USER
*/
public function run() {
$enable = $this->get_enable();
if (empty($enable)) {
die;
}
global $CFG;
// retrieve the token from the url
// if the token doesn't exist, set a class containing only get_token()
$token = optional_param('token',null,PARAM_ALPHANUM);
if (empty($token)) {
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php");
$server->setClass("ws_authentication");
$server->handle();
} else { // if token exist, do the authentication here
/// TODO: following function will need to be modified
$user = webservice_lib::mock_check_token($token);
if (empty($user)) {
throw new moodle_exception('wrongidentification');
} else {
/// TODO: probably change this
global $USER;
$USER = $user;
}
//retrieve the api name
$classpath = optional_param(classpath,null,PARAM_ALPHA);
require_once(dirname(__FILE__) . '/../../'.$classpath.'/external.php');
/// run the server
$server = new SoapServer($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token);
$server->setClass($classpath."_external"); //TODO: pass $user as parameter
$server->handle();
}
}
/**
* Run Zend SOAP server
* @global <type> $CFG
* @global <type> $USER
*/
public function zend_run() {
$enable = $this->get_enable();
public function run() {
$enable = $this->get_enable();
if (empty($enable)) {
die;
}
@ -114,7 +74,7 @@ final class soap_server extends webservice_server {
$autodiscover->handle();
} else {
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/zend_soap_server.php?wsdl"); // this current file here
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?wsdl"); // this current file here
$soap->setClass('ws_authentication');
$soap->handle();
}
@ -139,11 +99,11 @@ final class soap_server extends webservice_server {
$autodiscover = new Zend_Soap_AutoDiscover();
//this is a hack, because there is a bug in Zend framework (http://framework.zend.com/issues/browse/ZF-5736)
$autodiscover->setUri($CFG->wwwroot."/webservice/soap/zend_soap_server.php/".$token."/".$classpath);
$autodiscover->setUri($CFG->wwwroot."/webservice/soap/server.php/".$token."/".$classpath);
$autodiscover->setClass($classpath."_external");
$autodiscover->handle();
} else {
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/zend_soap_server.php?token=".$token."&classpath=".$classpath."&wsdl"); // this current file here
$soap = new Zend_Soap_Server($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=".$classpath."&wsdl"); // this current file here
$soap->setClass($classpath."_external");
$soap->handle();
}

View file

@ -1,71 +0,0 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* SOAP test client
*/
require_once(dirname(__FILE__) . '/../../../config.php');
//1. authentication
$client = new SoapClient($CFG->wwwroot."/webservice/soap/generatewsdl.php",array(
"trace" => 1,
"exceptions" => 0));
try {
$token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword"));
printLastRequestResponse($client);
} catch (SoapFault $exception) {
echo $exception;
}
//2. test functions
$client = new SoapClient($CFG->wwwroot."/webservice/soap/generatewsdl.php?token=".$token,array(
"trace" => 1,
"exceptions" => 0));
try {
var_dump($client->tmp_get_users(array('search' => "admin")));
printLastRequestResponse($client);
var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6")));
printLastRequestResponse($client);
var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b")));
printLastRequestResponse($client);
var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1)));
printLastRequestResponse($client);
} catch (SoapFault $exception) {
echo $exception;
}
function printLastRequestResponse($client) {
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
}
?>

View file

@ -34,7 +34,7 @@ Zend_Loader::registerAutoload();
//1. authentication
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/zend_soap_server.php?wsdl");
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?wsdl");
try {
$token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword"));
printLastRequestResponse($client);
@ -43,7 +43,7 @@ try {
}
//2. test functions
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/zend_soap_server.php?token=".$token."&classpath=user&wsdl");
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl");
var_dump($client->tmp_get_users(array('search' => "admin")));
printLastRequestResponse($client);
var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6")));

View file

@ -1,41 +0,0 @@
<?php
/**
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* LICENSE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @category Moodle
* @package webservice
* @copyright Copyright (c) 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL License
*/
/*
* Zend SOAP server
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once('lib.php');
if (empty($CFG->enablewebservices)) {
die;
}
$server = new soap_server();
$server->zend_run();
?>