3 * Handle ajax requests and send them to the proper handler.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
29 * Object-Oriented Ajax functions.
32 class AjaxDispatcher
{
33 /** The way the request was made, either a 'get' or a 'post' */
36 /** Name of the requested handler */
39 /** Arguments passed */
42 /** Load up our object with user supplied data */
43 function __construct() {
44 wfProfileIn( __METHOD__
);
48 if ( ! empty( $_GET["rs"] ) ) {
52 if ( !empty( $_POST["rs"] ) ) {
56 switch( $this->mode
) {
58 $this->func_name
= isset( $_GET["rs"] ) ?
$_GET["rs"] : '';
59 if ( ! empty( $_GET["rsargs"] ) ) {
60 $this->args
= $_GET["rsargs"];
62 $this->args
= array();
66 $this->func_name
= isset( $_POST["rs"] ) ?
$_POST["rs"] : '';
67 if ( ! empty( $_POST["rsargs"] ) ) {
68 $this->args
= $_POST["rsargs"];
70 $this->args
= array();
74 wfProfileOut( __METHOD__
);
76 # Or we could throw an exception:
77 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
80 wfProfileOut( __METHOD__
);
83 /** Pass the request to our internal function.
84 * BEWARE! Data are passed as they have been supplied by the user,
85 * they should be carefully handled in the function processing the
88 function performAction() {
89 global $wgAjaxExportList, $wgOut, $wgUser;
91 if ( empty( $this->mode
) ) {
95 wfProfileIn( __METHOD__
);
97 if ( ! in_array( $this->func_name
, $wgAjaxExportList ) ) {
98 wfDebug( __METHOD__
. ' Bad Request for unknown function ' . $this->func_name
. "\n" );
103 "unknown function " . (string) $this->func_name
105 } elseif ( !in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true )
106 && !$wgUser->isAllowed( 'read' ) )
111 'You must log in to view pages.' );
113 wfDebug( __METHOD__
. ' dispatching ' . $this->func_name
. "\n" );
115 if ( strpos( $this->func_name
, '::' ) !== false ) {
116 $func = explode( '::', $this->func_name
, 2 );
118 $func = $this->func_name
;
122 $result = call_user_func_array( $func, $this->args
);
124 if ( $result === false ||
$result === null ) {
125 wfDebug( __METHOD__
. ' ERROR while dispatching '
126 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
127 . "no data returned\n" );
129 wfHttpError( 500, 'Internal Error',
130 "{$this->func_name} returned no data" );
132 if ( is_string( $result ) ) {
133 $result = new AjaxResponse( $result );
136 $result->sendHeaders();
137 $result->printText();
139 wfDebug( __METHOD__
. ' dispatch complete for ' . $this->func_name
. "\n" );
141 } catch ( Exception
$e ) {
142 wfDebug( __METHOD__
. ' ERROR while dispatching '
143 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
144 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
146 if ( !headers_sent() ) {
147 wfHttpError( 500, 'Internal Error',
150 print $e->getMessage();
156 wfProfileOut( __METHOD__
);