diff --git a/scripts/tools/export/UserCsvExporter.php b/scripts/tools/export/UserCsvExporter.php new file mode 100644 index 0000000000..1a67149fec --- /dev/null +++ b/scripts/tools/export/UserCsvExporter.php @@ -0,0 +1,114 @@ + [ + 'prefix' => 'out', + 'longPrefix' => self::OPTION_OUTPUT_PATH, + 'required' => true, + 'description' => 'Path to export the user CSV file.', + ], + ]; + } + + protected function provideDescription() + { + return "Exports Users in CSV format."; + } + + protected function run() + { + $this->report = Report::createInfo('User CSV export started'); + + $userClass = new core_kernel_classes_Class(TaoOntology::CLASS_URI_TAO_USER); + $properties = $this->findUserPropertyList($userClass); + + $dataForExport = $this->collectDataFromUsers($userClass->getInstances(), $properties); + $columnNames = $this->findColumnNames($properties); + array_unshift($dataForExport, $columnNames); + + $exporter = new CsvExporter($dataForExport); + + $outputFile = $this->getOption(self::OPTION_OUTPUT_PATH); + if (file_put_contents($outputFile, $exporter->export())) { + $this->report->add(Report::createSuccess("Operation completed successfully")); + } else { + $this->report->add(Report::createError("Failed to write prepared CSV to the specified path")); + } + + return $this->report; + } + + private function findUserPropertyList($userClass): array + { + $properties = []; + $properties[UserRdf::PROPERTY_LOGIN] = $userClass->getProperty(UserRdf::PROPERTY_LOGIN); + $properties[UserRdf::PROPERTY_FIRSTNAME] = $userClass->getProperty(UserRdf::PROPERTY_FIRSTNAME); + $properties[UserRdf::PROPERTY_LASTNAME] = $userClass->getProperty(UserRdf::PROPERTY_LASTNAME); + $properties[UserRdf::PROPERTY_MAIL] = $userClass->getProperty(UserRdf::PROPERTY_MAIL); + $properties[UserRdf::PROPERTY_TIMEZONE] = $userClass->getProperty(UserRdf::PROPERTY_TIMEZONE); + $properties[UserRdf::PROPERTY_UILG] = $userClass->getProperty(UserRdf::PROPERTY_UILG); + $properties[UserRdf::PROPERTY_DEFLG] = $userClass->getProperty(UserRdf::PROPERTY_DEFLG); + + return $properties; + } + + private function collectDataFromUsers($users, $properties): array + { + $result = []; + foreach ($users as $user) { + $userData = [$user->getUri()]; + foreach ($properties as $property) { + $userData[] = (string) $user->getOnePropertyValue($property); + } + $result[] = $userData; + } + + return $result; + } + + private function findColumnNames(array $properties): array + { + $columnNames = ['URI']; + foreach ($properties as $property) { + $columnNames[] = $property->getLabel(); + } + + return $columnNames; + } +}