public function testDeepCloneTriplesItemContent() { // create a file / put it in item content property /** @var \core_kernel_versioning_Repository $repository */ $repository = \tao_models_classes_FileSourceService::singleton()->addLocalSource("repository test", \tao_helpers_File::createTempDir()); //see if clone item content works /** @var \core_kernel_versioning_File $file */ $file = $repository->createFile("test.xml", "test"); mkdir($repository->getPath() . 'test'); copy(__DIR__ . '/sample/test.xml', $repository->getPath() . 'test/test.xml'); copy(__DIR__ . '/sample/style.css', $repository->getPath() . 'test/style.css'); $rdfsTriple = new \core_kernel_classes_Triple(); $rdfsTriple->predicate = "http://www.tao.lu/Ontologies/TAOItem.rdf#ItemContent"; $rdfsTriple->object = $file->getUri(); $fileNameProp = new \core_kernel_classes_Property(PROPERTY_FILE_FILENAME); $return = CloneHelper::deepCloneTriples(array($rdfsTriple)); $this->assertNotEquals($rdfsTriple->object, $return[0]->object); $this->assertEquals($rdfsTriple->predicate, $return[0]->predicate); $this->assertCount(1, $return); $returnedFile = new \core_kernel_versioning_File($return[0]->object); $this->assertEquals($returnedFile->getPropertyValues($fileNameProp), $file->getPropertyValues($fileNameProp)); $this->assertNotEquals($file->getAbsolutePath(), $returnedFile->getAbsolutePath()); $files = scandir(dirname($returnedFile->getAbsolutePath())); $this->assertContains('test.xml', $files); $this->assertContains('style.css', $files); $file->delete(true); $returnedFile->delete(true); $repository->delete(true); }
protected static function cloneFile($fileUri) { \common_Logger::i('clone file ' . $fileUri); $file = new \core_kernel_versioning_File($fileUri); $newFile = $file->getRepository()->spawnFile($file->getAbsolutePath(), $file->getLabel(), function ($originalName) { return md5($originalName); }); return $newFile->getUri(); }
protected static function deleteDependencies(\core_kernel_classes_Triple $triple) { if ($triple->predicate == 'http://www.tao.lu/Ontologies/TAOItem.rdf#ItemContent') { $file = new \core_kernel_versioning_File($triple->object); if ($file->exists()) { $sourceDir = dirname($file->getAbsolutePath()); $file->delete(); \tao_helpers_File::delTree($sourceDir); } } elseif (CloneHelper::isFileReference($triple)) { $file = new \core_kernel_versioning_File($triple->object); if ($file->exists()) { $file->delete(); } } }
/** * Short description of method getVersionedFile * * @access public * @author Cédric Alfonsi, <*****@*****.**> * @param string rowId * @param string columnId * @param string data * @return core_kernel_classes_Resource */ public function getVersionedFile($rowId, $columnId, $data = null) { $returnValue = null; if (empty($data)) { throw new Exception('data can not be empty'); } if (!empty($data) && common_Utils::isUri($data)) { $data = new core_kernel_classes_Resource($data); } if (!core_kernel_versioning_File::isVersionedFile($data)) { throw new Exception('data has to be a valid versioned file uri'); } $returnValue = $data; return $returnValue; }
/** * Creates a new file with a specific name and path * * @param string $filename * @param string $relativeFilePath * @return core_kernel_versioning_File */ public function createFile($filename, $relativeFilePath = '') { $resource = core_kernel_classes_ResourceFactory::create(new core_kernel_classes_Class(CLASS_GENERIS_FILE)); $returnValue = new core_kernel_versioning_File($resource); $returnValue->setPropertiesValues(array(PROPERTY_FILE_FILENAME => $filename, PROPERTY_FILE_FILEPATH => trim($relativeFilePath, DIRECTORY_SEPARATOR), PROPERTY_FILE_FILESYSTEM => $this)); return $returnValue; }
/** * Download a resource file content * @param {String} uri Uri of the resource file */ public function downloadFile() { if ($this->hasRequestParameter('uri')) { $uri = tao_helpers_Uri::decode($this->getRequestParameter('uri')); $resource = new core_kernel_classes_Resource($uri); if (core_kernel_versioning_File::isVersionedFile($resource) || core_kernel_file_File::isFile($resource)) { $file = new core_kernel_file_File($uri); $fileName = $file->getOnePropertyValue(new core_kernel_classes_Property(PROPERTY_FILE_FILENAME)); $content = $file->getFileContent(); $size = strlen($content); $mimeType = tao_helpers_File::getMimeType($file->getAbsolutePath(), true); $this->setContentHeader($mimeType); header("Content-Length: {$size}"); header("Content-Disposition: attachment; filename=\"{$fileName}\""); header("Expires: 0"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); print $content; return; } else { throw new Exception('The resource (' . $uri . ') is not a valid file resource'); } } }
/** * Binds an UploadFileDescription with the target instance. * * @access protected * @author Jerome Bogaerts <*****@*****.**> * @param Property property The property to bind the data. * @param UploadFileDescription desc the upload file description. * @return void */ protected function bindUploadFileDescription(core_kernel_classes_Property $property, tao_helpers_form_data_UploadFileDescription $desc) { $instance = $this->getTargetInstance(); // Delete old files. foreach ($instance->getPropertyValues($property) as $oF) { $oldFile = new core_kernel_versioning_File($oF); $oldFile->delete(true); } $name = $desc->getName(); $size = $desc->getSize(); if (!empty($name) && !empty($size)) { // Move the file at the right place. $source = $desc->getTmpPath(); $repository = tao_models_classes_TaoService::singleton()->getUploadFileSource(); $file = $repository->spawnFile($source, $desc->getName()); tao_helpers_File::remove($source); $instance->setPropertyValue($property, $file->getUri()); // Update the UploadFileDescription with the stored file. $desc->setFile($file); } }
/** * * @author Lionel Lecaque, lionel@taotesting.com * @param core_kernel_classes_Resource $source * @param core_kernel_classes_Resource $destination * @param core_kernel_classes_Property $property */ protected function cloneInstanceProperty(core_kernel_classes_Resource $source, core_kernel_classes_Resource $destination, core_kernel_classes_Property $property) { $range = $property->getRange(); // Avoid doublons, the RDF TYPE property will be set by the implementation layer if ($property->getUri() != RDF_TYPE) { foreach ($source->getPropertyValuesCollection($property)->getIterator() as $propertyValue) { if (!is_null($range) && $range->getUri() == CLASS_GENERIS_FILE) { $file = new core_kernel_versioning_File($propertyValue->getUri()); $newFile = $file->getRepository()->spawnFile($file->getAbsolutePath(), $file->getLabel()); $destination->setPropertyValue($property, $newFile); } else { $destination->setPropertyValue($property, $propertyValue); } } } }
/** * Override the validate method of the form container to validate * linked elements * * @access public * @author Somsack Sipasseuth, <*****@*****.**> * @return boolean */ public function validate() { $returnValue = (bool) false; if ($this->form->isSubmited()) { if ($this->versionedFile->isVersioned()) { return true; } $fileNameElt = $this->form->getElement(tao_helpers_Uri::encode(PROPERTY_FILE_FILENAME)); $fileName = !is_null($fileNameElt) ? $fileNameElt->getValue() : ''; $filePathElt = $this->form->getElement(tao_helpers_Uri::encode(PROPERTY_FILE_FILEPATH)); $filePath = $filePathElt->getValue(); $fileRepositoryElt = $this->form->getElement(tao_helpers_Uri::encode(PROPERTY_FILE_FILESYSTEM)); $fileRepository = tao_helpers_Uri::decode($fileRepositoryElt->getValue()); //check if a resource with the same path exists yet in the repository $clazz = new core_kernel_classes_Class(CLASS_GENERIS_FILE); $options = array('like' => false, 'recursive' => true); $propertyFilter = array(PROPERTY_FILE_FILENAME => $fileName, PROPERTY_FILE_FILEPATH => $filePath, PROPERTY_FILE_FILESYSTEM => $fileRepository); $sameNameFiles = $clazz->searchInstances($propertyFilter, $options); if (!empty($sameNameFiles)) { $sameFileResource = array_pop($sameNameFiles); $sameFile = new core_kernel_versioning_File($sameFileResource->getUri()); $this->form->valid = false; $this->form->error = __('A similar resource has already been versioned') . ' (' . $sameFile->getAbsolutePath() . ')'; } } return (bool) $returnValue; }
/** * Deletes the content but does not unreference it * * @access public * @author Joel Bout, <*****@*****.**> * @param core_kernel_classes_Resource item * @return boolean */ public function deleteItemContent(core_kernel_classes_Resource $item) { $returnValue = (bool) false; //delete the folder for all languages! foreach ($item->getUsedLanguages($this->itemContentProperty) as $lang) { $files = $item->getPropertyValuesByLg($this->itemContentProperty, $lang); foreach ($files->getIterator() as $file) { if ($file instanceof core_kernel_classes_Resource) { $file = new core_kernel_file_File($file); if (core_kernel_versioning_File::isVersionedFile($file)) { $file = new core_kernel_versioning_File($file); } try { $file->delete(); } catch (core_kernel_versioning_exception_FileUnversionedException $e) { // file was not versioned after all, ignore in delte } } } } $returnValue = true; return (bool) $returnValue; }
private function getItemFile(core_kernel_classes_Resource $item, $type, $countryCode, $langCode, core_kernel_classes_Resource $user = null) { $returnValue = null; if (!isset($this->properties[TranslationProcessHelper::getPropertyName($type, $countryCode, $langCode)])) { $this->fail("The item property does not exist for the item {$item->getLabel()} ({$item->getUri()}) : {$type}, {$countryCode}, {$langCode} "); return $returnValue; } $file = null; if (in_array(strtolower($type), array('xliff_working', 'vff_working'))) { if (is_null($user)) { $this->fail('no user given'); return $returnValue; } $values = $item->getPropertyValues($this->properties[TranslationProcessHelper::getPropertyName($type, $countryCode, $langCode)]); foreach ($values as $uri) { if (common_Utils::isUri($uri)) { $aFile = new core_kernel_versioning_File($uri); $assignedUser = $aFile->getUniquePropertyValue($this->userProperty); if ($assignedUser->getUri() == $user->getUri()) { $file = $aFile; break; } } } } else { $values = $item->getPropertyValues($this->properties[TranslationProcessHelper::getPropertyName($type, $countryCode, $langCode)]); $this->assertEqual(count($values), 1); $file = new core_kernel_versioning_File(reset($values)); } if (!is_null($file) && $file->isVersioned()) { $returnValue = $file; } else { $this->fail("Cannot get the versioned {$type} file in {$countryCode}_{$langCode} for the item {$item->getLabel()} ({$item->getUri()})"); } return $returnValue; }
/** * Short description of method getValue * * @access public * @author Joel Bout, <*****@*****.**> * @param string rowId * @param string columnId * @param string data * @return mixed */ public function getValue($rowId, $columnId, $data = null) { $returnValue = null; if (isset($this->data[$rowId])) { if (isset($this->data[$rowId][$columnId])) { $returnValue = $this->data[$rowId][$columnId]; } } else { if (common_Utils::isUri($rowId)) { $this->data[$rowId] = array(); $activityExecution = new core_kernel_classes_Resource($rowId); $activityExecutionService = wfEngine_models_classes_ActivityExecutionService::singleton(); $processVariableService = wfEngine_models_classes_VariableService::singleton(); $unit = $processVariableService->get('unitUri', $activityExecution); $countryCode = (string) $processVariableService->get('countryCode', $activityExecution); $languageCode = (string) $processVariableService->get('languageCode', $activityExecution); if ($unit instanceof core_kernel_classes_Resource && !empty($countryCode) && !empty($languageCode)) { $activity = $activityExecutionService->getExecutionOf($activityExecution); //check if it is the translation activity or not: $xliff = null; $vff = null; if ($activity->getLabel() == 'Translate') { $xliffWorkingProperty = $this->getTranslationFileProperty('xliff_working', $countryCode, $languageCode); if (!is_null($xliffWorkingProperty)) { $xliff = $unit->getOnePropertyValue($xliffWorkingProperty); } $vffWorkingProperty = $this->getTranslationFileProperty('vff_working', $countryCode, $languageCode); if (!is_null($vffWorkingProperty)) { $vff = $unit->getOnePropertyValue($vffWorkingProperty); } } else { $xliffProperty = $this->getTranslationFileProperty('xliff', $countryCode, $languageCode); if (!is_null($xliffProperty)) { $xliff = $unit->getOnePropertyValue($xliffProperty); } $vffProperty = $this->getTranslationFileProperty('vff', $countryCode, $languageCode); if (!is_null($vffProperty)) { $vff = $unit->getOnePropertyValue($vffProperty); } } if ($xliff instanceof core_kernel_classes_Resource) { $xliff = new core_kernel_versioning_File($xliff); $this->data[$rowId]['xliff'] = $xliff->getUri(); $this->data[$rowId]['xliff_version'] = (string) $processVariableService->get('xliff', $activityExecution); } else { $this->data[$rowId]['xliff'] = 'n/a'; $this->data[$rowId]['xliff_version'] = 'n/a'; } if ($vff instanceof core_kernel_classes_Resource) { $vff = new core_kernel_versioning_File($vff); $this->data[$rowId]['vff'] = $vff->getUri(); $this->data[$rowId]['vff_version'] = (string) $processVariableService->get('vff', $activityExecution); } else { $this->data[$rowId]['vff'] = 'n/a'; $this->data[$rowId]['vff_version'] = 'n/a'; } } else { $this->data[$rowId] = array('xliff' => 'n/a', 'xliff_version' => 'n/a', 'vff' => 'n/a', 'vff_version' => 'n/a'); } if (isset($this->data[$rowId][$columnId])) { $returnValue = $this->data[$rowId][$columnId]; } } } return $returnValue; }
/** * Short description of method setItemContent * * @deprecated use \oat\taoQtiItem\model\qti\Service::saveXmlItemToRdfItem instead * * @access public * @author Joel Bout, <*****@*****.**> * @param core_kernel_classes_Resource $item * @param string $content * @param string $lang * @param string $commitMessage * @return bool * @throws common_Exception * @throws common_exception_Error */ public function setItemContent(core_kernel_classes_Resource $item, $content, $lang = '', $commitMessage = '') { $returnValue = false; if (is_null($item) && !$this->isItemModelDefined($item)) { throw new common_exception_Error('No item or itemmodel in ' . __FUNCTION__); } $lang = empty($lang) ? $lang = $this->getSessionLg() : $lang; $itemModel = $item->getUniquePropertyValue($this->itemModelProperty); $dataFile = (string) $itemModel->getOnePropertyValue(new core_kernel_classes_Property(TAO_ITEM_MODEL_DATAFILE_PROPERTY)); if ($this->hasItemContent($item, $lang)) { $itemContents = $item->getPropertyValuesByLg($this->itemContentProperty, $lang); $itemContent = $itemContents->get(0); if (!core_kernel_file_File::isFile($itemContent)) { throw new common_Exception('Item ' . $item->getUri() . ' has none file itemContent'); } $file = new core_kernel_versioning_File($itemContent); $returnValue = $file->setContent($content); } else { $repository = $this->getDefaultFileSource(); $file = $repository->createFile($dataFile, tao_helpers_Uri::getUniqueId($item->getUri()) . DIRECTORY_SEPARATOR . 'itemContent' . DIRECTORY_SEPARATOR . $lang); $item->setPropertyValueByLg($this->itemContentProperty, $file->getUri(), $lang); $returnValue = $file->setContent($content); } if ($returnValue) { $this->getEventManager()->trigger(new ItemUpdatedEvent($item->getUri())); } return (bool) $returnValue; }