/** * @param boolean $public * @return tao_models_classes_service_StorageDirectory */ public function spawnDirectory($public = false) { $id = common_Utils::getNewUri() . ($public ? '+' : '-'); $directory = $this->getDirectoryById($id); mkdir($directory->getPath(), 0700, true); return $directory; }
/** * SyncTask constructor. * @param Action|string $invocable * @param array $params */ public function __construct($invocable, $params) { $this->id = \common_Utils::getNewUri(); $this->invocable = $invocable; $this->setParameters($params); $this->setStatus(self::STATUS_CREATED); }
/** * * @param unknown $userId * @param core_kernel_classes_Resource $assembly * @return taoDelivery_models_classes_execution_KVDeliveryExecution */ public static function spawn(common_persistence_KeyValuePersistence $persistence, $userId, core_kernel_classes_Resource $assembly) { $identifier = self::DELIVERY_EXECUTION_PREFIX . common_Utils::getNewUri(); $de = new self($persistence, $identifier, array(RDFS_LABEL => $assembly->getLabel(), PROPERTY_DELVIERYEXECUTION_DELIVERY => $assembly->getUri(), PROPERTY_DELVIERYEXECUTION_SUBJECT => $userId, PROPERTY_DELVIERYEXECUTION_START => time(), PROPERTY_DELVIERYEXECUTION_STATUS => INSTANCE_DELIVERYEXEC_ACTIVE)); $de->save(); return $de; }
/** * * @param common_persistence_KeyValuePersistence $persistence * @param unknown $userId * @param core_kernel_classes_Resource $assembly * @return DeliveryExecution */ public static function spawn(common_persistence_KeyValuePersistence $persistence, $userId, core_kernel_classes_Resource $assembly) { $identifier = self::DELIVERY_EXECUTION_PREFIX . common_Utils::getNewUri(); $params = array(RDFS_LABEL => $assembly->getLabel(), PROPERTY_DELVIERYEXECUTION_DELIVERY => $assembly->getUri(), PROPERTY_DELVIERYEXECUTION_SUBJECT => $userId, PROPERTY_DELVIERYEXECUTION_START => microtime(), PROPERTY_DELVIERYEXECUTION_STATUS => InterfaceDeliveryExecution::STATE_ACTIVE); $kvDe = new static($persistence, $identifier, $params); $kvDe->save(); $de = new DeliveryExecution($kvDe); return $de; }
/** * @param $action * @param $parameters * @param boolean $repeatedly Whether task created repeatedly (for example when execution of task was failed and task puts to the queue again). * @return JsonTask * @throws \common_exception_Error */ public function createTask($action, $parameters, $repeatedly = false) { $task = new JsonTask($action, $parameters); $platform = $this->getPersistence()->getPlatForm(); $query = 'INSERT INTO ' . self::QUEUE_TABLE_NAME . ' (' . self::QUEUE_ID . ', ' . self::QUEUE_OWNER . ', ' . self::QUEUE_TASK . ', ' . self::QUEUE_STATUS . ', ' . self::QUEUE_ADDED . ', ' . self::QUEUE_UPDATED . ') ' . 'VALUES (?, ?, ?, ?, ?, ?)'; $persistence = $this->getPersistence(); $id = \common_Utils::getNewUri(); $persistence->exec($query, array($id, \common_session_SessionManager::getSession()->getUser()->getIdentifier(), json_encode($task), Task::STATUS_CREATED, $platform->getNowExpression(), $platform->getNowExpression())); $task->setId($id); return $task; }
/** * * @param core_kernel_classes_Resource $test * @param array $itemUris * @return boolean */ public function save(core_kernel_classes_Resource $test, array $content) { $serializer = $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID); $serial = $test->getOnePropertyValue($this->getProperty(TEST_TESTCONTENT_PROP)); if (!is_null($serial)) { $directory = $serializer->unserializeDirectory($serial); } else { // null so create one $fss = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); $base = $fss->getDirectory($this->getOption(self::OPTION_FILESYSTEM)); $directory = $base->getDirectory(\tao_helpers_Uri::getUniqueId(\common_Utils::getNewUri())); $test->editPropertyValues($this->getProperty(TEST_TESTCONTENT_PROP), $serializer->serialize($directory)); } return $directory->getFile('content.json')->put(json_encode($content)); }
public function setUp() { $this->login = '******'; $this->password = '******'; $kvStore = common_persistence_AdvKeyValuePersistence::getPersistence(AuthKeyValueAdapter::KEY_VALUE_PERSISTENCE_ID); $user = $kvStore->getDriver()->hGetAll($this->login); if (!$user) { $uri = \common_Utils::getNewUri(); $kvStore->getDriver()->hset($this->login, PROPERTY_USER_PASSWORD, ''); $kvStore->getDriver()->hset($this->login, 'parameters', json_encode(array("uri" => $uri, "http://www.w3.org/2000/01/rdf-schema#label" => "Test taker 1", "http://www.tao.lu/Ontologies/generis.rdf#userUILg" => "http://www.tao.lu/Ontologies/TAO.rdf#Langen-US", "http://www.tao.lu/Ontologies/generis.rdf#userDefLg" => "http://www.tao.lu/Ontologies/TAO.rdf#Langen-US", "http://www.tao.lu/Ontologies/generis.rdf#login" => $this->login, "http://www.tao.lu/Ontologies/generis.rdf#password" => core_kernel_users_Service::getPasswordHash()->encrypt($this->password), "http://www.tao.lu/Ontologies/generis.rdf#userRoles" => ["http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole"], "http://www.tao.lu/Ontologies/generis.rdf#userFirstName" => "Testtaker 1", "http://www.tao.lu/Ontologies/generis.rdf#userLastName" => "Family 047"))); } $config = array('max_size_cached_element' => 10000); $this->adapter = new AuthKeyValueAdapter($config); $this->adapter->setCredentials($this->login, $this->password); }
public static function createUser($data = array(), $lang = null) { if (!isset($data[PROPERTY_USER_LOGIN]) || !isset($data[PROPERTY_USER_PASSWORD])) { throw new \common_exception_InconsistentData('Cannot add user without login or password'); } if (is_null($lang)) { $lang = DEFAULT_LANG; } $login = $data[PROPERTY_USER_LOGIN]; $password = $data[PROPERTY_USER_PASSWORD]; $defaultData = array(RDFS_LABEL => 'Test taker', PROPERTY_USER_UILG => 'http://www.tao.lu/Ontologies/TAO.rdf#Lang' . $lang, PROPERTY_USER_DEFLG => 'http://www.tao.lu/Ontologies/TAO.rdf#Lang' . $lang, PROPERTY_USER_ROLES => array('http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole')); $data = array_merge($defaultData, $data); $data['uri'] = \common_Utils::getNewUri(); $kvStore = common_persistence_AdvKeyValuePersistence::getPersistence(AuthKeyValueAdapter::KEY_VALUE_PERSISTENCE_ID); $kvStore->hset(AuthKeyValueUserService::PREFIXES_KEY . ':' . $login, PROPERTY_USER_PASSWORD, $password); $kvStore->hset(AuthKeyValueUserService::PREFIXES_KEY . ':' . $login, 'parameters', json_encode($data)); return $data; }
/** * Imports the rdf file into the selected class * * @param string $file * @param core_kernel_classes_Class $class * @return common_report_Report */ private function flatImport($file, core_kernel_classes_Class $class) { $report = common_report_Report::createSuccess(__('Data imported successfully')); $graph = new EasyRdf_Graph(); $graph->parseFile($file); // keep type property $map = array(RDF_PROPERTY => RDF_PROPERTY); foreach ($graph->resources() as $resource) { $map[$resource->getUri()] = common_Utils::getNewUri(); } $format = EasyRdf_Format::getFormat('php'); $data = $graph->serialise($format); foreach ($data as $subjectUri => $propertiesValues) { $resource = new core_kernel_classes_Resource($map[$subjectUri]); $subreport = $this->importProperties($resource, $propertiesValues, $map, $class); $report->add($subreport); } return $report; }
/** * Short description of method checkProvidedUri * * @access private * @author Jerome Bogaerts, <*****@*****.**> * @param string uri * @return string */ private static function checkProvidedUri($uri) { $returnValue = (string) ''; if ($uri != '') { if (common_Utils::isUri($uri)) { $returnValue = $uri; } else { throw new common_Exception("Could not create new Resource, malformed URI provided: '" . $uri . "'."); } } else { $returnValue = common_Utils::getNewUri(); } return (string) $returnValue; }
public function testGetNewUri() { $toto = common_Utils::getNewUri(); $tata = common_Utils::getNewUri(); $this->assertNotSame($toto, $tata); }
/** * Short description of method duplicate * * @access public * @author Joel Bout, <*****@*****.**> * @param Resource resource * @param array excludedProperties * @return core_kernel_classes_Resource */ public function duplicate(core_kernel_classes_Resource $resource, $excludedProperties = array()) { $returnValue = null; $newUri = common_Utils::getNewUri(); $collection = $this->getRdfTriples($resource); if ($collection->count() > 0) { $platform = $this->getPersistence()->getPlatForm(); $multipleInsertQueryHelper = $platform->getMultipleInsertsSqlQueryHelper(); $user = common_session_SessionManager::isAnonymous() ? $platform->getNullString() : $this->getPersistence()->quote(\common_session_SessionManager::getSession()->getUser()->getIdentifier()); $columns = array("modelid", "subject", "predicate", "object", "l_language", "author", "epoch"); $query = $multipleInsertQueryHelper->getFirstStaticPart('statements', $columns); foreach ($collection->getIterator() as $triple) { if (!in_array($triple->predicate, $excludedProperties)) { $query .= $multipleInsertQueryHelper->getValuePart('statements', $columns, array("modelid" => $this->getNewTripleModelId(), "subject" => $this->getPersistence()->quote($newUri), "predicate" => $this->getPersistence()->quote($triple->predicate), "object" => $triple->object == null ? $platform->getNullString() : $this->getPersistence()->quote($triple->object), "l_language" => $triple->lg == null ? $platform->getNullString() : $this->getPersistence()->quote($triple->lg), "author" => $user, "epoch" => $this->getPersistence()->quote($platform->getNowExpression()))); } } $query = substr($query, 0, strlen($query) - 1); $query .= $multipleInsertQueryHelper->getEndStaticPart(); if ($this->getPersistence()->exec($query)) { $returnValue = new core_kernel_classes_Resource($newUri); } } return $returnValue; }
/** * Short description of method duplicate * * @access public * @author Joel Bout, <*****@*****.**> * @param Resource resource * @param array excludedProperties * @return \core_kernel_classes_Resource */ public function duplicate(\core_kernel_classes_Resource $resource, $excludedProperties = array()) { $returnValue = null; $referencer = ResourceReferencer::singleton(); $tableName = $referencer->resourceLocation($resource); if (empty($tableName)) { return $returnValue; } //the new Uri $newUri = \common_Utils::getNewUri(); $dbWrapper = \core_kernel_classes_DbWrapper::singleton(); //duplicate the row in the main table $query = 'SELECT * FROM "' . $tableName . '" WHERE "uri" = ?'; $result = $dbWrapper->query($query, array($resource->getUri())); $rows = $result->fetchAll(); if (count($rows) > 0) { //get the columns to duplicate $columnProps = array(); for ($i = 0; $i < $result->columnCount(); $i++) { $column = $result->getColumnMeta($i); if (preg_match("/^[0-9]{2,}/", $column['name'])) { $propertyUri = HardapiUtils::getLongName($column['name']); if (!in_array($propertyUri, $excludedProperties)) { //check if the property is excluded $columnProps[$propertyUri] = $column['name']; } } } // Fetch the first result. $instanceId = $rows[0]['id']; //build the insert query $insertQuery = 'INSERT INTO "' . $tableName . '" ("uri"'; foreach ($columnProps as $column) { if (!is_string($column)) { throw new Exception('columns should be a string'); } $insertQuery .= ', "' . $column . '"'; } $insertQuery .= ') VALUES ('; $insertQuery .= "'{$newUri}'"; foreach ($columnProps as $column) { $insertQuery .= ", '" . $rows[0][$column] . "'"; } $insertQuery .= ')'; $insertResult = $dbWrapper->exec($insertQuery); if ($insertResult !== false && $instanceId > -1) { //duplicated data $duplicatedResource = new \core_kernel_classes_Resource($newUri); $referencer->referenceResource($duplicatedResource, $tableName, $resource->getTypes(), true); $duplicateInstanceId = Utils::getInstanceId($duplicatedResource); //now we duplciate the rows of the Props table //linearize the excluded properties $excludedPropertyList = ''; foreach ($excludedProperties as $excludedProperty) { $excludedPropertyList .= "'{$excludedProperty}',"; } $excludedPropertyList = substr($excludedPropertyList, 0, strlen($excludedPropertyList) - 1); //query templates of the 3 ways to insert the props rows $insertPropValueQuery = 'INSERT INTO "' . $tableName . 'props" ("property_uri", "property_value", "l_language", "instance_id") VALUES (?,?,?,?)'; $insertPropForeignQuery = 'INSERT INTO "' . $tableName . 'props" ("property_uri", "property_foreign_uri", "l_language", "instance_id") VALUES (?,?,?,?)'; $insertPropEmptyQuery = 'INSERT INTO "' . $tableName . 'props" ("property_uri", "l_language", "instance_id") VALUES (?,?,?)'; //get the rows to duplicate try { $propsQuery = 'SELECT * FROM "' . $tableName . 'props" WHERE "instance_id" = ? '; $propsQuery .= empty($excludedPropertyList) ? '' : ' AND "property_uri" NOT IN (' . $excludedPropertyList . ') '; $propsResult = $dbWrapper->query($propsQuery, array($instanceId)); } catch (\PDOException $e) { throw new Exception("Unable to duplicate the resource {$resource->getUri()} : " . $e->getMessage()); } while ($row = $propsResult->fetch()) { $propUri = $row['property_uri']; $propValue = $row['property_value']; $propForeign = $row['property_foreign_uri']; $proplang = $row['l_language']; //insert them regarding the populated columns if (!is_null($propValue) && !empty($propValue)) { $dbWrapper->exec($insertPropValueQuery, array($propUri, $propValue, $proplang, $duplicateInstanceId)); } else { if (!is_null($propForeign) && !empty($propForeign)) { $dbWrapper->exec($insertPropForeignQuery, array($propUri, $propForeign, $proplang, $duplicateInstanceId)); } else { $dbWrapper->exec($insertPropEmptyQuery, array($propUri, $proplang, $duplicateInstanceId)); //costly to insert NULL values } } } //return the duplciated resource $returnValue = $duplicatedResource; } } return $returnValue; }
public function __construct() { $this->uri = \common_Utils::getNewUri(); }
/** * (non-PHPdoc) * @see core_kernel_persistence_ClassInterface::createInstanceWithProperties() */ public function createInstanceWithProperties(\core_kernel_classes_Class $type, $properties) { $returnValue = null; if (isset($properties[RDF_TYPE])) { throw new \core_kernel_persistence_Exception('Additional types in createInstanceWithProperties not permited'); } $uri = \common_Utils::getNewUri(); $table = '_' . HardapiUtils::getShortName($type); // prepare properties $hardPropertyNames = array("uri" => $uri); $dbWrapper = \core_kernel_classes_DbWrapper::singleton(); if (is_array($properties)) { if (count($properties) > 0) { // Get the table name $referencer = ResourceReferencer::singleton(); $queryProps = array(); foreach ($properties as $propertyUri => $value) { $property = new \core_kernel_classes_Property($propertyUri); $propertyLocation = $referencer->propertyLocation($property); if (in_array("{$table}props", $propertyLocation) || !$referencer->isPropertyReferenced($property)) { $propertyRange = $property->getRange(); $lang = $property->isLgDependent() ? \common_session_SessionManager::getSession()->getDataLanguage() : ''; $formatedValues = array(); if ($value instanceof \core_kernel_classes_Resource) { $formatedValues[] = $dbWrapper->quote($value->getUri()); } else { if (is_array($value)) { foreach ($value as $val) { if ($val instanceof \core_kernel_classes_Resource) { $formatedValues[] = $dbWrapper->quote($val->getUri()); } else { $formatedValues[] = $dbWrapper->quote($val); } } } else { $formatedValues[] = $dbWrapper->quote($value); } } if (is_null($propertyRange) || $propertyRange->getUri() == RDFS_LITERAL) { foreach ($formatedValues as $formatedValue) { $queryProps[] = "'{$property->getUri()}', {$formatedValue}, null, '{$lang}'"; } } else { foreach ($formatedValues as $formatedValue) { $queryProps[] = "'{$property->getUri()}', null, {$formatedValue}, '{$lang}'"; } } } else { $propertyName = HardapiUtils::getShortName($property); if (is_array($value)) { if (count($value) > 1) { throw new Exception("try setting multivalue for the non multiple property {$property->getLabel()} ({$property->getUri()})"); } else { $value = count($value) == 0 ? null : reset($value); // take the only element } } if ($value instanceof \core_kernel_classes_Resource) { $value = $value->getUri(); } $hardPropertyNames[$propertyName] = $value; } } } } // spawn $returnValue = new \core_kernel_classes_Resource($uri, __METHOD__); $varnames = '"' . implode('","', array_keys($hardPropertyNames)) . '"'; try { $query = 'INSERT INTO "' . $table . '" (' . $varnames . ') VALUES (' . implode(',', array_fill(0, count($hardPropertyNames), '?')) . ')'; $result = $dbWrapper->exec($query, array_values($hardPropertyNames)); // reference the newly created instance ResourceReferencer::singleton()->referenceResource($returnValue, $table, array($type), true); // @todo this shoould be retrievable without an aditional query $instanceId = Utils::getInstanceId($returnValue); // @todo Merge into a single query if (!empty($queryProps)) { $prefixed = array(); foreach ($queryProps as $row) { $prefixed[] = ' (' . $instanceId . ', ' . $row . ')'; } try { $query = 'INSERT INTO "' . $table . 'props" ("instance_id", "property_uri", "property_value", "property_foreign_uri", "l_language") VALUES ' . implode(',', $prefixed); $result = $dbWrapper->exec($query); } catch (\PDOException $e) { throw new Exception("Unable to set properties (multiple) Value for the instance {$returnValue->getUri()} in {$tableName} : " . $e->getMessage()); } } } catch (\PDOException $e) { throw new Exception("Unable to create instance for the class {$type->getUri()} in the table {$table} : " . $e->getMessage()); } return $returnValue; }
/** * (non-PHPdoc) * @see core_kernel_persistence_ClassInterface::createInstanceWithProperties() */ public function createInstanceWithProperties(core_kernel_classes_Class $type, $properties) { $returnValue = null; if (isset($properties[RDF_TYPE])) { throw new core_kernel_persistence_Exception('Additional types in createInstanceWithProperties not permited'); } $properties[RDF_TYPE] = $type; $returnValue = new core_kernel_classes_Resource(common_Utils::getNewUri(), __METHOD__); $returnValue->setPropertiesValues($properties); return $returnValue; }