Merge "Convert article delete to use OOUI"
[lhc/web/wiklou.git] / includes / AjaxDispatcher.php
1 <?php
2 /**
3 * Handle ajax requests and send them to the proper handler.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Ajax
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * @defgroup Ajax Ajax
28 */
29
30 /**
31 * Object-Oriented Ajax functions.
32 * @ingroup Ajax
33 */
34 class AjaxDispatcher {
35 /**
36 * The way the request was made, either a 'get' or a 'post'
37 * @var string $mode
38 */
39 private $mode;
40
41 /**
42 * Name of the requested handler
43 * @var string $func_name
44 */
45 private $func_name;
46
47 /** Arguments passed
48 * @var array $args
49 */
50 private $args;
51
52 /**
53 * @var Config
54 */
55 private $config;
56
57 /**
58 * Load up our object with user supplied data
59 */
60 function __construct( Config $config ) {
61 $this->config = $config;
62
63 $this->mode = "";
64
65 if ( !empty( $_GET["rs"] ) ) {
66 $this->mode = "get";
67 }
68
69 if ( !empty( $_POST["rs"] ) ) {
70 $this->mode = "post";
71 }
72
73 switch ( $this->mode ) {
74 case 'get':
75 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
76 if ( !empty( $_GET["rsargs"] ) ) {
77 $this->args = $_GET["rsargs"];
78 } else {
79 $this->args = [];
80 }
81 break;
82 case 'post':
83 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
84 if ( !empty( $_POST["rsargs"] ) ) {
85 $this->args = $_POST["rsargs"];
86 } else {
87 $this->args = [];
88 }
89 break;
90 default:
91 return;
92 # Or we could throw an exception:
93 # throw new MWException( __METHOD__ . ' called without any data (mode empty).' );
94 }
95 }
96
97 /**
98 * Pass the request to our internal function.
99 * BEWARE! Data are passed as they have been supplied by the user,
100 * they should be carefully handled in the function processing the
101 * request.
102 *
103 * @param User $user
104 */
105 function performAction( User $user ) {
106 if ( empty( $this->mode ) ) {
107 return;
108 }
109
110 if ( !in_array( $this->func_name, $this->config->get( 'AjaxExportList' ) ) ) {
111 wfDebug( __METHOD__ . ' Bad Request for unknown function ' . $this->func_name . "\n" );
112 wfHttpError(
113 400,
114 'Bad Request',
115 "unknown function " . $this->func_name
116 );
117 } elseif ( !User::isEveryoneAllowed( 'read' ) && !$user->isAllowed( 'read' ) ) {
118 wfHttpError(
119 403,
120 'Forbidden',
121 'You are not allowed to view pages.' );
122 } else {
123 wfDebug( __METHOD__ . ' dispatching ' . $this->func_name . "\n" );
124 try {
125 $result = call_user_func_array( $this->func_name, $this->args );
126
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" );
131
132 wfHttpError( 500, 'Internal Error',
133 "{$this->func_name} returned no data" );
134 } else {
135 if ( is_string( $result ) ) {
136 $result = new AjaxResponse( $result );
137 }
138
139 // Make sure DB commit succeeds before sending a response
140 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
141 $lbFactory->commitMasterChanges( __METHOD__ );
142
143 $result->sendHeaders();
144 $result->printText();
145
146 wfDebug( __METHOD__ . ' dispatch complete for ' . $this->func_name . "\n" );
147 }
148 } catch ( Exception $e ) {
149 wfDebug( __METHOD__ . ' ERROR while dispatching ' .
150 $this->func_name . "(" . var_export( $this->args, true ) . "): " .
151 get_class( $e ) . ": " . $e->getMessage() . "\n" );
152
153 if ( !headers_sent() ) {
154 wfHttpError( 500, 'Internal Error',
155 $e->getMessage() );
156 } else {
157 print $e->getMessage();
158 }
159 }
160 }
161 }
162 }