GSoC2008JAppData

From MWWiki

Jump to: navigation, search

Back to GSoC 2008

<?php
 
/**
 * Description of the script here.
 *
 * Created on Jul 22, 2008
 *
 * @package
 * @author Shannon Quinn
 * @version 0.1
 */
 
class JoomlaDataService extends AppDataService {
 
  /**
   * Fetch data for a list of ids.
   * 
   * @access public
   * @param UserId The user id to perform the action for
   * @param GroupId optional grouping ID
   * @param fields The list of fields to fetch
   * @param token The SecurityToken for this request
   * @return ResponseItem a response item with the error code set if
   *     there was a problem
   */
  public function getPersonData(UserId $userId, 
                                GroupId $groupId, 
                                $fields, 
                                $appId, 
                                SecurityToken $token) {
    return null;
  }
 
  /**
   * Delete someone's information.
   * 
   * @access public
   * @param UserId $userId
   * @param GroupId $groupId
   * @param unknown_type $fields
   * @param unknown_type $appId
   * @param SecurityToken $token
   * @return ResponseItem 
   */
  public function deletePersonData(UserId $userId, 
                                   GroupId $groupId, 
                                   $fields, 
                                   $appId, 
                                   SecurityToken $token) {
    // first, rifle through all the keys to make sure they contain valid characters
    foreach ($fields as $key) {
      if (!JoomlaAppDataService::isValidkey($key)) {
        return new ResponseItem(BAD_REQUEST, "Invalid characters found in " .
                                "Person app data", null);
      }
    }
 
    // next, who are we deleting?
    switch ($groupId->getType()) {
      case 'self':
        // TODO - Access the database and remove the user as identified by $key and $userId->getUserId($token)
        //        furthermore, check out get()->deleteAppData()
 
        break;
      default:
        return new ResponseItem(NOT_IMPLEMENTED, "Deleting items in batches is currently unsupported", null);
        break;
    }
 
    // return a blank response item - no error codes sent
    return new ResponseItem(null, null, array());
  }
 
 
  /**
   * Updates the data key for the given person with the new value.
   * 
   * The pattern of operations here is pretty much identical to deleting a 
   * user, except instead of wiping the user's fields clean, we modify them.
   *
   * @access public
   * @param id The person the data is for.
   * @param key The key of the data.
   * @param value The new value of the data.
   * @param token The SecurityToken for this request
   * @return ResponseItem a response item with the error code set if
   *     there was a problem
   */
  public function updatePersonData(UserID $userId, 
                                   GroupId $groupId, 
                                   $fields, 
                                   $values, 
                                   $appId, 
                                   SecurityToken $token) {
    // first, validate all the keys
    foreach($fields as $key) {
      if (!JoomlaAppDataService::isValidkey($key)) {
        return new ResponseItem(BAD_REQUEST, "Invalid characters found in " .
                                "Person app data", null);
      }
    }
 
    // now, which user are we modifying?
    switch ($groupId->getType()) {
      case 'self':
        // TODO - Access database and update user's information, accessed in an identical
        //        fashion to delete().  Furthermore, check out get()->setAppData()
        break;
      default:
        return new ResponseItem(NOT_IMPLEMENTED, "Updating items in batches is currently unsupported", null);
        break;
    }
  }
 
  /**
   * Determines whether the input is a valid key. Valid keys match the regular
   * expression [\w\-\.]+.
   * 
   * The stock function was one of the most inefficient validation functions
   * I have ever come across; given that the regex was even provided by ASF, 
   * I'm not quite sure why a more streamlined method of validation wasn't
   * implemented, but nevertheless I have put it in.
   * 
   * @static
   * @access public
   * @param string $key The key to validate.
   * @return boolean True if the key is a valid appdata key, false otherwise.
   */
  public static function isValidKey($key)
  {
    // this is a nested ternary statement...woot
    return (preg_match('/[^\w\-\.]/', $key) > 0 ? false : 
                      (empty($key) ? false : true));
 
    // so...if every single character in $key matches the regular expression,
    // then check to ensure that $key isn't empty.
  }
 
}
 
?>