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
{
34 * The way the request was made, either a 'get' or a 'post'
40 * Name of the requested handler
41 * @var string $func_name
51 * Load up our object with user supplied data
53 function __construct() {
54 wfProfileIn( __METHOD__
);
58 if ( ! empty( $_GET["rs"] ) ) {
62 if ( !empty( $_POST["rs"] ) ) {
66 switch ( $this->mode
) {
68 $this->func_name
= isset( $_GET["rs"] ) ?
$_GET["rs"] : '';
69 if ( ! empty( $_GET["rsargs"] ) ) {
70 $this->args
= $_GET["rsargs"];
72 $this->args
= array();
76 $this->func_name
= isset( $_POST["rs"] ) ?
$_POST["rs"] : '';
77 if ( ! empty( $_POST["rsargs"] ) ) {
78 $this->args
= $_POST["rsargs"];
80 $this->args
= array();
84 wfProfileOut( __METHOD__
);
86 # Or we could throw an exception:
87 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
90 wfProfileOut( __METHOD__
);
94 * Pass the request to our internal function.
95 * BEWARE! Data are passed as they have been supplied by the user,
96 * they should be carefully handled in the function processing the
99 function performAction() {
100 global $wgAjaxExportList, $wgUser;
102 if ( empty( $this->mode
) ) {
106 wfProfileIn( __METHOD__
);
108 if ( ! in_array( $this->func_name
, $wgAjaxExportList ) ) {
109 wfDebug( __METHOD__
. ' Bad Request for unknown function ' . $this->func_name
. "\n" );
114 "unknown function " . (string) $this->func_name
116 } elseif ( !User
::isEveryoneAllowed( 'read' ) && !$wgUser->isAllowed( 'read' ) ) {
120 'You are not allowed to view pages.' );
122 wfDebug( __METHOD__
. ' dispatching ' . $this->func_name
. "\n" );
125 $result = call_user_func_array( $this->func_name
, $this->args
);
127 if ( $result === false ||
$result === null ) {
128 wfDebug( __METHOD__
. ' ERROR while dispatching '
129 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
130 . "no data returned\n" );
132 wfHttpError( 500, 'Internal Error',
133 "{$this->func_name} returned no data" );
135 if ( is_string( $result ) ) {
136 $result = new AjaxResponse( $result );
139 $result->sendHeaders();
140 $result->printText();
142 wfDebug( __METHOD__
. ' dispatch complete for ' . $this->func_name
. "\n" );
144 } catch ( Exception
$e ) {
145 wfDebug( __METHOD__
. ' ERROR while dispatching '
146 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
147 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
149 if ( !headers_sent() ) {
150 wfHttpError( 500, 'Internal Error',
153 print $e->getMessage();
158 wfProfileOut( __METHOD__
);