/**
  * (non-PHPdoc)
  *
  * @see \oat\tao\model\media\MediaBrowser::getDirectory
  */
 public function getDirectory($parentLink = '', $acceptableMime = array(), $depth = 1)
 {
     if ($parentLink == '') {
         $class = new \core_kernel_classes_Class($this->rootClassUri);
     } else {
         $class = new \core_kernel_classes_Class(\tao_helpers_Uri::decode($parentLink));
     }
     $data = array('path' => 'taomedia://mediamanager/' . \tao_helpers_Uri::encode($class->getUri()), 'label' => $class->getLabel());
     if ($depth > 0) {
         $children = array();
         foreach ($class->getSubClasses() as $subclass) {
             $children[] = $this->getDirectory($subclass->getUri(), $acceptableMime, $depth - 1);
         }
         // add a filter for example on language (not for now)
         $filter = array();
         foreach ($class->searchInstances($filter) as $instance) {
             try {
                 $file = $this->getFileInfo($instance->getUri());
                 if (count($acceptableMime) == 0 || in_array($file['mime'], $acceptableMime)) {
                     $children[] = $file;
                 }
             } catch (\tao_models_classes_FileNotFoundException $e) {
                 \common_Logger::e($e->getMessage());
             }
         }
         $data['children'] = $children;
     } else {
         $data['parent'] = $parentLink;
     }
     return $data;
 }
 /**
  * Creates a new delivery
  * 
  * @param core_kernel_classes_Class $deliveryClass
  * @param core_kernel_classes_Resource $test
  * @param array $properties Array of properties of delivery
  * @return common_report_Report
  */
 public static function create(\core_kernel_classes_Class $deliveryClass, \core_kernel_classes_Resource $test, $properties = array())
 {
     \common_Logger::i('Creating delivery with ' . $test->getLabel() . ' under ' . $deliveryClass->getLabel());
     $storage = new TrackedStorage();
     $testCompilerClass = \taoTests_models_classes_TestsService::singleton()->getCompilerClass($test);
     $compiler = new $testCompilerClass($test, $storage);
     $report = $compiler->compile();
     if ($report->getType() == \common_report_Report::TYPE_SUCCESS) {
         //$tz = new \DateTimeZone(\common_session_SessionManager::getSession()->getTimeZone());
         $tz = new \DateTimeZone('UTC');
         if (!empty($properties[TAO_DELIVERY_START_PROP])) {
             $dt = new \DateTime($properties[TAO_DELIVERY_START_PROP], $tz);
             $properties[TAO_DELIVERY_START_PROP] = (string) $dt->getTimestamp();
         }
         if (!empty($properties[TAO_DELIVERY_END_PROP])) {
             $dt = new \DateTime($properties[TAO_DELIVERY_END_PROP], $tz);
             $properties[TAO_DELIVERY_END_PROP] = (string) $dt->getTimestamp();
         }
         $serviceCall = $report->getData();
         $properties[PROPERTY_COMPILEDDELIVERY_DIRECTORY] = $storage->getSpawnedDirectoryIds();
         $compilationInstance = DeliveryAssemblyService::singleton()->createAssemblyFromServiceCall($deliveryClass, $serviceCall, $properties);
         $report->setData($compilationInstance);
     }
     return $report;
 }
 /**
  * Creates a new simple delivery
  * 
  * @param core_kernel_classes_Class $deliveryClass
  * @param core_kernel_classes_Resource $test
  * @param string $label
  * @return common_report_Report
  */
 public function create(core_kernel_classes_Class $deliveryClass, core_kernel_classes_Resource $test, $label)
 {
     common_Logger::i('Creating ' . $label . ' with ' . $test->getLabel() . ' under ' . $deliveryClass->getLabel());
     $contentClass = new core_kernel_classes_Class(CLASS_SIMPLE_DELIVERYCONTENT);
     $content = $contentClass->createInstanceWithProperties(array(PROPERTY_DELIVERYCONTENT_TEST => $test->getUri()));
     $report = TemplateAssemblyService::singleton()->createAssemblyByContent($deliveryClass, $content, array(RDFS_LABEL => $label));
     $content->delete();
     return $report;
 }
 /**
  * Creates a new simple delivery
  * 
  * @param core_kernel_classes_Class $deliveryClass
  * @param core_kernel_classes_Resource $test
  * @param string $label
  * @return common_report_Report
  */
 public static function create(core_kernel_classes_Class $deliveryClass, core_kernel_classes_Resource $test, $label)
 {
     common_Logger::i('Creating ' . $label . ' with ' . $test->getLabel() . ' under ' . $deliveryClass->getLabel());
     $storage = new taoDelivery_models_classes_TrackedStorage();
     $testCompilerClass = taoTests_models_classes_TestsService::singleton()->getCompilerClass($test);
     $compiler = new $testCompilerClass($test, $storage);
     $report = $compiler->compile();
     if ($report->getType() == common_report_Report::TYPE_SUCCESS) {
         $serviceCall = $report->getData();
         $properties = array(RDFS_LABEL => $label, PROPERTY_COMPILEDDELIVERY_DIRECTORY => $storage->getSpawnedDirectoryIds());
         $compilationInstance = taoDelivery_models_classes_DeliveryAssemblyService::singleton()->createAssemblyFromServiceCall($deliveryClass, $serviceCall, $properties);
         $report->setData($compilationInstance);
     }
     return $report;
 }
