From 97666d062ddb817a7a0783480a4592c0ffb9fd62 Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Mon, 27 Mar 2006 18:53:15 +0000 Subject: [PATCH] OOP'ed Ajax functions, embedded in index.php for better compatibility with wikipedia.org setup --- includes/AjaxDispatcher.php | 83 ++++++++++++++++++++++++++++++++++++ includes/DefaultSettings.php | 5 +++ index.php | 12 ++++++ skins/common/ajax.js | 4 +- 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 includes/AjaxDispatcher.php diff --git a/includes/AjaxDispatcher.php b/includes/AjaxDispatcher.php new file mode 100644 index 0000000000..ae0d3e3470 --- /dev/null +++ b/includes/AjaxDispatcher.php @@ -0,0 +1,83 @@ +mode = ""; + + if (! empty($_GET["rs"])) { + $this->mode = "get"; + } + + if (!empty($_POST["rs"])) { + $this->mode = "post"; + } + + if ($this->mode == "get") { + $this->func_name = $_GET["rs"]; + if (! empty($_GET["rsargs"])) { + $this->args = $_GET["rsargs"]; + } else { + $this->args = array(); + } + } else { + $this->func_name = $_POST["rs"]; + if (! empty($_POST["rsargs"])) { + $this->args = $_POST["rsargs"]; + } else { + $this->args = array(); + } + } + wfProfileOut( 'AjaxDispatcher::AjaxDispatcher' ); + } + + function performAction() { + global $wgAjaxCachePolicy, $wgAjaxExportList; + if ( empty( $this->mode ) ) { + return; + } + wfProfileIn( 'AjaxDispatcher::performAction' ); + + if (! in_array( $this->func_name, $wgAjaxExportList ) ) { + echo "-:{$this->func_name} not callable"; + } else { + echo "+:"; + $result = call_user_func_array($this->func_name, $this->args); + header( 'Content-Type: text/html; charset=utf-8', true ); + $wgAjaxCachePolicy->writeHeader(); + echo $result; + } + wfProfileOut( 'AjaxDispatcher::performAction' ); + exit; + } +} + +?> diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7b0cd9522c..5748f7aa69 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1908,5 +1908,10 @@ $wgJobLogFile = false; */ $wgUseAjax = false; +/** + * List of Ajax-callable functions + */ +$wgAjaxExportList = array( 'wfSajaxSearch' ); + ?> diff --git a/index.php b/index.php index bc527799cb..45dbb07bad 100644 --- a/index.php +++ b/index.php @@ -90,6 +90,18 @@ OutputPage::setEncodings(); # Not really used yet $action = $wgRequest->getVal( 'action', 'view' ); $title = $wgRequest->getVal( 'title' ); +# +# Send Ajax requests to the Ajax dispatcher. +# +if ( $wgUseAjax && $action == 'ajax' ) { + require_once( 'ajax.php' ); + + $dispatcher = new AjaxDispatcher(); + $dispatcher->performAction(); + + exit; +} + $wgTitle = $mediaWiki->checkInitialQueries( $title,$action,$wgOut, $wgRequest, $wgContLang ); if ($wgTitle == NULL) { unset( $wgTitle ); diff --git a/skins/common/ajax.js b/skins/common/ajax.js index b1f3e0574b..9067654881 100644 --- a/skins/common/ajax.js +++ b/skins/common/ajax.js @@ -39,7 +39,7 @@ function sajax_do_call(func_name, args) { var i, x, n; var uri; var post_data; - uri = wgServer + "/" + wgScriptPath + "/ajax.php"; + uri = wgServer + "/" + wgScriptPath + "/index.php?action=ajax"; if (sajax_request_type == "GET") { if (uri.indexOf("?") == -1) uri = uri + "?rs=" + escape(func_name); @@ -47,7 +47,7 @@ function sajax_do_call(func_name, args) { uri = uri + "&rs=" + escape(func_name); for (i = 0; i < args.length-1; i++) uri = uri + "&rsargs[]=" + escape(args[i]); - uri = uri + "&rsrnd=" + new Date().getTime(); + //uri = uri + "&rsrnd=" + new Date().getTime(); post_data = null; } else { post_data = "rs=" + escape(func_name); -- 2.20.1