Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README to data warehouse export zip files #1300

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions classes/DataWarehouse/Export/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CCR\Loggable;
use DataWarehouse\Data\BatchDataset;
use DataWarehouse\Data\RawStatisticsConfiguration;
use DataWarehouse\Export\FileWriter\FileWriterFactory;
use Exception;
use Log;
Expand Down Expand Up @@ -34,6 +35,12 @@ class FileManager extends Loggable
*/
private $fileWriterFactory;

/**
* Raw statistics configuration.
* @var \DataWarehouse\Data\RawStatisticsConfiguration;
*/
private $rawStatsConfig;

/**
* Construct a new file manager.
*
Expand All @@ -46,6 +53,8 @@ public function __construct(Log $logger = null)
$this->fileWriterFactory = new FileWriterFactory($logger);
parent::__construct($logger);

$this->rawStatsConfig = RawStatisticsConfiguration::factory();

try {
$this->exportDir = xd_utilities\getConfiguration(
'data_warehouse_export',
Expand Down Expand Up @@ -243,6 +252,15 @@ public function createZipFile($dataFile, array $request)
));
}

$readmeText = $this->getReadmeText($request['realm']);
if (!$zip->addFromString('README.txt', $readmeText)) {
throw new Exception(sprintf(
'Failed to add "README.txt" to zip file "%s"',
$dataFile,
$zipFile
));
}

if (!$zip->close()) {
throw new Exception(sprintf(
'Failed to close zip file "%s"',
Expand Down Expand Up @@ -309,4 +327,27 @@ public function removeDeletedRequests(array $deletedRequestIds)
}
}
}

/**
* Get the contents for README.txt for a realm.
*
* @param string $realm The name of a realm.
* @return string
*/
private function getReadmeText($realm)
{
$fields = $this->rawStatsConfig->getBatchExportFieldDefinitions($realm);

$text = "{$realm} Realm Fields\n\n";

foreach ($fields as $field) {
$text .= sprintf(
"%s: %s\n",
$field['name'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we allow html in the documentation field? Is it work htmldecode this before putting in the text file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now, none of the documentation fields contain html. I don't think there is anything to forbid it.

I don't see any entities, but if we do decide to allow arbitrary html then just decoding wouldn't be enough since we'd still have to strip out any tags.

$field['documentation']
);
}

return $text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"id": 1,
"user_id": 1,
"realm": "jobs",
"realm": "Jobs",
"start_date": "2018-01-01",
"end_date": "2018-02-28",
"export_file_format": "CSV",
Expand All @@ -20,7 +20,7 @@
{
"id": 2,
"user_id": 1,
"realm": "jobs",
"realm": "Jobs",
"start_date": "2001-01-01",
"end_date": "2001-01-02",
"export_file_format": "JSON",
Expand Down
3 changes: 2 additions & 1 deletion tests/component/lib/Export/FileManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ public function testCreateZipFile(array $request)
$zip = new ZipArchive();
$openCode = $zip->open($zipFile, ZipArchive::CHECKCONS);
$this->assertTrue($openCode, 'Open zip file');
$this->assertEquals(1, $zip->numFiles, 'File count in zip file');
$this->assertEquals(2, $zip->numFiles, 'File count in zip file');
$dataFileName = self::$fileManager->getDataFileName($request);
$this->assertEquals($dataFileName, $zip->getNameIndex(0), 'Data file name');
$this->assertEquals('README.txt', $zip->getNameIndex(1), 'README file name');
$fileData = $zip->getFromName($dataFileName);
$this->assertEquals($testData, $fileData, 'Data file contents');
$zip->close();
Expand Down