|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\PersonalFile; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\ResourceNode; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
12
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
13
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
14
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
16
|
|
|
|
|
17
|
|
|
final class Version20230720143000 extends AbstractMigrationChamilo |
|
18
|
|
|
{ |
|
19
|
|
|
public function getDescription(): string |
|
20
|
|
|
{ |
|
21
|
|
|
return 'Migrate my_files of users to personal_files '; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function up(Schema $schema): void |
|
25
|
|
|
{ |
|
26
|
|
|
$kernel = $this->container->get('kernel'); |
|
|
|
|
|
|
27
|
|
|
$rootPath = $kernel->getProjectDir(); |
|
28
|
|
|
|
|
29
|
|
|
$fallbackUser = $this->getFallbackUser(); |
|
30
|
|
|
$directory = $this->getUpdateRootPath().'/app/upload/users/'; |
|
31
|
|
|
|
|
32
|
|
|
$variable = 'split_users_upload_directory'; |
|
33
|
|
|
$query = $this->entityManager->createQuery('SELECT s.selectedValue FROM Chamilo\CoreBundle\Entity\SettingsCurrent s WHERE s.variable = :variable') |
|
|
|
|
|
|
34
|
|
|
->setParameter('variable', $variable); |
|
35
|
|
|
$result = $query->getOneOrNullResult(); |
|
36
|
|
|
$splitUsersUploadDirectory = $result['selectedValue'] ?? 'false'; |
|
37
|
|
|
|
|
38
|
|
|
if ('true' === $splitUsersUploadDirectory) { |
|
39
|
|
|
$parentDirectories = glob($directory.'*', GLOB_ONLYDIR); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($parentDirectories as $parentDir) { |
|
42
|
|
|
$firstDigit = basename($parentDir); |
|
43
|
|
|
$subDirectories = glob($parentDir.'/*', GLOB_ONLYDIR); |
|
44
|
|
|
foreach ($subDirectories as $userDir) { |
|
45
|
|
|
$userId = basename($userDir); |
|
46
|
|
|
$this->processUserDirectory($userId, $userDir, $fallbackUser, true); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$userDirectories = glob($directory.'*', GLOB_ONLYDIR); |
|
51
|
|
|
foreach ($userDirectories as $userDir) { |
|
52
|
|
|
$userId = basename($userDir); |
|
53
|
|
|
$this->processUserDirectory($userId, $userDir, $fallbackUser, false); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function processUserDirectory(string $userId, string $userDir, User $fallbackUser, bool $splitUsersUploadDirectory): void |
|
59
|
|
|
{ |
|
60
|
|
|
$userEntity = $this->entityManager->getRepository(User::class)->find($userId); |
|
61
|
|
|
$userToAssign = $userEntity ?? $fallbackUser; |
|
62
|
|
|
|
|
63
|
|
|
if ($userEntity === null) { |
|
64
|
|
|
error_log("User with ID {$userId} not found. Using fallback_user."); |
|
65
|
|
|
} else { |
|
66
|
|
|
error_log("Processing files for user with ID {$userId}."); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($splitUsersUploadDirectory) { |
|
70
|
|
|
$baseDir = $userDir.'/'; |
|
71
|
|
|
} else { |
|
72
|
|
|
$baseDir = $this->getUpdateRootPath().'/app/upload/users/'.$userId.'/'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
error_log("Final path to check: {$baseDir}"); |
|
76
|
|
|
|
|
77
|
|
|
if (!is_dir($baseDir)) { |
|
78
|
|
|
error_log("Directory not found for user with ID {$userId}. Skipping."); |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$myFilesDir = $baseDir.'my_files/'; |
|
83
|
|
|
$files = glob($myFilesDir.'*'); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($files as $file) { |
|
86
|
|
|
if (!is_file($file)) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$title = basename($file); |
|
91
|
|
|
error_log("Processing file: {$file} for user with ID {$userToAssign->getId()}."); |
|
92
|
|
|
|
|
93
|
|
|
$queryBuilder = $this->entityManager->createQueryBuilder(); |
|
94
|
|
|
$queryBuilder |
|
95
|
|
|
->select('p') |
|
96
|
|
|
->from(ResourceNode::class, 'n') |
|
97
|
|
|
->innerJoin(PersonalFile::class, 'p', Join::WITH, 'p.resourceNode = n.id') |
|
98
|
|
|
->where('n.title = :title') |
|
99
|
|
|
->andWhere('n.creator = :creator') |
|
100
|
|
|
->setParameter('title', $title) |
|
101
|
|
|
->setParameter('creator', $userToAssign->getId()); |
|
102
|
|
|
|
|
103
|
|
|
$result = $queryBuilder->getQuery()->getOneOrNullResult(); |
|
104
|
|
|
|
|
105
|
|
|
if ($result) { |
|
106
|
|
|
error_log('MIGRATIONS :: '.$file.' (Skipped: Already exists) ...'); |
|
107
|
|
|
continue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
error_log("MIGRATIONS :: Associating file {$file} to user with ID {$userToAssign->getId()}."); |
|
111
|
|
|
$personalFile = new PersonalFile(); |
|
112
|
|
|
$personalFile->setTitle($title); |
|
113
|
|
|
$personalFile->setCreator($userToAssign); |
|
114
|
|
|
$personalFile->setParentResourceNode($userToAssign->getResourceNode()->getId()); |
|
115
|
|
|
$personalFile->setResourceName($title); |
|
116
|
|
|
$mimeType = mime_content_type($file); |
|
117
|
|
|
$uploadedFile = new UploadedFile($file, $title, $mimeType, null, true); |
|
118
|
|
|
$personalFile->setUploadFile($uploadedFile); |
|
119
|
|
|
$personalFile->addUserLink($userToAssign); |
|
120
|
|
|
|
|
121
|
|
|
// Save the object to the database |
|
122
|
|
|
$this->entityManager->persist($personalFile); |
|
123
|
|
|
$this->entityManager->flush(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function getFallbackUser(): ?User |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->entityManager->getRepository(User::class)->findOneBy(['status' => User::ROLE_FALLBACK], ['id' => 'ASC']); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.