You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by ch...@apache.org on 2008/07/19 23:48:33 UTC

svn commit: r678227 - in /incubator/shindig/trunk/php/src/social-api: converters/InputAtomConverter.php converters/InputConverter.php converters/InputJsonConverter.php dataservice/AppDataHandler.php

Author: chabotc
Date: Sat Jul 19 14:48:32 2008
New Revision: 678227

URL: http://svn.apache.org/viewvc?rev=678227&view=rev
Log:
This commit adds support for Atom format input for creating
Activities & setting app data (ie all the write actions that
are available through javascript right now).



Modified:
    incubator/shindig/trunk/php/src/social-api/converters/InputAtomConverter.php
    incubator/shindig/trunk/php/src/social-api/converters/InputConverter.php
    incubator/shindig/trunk/php/src/social-api/converters/InputJsonConverter.php
    incubator/shindig/trunk/php/src/social-api/dataservice/AppDataHandler.php

Modified: incubator/shindig/trunk/php/src/social-api/converters/InputAtomConverter.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social-api/converters/InputAtomConverter.php?rev=678227&r1=678226&r2=678227&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social-api/converters/InputAtomConverter.php (original)
+++ incubator/shindig/trunk/php/src/social-api/converters/InputAtomConverter.php Sat Jul 19 14:48:32 2008
@@ -1,5 +1,4 @@
 <?php
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements. See the NOTICE file
@@ -21,26 +20,55 @@
 /**
  * Convert Atom representations to the internal data structure representation
  */
