You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by li...@apache.org on 2008/03/12 21:10:08 UTC

svn commit: r636487 - in /incubator/shindig/trunk/php/gadgets/src: ./ http/

Author: lindner
Date: Wed Mar 12 13:09:58 2008
New Revision: 636487

URL: http://svn.apache.org/viewvc?rev=636487&view=rev
Log:
Patch provided by Chris Chabot for SHINDIG-122
This patch fixes a few minor bugs, plus makes it work with the trunk's features and container javascript again (workings of which got changed in SHINDIG-118) 

Removed:
    incubator/shindig/trunk/php/gadgets/src/BidiSubstituter.php
    incubator/shindig/trunk/php/gadgets/src/MessageBundleSubstituter.php
    incubator/shindig/trunk/php/gadgets/src/ModuleSubstituter.php
    incubator/shindig/trunk/php/gadgets/src/UserPrefSubstituter.php
Modified:
    incubator/shindig/trunk/php/gadgets/src/BasicRemoteContentFetcher.php
    incubator/shindig/trunk/php/gadgets/src/Gadget.php
    incubator/shindig/trunk/php/gadgets/src/GadgetFeatureRegistry.php
    incubator/shindig/trunk/php/gadgets/src/GadgetServer.php
    incubator/shindig/trunk/php/gadgets/src/JsFeatureLoader.php
    incubator/shindig/trunk/php/gadgets/src/http/FilesServlet.php
    incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php
    incubator/shindig/trunk/php/gadgets/src/http/HttpServlet.php
    incubator/shindig/trunk/php/gadgets/src/http/JsServlet.php

Modified: incubator/shindig/trunk/php/gadgets/src/BasicRemoteContentFetcher.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/BasicRemoteContentFetcher.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/BasicRemoteContentFetcher.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/BasicRemoteContentFetcher.php Wed Mar 12 13:09:58 2008
@@ -22,7 +22,7 @@
  * Basic remote content fetcher, uses curl_multi to fetch multiple resources at the same time
  */
 
-class BasicRemoteContentFetcher extends remoteContentFetcher {
+class BasicRemoteContentFetcher extends RemoteContentFetcher {
 	private $requests = array();
 	
 	public function fetchRequest($request)

Modified: incubator/shindig/trunk/php/gadgets/src/Gadget.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/Gadget.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/Gadget.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/Gadget.php Wed Mar 12 13:09:58 2008
@@ -30,7 +30,7 @@
 	private $jsLibraries;
 	private $substitutions;
 	private $userPrefValues;
-	private $currentMessageBundle = array();
+	private $messageBundle = array();
 	// As in UserPref, no enums so fake it
 	public $contentTypes    = array('HTML', 'URL');
 	public $id;
@@ -94,9 +94,9 @@
 		return $this->substitutions->substitute($this->getContentType() == 'URL' ? $this->contentHref : null);
 	}
 	
-	public function getCurrentMessageBundle()
+	public function getMessageBundle()
 	{
-		return $this->currentMessageBundle;
+		return $this->messageBundle;
 	}
 	
 	public function getDescription()
@@ -194,9 +194,9 @@
 		return $this->userPrefValues;
 	}
 	
-	public function setCurrentMessageBundle($messageBundle)
+	public function setMessageBundle($messageBundle)
 	{
-		$this->currentMessageBundle = $messageBundle;
+		$this->messageBundle = $messageBundle;
 	}
 	
 	/* gadget Spec functions */

Modified: incubator/shindig/trunk/php/gadgets/src/GadgetFeatureRegistry.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/GadgetFeatureRegistry.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/GadgetFeatureRegistry.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/GadgetFeatureRegistry.php Wed Mar 12 13:09:58 2008
@@ -33,10 +33,6 @@
 		if (empty($featurePath) || $featurePath == null) {
 			return;
 		}
-		$FEAT_MSG_BUNDLE = "core.msgbundlesubst";
-		$FEAT_BIDI = "core.bidisubst";
-		$FEAT_MODULE = "core.modulesubst";
-		$FEAT_USER_PREF_SUBST = "core.prefsubst";
 		$coreDeps = array();
 		$loader = new JsFeatureLoader();
 		$jsFeatures = $loader->loadFeatures($featurePath, $this);