Ejemplo n.º 5
0
 /**
  * @author Bertrand Chevrier, <*****@*****.**>
  * @param  core_kernel_classes_Class $range
  * @param  boolean $recursive
  * @return array
  */
 public function rangeToTree(core_kernel_classes_Class $range, $recursive = false)
 {
     $data = array();
     foreach ($range->getSubClasses(false) as $rangeClass) {
         $classData = array('data' => $rangeClass->getLabel(), 'attributes' => array('id' => tao_helpers_Uri::encode($rangeClass->getUri()), 'class' => 'node-instance'));
         $children = $this->rangeToTree($rangeClass, true);
         if (count($children) > 0) {
             $classData['state'] = 'closed';
             $classData['children'] = $children;
         }
         $data[] = $classData;
     }
     if (!$recursive) {
         $returnValue = array('data' => $range->getLabel(), 'attributes' => array('id' => tao_helpers_Uri::encode($range->getUri()), 'class' => 'node-class'), 'children' => $data);
     } else {
         $returnValue = $data;
     }
     return $returnValue;
 }
 /**
  * (non-PHPdoc)
  * @see \oat\oatbox\action\Action::__invoke()
  */
 public function __invoke($params)
 {
     \common_ext_ExtensionsManager::singleton()->getExtensionById('taoDeliveryRdf');
     if (count($params) != 2) {
         return new \common_report_Report(\common_report_Report::TYPE_ERROR, __('Usage: %s DELIVERY_CLASS_URI OUTPUT_DIRECTORY', __CLASS__));
     }
     $deliveryClassUri = array_shift($params);
     $deliveryClass = new \core_kernel_classes_Class($deliveryClassUri);
     $dir = array_shift($params);
     if (!file_exists($dir) && !mkdir($dir)) {
         return new \common_report_Report(\common_report_Report::TYPE_ERROR, __('Directory %s doesn\'t exist', $dir));
     }
     $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
     $report = new \common_report_Report(\common_report_Report::TYPE_SUCCESS, __('Exporting %s', $deliveryClass->getLabel()));
     foreach ($deliveryClass->getInstances(true) as $delivery) {
         $destFile = $dir . \tao_helpers_File::getSafeFileName($delivery->getLabel()) . '.zip';
         $tmpFile = Assembler::exportCompiledDelivery($delivery);
         \tao_helpers_File::move($tmpFile, $destFile);
         $report->add(new \common_report_Report(\common_report_Report::TYPE_SUCCESS, __('Exported %1$s to %2$s', $delivery->getLabel(), $destFile)));
     }
     return $report;
 }
 /**
  * @param Document $document
  */
 protected function indexTypes(Document $document)
 {
     $toDo = array();
     foreach ($this->resource->getTypes() as $class) {
         $toDo[] = $class->getUri();
         $document->addField(Document\Field::Text('class', $class->getLabel()));
     }
     $done = array(RDFS_CLASS, TAO_OBJECT_CLASS);
     $toDo = array_diff($toDo, $done);
     $classLabels = array();
     while (!empty($toDo)) {
         $class = new \core_kernel_classes_Class(array_pop($toDo));
         $classLabels[] = $class->getLabel();
         foreach ($class->getParentClasses() as $parent) {
             if (!in_array($parent->getUri(), $done)) {
                 $toDo[] = $parent->getUri();
             }
         }
         $done[] = $class->getUri();
     }
     $field = Document\Field::Keyword('class_r', $classLabels);
     $field->isStored = false;
     $document->addField($field);
 }
 /**
  * generis tree representation of a class node
  * without it's content
  * 
  * @param core_kernel_classes_Class $class
  * @return array
  */
 public function buildClassNode(core_kernel_classes_Class $class, core_kernel_classes_Class $parent = null)
 {
     $label = $class->getLabel();
     $label = empty($label) ? __('no label') : $label;
     return array('data' => _dh($label), 'type' => 'class', '_data' => array('uri' => $class->getUri(), 'classUri' => is_null($parent) ? null : $parent->getUri()), 'attributes' => array('id' => tao_helpers_Uri::encode($class->getUri()), 'class' => 'node-class', 'data-uri' => $class->getUri()));
 }
