Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Attachments and Links Import/Export, Database Restore, and Control Cl…
Browse files Browse the repository at this point in the history
…eanup (#451)

* Attachments and Links Import/Export, Database Restore, and Control Cleanup

* Attachments can now be exported and imported.  On export, attachments are downloaded into a Tar Gzip and securely extracted on import.

* Links and Attachments data is now provided within the Levels export.  Users must import both the Level data and the Attachment files to restore the levels with attachments.

* A database restore option has been added which utilizes the backed up database content.  This overwrites all data in the database.

* The Control page has been reorganized to align the various functionality better.

* Memcached flushing has been added to all relevant data imports.

* Error handling has been added to the various import functions.

* * Removed getter function for the Attachment constant.

* Switched double quotes with single quotes.
  • Loading branch information
justinwray authored and gsingh93 committed Feb 21, 2017
1 parent ca40091 commit be35c6b
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 27 deletions.
14 changes: 14 additions & 0 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public function getBackupCmd(): string {
return $backup_cmd;
}

public function getRestoreCmd(): string {
$usr = must_have_idx($this->config, 'DB_USERNAME');
$pwd = must_have_idx($this->config, 'DB_PASSWORD');
$db = must_have_idx($this->config, 'DB_NAME');
$restore_cmd =
'mysql -u '.
escapeshellarg($usr).
' --password='.
escapeshellarg($pwd).
' '.
escapeshellarg($db);
return $restore_cmd;
}

public async function genConnection(): Awaitable<AsyncMysqlConnection> {
await $this->genConnect();
invariant($this->conn !== null, 'Connection cant be null.');
Expand Down
90 changes: 70 additions & 20 deletions src/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1010,10 +1010,16 @@ public function renderControlsContent(): :xhp {
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button
class="fb-cta cta--yellow"
data-action="backup-db">
{tr('Back Up Database')}
class="fb-cta cta--red"
data-action="import-game">
{tr('Import Full Game')}
</button>
<input
class="completely-hidden"
id="import-game_file"
type="file"
name="game_file"
/>
</div>
</div>
</div>
Expand All @@ -1028,23 +1034,6 @@ class="fb-cta cta--yellow"
</div>
</div>
</div>
<div class="col col-pad col-1-3">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button
class="fb-cta cta--yellow"
data-action="import-game">
{tr('Import Full Game')}
</button>
<input
class="completely-hidden"
id="import-game_file"
type="file"
name="game_file"
/>
</div>
</div>
</div>
</div>
</section>
<section class="admin-box">
Expand Down Expand Up @@ -1072,6 +1061,32 @@ class="fb-cta cta--yellow"
</div>
</div>
</div>
<div class="col col-pad col-1-3">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button class="fb-cta cta--red js-restore-database">
{tr('Restore Database')}
</button>
<input
class="completely-hidden"
id="restore-database_file"
type="file"
name="database_file"
/>
</div>
</div>
</div>
<div class="col col-pad col-1-3">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button
class="fb-cta cta--yellow"
data-action="backup-db">
{tr('Backup Database')}
</button>
</div>
</div>
</div>
</div>
</section>
<section class="admin-box">
Expand Down Expand Up @@ -1170,6 +1185,41 @@ class="fb-cta cta--yellow"
</div>
</div>
</div>
<div class="col col-pad col-1-4">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button
class="fb-cta cta--red"
data-action="import-attachments">
{tr('Import Attachments')}
</button>
<input
class="completely-hidden"
id="import-attachments_file"
type="file"
name="attachments_file"
/>
</div>
</div>
</div>
<div class="col col-pad col-1-4">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
<button
class="fb-cta cta--yellow"
data-action="export-attachments">
{tr('Export Attachments')}
</button>
</div>
</div>
</div>
</div>
</section>
<section class="admin-box">
<header class="admin-box-header">
<h3>{tr('Categories')}</h3>
</header>
<div class="fb-column-container">
<div class="col col-pad col-1-4">
<div class="form-el el--block-label el--full-text">
<div class="admin-buttons">
Expand Down
20 changes: 19 additions & 1 deletion src/controllers/ajax/AdminAjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ protected function getActions(): array<string> {
'pause_game',
'unpause_game',
'reset_game',
'export_attachments',
'backup_db',
'export_game',
'export_teams',
'export_logos',
'export_levels',
'export_categories',
'restore_db',
'import_game',
'import_teams',
'import_logos',
'import_levels',
'import_categories',
'import_attachments',
'flush_memcached',
'reset_database',
);
Expand Down Expand Up @@ -432,8 +435,11 @@ protected function getActions(): array<string> {
case 'unpause_game':
await Control::genUnpause();
return Utils::ok_response('Success', 'admin');
case 'export_attachments':
await Control::exportAttachments();
return Utils::ok_response('Success', 'admin');
case 'backup_db':
Control::backupDb();
await Control::backupDb();
return Utils::ok_response('Success', 'admin');
case 'export_game':
await Control::exportGame();
Expand All @@ -450,6 +456,12 @@ protected function getActions(): array<string> {
case 'export_categories':
await Control::exportCategories();
return Utils::ok_response('Success', 'admin');
case 'restore_db':
$result = await Control::restoreDb();
if ($result) {
return Utils::ok_response('Success', 'admin');
}
return Utils::error_response('Error importing', 'admin');
case 'import_game':
$result = await Control::importGame();
if ($result) {
Expand Down Expand Up @@ -480,6 +492,12 @@ protected function getActions(): array<string> {
return Utils::ok_response('Success', 'admin');
}
return Utils::error_response('Error importing', 'admin');
case 'import_attachments':
$result = await Control::importAttachments();
if ($result) {
return Utils::ok_response('Success', 'admin');
}
return Utils::error_response('Error importing', 'admin');
case 'flush_memcached':
$result = await Control::genFlushMemcached();
if ($result) {
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/importers/BinaryImporterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh // strict

class BinaryImporterController {
public static function getFilename(string $file_name): mixed {
$file = Utils::getFILES();
if ($file->contains($file_name)) {
$input_filename = $file[$file_name]['tmp_name'];
return $input_filename;
}
return false;
}
}
26 changes: 23 additions & 3 deletions src/controllers/modals/ActionModalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,34 @@ class="fb-cta cta--yellow js-close-modal js-confirm-save">
<div class="action-main">
<p>{tr('Items have been imported successfully')}</p>
<div class="action-actionable">
<a
href="#"
class="fb-cta cta--yellow js-close-modal js-confirm-save">
<a href="#" class="fb-cta cta--yellow js-close-modal">
{tr('OK')}
</a>
</div>
</div>;
return tuple($title, $content);
case 'restore-database':
$title =
<h4>
{tr('restore_')}<span class="highlighted">{tr('Database')}</span>
</h4>;
$content =
<div class="action-main">
<p>
{tr(
'Are you sure you want to restore the database? This will overwrite ALL existing data!',
)}
</p>
<div class="action-actionable">
<a href="#" class="fb-cta cta--red js-close-modal">
{tr('No')}
</a>
<a href="#" id="restore_database" class="fb-cta cta--yellow">
{tr('Yes')}
</a>
</div>
</div>;
return tuple($title, $content);
case 'reset-database':
$title =
<h4>
Expand Down
22 changes: 22 additions & 0 deletions src/models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private function __construct(
private int $id,
private int $levelId,
private string $filename,
private string $type,
) {}

public function getId(): int {
Expand All @@ -27,6 +28,10 @@ public function getFilename(): string {
return $this->filename;
}

public function getType(): string {
return $this->type;
}

public function getLevelId(): int {
return $this->levelId;
}
Expand Down Expand Up @@ -265,13 +270,30 @@ public function getLevelId(): int {
}
}

public static async function genImportAttachments(
int $level_id,
string $filename,
string $type,
): Awaitable<bool> {
$db = await self::genDb();
await $db->queryf(
'INSERT INTO attachments (filename, type, level_id, created_ts) VALUES (%s, %s, %d, NOW())',
$filename,
(string) $type,
$level_id,
);

return true;
}

private static function attachmentFromRow(
Map<string, string> $row,
): Attachment {
return new Attachment(
intval(must_have_idx($row, 'id')),
intval(must_have_idx($row, 'level_id')),
must_have_idx($row, 'filename'),
must_have_idx($row, 'type'),
);
}
}
Loading

0 comments on commit be35c6b

Please sign in to comment.