7 * Handle ajax requests and send them to the proper handler.
11 * Object-Oriented Ajax functions.
14 class AjaxDispatcher
{
15 /** The way the request was made, either a 'get' or a 'post' */
18 /** Name of the requested handler */
21 /** Arguments passed */
24 /** Load up our object with user supplied data */
25 function __construct() {
26 wfProfileIn( __METHOD__
);
30 if ( ! empty( $_GET["rs"] ) ) {
34 if ( !empty( $_POST["rs"] ) ) {
38 switch( $this->mode
) {
40 $this->func_name
= isset( $_GET["rs"] ) ?
$_GET["rs"] : '';
41 if ( ! empty( $_GET["rsargs"] ) ) {
42 $this->args
= $_GET["rsargs"];
44 $this->args
= array();
48 $this->func_name
= isset( $_POST["rs"] ) ?
$_POST["rs"] : '';
49 if ( ! empty( $_POST["rsargs"] ) ) {
50 $this->args
= $_POST["rsargs"];
52 $this->args
= array();
56 wfProfileOut( __METHOD__
);
58 # Or we could throw an exception:
59 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
62 wfProfileOut( __METHOD__
);
65 /** Pass the request to our internal function.
66 * BEWARE! Data are passed as they have been supplied by the user,
67 * they should be carefully handled in the function processing the
70 function performAction() {
71 global $wgAjaxExportList, $wgOut;
73 if ( empty( $this->mode
) ) {
77 wfProfileIn( __METHOD__
);
79 if ( ! in_array( $this->func_name
, $wgAjaxExportList ) ) {
80 wfDebug( __METHOD__
. ' Bad Request for unknown function ' . $this->func_name
. "\n" );
85 "unknown function " . (string) $this->func_name
88 wfDebug( __METHOD__
. ' dispatching ' . $this->func_name
. "\n" );
90 if ( strpos( $this->func_name
, '::' ) !== false ) {
91 $func = explode( '::', $this->func_name
, 2 );
93 $func = $this->func_name
;
97 $result = call_user_func_array( $func, $this->args
);
99 if ( $result === false ||
$result === null ) {
100 wfDebug( __METHOD__
. ' ERROR while dispatching '
101 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
102 . "no data returned\n" );
104 wfHttpError( 500, 'Internal Error',
105 "{$this->func_name} returned no data" );
107 if ( is_string( $result ) ) {
108 $result = new AjaxResponse( $result );
111 $result->sendHeaders();
112 $result->printText();
114 wfDebug( __METHOD__
. ' dispatch complete for ' . $this->func_name
. "\n" );
116 } catch ( Exception
$e ) {
117 wfDebug( __METHOD__
. ' ERROR while dispatching '
118 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
119 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
121 if ( !headers_sent() ) {
122 wfHttpError( 500, 'Internal Error',
125 print $e->getMessage();
131 wfProfileOut( __METHOD__
);