Ejemplo n.º 9
0
 /**
  * Import the properties of the resource
  * 
  * @param core_kernel_classes_Resource $resource
  * @param array $propertiesValues
  * @param array $map
  * @param core_kernel_classes_Class $class
  * @return common_report_Report
  */
 private function importProperties(core_kernel_classes_Resource $resource, $propertiesValues, $map, $class)
 {
     if (isset($propertiesValues[RDF_TYPE])) {
         // assuming single Type
         if (count($propertiesValues[RDF_TYPE]) > 1) {
             return new common_report_Report(common_report_Report::TYPE_ERROR, __('Resource not imported due to multiple types'));
         } else {
             foreach ($propertiesValues[RDF_TYPE] as $k => $v) {
                 $classType = isset($map[$v['value']]) ? new core_kernel_classes_Class($map[$v['value']]) : $class;
                 //$resource->setType($classType);
                 $classType->createInstance(null, null, $resource->getUri());
             }
         }
         unset($propertiesValues[RDF_TYPE]);
     }
     if (isset($propertiesValues[RDFS_SUBCLASSOF])) {
         $resource = new core_kernel_classes_Class($resource);
         // assuming single subclass
         if (isset($propertiesValues[RDF_TYPE]) && count($propertiesValues[RDF_TYPE]) > 1) {
             return new common_report_Report(common_report_Report::TYPE_ERROR, __('Resource not imported due to multiple super classes'));
         }
         foreach ($propertiesValues[RDFS_SUBCLASSOF] as $k => $v) {
             $classSup = isset($map[$v['value']]) ? new core_kernel_classes_Class($map[$v['value']]) : $class;
             $resource->setSubClassOf($classSup);
         }
         unset($propertiesValues[RDFS_SUBCLASSOF]);
     }
     foreach ($propertiesValues as $prop => $values) {
         $property = new core_kernel_classes_Property(isset($map[$prop]) ? $map[$prop] : $prop);
         foreach ($values as $k => $v) {
             $value = isset($map[$v['value']]) ? $map[$v['value']] : $v['value'];
             if (isset($v['lang'])) {
                 $resource->setPropertyValueByLg($property, $value, $v['lang']);
             } else {
                 $resource->setPropertyValue($property, $value);
             }
         }
     }
     $msg = $resource instanceof core_kernel_classes_Class ? __('Successfully imported class "%s"', $resource->getLabel()) : __('Successfully imported "%s"', $resource->getLabel());
     return new common_report_Report(common_report_Report::TYPE_SUCCESS, $msg, $resource);
 }
