158 lines
3.8 KiB
PHP
158 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: lucas
|
|
* Date: 15/03/18
|
|
* Time: 15:50
|
|
*/
|
|
|
|
namespace api\module\department;
|
|
|
|
|
|
use database\core\Repo;
|
|
use database\repo\meta;
|
|
use error\core\Error;
|
|
use error\core\Err;
|
|
use database\repo\department;
|
|
use Ifsnop\Mysqldump\Mysqldump;
|
|
|
|
class versionController{
|
|
|
|
|
|
/* (4) List available versions for this department
|
|
*
|
|
* @return versions<array> Version list
|
|
*
|
|
---------------------------------------------------------*/
|
|
public function get($args){
|
|
|
|
//search for the current department in the session and return its versions
|
|
/** @var meta $depRepo */
|
|
$depRepo = Repo::getRepo("meta");
|
|
|
|
//if no department found, return an empty array
|
|
return ['versions' => $depRepo->getAllVersions($_SESSION['CurrentDepartmentId']) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Remove an existing version for this department
|
|
*
|
|
* @version<int> Version name (typically snapshot date)
|
|
*
|
|
* @return deleted<bool> Whether the version has been deleted
|
|
*
|
|
---------------------------------------------------------*/
|
|
public function delete($args){
|
|
$version = null;
|
|
extract($args);
|
|
|
|
/* (1) Try to delete */
|
|
$deleted = Repo::getRepo("meta")->deleteVersion($version);
|
|
|
|
if( !$deleted )
|
|
return ['error' => new Error(Err::ModuleError)];
|
|
|
|
/* (2) Update version list */
|
|
$_SESSION['VERSION']['list'] = Repo::getRepo("meta")->getAllVersions($_SESSION['CurrentDepartmentId']);
|
|
|
|
/* (3) Update current */
|
|
$_SESSION['VERSION']['current'] = null;
|
|
|
|
// select version with default = 1
|
|
foreach($_SESSION['VERSION']['list'] as $v){
|
|
if( $v['default'] == 1 ){
|
|
$_SESSION['VERSION']['current'] = intval($v['iddatabase']);
|
|
$_SESSION['CurrentDatabase'] = $v['dbName'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
// if no default -> select first
|
|
if( !is_int($_SESSION['VERSION']) ){
|
|
$_SESSION['VERSION']['current'] = intval($_SESSION['VERSION']['list'][0]['iddatabase']);
|
|
$_SESSION['CurrentDatabase'] = $_SESSION['VERSION']['list'][0]['dbName'];
|
|
}
|
|
|
|
return ['deleted' => true];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Creates a new version (snapshot of database) from now
|
|
*
|
|
* @return created_id<String> The created version id (date)
|
|
*
|
|
---------------------------------------------------------*/
|
|
public function post($args){
|
|
$label = null;
|
|
extract($args);
|
|
|
|
/* (2) Try to create the snapshot */
|
|
try{
|
|
|
|
/* (2.1) Get database configuration */
|
|
$conf = Repo::getDBConfig();
|
|
|
|
/* (2.2) Try to dump the database */
|
|
/** @var Mysqldump*/
|
|
$dump = new Mysqldump(
|
|
'mysql:host='.$conf['host'].';dbname='.$_SESSION['CurrentDatabase'],
|
|
$conf['username'],
|
|
$conf['password']
|
|
);
|
|
|
|
//create temporary file;
|
|
$file = tmpfile();
|
|
//get URI
|
|
$metaDatas = stream_get_meta_data($file);
|
|
$tmpFilename = $metaDatas['uri'];
|
|
//close file pointer
|
|
fclose($file);
|
|
//fill the file with sql dump
|
|
$dump->start($tmpFilename);
|
|
|
|
/** @var department $depRepo */
|
|
$depRepo = Repo::getRepo("department");
|
|
$dbName = $depRepo->createVersionDatabase($_SESSION['CurrentDepartmentId'],file_get_contents($tmpFilename));
|
|
|
|
/** @var meta $metaRep */
|
|
$metaRep = Repo::getRepo("meta");
|
|
$versionId = $metaRep->createVersion($label, $dbName,$_SESSION['CurrentDepartmentId'] ,false);
|
|
|
|
/* (2.5) Return status */
|
|
return ['created_id' => $versionId ];
|
|
|
|
/* (3) On error -> dispatch error */
|
|
}catch(\Exception $e){
|
|
|
|
return ['error' => new Error(Err::RepoError)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* (6) Modify a version and use it
|
|
*
|
|
*
|
|
* @return bool success
|
|
*
|
|
---------------------------------------------------------*/
|
|
public function put($args){
|
|
$label = null;
|
|
$version = null;
|
|
$default = null;
|
|
extract($args);
|
|
|
|
/** @var meta $metaRepo */
|
|
$metaRepo = Repo::getRepo("meta");
|
|
|
|
|
|
/* (4) Return status */
|
|
return [ 'updated' => $metaRepo->updateVersion($version,$label,$default) ];
|
|
|
|
}
|
|
} |