Doc tweaks:
[lhc/web/wiklou.git] / includes / AjaxDispatcher.php
1 <?php
2
3 if( !defined( 'MEDIAWIKI' ) ) {
4 die( 1 );
5 }
6
7 if ( ! $wgUseAjax ) {
8 die( 1 );
9 }
10
11 require_once( 'AjaxFunctions.php' );
12
13 /**
14 * Object-Oriented Ajax functions.
15 * @addtogroup Ajax
16 */
17 class AjaxDispatcher {
18 var $mode;
19 var $func_name;
20 var $args;
21
22 function __construct() {
23 wfProfileIn( __METHOD__ );
24
25 $this->mode = "";
26
27 if (! empty($_GET["rs"])) {
28 $this->mode = "get";
29 }
30
31 if (!empty($_POST["rs"])) {
32 $this->mode = "post";
33 }
34
35 if ($this->mode == "get") {
36 $this->func_name = isset( $_GET["rs"] ) ? $_GET["rs"] : '';
37 if (! empty($_GET["rsargs"])) {
38 $this->args = $_GET["rsargs"];
39 } else {
40 $this->args = array();
41 }
42 } else {
43 $this->func_name = isset( $_POST["rs"] ) ? $_POST["rs"] : '';
44 if (! empty($_POST["rsargs"])) {
45 $this->args = $_POST["rsargs"];
46 } else {
47 $this->args = array();
48 }
49 }
50 wfProfileOut( __METHOD__ );
51 }
52
53 function performAction() {
54 global $wgAjaxExportList, $wgOut;
55
56 if ( empty( $this->mode ) ) {
57 return;
58 }
59 wfProfileIn( __METHOD__ );
60
61 if (! in_array( $this->func_name, $wgAjaxExportList ) ) {
62 wfHttpError( 400, 'Bad Request',
63 "unknown function " . (string) $this->func_name );
64 } else {
65 try {
66 $result = call_user_func_array($this->func_name, $this->args);
67
68 if ( $result === false || $result === NULL ) {
69 wfHttpError( 500, 'Internal Error',
70 "{$this->func_name} returned no data" );
71 }
72 else {
73 if ( is_string( $result ) ) {
74 $result= new AjaxResponse( $result );
75 }
76
77 $result->sendHeaders();
78 $result->printText();
79 }
80
81 } catch (Exception $e) {
82 if (!headers_sent()) {
83 wfHttpError( 500, 'Internal Error',
84 $e->getMessage() );
85 } else {
86 print $e->getMessage();
87 }
88 }
89 }
90
91 wfProfileOut( __METHOD__ );
92 $wgOut = null;
93 }
94 }
95
96 ?>