public function testDeepCloneTriplesFile() { /** @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->spawnFile(__DIR__ . '/sample/test.xml', "test", function ($originalName) { return md5($originalName); }); //see if clone file works $rdfsTriple = new \core_kernel_classes_Triple(); $rdfsTriple->predicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#value"; $rdfsTriple->object = $file->getUri(); $return = CloneHelper::deepCloneTriples(array($rdfsTriple)); $this->assertCount(1, $return); $this->assertEquals($rdfsTriple->predicate, $return[0]->predicate); $this->assertNotEquals($rdfsTriple->object, $return[0]->object); $fileCopy = new \core_kernel_file_File($return[0]->object); $this->assertFileExists($fileCopy->getAbsolutePath()); $this->assertEquals($file->getLabel(), $fileCopy->getLabel()); $this->assertNotEquals($file->getAbsolutePath(), $fileCopy->getAbsolutePath()); $file->delete(true); $fileCopy->delete(true); $repository->delete(true); }
public function getStrings($values) { $contentStrings = array(); $xmlTokenizer = new taoItems_models_classes_search_XmlItemContentTokenizer(); foreach ($values as $valueUri) { $file = new core_kernel_file_File($valueUri); try { $content = file_get_contents($file->getAbsolutePath()); if ($content === false) { common_Logger::w('File ' . $file->getAbsolutePath() . ' not found for item'); } else { // Try to make it a DOM Document... $dom = new DOMDocument('1.0', 'UTF-8'); if (@$dom->loadXML($content) === true) { $contentStrings = array_merge($contentStrings, $xmlTokenizer->getStrings($dom)); unset($dom); } else { common_Logger::d('Skipped non XML content for ' . $file->getUri()); } } } catch (common_Exception $exc) { common_Logger::w('Invalid file ' . $valueUri . ' for ItemContentTokenizer: ' . $exc->getMessage()); } } return $contentStrings; }
public function getStrings($values) { $contentStrings = array(); $xmlTokenizer = new taoItems_models_classes_search_XmlItemContentTokenizer(); $rawTokenizer = new RawValue(); foreach ($values as $valueUri) { $file = new core_kernel_file_File($valueUri); $content = file_get_contents($file->getAbsolutePath()); if ($content === false) { common_Logger::w('File ' . $file->getAbsolutePath() . ' not found for fileressource ' . $itemContent->getUri()); } else { // Try to make it a DOM Document... $dom = new DOMDocument('1.0', 'UTF-8'); if (@$dom->loadXML($content) === true) { $contentStrings = array_merge($contentStrings, $xmlTokenizer->getStrings($dom)); unset($dom); } else { $contentStrings = array_merge($contentStrings, $rawTokenizer->getStrings($content)); } } } return $contentStrings; }
public function run() { common_ext_ExtensionsManager::singleton()->getExtensionById('taoTests'); common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest'); $ds = DIRECTORY_SEPARATOR; // 1. Find all the tests that have the QTI Test Model. $testClass = new core_kernel_classes_Class(TAO_TEST_CLASS); $qtiTests = $testClass->searchInstances(array(PROPERTY_TEST_TESTMODEL => INSTANCE_TEST_MODEL_QTI), array('recursive' => true, 'like' => false)); // 2. Find the test definition file for each test. $testContentProp = new core_kernel_classes_Property(TEST_TESTCONTENT_PROP); foreach ($qtiTests as $qtiTest) { $testContentResource = $qtiTest->getOnePropertyValue($testContentProp); $testContent = new core_kernel_file_File($testContentResource); $qtiTestFilePath = $testContent->getAbsolutePath(); $this->out("test.xml file found at '{$qtiTestFilePath}'."); // 2.1. Create a directory using the current file name. $pathinfo = pathinfo($qtiTestFilePath); $targetDirPath = $pathinfo['dirname'] . $ds . $pathinfo['filename']; if (!@mkdir($targetDirPath)) { $this->err("Unable to create QTI Test Content directory at location '{$targetDirPath}'."); } else { $this->out("QTI Test Content directory created at location '{$targetDirPath}'."); } // 2.2 Copy test.xml into the newly created directory. $qtiTestFileCopyDest = $targetDirPath . $ds . 'tao-qtitest-testdefinition.xml'; if (!@copy($qtiTestFilePath, $qtiTestFileCopyDest)) { $this->err("Unable to copy test.xml file from '{$qtiTestFilePath}' to '{$qtiTestFileCopyDest}'."); } else { $this->out("test.xml file copied from '{$qtiTestFilePath}' to '{$qtiTestFileCopyDest}'."); } // 2.3 Update metadata in Knowledge Base. $newFileResource = $testContent->getFileSystem()->createFile('', basename($targetDirPath)); $this->out("New File Resource in ontology refers to '" . $newFileResource->getAbsolutePath() . "'."); $qtiTest->removePropertyValues($testContentProp); $qtiTest->setPropertyValue($testContentProp, $newFileResource); $testContentResource->delete(true); if (!@unlink($qtiTestFilePath)) { $this->err("Unable to remove old test.xml file located at '{$qtiTestFilePath}'."); } else { $this->out("Old test.xml file located at '{$qtiTestFilePath}' removed from file system."); } } }
protected function createNamedSubDirectory(core_kernel_file_File $rootDirectory, $activity) { $name = md5($activity->getUri()); $relPath = $rootDirectory->getRelativePath() . DIRECTORY_SEPARATOR . $name; $absPath = $rootDirectory->getAbsolutePath() . DIRECTORY_SEPARATOR . $name; if (!is_dir($absPath) && !mkdir($absPath)) { throw new taoItems_models_classes_CompilationFailedException("Could not create sub-directory '{$absPath}'."); } return $rootDirectory->getFileSystem()->createFile('', $relPath); }
/** * 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'); } } }
/** * Delete the content of a QTI test * @param core_kernel_classes_Resource $test * @throws common_exception_Error */ public function deleteContent(core_kernel_classes_Resource $test) { $content = $test->getOnePropertyValue(new core_kernel_classes_Property(TEST_TESTCONTENT_PROP)); if (!is_null($content)) { $file = new core_kernel_file_File($content); try { $path = $file->getAbsolutePath(); if (is_dir($path)) { if (!tao_helpers_File::delTree($path)) { throw new common_exception_Error("Unable to remove test content directory located at '" . $file->getAbsolutePath() . "'."); } } } catch (common_Exception $e) { // Empty file... } $file->delete(); $test->removePropertyValue(new core_kernel_classes_Property(TEST_TESTCONTENT_PROP), $file); } }
/** * Get the expected absolute path to a given $qtiResource that is already stored in TAO. * * @param core_kernel_file_File|string $testContent The pointer to the TAO Test Content folder. * @param oat\taoQtiItem\model\qti\Resource|string $qtiTestResource The QTI resource to be copied into $testContent. If given as a string, it must be the relative (to the TAO Content Folder) path to the resource file. * @throws InvalidArgumentException If one of the above arguments is invalid. * @return string The absolute path to $qtiResource. */ public static function storedQtiResourcePath($testContent, $qtiResource) { if ($testContent instanceof core_kernel_file_File) { $contentPath = $testContent->getAbsolutePath(); } else { if (is_string($testContent) === true) { $contentPath = $testContent; } else { throw new InvalidArgumentException("The 'testContent' argument must be a string or a taoQTI_models_classes_QTI_Resource object."); } } if ($qtiResource instanceof taoQti_models_classes_QTI_Resource) { $filePath = $qtiResource->getFile(); } else { if (is_string($qtiResource) === true) { $filePath = $qtiResource; } else { throw new InvalidArgumentException("The 'qtiResource' argument must be a string or a taoQTI_models_classes_QTI_Resource object."); } } $ds = DIRECTORY_SEPARATOR; $filePath = ltrim($filePath, '/'); $contentPath = rtrim($contentPath, $ds); return $contentPath . $ds . str_replace('/', $ds, $filePath); }
/** * Clone a QTI Test Resource. * * @param core_kernel_classes_Resource $source The resource to be cloned. * @param core_kernel_classes_Resource $destination An existing resource to be filled as the clone of $source. */ public function cloneContent(core_kernel_classes_Resource $source, core_kernel_classes_Resource $destination) { $contentProperty = new core_kernel_classes_Property(TEST_TESTCONTENT_PROP); $existingDir = new core_kernel_file_File($source->getUniquePropertyValue($contentProperty)); $service = taoQtiTest_models_classes_QtiTestService::singleton(); $dir = $service->createContent($destination, false); if ($existingDir->fileExists()) { tao_helpers_File::copy($existingDir->getAbsolutePath(), $dir->getAbsolutePath(), true, false); } else { common_Logger::w('Test "' . $source->getUri() . '" had no content, nothing to clone'); } }
/** * Short description of method getItemFolder * * @access public * @author Joel Bout, <*****@*****.**> * @param Resource item * @param string lang * @return string */ public function getItemFolder(core_kernel_classes_Resource $item, $lang = '') { $returnValue = (string) ''; if ($lang === '') { $files = $item->getPropertyValues(new core_kernel_classes_Property(TAO_ITEM_CONTENT_PROPERTY)); } else { $files = $item->getPropertyValuesByLg(new core_kernel_classes_Property(TAO_ITEM_CONTENT_PROPERTY), $lang)->toArray(); } if (count($files) == 0) { // no content found assign default $returnValue = $this->getDefaultItemFolder($item, $lang); } else { if (count($files) > 1) { throw new common_Exception(__METHOD__ . ': Item ' . $item->getUri() . ' has multiple.'); } $content = new core_kernel_file_File(current($files)); $returnValue = dirname($content->getAbsolutePath()) . DIRECTORY_SEPARATOR; } return (string) $returnValue; }
/** * Short description of method revert * * @access public * @author Cédric Alfonsi, <*****@*****.**> * @param File resource * @param int revision * @param string msg * @return boolean * @see core_kernel_versioning_File::revert() */ public function revert(core_kernel_file_File $resource, $revision = null, $msg = "") { $returnValue = (bool) false; /** * @todo make all the functions coherent * Sometimes u pass a path sometimes not, the resource is enough * if we use just the resource, the only way to use svn will be to use generis ... so ... 6 */ $path = $resource->getAbsolutePath(); try { $returnValue = core_kernel_versioning_subversionWindows_Utils::exec($resource, 'revert "' . $path . '"'); } catch (Exception $e) { die('Error code `svn_error_revert` in ' . $e->getMessage()); } return (bool) $returnValue; }
/** * Revert a file * * @access public * @author Cédric Alfonsi, <*****@*****.**> * @param File resource * @param int revision * @param string msg * @return boolean * @see core_kernel_versioning_File::revert() */ public function revert(core_kernel_file_File $resource, $revision = null, $msg = "") { $returnValue = (bool) false; if ($resource->getRepository()->authenticate()) { //no revision, revert local change if (is_null($revision)) { $returnValue = svn_revert($resource->getAbsolutePath()); } else { $path = realpath($resource->getAbsolutePath()); common_Logger::i('svn_revert ' . $path); //get the svn revision number $log = svn_log($path); $oldRevision = count($log) - $revision; if (isset($log[$oldRevision])) { $svnRevision = $log[$oldRevision]; $svnRevisionNumber = $svnRevision['rev']; //destroy the existing version helpers_VersionedFile::rmWorkingCopy($path); //replace with the target revision if ($resource->update($svnRevisionNumber)) { if (is_file($path)) { //get old content $content = $resource->getFileContent(); //update to the current version $resource->update(); //set the new content $resource->setContent($content); //commit the change } if (is_dir($path)) { $i = 10; do { $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('versionedFolder'); common_Logger::i(__LINE__ . ' ' . $tmpDir); $exists = file_exists($tmpDir); $i--; } while ($exists || !$i); helpers_VersionedFile::cpWorkingCopy($path, $tmpDir); $resource->update(); //@TODO: empty the current folder $path, to delete no longer versioned files helpers_VersionedFile::cpWorkingCopy($tmpDir, $path); } if ($resource->commit($msg)) { $returnValue = true; } else { @helpers_VersionedFile::rmWorkingCopy($path); $resource->update(); } } else { @helpers_VersionedFile::rmWorkingCopy($path); $resource->update(); } } } } return (bool) $returnValue; }
/** * Old function to retrieve the local filepath * * @param string $link * @return string * @deprecated */ protected function getLocalPath($link) { $file = new \core_kernel_file_File($link); return $file->getAbsolutePath(); }
/** * (non-PHPdoc) * @see \oat\taoMediaManager\model\fileManagement\FileManagement::deleteFile() */ public function deleteFile($link) { $file = new \core_kernel_file_File($link); unlink($file->getAbsolutePath()); return $file->delete(); }
/** * Assign items to a QTI test. * @param XmlDocument $doc * @param array $items * @return type * @throws taoQtiTest_models_classes_QtiTestServiceException */ private function setItemsToDoc(XmlDocument $doc, array $items, $sectionIndex = 0) { $sections = $doc->getDocumentComponent()->getComponentsByClassName('assessmentSection'); if (!isset($sections[$sectionIndex])) { throw new taoQtiTest_models_classes_QtiTestServiceException('No section found in test at index : ' . $sectionIndex, taoQtiTest_models_classes_QtiTestServiceException::TEST_READ_ERROR); } $section = $sections[$sectionIndex]; $itemContentProperty = new core_kernel_classes_Property(TAO_ITEM_CONTENT_PROPERTY); $itemRefs = new SectionPartCollection(); $itemRefIdentifiers = array(); foreach ($items as $itemResource) { $itemContent = new core_kernel_file_File($itemResource->getUniquePropertyValue($itemContentProperty)); $itemDoc = new XmlDocument(); try { $itemDoc->load($itemContent->getAbsolutePath()); } catch (StorageException $e) { // We consider the item not compliant with QTI, let's try the next one. continue; } $itemRefIdentifier = $itemDoc->getDocumentComponent()->getIdentifier(); //enable more than one reference if (array_key_exists($itemRefIdentifier, $itemRefIdentifiers)) { $itemRefIdentifiers[$itemRefIdentifier] += 1; $itemRefIdentifier .= '-' . $itemRefIdentifiers[$itemRefIdentifier]; } else { $itemRefIdentifiers[$itemRefIdentifier] = 0; } $itemRefs[] = new AssessmentItemRef($itemRefIdentifier, $itemResource->getUri()); } $section->setSectionParts($itemRefs); return count($itemRefs); }