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 ( !in_array( 'read', User
::getGroupPermissions( array( '*' ) ), true )
117 && !$wgUser->isAllowed( 'read' ) )
122 'You must log in to view pages.' );
124 wfDebug( __METHOD__
. ' dispatching ' . $this->func_name
. "\n" );
127 $result = call_user_func_array( $this->func_name
, $this->args
);
129 if ( $result === false ||
$result === null ) {
130 wfDebug( __METHOD__
. ' ERROR while dispatching '
131 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
132 . "no data returned\n" );
134 wfHttpError( 500, 'Internal Error',
135 "{$this->func_name} returned no data" );
137 if ( is_string( $result ) ) {
138 $result = new AjaxResponse( $result );
141 $result->sendHeaders();
142 $result->printText();
144 wfDebug( __METHOD__
. ' dispatch complete for ' . $this->func_name
. "\n" );
146 } catch ( Exception
$e ) {
147 wfDebug( __METHOD__
. ' ERROR while dispatching '
148 . $this->func_name
. "(" . var_export( $this->args
, true ) . "): "
149 . get_class( $e ) . ": " . $e->getMessage() . "\n" );
151 if ( !headers_sent() ) {
152 wfHttpError( 500, 'Internal Error',
155 print $e->getMessage();
160 wfProfileOut( __METHOD__
);