Commit bdeddc87 authored by dj3c1t's avatar dj3c1t

base import previous version

parent d43554db
<?php
namespace Dj3c1t\SourceMLBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ImportController extends Controller {
public function previousVersionAction(Request $request) {
$message = "";
try {
$this->get('sourceml.import_previous_version')->import();
$message = "[".date("Y-m-d H:i:s")."] import done";
}
catch(\Exception $e) {
$message = "[".date("Y-m-d H:i:s")."] import ERROR ".$e->getMessage();
}
return new Response($message."\n");
}
}
# -------------------------------------------------------------------
# Import
#
sourceml_import_previous_version:
path: /import/previous_version
defaults: { _controller: Dj3c1tSourceMLBundle:Import:previousVersion }
# -------------------------------------------------------------------
# Status
#
......
......@@ -55,3 +55,7 @@ services:
sourceml.source_waveform:
class: Dj3c1t\SourceMLBundle\Service\SourceWaveform
arguments: ['@service_container']
sourceml.import_previous_version:
class: Dj3c1t\SourceMLBundle\Service\ImportPreviousVersion
arguments: ['@service_container']
<?php
namespace Dj3c1t\SourceMLBundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Dj3c1t\SourceMLBundle\Entity\Source;
use Dj3c1t\SourceMLBundle\Entity\DerivationSource;
use Dj3c1t\AppBundle\Entity\User;
class ImportPreviousVersion {
protected $container;
protected $mw_app;
protected $mw_env;
protected $mw_data;
protected $mw_sgbd;
public function __construct(Container $container) {
$this->container = $container;
$this->init();
}
// ----------------------------------------------------------------------
// init
//
public function init() {
$request = $this->container->get('request_stack')->getCurrentRequest();
if(!($this->mw_app = $request->attributes->get("mw_app"))) {
throw new \Exception("can't find mw_app in request attributes");
}
$this->mw_env = $this->mw_app->env();
$this->mw_data = $this->mw_env->data();
$this->mw_sgbd = $this->mw_data->sgbd();
}
// ----------------------------------------------------------------------
// import
//
public function import() {
$this->log("start import");
$this->log(
"source installation: "
.dirname($this->mw_env->path("mw_dir"))
);
$this->log(
"destination installation: "
.dirname($this->container->get('kernel')->getRootDir())
);
// $this->importUsers();
$this->importLicences();
$this->importAuthors();
$this->importSources();
$this->importSourcesAuthors();
$this->importSourcesInfos();
$this->importCompositions();
$this->importDerivation();
$this->importDocument();
}
// ----------------------------------------------------------------------
// users
//
public function importUsers() {
$this->log("import users");
$em = $this->container->get('doctrine')->getManager();
$userRepo = $em->getRepository('Dj3c1tAppBundle:User');
$roleRepo = $em->getRepository('Dj3c1tAppBundle:Role');
$adminRole = $roleRepo->findOneByRole("ROLE_ADMIN");
$userRole = $roleRepo->findOneByRole("ROLE_USER");
$encoder_factory = $this->container->get('security.encoder_factory');
if(($users = $this->mw_data->users()) === null) {
$this->error("can't load users from mw_data");
}
$mw_users = array();
$mw_emails = array();
foreach($users['list'] as $mw_user) {
$this->log("creating new user for ".$mw_user["login"]);
if(isset($mw_users[$mw_user["login"]]) || $userRepo->findOneByUsername($mw_user["login"])) {
$this->log("user ".$mw_user["login"]." already exists, skiping this one");
continue;
}
$mw_users[$mw_user["login"]] = true;
if(isset($mw_emails[$mw_user["email"]]) || $userRepo->findOneByEmail($mw_user["email"])) {
$this->log("user ".$mw_user["login"]." has an email (".$mw_user["email"].") already used by another user");
$this->log("please set a different email per user before importing");
$this->error("can't import users");
}
$mw_emails[$mw_user["email"]] = true;
$user = new User();
$user->setIsActive(true);
$user->setUsername($mw_user["login"]);
$user->setSalt("");
$user->setPassword(
$encoder_factory->getEncoder($user)->encodePassword(
substr(md5(rand()), 0, 8),
$user->getSalt()
)
);
$user->setEmail($mw_user["email"]);
foreach($mw_user["roles"] as $mw_role_id) {
switch($mw_role_id) {
case 1:
if(!$user->hasRole($userRole)) {
$user->addRole($userRole);
}
break;
case 2:
case 3:
if(!$user->hasRole($adminRole)) {
$user->addRole($adminRole);
}
break;
}
}
$em->persist($user);
}
$this->log("persisting users to database");
$em->flush();
}
// ----------------------------------------------------------------------
// licences
//
public function importLicences() {
$this->log("import licences");
// todo ...
}
// ----------------------------------------------------------------------
// authors
//
public function importAuthors() {
$this->log("import authors");
// todo ...
}
// ----------------------------------------------------------------------
// sources
//
public function importSources() {
$this->log("import sources");
// todo ...
}
// ----------------------------------------------------------------------
// sources authors
//
public function importSourcesAuthors() {
$this->log("import sources authors");
// todo ...
}
// ----------------------------------------------------------------------
// sources infos : description, order and image
//
public function importSourcesInfos() {
$this->log("import sources infos");
// todo ...
}
// ----------------------------------------------------------------------
// compositions
//
public function importCompositions() {
$this->log("import compositions");
// todo ...
}
// ----------------------------------------------------------------------
// derivations
//
public function importDerivation() {
$this->log("import derivations");
// todo ...
}
// ----------------------------------------------------------------------
// documents
//
public function importDocument() {
$this->log("import documents");
// todo ...
}
// ----------------------------------------------------------------------
// utils
//
public function log($content) {
echo "[".date("Y-m-d H:i:s")."] ".$content."\n";
}
public function error($content) {
throw new \Exception("[".date("Y-m-d H:i:s")."] ERROR ".$content."\n");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment