Static access to the dynamic module config handler
public static DynamicModuleConfiguration ( ) : |
||
return |
public function ParseStepsToJson($steps) { $modulesConfig = \Swiftriver\Core\Setup::DynamicModuleConfiguration(); $return; $return->steps = array(); foreach ($steps as $step) { $s; $s->name = $step->Name(); $s->description = $step->Description(); $s->configurationProperties = $step->ReturnRequiredParameters(); if (array_key_exists($s->name, $modulesConfig->Configuration)) { $configuration = $modulesConfig->Configuration[$s->name]; if ($configuration != null) { foreach ($s->configurationProperties as $property) { foreach ($configuration as $key => $config) { if ($property->name == $key) { $property->value = $config->value; } } } } } $s->active = isset($step->active); $return->steps[] = $s; unset($s); } return json_encode($return); }
/** * Interface method that all PrePorcessing Steps must implement * * @param \Swiftriver\Core\ObjectModel\Content[] $contentItems * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration * @param \Log $logger * @return \Swiftriver\Core\ObjectModel\Content[] */ public function Process($contentItems, $configuration, $logger) { try { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method invoked]", \PEAR_LOG_DEBUG); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Loading module configuration]", \PEAR_LOG_DEBUG); $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration; if (!key_exists($this->Name(), $config)) { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [The SiCDS Pre Processing Step was called but no configuration exists for this module]", \PEAR_LOG_ERR); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } $config = $config[$this->Name()]; foreach ($this->ReturnRequiredParameters() as $requiredParam) { if (!key_exists($requiredParam->name, $config)) { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [The SiCDS Pre Processing Step was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } } $apiKey = (string) $config["API Key"]->value; $serviceUrl = (string) $config["Service Url"]->value; $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Loading module configuration]", \PEAR_LOG_DEBUG); $uniqueContentItems = array(); $parser = new \Swiftriver\SiCDSInterface\Parser(); $serviceInterface = new \Swiftriver\SiCDSInterface\ServiceInterface(); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Looping through the content items]", \PEAR_LOG_DEBUG); foreach ($contentItems as $item) { try { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Parsing content item into JSON]", \PEAR_LOG_DEBUG); $jsonForService = $parser->ParseItemToRequestJson($item, $apiKey); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Parsing content item into JSON]", \PEAR_LOG_DEBUG); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [START: Calling the SiCDS]", \PEAR_LOG_DEBUG); $jsonFromService = $serviceInterface->InterafceWithService($serviceUrl, $jsonForService, $configuration); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Calling the SiCDS]", \PEAR_LOG_DEBUG); if ($parser->ContentIsUnique($jsonFromService, $item->id)) { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Content with Id: {$item->id} is unique]", \PEAR_LOG_DEBUG); $uniqueContentItems[] = $item; } else { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Content with Id: {$item->id} a duplicate]", \PEAR_LOG_DEBUG); } } catch (\Exception $e) { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [An exception was thrown]", \PEAR_LOG_ERR); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [{$e}]", \PEAR_LOG_ERR); $uniqueContentItems[] = $item; } } $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [END: Looping through the content items]", \PEAR_LOG_DEBUG); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $uniqueContentItems; } catch (\Exception $e) { $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [An exception was thrown]", \PEAR_LOG_ERR); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [{$e}]", \PEAR_LOG_ERR); $logger->log("Swiftriver::PreProcessingSteps::SiCDSPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } }
public function RunWorkflow($json, $key) { $logger = \Swiftriver\Core\Setup::GetLogger(); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [Method invoked]", \PEAR_LOG_INFO); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Parsing the JSON input]", \PEAR_LOG_DEBUG); try { //Call the parent to decode the json $preProcessingStepName = parent::ParseJsonToPreProcessingStepName($json); $configuration = parent::ParseJsonToPreProcessingStepConfiguration($json); } catch (\Exception $e) { //Catch and report the exception if one is thrown $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [{$e}]", \PEAR_LOG_ERR); return parent::FormatErrorMessage($e); } $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [END: Parsing the JSON input]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Listing all available pre processors]", \PEAR_LOG_DEBUG); //Build a new pre processor $preProcessor = new \Swiftriver\Core\PreProcessing\PreProcessor(); //list all the availaibel steps $steps = $preProcessor->ListAllAvailablePreProcessingSteps(); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [END: Listing all available pre processors]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Looking for the pre processor to activate]", \PEAR_LOG_DEBUG); //Loop throught the steps looking for one with the same name as came from the JOSN foreach ($steps as $s) { if ($s->Name() == $preProcessingStepName) { $step = $s; } } //If not found, return an error. if (!isset($step) || $step == null) { $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [No pre processor with a name matching {$preProcessingStepName} was found.]", \PEAR_LOG_DEBUG); return parent::FormatErrorMessage("No pre processor matching the name {$preProcessingStepName} could be found"); } $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [END: Looking for the pre processor to activate]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Collecting Configuration properties for Pre Processing Step]", \PEAR_LOG_DEBUG); $thisConfig = array(); foreach ($step->ReturnRequiredParameters() as $param) { foreach ($configuration as $key => $value) { if ($param->name == $key) { $param->value = $value; } } $thisConfig[] = $param; } $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [END: Collecting Configuration properties for Pre Processing Step]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Saving configuration properties for Pre Processing Step]", \PEAR_LOG_DEBUG); $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration(); $config->Configuration[$preProcessingStepName] = $thisConfig; $config->Save(); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [START: Saving configuration properties for Pre Processing Step]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::PreProcessingSteps::SavePreProcessingStep::RunWorkflow [Method finished]", \PEAR_LOG_INFO); parent::FormatMessage("OK"); }
public function RunWorkflow($json, $key) { $logger = \Swiftriver\Core\Setup::GetLogger(); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [Method invoked]", \PEAR_LOG_INFO); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Parsing the input JSON]", \PEAR_LOG_DEBUG); try { $name = parent::ParseJsonToEventHandlerName($json); $config = parent::ParseJsonToEventHandlerConfiguration($json); } catch (\Exception $e) { $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [An Exception was thrown]", \PEAR_LOG_ERR); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [{$e}]", \PEAR_LOG_ERR); return parent::FormatErrorMessage($e); } $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Parsing the input JSON]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Instanciating the event distributor]", \PEAR_LOG_DEBUG); $eventDistributor = new \Swiftriver\Core\EventDistribution\EventDistributor(); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Instanciating the event distributor]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Listing all event handlers]", \PEAR_LOG_DEBUG); $handlers = $eventDistributor->ListAllAvailableEventHandlers(); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Listing all event handlers]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Looking for an event handler with matching name]", \PEAR_LOG_DEBUG); foreach ($handlers as $handler) { if ($handler->Name() == $name) { $thisHandler = $handler; } } if (!isset($thisHandler) || $thisHandler == null) { $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [No event handler was found matching the name {$name}]", \PEAR_LOG_DEBUG); return parent::FormatErrorMessage("No event handler was found matching the name {$name}"); } $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Looking for an event handler with matching name]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Adding all the configured properties to the event handler]", \PEAR_LOG_DEBUG); $thisConfig = array(); foreach ($thisHandler->ReturnRequiredParameters() as $param) { foreach ($config as $key => $value) { if ($param->name == $key) { $param->value = $value; } } $thisConfig[] = $param; } $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Adding all the configured properties to the event handler]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [START: Saving the configuration]", \PEAR_LOG_DEBUG); $configuration = \Swiftriver\Core\Setup::DynamicModuleConfiguration(); $configuration->Configuration[$name] = $thisConfig; $configuration->Save(); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [END: Saving the configuration]", \PEAR_LOG_DEBUG); $logger->log("Core::Workflows::EventHandlers::SaveEventHandlers::RunWorkflow [Method finished]", \PEAR_LOG_INFO); parent::FormatMessage("OK"); }
/** * Given a collection of content items, * * @param \Swiftriver\Core\ObjectModel\Content[] $contentItems * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration * @param \Log $logger * @return \Swiftriver\Core\ObjectModel\Content[] */ public function Process($contentItems, $configuration, $logger) { $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [Method invoked]", \PEAR_LOG_DEBUG); //if the content is not valid, jsut return it if (!isset($contentItems) || !is_array($contentItems) || count($contentItems) < 1) { $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [No content supplied]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } //get the module configuraiton $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration; if (!key_exists($this->Name(), $config)) { $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [The Yahoo Placemaker Turbine was called but no configuration exists for this module]", \PEAR_LOG_ERR); $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } $config = $config[$this->Name()]; foreach ($this->ReturnRequiredParameters() as $requiredParam) { if (!key_exists($requiredParam->name, $config)) { $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [The Yahoo Placemaker Turbine was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } } $appid = (string) $config["Yahoo Placemaker App Id"]->value; $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [START: Looping through content items]", \PEAR_LOG_DEBUG); for ($i = 0; $i < \count($contentItems); $i++) { $content = $contentItems[$i]; if (\count($content->gisData) == 0 && \count($content->source->gisData) == 0) { $text = $content->text[0]->title; foreach ($content->text[0]->text as $t) { $text .= " " . $t; } $gis = $this->YahooPlacemakerRequest($text, $appid); $content->gisData[] = $gis; } $contentItems[$i] = $content; } $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [END: Looping through content items]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::YahooPlacemakerPreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); //return the translated content return $contentItems; }
/** * Given a GenericEvent object, this method should do * something amazing with the data contained in the * event arguments. * * @param GenericEvent $event * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration * @param \Log $logger */ public function HandleEvent($event, $configuration, $logger) { $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method invoked]", \PEAR_LOG_DEBUG); //Get the $event->arguments as a content item $content = $event->arguments; //get the module configuraiton $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration; if (!key_exists($this->Name(), $config)) { $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [The Ushahidi Event Handler was called but no configuration exists for this module]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } $config = $config[$this->Name()]; foreach ($this->ReturnRequiredParameters() as $requiredParam) { if (!key_exists($requiredParam->name, $config)) { $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [The Ushahidi Event Handler was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } } //extract the Url for Ushahidi $uri = (string) $config["Ushahidi Url"]->value; $uri = rtrim($uri, "/") . "/api"; //null check the uri if ($uri == null || $uri == "") { $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [The Ushahidi Event Handler was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Pushing trusted content item to Ushahidi]", \PEAR_LOG_DEBUG); foreach ($content as $item) { if ($item->source->score > 90) { // Trusted content //Instanciate the parser that will be used to parse the content item into Ushahidi format $toUshahidiParser = new \Swiftriver\UshahidiPassivePush\ContentToUshahidiAPIParser(); //Get the ushahidi formatted params from the parser $parameters = $toUshahidiParser->ParseContentItemToUshahidiAPIFormat($item); //include the service wrapper $service = new \Swiftriver\UshahidiPassivePush\ServiceInterface(); $json_returned = ""; try { //Call the service and get the return $serviceReturn = $service->InterafceWithService($uri, $parameters, $configuration); //null check return if ($serviceReturn == null || !is_string($serviceReturn)) { throw new \Exception("The service returned null or a none string"); } //try to decode the json from the service $json_returned = $serviceReturn; $json = json_decode($serviceReturn); //Check that there is valid JSON if (!$json) { throw new \Exception("The service returned a none json string"); } if (!property_exists($json, "error")) { throw new \Exception("The service returned JSON but it did not contain the property 'error'"); } if ($json->error->code != "0") { throw new \Exception("The service returned an error: " . $json->error->message); } } catch (\Exception $e) { $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [An exception was thrown]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [{$e}]", \PEAR_LOG_ERR); //Output the Returned value $logger->log("Value returned from service call:" . $json_returned, \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); } } else { // Not a trusted content item $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Content item not pushed, score < 9, score is:" . $item->source->score . "]", \PEAR_LOG_DEBUG); } } $logger->log("Swiftriver::EventHandlers::UshahidiPassivePushEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); }
/** * Given a collection of content items, this method will use the Enterprise version * of the Google Translation Services to both assertain the language the content * was writen in and where possible and nessasary, translate that content to * the base language. * Each piece of content that comes into this method, can expect to be returned with this * pattern - ie: the $content->text[0] LanguageSpecificText class being in the base * language and (if applicable) the original LanguageSpecificText class begin at * $content->text[1] * * @param \Swiftriver\Core\ObjectModel\Content[] $contentItems * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration * @param \Log $logger * @return \Swiftriver\Core\ObjectModel\Content[] */ public function Process($contentItems, $configuration, $logger) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Method invoked]", \PEAR_LOG_DEBUG); //if the content is not valid, jsut return it if (!isset($contentItems) || !is_array($contentItems) || count($contentItems) < 1) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [No content supplied]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration; //Check for the existance of config for this pre processing step if (!key_exists($this->Name(), $config)) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [The Google Enterprise Language Service Turbine was called but no configuration exists for this module]", \PEAR_LOG_ERR); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } //Get the config $config = $config[$this->Name()]; //Check that all the config entries are there foreach ($this->ReturnRequiredParameters() as $requiredParam) { if (!key_exists($requiredParam->name, $config)) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [The Google Enterprise Language Service Turbine was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); return $contentItems; } } //get the api key $apikey = (string) $config["Google API Key"]->value; //get the base language for this swift instance $baseLanguageCode = $configuration->BaseLanguageCode; //create the return array $translatedContent = array(); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [START: Looping through content items]", \PEAR_LOG_DEBUG); //Loop throught the content foreach ($contentItems as $content) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [START: Running Workflow for content]", \PEAR_LOG_DEBUG); //Get the first language specific text blok $lsp = \reset($content->text); //If the language code is set and matches the base language code then skip it if (isset($lsp->languageCode) && $lsp->languageCode != null && \strtolower($lsp->languageCode) == \strtolower($baseLanguageCode)) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Content text is already in the base language]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [END: Running Workflow for content]", \PEAR_LOG_DEBUG); $translatedContent[] = $content; continue; } try { //Get the title $title = $lsp->title; //url encode the title $title = \urlencode($title); //Get the text $text = ""; //Concatenate all text foreach ($lsp->text as $t) { $text .= " {$t}"; } //Urlencode text $text = \urlencode($text); //Construct the service uri $uri = "https://www.googleapis.com/language/translate/v2?key={$apikey}&target={$baseLanguageCode}&q={$title}&q={$text}"; //create a service wrapper $serviceWrapper = new \Swiftriver\Core\Modules\SiSW\ServiceWrapper($uri); //Call the service and get back the json $json = $serviceWrapper->MakeGETRequest(); //decode the json $object = \json_decode($json); //if not translation was required if (\reset($object->data->translations)->detectedSourceLanguage == \strtolower($baseLanguageCode)) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Google Language service detected that content text is already in the base language]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [END: Running Workflow for content]", \PEAR_LOG_DEBUG); //Set the language code $lsp->languageCode = $baseLanguageCode; //Set the text block back to the content item $content->text[0] = $lsp; } else { $languagecode = \reset($object->data->translations)->detectedSourceLanguage; $title = \reset($object->data->translations)->translatedText; $text = \count($object->data->translations) > 1 ? $object->data->translations[1]->translatedText : ""; $newLsp = new \Swiftriver\Core\ObjectModel\LanguageSpecificText($baseLanguageCode, $title, array($text)); $lsp->languageCode = $languagecode; $content->text[0] = $newLsp; $content->text[1] = $lsp; } $translatedContent[] = $content; } catch (\Exception $e) { $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [{$e}]", \PEAR_LOG_ERR); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [An exception was throw, moving to the next content item]", \PEAR_LOG_DEBUG); $translatedContent[] = $content; } $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [END: Running Workflow for content]", \PEAR_LOG_DEBUG); } $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [END: Looping through content items]", \PEAR_LOG_DEBUG); $logger->log("PreProcessingSteps::GoogleEnterpriseLanguageServicePreProcessingStep::Process [Method finished]", \PEAR_LOG_DEBUG); //return the translated content return $translatedContent; }
/** * Given a GenericEvent object, this method should do * something amazing with the data contained in the * event arguments. * * @param GenericEvent $event * @param \Swiftriver\Core\Configuration\ConfigurationHandlers\CoreConfigurationHandler $configuration * @param \Log $logger */ public function HandleEvent($event, $configuration, $logger) { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method invoked]", \PEAR_LOG_DEBUG); //Get the $event->arguments as a content item $content = $event->arguments; //check that arguments property of the $event passed in is a content item if ($content == null || !\Swiftriver\Core\ObjectModel\TypeComparisons\IsContent::CheckType($content)) { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [The obejct passed in the GenericEvent->arguments property was not of type Content.]", \PEAR_LOG_DEBUG); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } //get the module configuraiton $config = \Swiftriver\Core\Setup::DynamicModuleConfiguration()->Configuration; if (!key_exists($this->Name(), $config)) { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [The Ushahidi Event Handler was called but no configuration exists for this module]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } $config = $config[$this->Name()]; foreach ($this->ReturnRequiredParameters() as $requiredParam) { if (!key_exists($requiredParam->name, $config)) { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [The Ushahidi Event Handler was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } } //extract the Url for Ushahidi $uri = (string) $config["Ushahidi Url"]->value; $uri = rtrim($uri, "/") . "/api"; //null check the uri if ($uri == null || $uri == "") { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [The Ushahidi Event Handler was called but all the required configuration properties could not be loaded]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); return; } //Instanciate the parser that will be used to parse the content item into Ushahidi format $toUshahidiParser = new \Swiftriver\UshahidiAPIInterface\ContentToUshahidiAPIParser(); //Get the ushahidi formatted params from the parser $parameters = $toUshahidiParser->ParseContentItemToUshahidiAPIFormat($content); //include the service wrapper $service = new \Swiftriver\UshahidiAPIInterface\ServiceInterface(); $json_returned = ""; try { //Call the service and get the return $serviceReturn = $service->InterafceWithService($uri, $parameters, $configuration); //null check return if ($serviceReturn == null || !is_string($serviceReturn)) { throw new \Exception("The service returned null or a none string"); } //try to decode the json from the service $json_returned = $serviceReturn; $json = json_decode($serviceReturn); //Check that there is valid JSON if (!$json) { throw new \Exception("The service returned a none json string"); } if (!property_exists($json, "success")) { throw new \Exception("The service returned JSON but it did not contain the property 'success'"); } if ($json->success === false) { throw new \Exception("The service returned a false in the success flag"); } } catch (\Exception $e) { $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [An exception was thrown]", \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [{$e}]", \PEAR_LOG_ERR); //Output the Returned value $logger->log("Value returned from service call:" . $json_returned, \PEAR_LOG_ERR); $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); } $logger->log("Swiftriver::EventHandlers::UshahidiAPIEventHandler::HandleEvent [Method finished]", \PEAR_LOG_DEBUG); }