r13364 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r13363 | r13364 (on ViewVC) | r13365 >
Date:18:53, 27 March 2006
Author:jeluf
Status:new
Tags:
Comment:OOP'ed Ajax functions, embedded in index.php for better compatibility with wikipedia.org setup
Modified paths:

Diff [purge]

Index: trunk/phase3/skins/common/ajax.js
===================================================================
--- trunk/phase3/skins/common/ajax.js	(revision 13363)
+++ trunk/phase3/skins/common/ajax.js	(revision 13364)
@@ -39,7 +39,7 @@
 	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 @@
 			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);
Index: trunk/phase3/index.php
===================================================================
--- trunk/phase3/index.php	(revision 13363)
+++ trunk/phase3/index.php	(revision 13364)
@@ -90,6 +90,18 @@
 $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 );
Index: trunk/phase3/includes/AjaxDispatcher.php
===================================================================
--- trunk/phase3/includes/AjaxDispatcher.php	(revision 0)
+++ trunk/phase3/includes/AjaxDispatcher.php	(revision 13364)
@@ -0,0 +1,83 @@
+<?php
+
+//$wgRequestTime = microtime();
+
+// unset( $IP );
+// @ini_set( 'allow_url_fopen', 0 ); # For security...
+
+# Valid web server entry point, enable includes.
+# Please don't move this line to includes/Defines.php. This line essentially defines
+# a valid entry point. If you put it in includes/Defines.php, then any script that includes
+# it becomes an entry point, thereby defeating its purpose.
+// define( 'MEDIAWIKI', true );
+// require_once( './includes/Defines.php' );
+// require_once( './LocalSettings.php' );
+// require_once( 'includes/Setup.php' );
+require_once( 'AjaxFunctions.php' );
+
+if ( ! $wgUseAjax ) {
+	die ( -1 );
+}
+
+class AjaxDispatcher {
+	var $mode;
+	var $func_name;
+	var $args;
+
+	function AjaxDispatcher() {
+		global $wgAjaxCachePolicy;
+
+		wfProfileIn( 'AjaxDispatcher::AjaxDispatcher' );
+
+		$wgAjaxCachePolicy = new AjaxCachePolicy();
+
+		$this->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;
+	}
+}
+
+?>

Property changes on: trunk/phase3/includes/AjaxDispatcher.php
___________________________________________________________________
Name: svn:eol-style
   + native
Name: svn:keywords
   + Author Date Id Revision

Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 13363)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 13364)
@@ -1908,5 +1908,10 @@
  */
 $wgUseAjax = false;
 
+/**
+ * List of Ajax-callable functions
+ */
+$wgAjaxExportList = array( 'wfSajaxSearch' );
 
+
 ?>
Views
Toolbox