Ejemplo n.º 10
0
 /**
  * Short description of method createUniqueLabel
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @param  Class clazz
  * @param  boolean subClassing
  * @return string
  */
 public function createUniqueLabel(core_kernel_classes_Class $clazz, $subClassing = false)
 {
     $returnValue = (string) '';
     if ($subClassing) {
         $labelBase = $clazz->getLabel() . '_';
         $count = count($clazz->getSubClasses()) + 1;
     } else {
         $labelBase = $clazz->getLabel() . ' ';
         $count = count($clazz->getInstances()) + 1;
     }
     $options = array('lang' => \common_session_SessionManager::getSession()->getDataLanguage(), 'like' => false, 'recursive' => false);
     do {
         $exist = false;
         $label = $labelBase . $count;
         $result = $clazz->searchInstances(array(RDFS_LABEL => $label), $options);
         if (count($result) > 0) {
             $exist = true;
             $count++;
         }
     } while ($exist);
     $returnValue = $label;
     return (string) $returnValue;
 }
Ejemplo n.º 11
0
 public function addInstanceForm()
 {
     if (!tao_helpers_Request::isAjax()) {
         throw new Exception("wrong request mode");
     }
     $clazz = new core_kernel_classes_Class(CLASS_TAO_USER);
     $formContainer = new tao_actions_form_CreateInstance(array($clazz), array());
     $myForm = $formContainer->getForm();
     if ($myForm->isSubmited()) {
         if ($myForm->isValid()) {
             $properties = $myForm->getValues();
             $instance = $this->createInstance(array($clazz), $properties);
             $this->setData('message', __($instance->getLabel() . ' created'));
             //$this->setData('reload', true);
             $this->setData('selectTreeNode', $instance->getUri());
         }
     }
     $this->setData('formTitle', __('Create instance of ') . $clazz->getLabel());
     $this->setData('myForm', $myForm->render());
     $this->setView('form.tpl', 'tao');
 }
