7 * Handle ajax requests and send them to the proper handler.
10 if ( !( defined( 'MEDIAWIKI' ) && $wgUseAjax ) ) {
14 require_once( 'AjaxFunctions.php' );
17 * Object-Oriented Ajax functions.
20 class AjaxDispatcher
{
21 /** The way the request was made, either a 'get' or a 'post' */
24 /** Name of the requested handler */
27 /** Arguments passed */
30 /** Load up our object with user supplied data */
31 function __construct() {
32 wfProfileIn( __METHOD__
);
36 if ( ! empty( $_GET["rs"] ) ) {
40 if ( !empty( $_POST["rs"] ) ) {
44 switch( $this->mode
) {
47 $this->func_name
= isset( $_GET["rs"] ) ?
$_GET["rs"] : '';
48 if ( ! empty( $_GET["rsargs"] ) ) {
49 $this->args
= $_GET["rsargs"];
51 $this->args
= array();
56 $this->func_name
= isset( $_POST["rs"] ) ?
$_POST["rs"] : '';
57 if ( ! empty( $_POST["rsargs"] ) ) {
58 $this->args
= $_POST["rsargs"];
60 $this->args
= array();
65 wfProfileOut( __METHOD__
);
67 # Or we could throw an exception:
68 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
72 wfProfileOut( __METHOD__
);
75 /** Pass the request to our internal function.
76 * BEWARE! Data are passed as they have been supplied by the user,
77 * they should be carefully handled in the function processing the
80 function performAction() {
81 global $wgAjaxExportList, $wgOut;
83 if ( empty( $this->mode
) ) {
87 wfProfileIn( __METHOD__
);
89 if ( ! in_array( $this->func_name
, $wgAjaxExportList ) ) {
90 wfDebug( __METHOD__
. ' Bad Request for unknown function ' . $this->func_name
. "\n" );
92 wfHttpError( 400, 'Bad Request',
93 "unknown function " . (string) $this->func_name
);
95 wfDebug( __METHOD__
. ' dispatching ' . $this->func_name
. "\n" );
97 if ( strpos( $this->func_name
, '::' ) !== false ) {
98 $func = explode( '::', $this->func_name
, 2 );
100 $func = $this->func_name
;
103 $result = call_user_func_array( $func, $this->args
);
105 if ( $result === false ||
$result === null ) {
106 wfDebug( __METHOD__
. ' ERROR while dispatching '
107 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
108 . "no data returned\n" );
110 wfHttpError( 500, 'Internal Error',
111 "{$this->func_name} returned no data" );
114 if ( is_string( $result ) ) {
115 $result = new AjaxResponse( $result );
118 $result->sendHeaders();
119 $result->printText();
121 wfDebug( __METHOD__
. ' dispatch complete for ' . $this->func_name
. "\n" );
124 } catch ( Exception
$e ) {
125 wfDebug( __METHOD__
. ' ERROR while dispatching '
126 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
127 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
129 if ( !headers_sent() ) {
130 wfHttpError( 500, 'Internal Error',
133 print $e->getMessage();
138 wfProfileOut( __METHOD__
);