Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / includes / AjaxDispatcher.php
blobbddbeb3497578c4c390cf5e94f1b1d98e5d1f265
1 <?php
2 /**
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
20 * @file
21 * @ingroup Ajax
24 /**
25 * @defgroup Ajax Ajax
28 /**
29 * Object-Oriented Ajax functions.
30 * @ingroup Ajax
32 class AjaxDispatcher {
33 /**
34 * The way the request was made, either a 'get' or a 'post'
35 * @var string $mode
37 private $mode;
39 /**
40 * Name of the requested handler
41 * @var string $func_name
43 private $func_name;
45 /** Arguments passed
46 * @var array $args
48 private $args;
50 /**
51 * Load up our object with user supplied data
53 function __construct() {
54 wfProfileIn( __METHOD__ );
56 $this->mode = "";
58 if ( ! empty( $_GET["rs"] ) ) {
59 $this->mode = "get";
62 if ( !empty( $_POST["rs"] ) ) {
63 $this->mode = "post";
66 switch ( $this->mode ) {
67 case 'get':
68 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
69 if ( ! empty( $_GET["rsargs"] ) ) {
70 $this->args = $_GET["rsargs"];
71 } else {
72 $this->args = array();
74 break;
75 case 'post':
76 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
77 if ( ! empty( $_POST["rsargs"] ) ) {
78 $this->args = $_POST["rsargs"];
79 } else {
80 $this->args = array();
82 break;
83 default:
84 wfProfileOut( __METHOD__ );
85 return;
86 # Or we could throw an exception:
87 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
90 wfProfileOut( __METHOD__ );
93 /**
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
97 * request.
99 function performAction() {
100 global $wgAjaxExportList, $wgUser;
102 if ( empty( $this->mode ) ) {
103 return;
106 wfProfileIn( __METHOD__ );
108 if ( ! in_array( $this->func_name, $wgAjaxExportList ) ) {
109 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
111 wfHttpError(
112 400,
113 'Bad Request',
114 "unknown function " . (string) $this->func_name
116 } elseif ( !User::isEveryoneAllowed( 'read' ) && !$wgUser->isAllowed( 'read' ) ) {
117 wfHttpError(
118 403,
119 'Forbidden',
120 'You are not allowed to view pages.' );
121 } else {
122 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
124 try {
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" );
134 } else {
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',
151 $e->getMessage() );
152 } else {
153 print $e->getMessage();
158 wfProfileOut( __METHOD__ );