Author: lindner
Date: Tue Mar 18 17:37:36 2008
New Revision: 638661
URL: http://svn.apache.org/viewvc?rev=638661&view=rev
Log:
Apply patch from Chris Chabot for SHINDIG-131
- removes the old syndication config hack and adds proper syndicator handling,
so that it now only includes the config of the requested features, and
supports
staggered syndicator configuration.
Added:
incubator/shindig/trunk/php/gadgets/src/SyndicatorConfig.php
Modified:
incubator/shindig/trunk/php/gadgets/config.php
incubator/shindig/trunk/php/gadgets/src/GadgetContext.php
incubator/shindig/trunk/php/gadgets/src/JsLibrary.php
incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php
Modified: incubator/shindig/trunk/php/gadgets/config.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/config.php?rev=638661&r1=638660&r2=638661&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/config.php (original)
+++ incubator/shindig/trunk/php/gadgets/config.php Tue Mar 18 17:37:36 2008
@@ -16,7 +16,7 @@
// Otherwise also checkout the features, config and javascript
directories and set
// these to their locations
'features_path' => realpath(dirname(__FILE__)).'/../../features/',
- 'syndicator_config' =>
realpath(dirname(__FILE__)).'/../../config/syndicator.js',
+ 'syndicator_path' => realpath(dirname(__FILE__)).'/../../config/',
'javascript_path' => realpath(dirname(__FILE__)).'/../../javascript/',
// If you want to use the yuicompressor
(http://developer.yahoo.com/yui/compressor/) to minify your javascript
Modified: incubator/shindig/trunk/php/gadgets/src/GadgetContext.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/GadgetContext.php?rev=638661&r1=638660&r2=638661&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/GadgetContext.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/GadgetContext.php Tue Mar 18
17:37:36 2008
@@ -39,6 +39,8 @@
private $blacklist = null;
private $ignoreCache = null;
private $forcedJsLibs = null;
+ private $syndicatorConfig = null;
+ private $syndicator = null;
public function __construct($renderingContext)
{
@@ -51,9 +53,21 @@
$this->setUrl($this->getUrlParam());
$this->setModuleId($this->getModuleIdParam());
$this->setView($this->getViewParam());
+ $this->setSyndicator($this->getSyndicatorParam());
//NOTE All classes are inititialized when called (aka lazy
loading) because we don't
//need all of them in every situation
}
+
+ private function getSyndicatorParam()
+ {
+ $synd = 'default';
+ if (!empty($_GET['synd'])) {
+ $synd = $_GET['synd'];
+ } elseif (!empty($_POST['synd'])) {
+ $synd = $_POST['synd'];
+ }
+ return $synd;
+ }
private function getIgnoreCacheParam()
{
@@ -160,6 +174,25 @@
}
return new Locale($language, $country);
}
+
+ private function instanceSyndicatorConfig()
+ {
+ global $config;
+ return new SyndicatorConfig($config['syndicator_path']);
+ }
+
+ public function getSyndicator()
+ {
+ return $this->syndicator;
+ }
+
+ public function getSyndicatorConfig()
+ {
+ if ($this->syndicatorConfig == null) {
+ $this->syndicatorConfig =
$this->instanceSyndicatorConfig();
+ }
+ return $this->syndicatorConfig;
+ }
public function getCache()
{
@@ -206,6 +239,16 @@
public function getView()
{
return $this->view;
+ }
+
+ public function setSyndicator($syndicator)
+ {
+ $this->syndicator = $syndicator;
+ }
+
+ public function setSyndicatorConfig($syndicatorConfig)
+ {
+ $this->syndicatorConfig = $syndicatorConfig;
}
public function setBlacklist($blacklist)
Modified: incubator/shindig/trunk/php/gadgets/src/JsLibrary.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/JsLibrary.php?rev=638661&r1=638660&r2=638661&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/JsLibrary.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/JsLibrary.php Tue Mar 18 17:37:36
2008
@@ -22,9 +22,11 @@
private $types = array('FILE', 'RESOURCE', 'URL', 'INLINE');
private $type;
private $content;
+ private $featureName; // used to track what feature this belongs to
- public function __construct($type, $content)
+ public function __construct($type, $content, $featureName = '')
{
+ $this->featureName = $featureName;
$this->type = $type;
$this->content = $content;
}
@@ -39,6 +41,11 @@
return $this->content;
}
+ public function getFeatureName()
+ {
+ return $this->featureName;
+ }
+
public function toString()
{
if ($this->type == 'URL') {
@@ -50,10 +57,17 @@
static function create($type, $content)
{
- if ($type == 'FILE' || $type == 'RESOURCE') {
+ $feature = '';
+ if ($type == 'FILE') {
+ $feature = dirname($content);
+ if (substr($feature,strlen($feature) - 1, 1) == '/') {
+ // strip tailing /, if any, so that the
following strrpos works in any situation
+ $feature = substr($feature, 0, strlen($feature)
- 1);
+ }
+ $feature = substr($feature, strrpos($feature, '/') + 1);
$content = JsLibrary::loadData($content, $type);
}
- return new JsLibrary($type, $content);
+ return new JsLibrary($type, $content, $feature);
}
static private function loadData($name, $type)
Added: incubator/shindig/trunk/php/gadgets/src/SyndicatorConfig.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/SyndicatorConfig.php?rev=638661&view=auto
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/SyndicatorConfig.php (added)
+++ incubator/shindig/trunk/php/gadgets/src/SyndicatorConfig.php Tue Mar 18
17:37:36 2008
@@ -0,0 +1,91 @@
+<?
+
+class SyndicatorConfig {
+ public $default_syndicator = 'default';
+ public $syndicator_key = 'gadgets.syndicator';
+ private $config = array();
+
+ public function __construct($defaultSyndicator)
+ {
+ if (! empty($defaultSyndicator)) {
+ $this->loadSyndicators($defaultSyndicator);
+ }
+ }
+
+ private function loadSyndicators($syndicators)
+ {
+ if (! file_exists($syndicators) || ! is_dir($syndicators)) {
+ throw new Exception("Invalid syndicator path");
+ }
+ foreach (glob("$syndicators/*") as $file) {
+ if (! is_readable($file)) {
+ throw new Exception("Could not read syndicator
config: $file");
+ }
+ if (is_dir($file)) {
+ // support recursive loading of sub directories
+ $this->loadSyndicators($file);
+ } else {
+ $this->loadFromFile($file);
+ }
+ }
+ }
+
+ private function loadFromFile($file)
+ {
+ $contents = file_get_contents($file);
+ // remove all comments (both /* */ and // style) because this
confuses the json parser
+ // note: the json parser also crashes on trailing ,'s in
records so please don't use them
+ $contents = preg_replace('/\/\/.*$/m', '',
preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', $contents));
+ $config = json_decode($contents, true);
+ if (! isset($config[$this->syndicator_key][0])) {
+ throw new Exception("No gadgets.syndicator value set");
+ }
+ $syndicator = $config[$this->syndicator_key][0];
+ $this->config[$syndicator] = array();
+ foreach ($config as $key => $val) {
+ $this->config[$syndicator][$key] = $val;
+ }
+ }
+
+ public function getConfig($syndicator, $name)
+ {
+ $config = array();
+ if (isset($this->config[$syndicator]) &&
isset($this->config[$syndicator][$name])) {
+ $config = $this->config[$syndicator][$name];
+ }
+ if ($syndicator != $this->default_syndicator &&
isset($this->config[$syndicator][$name])) {
+ $config =
$this->mergeConfig($this->config[$syndicator][$name], $config);
+ }
+ return $config;
+ }
+
+ // Code sniplet borrowed from:
http://nl.php.net/manual/en/function.array-merge-recursive.php#81409
+ // default array merge recursive doesn't overwrite values, but creates
multiple elementents for that key,
+ // which is not what we want here, we want array_merge like behavior
+ private function mergeConfig() // $array1, $array2, etc
+ {
+ $arrays = func_get_args();
+ $narrays = count($arrays);
+ for($i = 0; $i < $narrays; $i ++) {
+ if (! is_array($arrays[$i])) {
+ trigger_error('Argument #' . ($i + 1) . ' is
not an array - trying to merge array with scalar! Returning null!',
E_USER_WARNING);
+ return;
+ }
+ }
+ $ret = $arrays[0];
+ for($i = 1; $i < $narrays; $i ++) {
+ foreach ($arrays[$i] as $key => $value) {
+ if (((string) $key) === ((string)
intval($key))) { // integer or string as integer key - append
+ $ret[] = $value;
+ } else {
+ if (is_array($value) &&
isset($ret[$key])) {
+ $ret[$key] =
array_merge_recursive2($ret[$key], $value);
+ } else {
+ $ret[$key] = $value;
+ }
+ }
+ }
+ }
+ return $ret;
+ }
+}
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=638661&r1=638660&r2=638661&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php
(original)
+++ incubator/shindig/trunk/php/gadgets/src/http/GadgetRenderingServlet.php Tue
Mar 18 17:37:36 2008
@@ -103,7 +103,6 @@
*/
private function outputHtmlGadget($gadget, $context)
{
- global $config;
$this->setContentType("text/html; charset=UTF-8");
$output = '';
$output .= "<html>\n<head>\n";
@@ -119,7 +118,7 @@
if ($type == 'URL') {
// TODO: This case needs to be handled more
gracefully by the js
// servlet. We should probably inline external
JS as well.
- $externJs .= sprintf($externFmt,
$library->getContent());
+ $externJs .= sprintf($externFmt,
$library->getContent())."\n";
} else if ($type == 'INLINE') {
$inlineJs .= $library->getContent() . "\n";
} else {
@@ -133,7 +132,7 @@
// Forced libs first.
if (! empty($forcedLibs)) {
$libs = explode(':', $forcedLibs);
- $output .= sprintf($externFmt, $this->getJsUrl($libs));
+ $output .= sprintf($externFmt,
$this->getJsUrl($libs))."\n";
}
if (strlen($inlineJs) > 0) {
$output .= "<script><!--\n" . $inlineJs .
"\n-->\n</script>\n";
@@ -141,21 +140,12 @@
if (strlen($externJs) > 0) {
$output .= $externJs;
}
- //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");
- }
- // 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);
- // build the messages to include in the
gadgets.Prefs.setMessages_() javascript call
- $msgs = '';
- if ($gadget->getMessageBundle()) {
- $bundle = $gadget->getMessageBundle();
- $msgs = json_encode($bundle->getMessages());
- }
- // Add the gadget.config.init and gadgets.Prefs.setMessages_
calls to the document
- $output .= "\n<script>\ngadgets.config.init(" .
json_encode($syndData['gadgets.features']) . ");\ngadgets.Prefs.setMessages_("
. $msgs . ");\n</script>\n";
+
+ $output .= "<script><!--\n";
+ $output .= $this->appendJsConfig($context, $gadget);
+ $output .= $this->appendMessages($gadget);
+ $output .= "-->\n</script>\n";
+
$gadgetExceptions = array();
$content = $gadget->getContentData($context->getView());
if (empty($content)) {
@@ -281,5 +271,30 @@
}
$buf .= ".js?v=" . sha1($inlineJs);
return $buf;
+ }
+
+ private function appendJsConfig($context, $gadget)
+ {
+ $syndicator = $context->getSyndicator();
+ $syndicatorConfig = $context->getSyndicatorConfig();
+ $gadgetConfig = array();
+ $featureConfig = $syndicatorConfig->getConfig($syndicator,
'gadgets.features');
+ foreach ($gadget->getJsLibraries() as $library) {
+ $feature = $library->getFeatureName();
+ if (!isset($gadgetConfig[$feature]) &&
!empty($featureConfig[$feature])) {
+ $gadgetConfig[$feature] =
$featureConfig[$feature];
+ }
+ }
+ return "gadgets.config.init(" . json_encode($gadgetConfig) .
");\n";
+ }
+
+ private function appendMessages($gadget)
+ {
+ $msgs = '';
+ if ($gadget->getMessageBundle()) {
+ $bundle = $gadget->getMessageBundle();
+ $msgs = json_encode($bundle->getMessages());
+ }
+ return "gadgets.Prefs.setMessages_($msgs);\n";
}
}