-class InputAtomConverter {
+class InputAtomConverter extends InputConverter {
 
 	public function convertPeople($requestParam)
 	{
-		$xml = simplexml_load_string($requestParam);
-		print_r($xml);
-		die();
+		throw new Exception("Operation not supported");
 	}
 
 	public function convertActivities($requestParam)
 	{
-		$xml = simplexml_load_string($requestParam);
-		print_r($xml);
-		die();
+		$activity = array();
+		$xml = simplexml_load_string($requestParam, 'SimpleXMLElement', LIBXML_NOCDATA);
+		if (!isset($xml->title)) {
+			throw new Exception("Mallformed activity xml");
+		}
+		// remember to either type cast to (string) or trim() the string so we don't get 
+		// SimpleXMLString types in the internal data representation. I often prefer
+		// using trim() since it cleans up the data too
+		$activity['id'] = isset($xml->id) ? trim($xml->id) : ''; 
+		$activity['title'] = trim($xml->title);
+		$activity['body'] = isset($xml->summary) ? trim($xml->summary) : ''; 
+		$activity['streamTitle'] = isset($xml->content->activity->streamTitle) ? trim($xml->content->activity->streamTitle) : ''; 
+		$activity['streamId'] = isset($xml->content->activity->streamId) ? trim($xml->content->activity->streamId) : ''; 
+		$activity['updated'] = isset($xml->updated) ? trim($xml->updated) : ''; 
+		if (isset($xml->content->activity->mediaItems)) {
+			$activity['mediaItems'] = array();
+			foreach ($xml->content->activity->mediaItems->MediaItem as $mediaItem) {
+				$item = array();
+				if (!isset($mediaItem->type) || !isset($mediaItem->mimeType) || !isset($mediaItem->url)) {
+					throw new Exception("Invalid media item in activity xml");
+				}
+				$item['type'] = trim($mediaItem->type);
+				$item['mimeType'] = trim($mediaItem->mimeType);
+				$item['url'] = trim($mediaItem->url);
+				$activity['mediaItems'][] = $item;
+			}
+		}
+		return $activity;
 	}
 
 	public function convertAppData($requestParam)
 	{
-		$xml = simplexml_load_string($requestParam);
-		print_r($xml);
-		die();
+		$xml = simplexml_load_string($requestParam, 'SimpleXMLElement', LIBXML_NOCDATA);
+		if (!isset($xml->content) || !isset($xml->content->appdata)) {
+			throw new Exception("Mallformed AppData xml");
+		}
+		$data = array();
+		foreach (get_object_vars($xml->content->appdata) as $key => $val) {
+			$data[trim($key)] = trim($val);
+		}
+		return $data;
 	}
 }

Modified: incubator/shindig/trunk/php/src/social-api/converters/InputConverter.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social-api/converters/InputConverter.php?rev=678227&r1=678226&r2=678227&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social-api/converters/InputConverter.php (original)
+++ incubator/shindig/trunk/php/src/social-api/converters/InputConverter.php Sat Jul 19 14:48:32 2008
@@ -25,7 +25,7 @@
  * hoisting rules somewhere..
  */
 abstract class InputConverter {
-	abstract public function convertPeople();
-	abstract public function convertActivities();
-	abstract public function convertAppData();	
+	abstract public function convertPeople($requestParam);
+	abstract public function convertActivities($requestParam);
+	abstract public function convertAppData($requestParam);
 }
\ No newline at end of file

Modified: incubator/shindig/trunk/php/src/social-api/converters/InputJsonConverter.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social-api/converters/InputJsonConverter.php?rev=678227&r1=678226&r2=678227&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social-api/converters/InputJsonConverter.php (original)
+++ incubator/shindig/trunk/php/src/social-api/converters/InputJsonConverter.php Sat Jul 19 14:48:32 2008
@@ -20,25 +20,37 @@
 /**
  * Convert json representations to the internal data structure representation
  */
-class InputJsonConverter {
+class InputJsonConverter extends InputConverter {
 
 	public function convertPeople($requestParam)
 	{
-		return json_decode($requestParam, true);
+		throw new Exception("Opperation not supported");
 	}
 
 	public function convertActivities($requestParam)
 	{
-		return json_decode($requestParam, true);
+		$ret = json_decode($requestParam, true);
+		if ($ret == $requestParam) {
+			throw new Exception("Mallformed activity json string");
+		}
+		return $ret;
 	}
 
 	public function convertAppData($requestParam)
 	{
-		return json_decode($requestParam, true);
+		$ret = json_decode($requestParam, true);
+		if ($ret == $requestParam) {
+			throw new Exception("Mallformed app data json string");
+		}
+		return $ret;
 	}
 
 	public function convertJsonBatch($requestParam)
 	{
-		return json_decode($requestParam, true);
+		$ret = json_decode($requestParam, true);
+		if ($ret == $requestParam) {
+			throw new Exception("Mallformed json batch string");
+		}
+		return $ret;
 	}
 }

Modified: incubator/shindig/trunk/php/src/social-api/dataservice/AppDataHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social-api/dataservice/AppDataHandler.php?rev=678227&r1=678226&r2=678227&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social-api/dataservice/AppDataHandler.php (original)
+++ incubator/shindig/trunk/php/src/social-api/dataservice/AppDataHandler.php Sat Jul 19 14:48:32 2008
@@ -35,9 +35,6 @@
 	 * /appdata/john.doe/@friends/app?fields=count
 	 * /appdata/john.doe/@self/app
 	 *
-	 * The post data should be a regular json object. All of the fields vars will
-	 * be pulled from the values and set on the person object. If there are no
-	 * fields vars then all of the data will be overridden.
 	 */
 	public function handleDelete(RestRequestItem $requestItem)
 	{
@@ -74,7 +71,15 @@
 	public function handlePost(RestRequestItem $requestItem)
 	{
 		$requestItem->parseUrlWithTemplate(self::$APP_DATA_PATH);
-		return $this->service->updatePersonData($requestItem->getUser(), $requestItem->getGroup(), $requestItem->getFields(), $requestItem->getPostData(), $requestItem->getAppId(), $requestItem->getToken());
+		// if no ?fields=foo,bar was specified, we try to guess them from the post data
+		$postFields = array();
+		if ($requestItem->getPostData() != null) {
+			$data = $requestItem->getPostData();
+			foreach ($data as $key => $val) {
+				$postFields[] = $key;
+			}
+		}
+		return $this->service->updatePersonData($requestItem->getUser(), $requestItem->getGroup(), $requestItem->getFieldsWithDefaultValue($postFields), $requestItem->getPostData(), $requestItem->getAppId(), $requestItem->getToken());
 	}
 
 	/**