Author: chabotc
Date: Mon Dec 1 22:54:46 2008
New Revision: 722390
URL: http://svn.apache.org/viewvc?rev=722390&view=rev
Log:
SHINDIG-541 by Impetus technologies and improved upon by Pan Jie - Support
Tiered message bundles
Modified:
incubator/shindig/trunk/php/src/common/sample/CacheFile.php
incubator/shindig/trunk/php/src/gadgets/GadgetServer.php
incubator/shindig/trunk/php/src/gadgets/MessageBundleParser.php
incubator/shindig/trunk/php/test/gadgets/MessageBundleParserTest.php
Modified: incubator/shindig/trunk/php/src/common/sample/CacheFile.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/sample/CacheFile.php?rev=722390&r1=722389&r2=722390&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/sample/CacheFile.php (original)
+++ incubator/shindig/trunk/php/src/common/sample/CacheFile.php Mon Dec 1
22:54:46 2008
@@ -96,7 +96,7 @@
}
if (File::exists($cacheFile) && File::readable($cacheFile)) {
$now = time();
- if (($mtime = filemtime($cacheFile)) !== false && ($now - $mtime) <
$expiration) {
+ if (($mtime = @filemtime($cacheFile)) !== false && ($now - $mtime) <
$expiration) {
if (($data = @file_get_contents($cacheFile)) !== false) {
$data = unserialize($data);
return $data;
@@ -135,4 +135,4 @@
throw new CacheException("Cache file could not be deleted");
}
}
-}
\ No newline at end of file
+}
Modified: incubator/shindig/trunk/php/src/gadgets/GadgetServer.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/GadgetServer.php?rev=722390&r1=722389&r2=722390&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/GadgetServer.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/GadgetServer.php Mon Dec 1
22:54:46 2008
@@ -40,24 +40,43 @@
return $gadget;
}
- private function getBundle(LocaleSpec $localeSpec = null, $context) {
- if ($localeSpec != null) {
- $uri = $localeSpec->getURI();
- if ($uri != null) {
- $fetcher = $context->getHttpFetcher();
- $response = $fetcher->fetch(new RemoteContentRequest($uri), $context);
- $parser = new MessageBundleParser();
- $bundle = $parser->parse($response->getResponseContent());
- return $bundle;
- } else {
- $messages = array();
- foreach ($localeSpec->getLocaleMessageBundles() as $messageBundle) {
- $messages[$messageBundle->getName()] = $messageBundle->getDesc();
- }
- return new MessageBundle($messages);
- }
+ private function getBundle($context, $gadget) {
+ $locale = $context->getLocale();
+ $localeSpec = $this->localeSpec($gadget, $locale); // en-US
+ $language_allSpec = $this->localeSpec($gadget, new
Locale($locale->getLanguage(), "all")); // en-all
+ $all_allSpec = $this->localeSpec($gadget, new Locale("all", "all")); //
all-all
+ $messagesArray = $this->getMessagesArrayForSpecs($context,
array($localeSpec, $language_allSpec, $all_allSpec));
+ if (count($messagesArray) == 0) {
+ return null;
+ }
+ for ($i = 1; $i < count($messagesArray); ++ $i) {
+ $diff = array_diff_key($messagesArray[$i], $messagesArray[0]);
+ foreach ($diff as $diffKey => $diffValue) {
+ $messagesArray[0][$diffKey] = $diffValue;
+ }
+ }
+ return new MessageBundle($messagesArray[0]);
+ }
+
+ private function getMessagesArrayForSpecs($context, Array $specs) {
+ $requestArray = array();
+ $contextArray = array();
+ $messagesArray = array();
+ foreach ($specs as $spec) {
+ if ($spec == null) continue;
+ $uri = $spec->getURI();
+ if ($uri == null) continue;
+ $requestArray[] = new RemoteContentRequest($uri);
+ $contextArray[] = $context;
+ }
+ if (count($requestArray) == 0) return array();
+ $fetcher = $context->getHttpFetcher();
+ $responseArray = $fetcher->multiFetch($requestArray, $contextArray);
+ $parser = new MessageBundleParser();
+ foreach ($responseArray as $response) {
+ $messagesArray[] = $parser->parse($response->getResponseContent());
}
- return null;
+ return $messagesArray;
}
private function localeSpec($gadget, $locale) {
@@ -71,53 +90,30 @@
return null;
}
- private function getLocaleSpec($gadget, $context) {
- $locale = $context->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 $gadget, $context) {
//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, $context);
-
- // get the message bundle for this gadget
- $bundle = $this->getBundle($localeSpec, $context);
-
+ // get the message bundle for this gadget
+ $bundle = $this->getBundle($context, $gadget);
//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;
+ $locale = $context->getLocale();
+ $localeSpec = $this->localeSpec($gadget, $locale); // en-US
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");
-
+ $substitutor->addSubstitution('BIDI', "REVERSE_DIR", $rtl ? "ltr" :
"rtl");
// userPref's
$upValues = $gadget->getUserPrefValues();
foreach ($gadget->getUserPrefs() as $pref) {
@@ -131,9 +127,7 @@
}
$substitutor->addSubstitution('UP', $name, $value);
}
-
$this->substitutePreloads($gadget, $substitutor);
-
// Process required / desired features
$requires = $gadget->getRequires();
$needed = array();
Modified: incubator/shindig/trunk/php/src/gadgets/MessageBundleParser.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/MessageBundleParser.php?rev=722390&r1=722389&r2=722390&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/MessageBundleParser.php (original)
+++ incubator/shindig/trunk/php/src/gadgets/MessageBundleParser.php Mon Dec 1
22:54:46 2008
@@ -38,6 +38,6 @@
$this->processMessage($messages, $msg);
}
}
- return new MessageBundle($messages);
+ return $messages;
}
}
\ No newline at end of file
Modified: incubator/shindig/trunk/php/test/gadgets/MessageBundleParserTest.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/gadgets/MessageBundleParserTest.php?rev=722390&r1=722389&r2=722390&view=diff
==============================================================================
--- incubator/shindig/trunk/php/test/gadgets/MessageBundleParserTest.php
(original)
+++ incubator/shindig/trunk/php/test/gadgets/MessageBundleParserTest.php Mon
Dec 1 22:54:46 2008
@@ -46,7 +46,6 @@
$this->MessageBundleParser = null;
$this->MessageBundle = null;
-
parent::tearDown();
}
@@ -65,8 +64,10 @@
$this->MessageBundle = $this->MessageBundleParser->parse($xml);
- $this->assertTrue($this->MessageBundle instanceof MessageBundle);
-
+ $this->assertEquals('Message 1', $this->MessageBundle['name1']);
+ $this->assertEquals('Message 2', $this->MessageBundle['name2']);
+ $this->assertEquals('Message 3', $this->MessageBundle['name3']);
+ $this->assertEquals('Message 4', $this->MessageBundle['name4']);
}
}