Ejemplo n.º 12
0
 /**
  * Calling this method will transfer all instances of $class from the statements table
  * to specific optimized relational tables.
  * 
  * During optimization, the current user has all privileges on the persistent memory. At
  * the end of the process, the old privileges will be set back.
  * 
  * The $options array can contain the following key => values (all booleans):
  * 
  * - recursive: compile the target class and its subclasses (default: false).
  * - append: append data to the existing optimized table if it already exists (default: false).
  * - rmSources: remove the triples in the statement table after transfer (default: true).
  *
  * @access public
  * @author Bertrand Chevrier, <*****@*****.**>
  * @param  \core_kernel_classes_Class class
  * @param  array options
  * @return boolean Will return true if it succeeds, false otherwise.
  */
 public function hardify(\core_kernel_classes_Class $class, $options = array())
 {
     $returnValue = (bool) false;
     $oldUpdatableModels = core_kernel_persistence_smoothsql_SmoothModel::getUpdatableModelIds();
     try {
         // Give access to all models during hardification.
         core_kernel_persistence_smoothsql_SmoothModel::forceUpdatableModelIds(self::getAllModelIds());
         $classLabel = $class->getLabel();
         \common_Logger::i("Hardifying class {$classLabel}", array("GENERIS"));
         if (defined("DEBUG_PERSISTENCE") && DEBUG_PERSISTENCE) {
             if (in_array($class->getUri(), self::$debug_tables)) {
                 return;
             }
             \common_Logger::d('hardify ' . $class->getUri());
             self::$debug_tables[] = $class->getUri();
             $countStatement = $this->countStatements();
         }
         if (in_array($class->getUri(), self::$blackList)) {
             return $returnValue;
         }
         // ENTER IN SMOOTH SQL MODE
         PersistenceProxy::forceMode(PERSISTENCE_SMOOTH);
         //recursive will hardify the class and it's subclasses in the same table!
         isset($options['recursive']) ? $recursive = $options['recursive'] : ($recursive = false);
         //createForeigns will hardify the class that are range of the properties
         isset($options['createForeigns']) ? $createForeigns = $options['createForeigns'] : ($createForeigns = false);
         //check if we append the data in case the hard table exists or truncate the table and add the new rows
         isset($options['append']) ? $append = $options['append'] : ($append = false);
         //if true, the instances of the class will  be removed from the statements table!
         isset($options['rmSources']) ? $rmSources = (bool) $options['rmSources'] : ($rmSources = false);
         //if defined, we took all the properties of the class and it's parents till the topclass
         isset($options['topclass']) ? $topclass = $options['topclass'] : ($topclass = new \core_kernel_classes_Class(RDFS_RESOURCE));
         //if defined, compile the additional properties
         isset($options['additionalProperties']) ? $additionalProperties = $options['additionalProperties'] : ($additionalProperties = array());
         //if defined, reference the additional class to the table
         isset($options['referencesAllTypes']) ? $referencesAllTypes = $options['referencesAllTypes'] : ($referencesAllTypes = false);
         $tableName = '_' . Utils::getShortName($class);
         $myTableMgr = new TableManager($tableName);
         $referencer = ResourceReferencer::singleton();
         //get the table columns from the class properties
         $columns = array();
         $ps = new PropertySwitcher($class);
         $properties = $ps->getProperties($additionalProperties);
         $columns = $ps->getTableColumns($additionalProperties, self::$blackList);
         //init the count value in hardened classes:
         if (isset($this->hardenedClasses[$class->getUri()])) {
             PersistenceProxy::restoreImplementation();
             return true;
             //already being compiled
         } else {
             $this->hardenedClasses[$class->getUri()] = 0;
         }
         if (!$append || $append && !$myTableMgr->exists()) {
             //create the table
             if ($myTableMgr->exists()) {
                 $myTableMgr->remove();
             }
             $myTableMgr->create($columns);
             //reference the class
             $referencer->referenceClass($class, array("topclass" => $topclass, "additionalProperties" => $additionalProperties));
             if ($referencesAllTypes) {
                 $referencer->referenceInstanceTypes($class);
             }
         }
         //insert the resources
         $startIndex = 0;
         $instancePackSize = 100;
         $instances = $class->getInstances(false, array('offset' => $startIndex, 'limit' => $instancePackSize));
         $count = count($instances);
         $notDeletedInstances = array();
         do {
             //reset timeout:
             //set_time_limit(30);
             \helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::MEDIUM);
             $rows = array();
             foreach ($instances as $index => $resource) {
                 if ($referencer->isResourceReferenced($resource)) {
                     PersistenceProxy::forceMode(PERSISTENCE_HARD);
                     $resource->delete();
                     PersistenceProxy::restoreImplementation();
                 }
                 $row = array('uri' => $resource->getUri());
                 foreach ($properties as $property) {
                     $propValue = $resource->getOnePropertyValue($property);
                     $row[Utils::getShortName($property)] = $propValue;
                 }
                 $rows[] = $row;
             }
             $rowMgr = new RowManager($tableName, $columns);
             $rowMgr->insertRows($rows);
             foreach ($instances as $resource) {
                 $referencer->referenceResource($resource, $tableName, null, true);
                 if ($rmSources) {
                     //remove exported resources in smooth sql, if required:
                     // Be carefull, the resource can still exist even if
                     // delete returns true. Indeed, modelIds can be mixed between
                     // multiple models and only a part of the triples that consitute
                     // the resource might have been deleted.
                     if (!$resource->delete() || $resource->exists()) {
                         //@TODO : modified resource::delete() because resource not in local modelId cannot be deleted
                         $notDeletedInstances[] = $resource->getUri();
                         $startIndex++;
                     }
                 }
             }
             if (!$rmSources) {
                 //increment start index only if not removed
                 $startIndex += $instancePackSize;
             }
             //record hardened instances number
             if (isset($this->hardenedClasses[$class->getUri()])) {
                 $this->hardenedClasses[$class->getUri()] += $count;
             } else {
                 $this->hardenedClasses[$class->getUri()] = $count;
             }
             //update instance array and count value
             $instances = $class->getInstances(false, array('offset' => $startIndex, 'limit' => $instancePackSize));
             foreach ($notDeletedInstances as $uri) {
                 unset($instances[$uri]);
             }
             $count = count($instances);
             \helpers_TimeOutHelper::reset();
         } while ($count > 0);
         $returnValue = true;
         // Treat subclasses of the current class
         if ($recursive) {
             foreach ($class->getSubClasses(true) as $subClass) {
                 $returnValue = $this->hardify($subClass, array_merge($options, array('recursive' => false, 'append' => true)));
             }
         }
         //reset cache:
         $referencer->clearCaches();
         // EXIT SMOOTH SQL MODE
         PersistenceProxy::restoreImplementation();
         if (defined("DEBUG_PERSISTENCE") && DEBUG_PERSISTENCE) {
             $this->unhardify($class, array_merge($options, array('recursive' => false, 'removeForeigns' => false)));
             \common_Logger::d('unhardened result statements ' . $this->countStatements() . ' / ' . $countStatement);
         }
         // Give the normal rights on models to the session.
         core_kernel_persistence_smoothsql_SmoothModel::forceUpdatableModelIds($oldUpdatableModels);
     } catch (Exception $e) {
         \common_Logger::e('An error occured during hardification: ' . $e->getMessage());
         core_kernel_persistence_smoothsql_SmoothModel::forceUpdatableModelIds($oldUpdatableModels);
     }
     return (bool) $returnValue;
 }
