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, $wgUser;
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
87 } elseif ( !in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true )
88 && !$wgUser->isAllowed( 'read' ) )
93 'You must log in to view pages.' );
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
;
104 $result = call_user_func_array( $func, $this->args
);
106 if ( $result === false ||
$result === null ) {
107 wfDebug( __METHOD__
. ' ERROR while dispatching '
108 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
109 . "no data returned\n" );
111 wfHttpError( 500, 'Internal Error',
112 "{$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" );
123 } catch ( Exception
$e ) {
124 wfDebug( __METHOD__
. ' ERROR while dispatching '
125 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
126 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
128 if ( !headers_sent() ) {
129 wfHttpError( 500, 'Internal Error',
132 print $e->getMessage();
138 wfProfileOut( __METHOD__
);