@@ -47,14 +43,6 @@
 					$this->core[$entry->name] = $entry->name;
 				}
 			}
-			$this->core[$FEAT_MSG_BUNDLE] = $FEAT_MSG_BUNDLE;
-			$this->register($FEAT_MSG_BUNDLE, $coreDeps, new MessageBundleSubstituter());
-			$this->core[$FEAT_BIDI] = $FEAT_BIDI;
-			$this->register($FEAT_BIDI, $coreDeps, new BidiSubstituter());
-			$this->core[$FEAT_MODULE] = $FEAT_MODULE;
-			$this->register($FEAT_MODULE, $coreDeps, new ModuleSubstituter());
-			$this->core[$FEAT_USER_PREF_SUBST] = $FEAT_USER_PREF_SUBST;
-			$this->register($FEAT_USER_PREF_SUBST, $coreDeps, new UserPrefSubstituter());
 			// Make sure non-core features depend on core.
 			foreach ( $jsFeatures as $entry ) {
 				if (strtolower(substr($entry->name, 0, strlen('core'))) != 'core') {

Modified: incubator/shindig/trunk/php/gadgets/src/GadgetServer.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/GadgetServer.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/GadgetServer.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/GadgetServer.php Wed Mar 12 13:09:58 2008
@@ -54,7 +54,7 @@
 		if ($this->blacklist != null && $this->blacklist->isBlacklisted($this->gadgetId->getURI())) {
 			throw new GadgetException("Gadget is blacklisted");
 		}
-		$request = new remoteContentRequest($this->gadgetId->getURI());
+		$request = new RemoteContentRequest($this->gadgetId->getURI());
 		$xml = $this->httpFetcher->fetch($request);
 		if ($xml->getHttpCode() != '200') {
 			throw new GadgetException("Failed to retrieve gadget content");
@@ -63,9 +63,98 @@
 		$gadget = $specParser->parse($xml->getResponseContent(), $this->gadgetId, $this->userPrefs);
 		return $gadget;
 	}
+		
+	private function getBundle($localeSpec, $context)
+	{
+		if ($localeSpec != null) {
+			$uri = $localeSpec->getURI();
+			if ($uri != null) {
+				$fetcher = $context->getHttpFetcher();
+				$response = $fetcher->fetch(new RemoteContentRequest($uri));
+				$parser = new MessageBundleParser();
+				$bundle = $parser->parse($response->getResponseContent());
+				return $bundle;				
+			}
+		}
+		return null;
+	}
+	
+	private function localeSpec($gadget, $locale)
+	{
+		$localeSpecs = $gadget->getLocaleSpecs();
+		foreach ( $localeSpecs as $locSpec ) {
+			//fix me
+			if ($locSpec->getLocale()->equals($locale)) {
+				return $locSpec;
+			}
+		}
+		return null;
+	}
+	
+	private function getLocaleSpec($gadget)
+	{
+		$locale = $this->gc->getLocale();
+		// en-US
+		$localeSpec = $this->localeSpec($gadget, $locale);
+		if ($localeSpec == null) {
+			// en-all
+			$localeSpec = $this->localeSpec($gadget, new Locale($locale->getLanguage(), "all"));
+		}
+		if ($localeSpec == null) {
+			// all-all
+			$localeSpec = $this->localeSpec($gadget, new Locale("all", "all"));
+		}
+		return $localeSpec;
+	}
 	
 	private function featuresLoad($gadget)
 	{
+		//NOTE i've been a bit liberal here with folding code into this function, while it did get a bit long, the many include()'s are slowing us down
+		// Should really clean this up a bit in the future though
+		$localeSpec = $this->getLocaleSpec($gadget);
+		
+		// get the message bundle for this gadget
+		$bundle = $this->getBundle($localeSpec, $this->gc);
+		
+		//FIXME this is a half-assed solution between following the refactoring and maintaining some of the old code, fixing this up later
+		$gadget->setMessageBundle($bundle);
+		
+		// perform substitutions
+		$substitutor = $gadget->getSubstitutions();
+		
+		// Module ID
+		$substitutor->addSubstitution('MODULE', "ID", $gadget->getId()->getModuleId());
+		
+		// Messages (multi-language)
+		if ($bundle) {
+			$gadget->getSubstitutions()->addSubstitutions('MSG', $bundle->getMessages());
+		}
+		
+		// Bidi support
+		$rtl = false;
+		if ($localeSpec != null) {
+			$rtl = $localeSpec->isRightToLeft();
+		}
+		$substitutor->addSubstitution('BIDI', "START_EDGE", $rtl ? "right" : "left");
+		$substitutor->addSubstitution('BIDI', "END_EDGE", $rtl ? "left" : "right");
+		$substitutor->addSubstitution('BIDI', "DIR", $rtl ? "rtl" : "ltr");
+		$substitutor->addSubstitution('BIDI', "REVERSE_DIR", $rtl ? "ltr" : "rtl");
+		
+		// userPref's
+		$upValues = $gadget->getUserPrefValues();
+		foreach ( $gadget->getUserPrefs() as $pref ) {
+			$name = $pref->getName();
+			$value = $upValues->getPref($name);
+			if ($value == null) {
+				$value = $pref->getDefaultValue();
+			}
+			if ($value == null) {
+				$value = "";
+			}
+			$substitutor->addSubstitution('USER_PREF', $name, $value);
+		}
+		
+		// Process required / desired features
 		$requires = $gadget->getRequires();
 		$needed = array();
 		$optionalNames = array();

Modified: incubator/shindig/trunk/php/gadgets/src/JsFeatureLoader.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/JsFeatureLoader.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/JsFeatureLoader.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/JsFeatureLoader.php Wed Mar 12 13:09:58 2008
@@ -45,15 +45,13 @@
 	private function loadFiles($path, &$features)
 	{
 		if (is_dir($path)) {
-			$dh = @opendir($path);
-			while ( ($file = @readdir($dh)) !== false ) {
+			foreach (glob("$path/*") as $file) {
 				// prevents us from looping over '.', '..' and 'hidden files', this last bit IS 
-				// different from the java version but unix standard really..
-				if (substr($file, 0, 1) != '.') {
-					$features = $this->loadFiles($path.'/'.$file, $features);
+				// different from the java version but it's the unix standard really..
+				if (substr(basename($file), 0, 1) != '.') {
+					$features = $this->loadFiles($file, $features);
 				}
 			}
-			@closedir($dh);
 		} else {
 			if (basename($path) == 'feature.xml') {
 				$feature = $this->processFile($path);

Modified: incubator/shindig/trunk/php/gadgets/src/http/FilesServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/http/FilesServlet.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/http/FilesServlet.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/http/FilesServlet.php Wed Mar 12 13:09:58 2008
@@ -38,6 +38,19 @@
 			echo "<html><body><h1>404 - Not Found</h1></body></html>";
 			die();
 		}
+		$dot = strrpos($file, '.');
+		if ($dot) {
+			$ext = strtolower(substr($file, $dot+1));
+			if ($ext == 'html' || $ext == 'htm') {
+				$this->setContentType('text/html');
+			} elseif ($ext == 'js') {
+				$this->setContentType('text/javascript');
+			} elseif ($ext == 'css') {
+				$this->setContentType('text/css');
+			}
+		}
+		$this->setNoCache(true);
+		$this->setLastModified(filemtime($file));
 		readfile($file);
 	}
 }

Modified: incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php Wed Mar 12 13:09:58 2008
@@ -54,6 +54,7 @@
 			$this->outputError($e);
 		}
 	}
+	
 	private function outputError($e)
 	{
 		global $config;
@@ -129,7 +130,13 @@
 		// remove both /* */ and // style comments, they crash the json_decode function
 		$contents = preg_replace('/\/\/.*$/m', '', preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', file_get_contents($config['syndicator_config'])));
 		$syndData = json_decode($contents, true);
-		$output .= '<script>gadgets.config.init(' . json_encode($syndData['gadgets.features']) . ');</script>';
+		
+		$msgs = '';
+		if ($gadget->getMessageBundle()) {
+			$bundle = $gadget->getMessageBundle();
+			$msgs = json_encode($bundle->getMessages());
+		}
+		$output .= "\n<script>\ngadgets.config.init(" . json_encode($syndData['gadgets.features']) . ");\ngadgets.Prefs.setMessages_(".$msgs.");\n</script>\n";
 		$gadgetExceptions = array();
 		$content = $gadget->getContentData($view);
 		if (empty($content)) {

Modified: incubator/shindig/trunk/php/gadgets/src/http/HttpServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/http/HttpServlet.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/http/HttpServlet.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/http/HttpServlet.php Wed Mar 12 13:09:58 2008
@@ -31,7 +31,8 @@
 	private $lastModified = false;
 	private $contentType = 'text/html';
 	private $charset = 'UTF-8';
-	public  $noHeaders = false;
+	public $noHeaders = false;
+	private $noCache = false;
 	
 	public function __construct()
 	{
@@ -42,44 +43,49 @@
 	public function __destruct()
 	{
 		global $config;
-		if (!$this->noHeaders) {
-			// attempt at some propper header handling from php
-			// this departs a little from the shindig code but it should give is valid http protocol handling
+		if (! $this->noHeaders) {
 			header("Content-Type: $this->contentType; charset={$this->charset}");
 			header('Connection: Keep-Alive');
 			header('Keep-Alive: timeout=15, max=30');
 			header('Accept-Ranges: bytes');
 			header('Content-Length: ' . ob_get_length());
-			header('Cache-Control: public,max-age=' . $config['cache_time'] . ',must-revalidate');
-			header("Expires: " . gmdate("D, d M Y H:i:s", time() + $config['cache_time']) . " GMT");
 			$content = ob_get_clean();
-			// Obey browsers (or proxy's) request to send a fresh copy if we recieve a no-cache pragma or cache-control request
-			if (! isset($_SERVER['HTTP_PRAGMA']) || ! strstr(strtolower($_SERVER['HTTP_PRAGMA']), 'no-cache') && (! isset($_SERVER['HTTP_CACHE_CONTROL']) || ! strstr(strtolower($_SERVER['HTTP_CACHE_CONTROL']), 'no-cache'))) {
-				// If the browser send us a E-TAG check if it matches (sha1 sum of content), if so send a not modified header instead of content
-				$etag = sha1($content);
-				if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
-					header("ETag: \"$etag\"");
-					if ($this->lastModified) {
-						header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
-					}
-					header("HTTP/1.1 304 Not Modified");
-					header('Content-Length: 0');
-					die();
-				}
-				header("ETag: \"$etag\"");
-				// If no etag is present, then check if maybe this browser supports if_modified_since tags,
-				// check it against our lastModified (if it's set)
-				if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $this->lastModified && ! isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
-					$if_modified_since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
-					if ($this->lastModified <= $if_modified_since) {
-						header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
+			if ($this->noCache) {
+				header("Cache-Control: no-cache, must-revalidate");
+				header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
+			} else {
+				// attempt at some propper header handling from php
+				// this departs a little from the shindig code but it should give is valid http protocol handling
+				header('Cache-Control: public,max-age=' . $config['cache_time'] . ',must-revalidate');
+				header("Expires: " . gmdate("D, d M Y H:i:s", time() + $config['cache_time']) . " GMT");
+				// Obey browsers (or proxy's) request to send a fresh copy if we recieve a no-cache pragma or cache-control request
+				if (! isset($_SERVER['HTTP_PRAGMA']) || ! strstr(strtolower($_SERVER['HTTP_PRAGMA']), 'no-cache') && (! isset($_SERVER['HTTP_CACHE_CONTROL']) || ! strstr(strtolower($_SERVER['HTTP_CACHE_CONTROL']), 'no-cache'))) {
+					// If the browser send us a E-TAG check if it matches (sha1 sum of content), if so send a not modified header instead of content
+					$etag = sha1($content);
+					if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
+						header("ETag: \"$etag\"");
+						if ($this->lastModified) {
+							header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
+						}
 						header("HTTP/1.1 304 Not Modified");
 						header('Content-Length: 0');
 						die();
 					}
-				}
-				if ($this->lastModified) {
-					header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
+					header("ETag: \"$etag\"");
+					// If no etag is present, then check if maybe this browser supports if_modified_since tags,
+					// check it against our lastModified (if it's set)
+					if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $this->lastModified && ! isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
+						$if_modified_since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
+						if ($this->lastModified <= $if_modified_since) {
+							header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
+							header("HTTP/1.1 304 Not Modified");
+							header('Content-Length: 0');
+							die();
+						}
+					}
+					if ($this->lastModified) {
+						header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->lastModified));
+					}
 				}
 			}
 			echo $content;
@@ -104,6 +110,16 @@
 	public function setLastModified($modified)
 	{
 		$this->lastModified = max($this->lastModified, $modified);
+	}
+	
+	public function setNoCache($cache = false)
+	{
+		$this->noCache = $cache;
+	}
+	
+	public function getNoCache()
+	{
+		return $this->noCache;
 	}
 	
 	public function error($msg)

Modified: incubator/shindig/trunk/php/gadgets/src/http/JsServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/http/JsServlet.php?rev=636487&r1=636486&r2=636487&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/http/JsServlet.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/http/JsServlet.php Wed Mar 12 13:09:58 2008
@@ -19,7 +19,7 @@
  */
 
 class JsServlet extends HttpServlet {
-	
+
 	public function doGet()
 	{
 		global $config;
@@ -53,13 +53,13 @@
 			$jsData = '';
 			$done = array();
 			do {
-				foreach ( $found as $entry ) {
+				foreach ($found as $entry) {
 					if (! in_array($entry, $done)) {
 						$feat = $registry->getEntry($entry);
 						$feature = $feat->getFeature();
 						if ($feature instanceof JsLibraryFeatureFactory) {
 							$jsLib = $feature;
-							foreach ( $jsLib->getLibraries($context) as $lib ) {
+							foreach ($jsLib->getLibraries($context) as $lib) {
 								if ($lib->getType() != 'URL') {
 									$jsData .= $lib->getContent();
 								}
@@ -68,30 +68,27 @@
 						$done[] = $entry;
 					}
 				}
-			} while ( count($done) != count($found) );
+			} while (count($done) != count($found));
 			if (! strlen($jsData)) {
 				header("HTTP/1.0 404 Not Found", true);
 				die();
 			}
-			if (!isset($_GET['c']) || $_GET['c'] != 1) {
-				$contents = preg_replace('/\/\/.*$/m','',preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', file_get_contents($config['syndicator_config'])));
+			if (! isset($_GET['c']) || $_GET['c'] != 1) {
+				$contents = preg_replace('/\/\/.*$/m', '', preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', file_get_contents($config['syndicator_config'])));
 				$syndData = json_decode($contents, true);
-				$jsData .= "\ngadgets.config.init(".json_encode($syndData['gadgets.features']).");\n";
+				$jsData .= "\ngadgets.config.init(" . json_encode($syndData['gadgets.features']) . ");\n";
 			}
 			$this->setCachingHeaders();
 			header('Content-Length: ' . strlen($jsData));
+			header("Content-Type: text/javascript");
 			echo $jsData;
-			//FIXME quick hack to make it work with the new syndicator.js config, needs a propper implimentation later
-			if (!file_exists($config['syndicator_config']) || !is_readable($config['syndicator_config'])) {
-				throw new GadgetException("Invalid syndicator config");
-			}
 		} else {
 			header("HTTP/1.0 404 Not Found", true);
 		
 		}
 		die();
 	}
-	
+
 	private function setCachingHeaders()
 	{
 		header("Expires: Tue, 01 Jan 2030 00:00:01 GMT");