Ejemplo n.º 13
0
 /**
  * Decompile a specific class with the decompilation options found in the data returned
  * by the decompileClass method.
  *
  * This method returns an associative array containing the following informations:
  *
  * array(
  *    "success" => true/false,
  *    "count" => integer // the class instances that were unoptimized
  *    "relatedClasses" => array("class1", "class2", ...) // the classes that were impacted by the unoptimization
  *                                                // depending on the unoptimization options
  * )
  *
  * @see Optimization::decompileClass()
  * @param \core_kernel_classes_Class class The class you would like to compile.
  */
 public static function decompileClass(\core_kernel_classes_Class $class)
 {
     $result = array('success' => false);
     $optimizableClasses = self::getOptimizableClasses();
     if (isset($optimizableClasses[$class->getUri()]) && isset($optimizableClasses[$class->getUri()]['decompile'])) {
         //build the option array and launch the compilation:
         $userDefinedOptions = array();
         $options = array_merge($optimizableClasses[$class->getUri()]['decompile'], $userDefinedOptions);
         $switcher = new Switcher();
         $switcher->unhardify($class, $options);
         //prepare return value
         $decompiledClass = $switcher->getDecompiledClasses();
         $count = isset($decompiledClass[$class->getUri()]) ? $decompiledClass[$class->getUri()] : 0;
         $relatedClasses = array();
         foreach ($decompiledClass as $relatedClassUri => $nb) {
             if ($relatedClassUri != $class->getUri()) {
                 $relatedClass = new \core_kernel_classes_Class($relatedClassUri);
                 $relatedClasses[$relatedClass->getLabel()] = $nb;
             }
         }
         $result = array('success' => true, 'count' => $count, 'relatedClasses' => $relatedClasses);
         unset($switcher);
     }
     return $result;
 }