/** * Fetches a widget's code from the external Repository * * @param $name string Name of the widget * @return $code mixed */ protected function fetchFromRepository(&$name) { $msgs = new MessageList(); // default feed $feed = new RepositoryFeed(); $result = $feed->fetch(); if ($result === false) { $msgs->pushMessageById(self::NAME . '-error-feed'); return $msgs; } $_name = strtolower($name); $widgetLocator = $feed->getWidgetLocatorByName($_name); if (!$widgetLocator instanceof WidgetLocator) { $msgs->pushMessageById(self::NAME . '-widget-not-found', array($name)); return $msgs; } $url = $widgetLocator->codelink; $code = $this->wget($url); if ($code === false) { $msgs->pushMessageById(self::NAME . '-error-code-fetch', array($name)); return $msgs; } // if we got lucky, save it to the trans-cache $this->saveInTransCache($name, $code); return $code; }
/** * Parser Function #widget */ public function pfnc_widget(&$parser, $_name = null) { if (empty($_name)) { $msg = new MessageList(); $msg->pushMessageById(self::NAME . '-missing-name'); return $this->handleError($msg); } $params = func_get_args(); array_shift($params); # $parser array_shift($params); # $name // make sure we are not tricked $name = $this->makeSecureName($_name); // get Factory istance $factory = MW_WidgetFactory::gs(); // try building a widget from the provided name $widget = $factory->newFromWidgetName($name); if (!$widget instanceof Widget) { return $this->handleError($widget); } // render the widget with the provided parameters // TODO change to 'borg' pattern $renderer = MW_WidgetRenderer::gs(); $output = $renderer->render($widget, $params); if (!is_string($output)) { return $this->handleError($output); } return array($output, 'noparse' => true, 'isHTML' => true); }
/** * Go through all registered code store * * @param $name string * @return $obj mixed Widget / MW_SecureWidgetsMessageList */ public function newFromWidgetName(&$name) { $this->fetchOtherStores(); $msgs = new MessageList(); foreach ($this->codeStore as $store) { $store->setName($name); $rawCode = $store->getCode(); if (is_string($rawCode)) { $code = self::extractCode($rawCode); if ($code === false) { $msgs->pushMessageById(self::NAME . '-nocode'); continue; } return new Widget($name, $code); } else { $msgs->insertMessages($rawCode); } } //foreach // error return $msgs; }
/** * @return $obj mixed String with result OR MessageList object instance in case of errors */ public static function render(&$widget, &$params) { // error stack object $msgs = new MessageList(); $code = $widget->getCode(); $name = $widget->getName(); // extract parameters from widget template $tp = WidgetParameters::newFromTemplate($code); // prepare the input variables $ip = WidgetParameters::newFromParamList($params); // Case 1: template does not have parameters // Don't make waves even in the case where // input variables are provided where none are required... if ($tp->isEmpty()) { return $code; } // Case 2: template has parameters but no input variables provided if ($ip->isEmpty()) { $msg = new MessageList(); return $msg->pushMessageById(self::NAME . '-missing-inputs', array($name)); } // Case 3: template specifies parameter types and input variables do not match foreach ($tp as $index => $e) { if (!isset($e['n'])) { throw new Exception(__METHOD__ . ": name parameter missing in template"); } if (!isset($e['t'])) { throw new Exception(__METHOD__ . ": type parameter missing in template"); } $patt = $e['r']; $name = $e['n']; $type = $e['t']; $value = null; // make sure we have an input variable that corresponds // the a required template parameter if (isset($ip[$name])) { $value = $ip[$name]['v']; } else { // is there a default value then?? if (isset($e['d'])) { $value = $e['d']; } else { $msgs->pushMessageById(self::NAME . '-missing-input', array($name, $type)); continue; } } $result = TypeChecker::checkParam($type, $value); if ($result === null) { $msgs->pushMessageById(self::NAME . '-unsupported-type', array($name, $type)); continue; } if ($result === false) { $msgs->pushMessageById(self::NAME . '-type-mismatch', array($name, $type)); continue; } // everything looks OK - add value to template parameters list $tp->setParam($index, 'v', $value); } //foreach // if we have error messages, exit now if (!$msgs->isEmpty()) { return $msgs; } // Perform replacements in template and return the resulting code return WidgetParameters::doReplacementsInWidgetTemplate($widget, $tp); }