|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
|
8
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse; |
|
9
|
|
|
use Chamilo\CoreBundle\Entity\ResourceLink; |
|
10
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
11
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
|
13
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
|
14
|
|
|
use Chamilo\CourseBundle\Entity\CDocument; |
|
15
|
|
|
use Chamilo\CourseBundle\Repository\CDocumentRepository; |
|
16
|
|
|
use Chamilo\CourseBundle\Repository\CGroupRepository; |
|
17
|
|
|
use Chamilo\Kernel; |
|
18
|
|
|
use Doctrine\DBAL\Connection; |
|
19
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Version20201212203625 extends AbstractMigrationChamilo |
|
26
|
|
|
{ |
|
27
|
|
|
public function getDescription(): string |
|
28
|
|
|
{ |
|
29
|
|
|
return 'Migrate documents'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function up(Schema $schema): void |
|
33
|
|
|
{ |
|
34
|
|
|
$container = $this->getContainer(); |
|
35
|
|
|
$doctrine = $container->get('doctrine'); |
|
36
|
|
|
$em = $doctrine->getManager(); |
|
37
|
|
|
/** @var Connection $connection */ |
|
38
|
|
|
$connection = $em->getConnection(); |
|
39
|
|
|
|
|
40
|
|
|
$documentRepo = $container->get(CDocumentRepository::class); |
|
41
|
|
|
$courseRepo = $container->get(CourseRepository::class); |
|
42
|
|
|
$sessionRepo = $container->get(SessionRepository::class); |
|
43
|
|
|
$groupRepo = $container->get(CGroupRepository::class); |
|
44
|
|
|
$userRepo = $container->get(UserRepository::class); |
|
45
|
|
|
|
|
46
|
|
|
/** @var Kernel $kernel */ |
|
47
|
|
|
$kernel = $container->get('kernel'); |
|
48
|
|
|
$rootPath = $kernel->getProjectDir(); |
|
49
|
|
|
|
|
50
|
|
|
$userList = []; |
|
51
|
|
|
$groupList = []; |
|
52
|
|
|
$sessionList = []; |
|
53
|
|
|
$batchSize = self::BATCH_SIZE; |
|
54
|
|
|
$urlRepo = $em->getRepository(AccessUrl::class); |
|
55
|
|
|
$urls = $urlRepo->findAll(); |
|
56
|
|
|
|
|
57
|
|
|
// Adding documents to the resource node tree. |
|
58
|
|
|
$admin = $this->getAdmin(); |
|
59
|
|
|
/** @var AccessUrl $url */ |
|
60
|
|
|
foreach ($urls as $url) { |
|
61
|
|
|
$accessUrlRelCourses = $url->getCourses(); |
|
62
|
|
|
/** @var AccessUrlRelCourse $accessUrlRelCourse */ |
|
63
|
|
|
foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
|
64
|
|
|
$counter = 1; |
|
65
|
|
|
$course = $accessUrlRelCourse->getCourse(); |
|
66
|
|
|
$courseId = $course->getId(); |
|
67
|
|
|
$courseCode = $course->getCode(); |
|
68
|
|
|
$course = $courseRepo->find($courseId); |
|
69
|
|
|
|
|
70
|
|
|
$sql = "SELECT * FROM c_document WHERE c_id = $courseId ORDER BY filetype DESC"; |
|
71
|
|
|
$result = $connection->executeQuery($sql); |
|
72
|
|
|
$documents = $result->fetchAllAssociative(); |
|
73
|
|
|
foreach ($documents as $documentData) { |
|
74
|
|
|
$documentId = $documentData['iid']; |
|
75
|
|
|
$documentPath = $documentData['path']; |
|
76
|
|
|
|
|
77
|
|
|
/** @var CDocument $document */ |
|
78
|
|
|
$document = $documentRepo->find($documentId); |
|
79
|
|
|
if ($document->hasResourceNode()) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$sql = "SELECT * FROM c_item_property |
|
84
|
|
|
WHERE tool = 'document' AND c_id = $courseId AND ref = $documentId"; |
|
85
|
|
|
$result = $connection->executeQuery($sql); |
|
86
|
|
|
$items = $result->fetchAllAssociative(); |
|
87
|
|
|
|
|
88
|
|
|
// For some reason this document doesnt have a c_item_property value. |
|
89
|
|
|
if (empty($items)) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$createNode = false; |
|
94
|
|
|
$resourceNode = null; |
|
95
|
|
|
$parent = null; |
|
96
|
|
|
if ('.' !== dirname($documentPath)) { |
|
97
|
|
|
$parentId = \DocumentManager::get_document_id( |
|
98
|
|
|
['real_id' => $courseId], |
|
99
|
|
|
dirname($documentPath) |
|
100
|
|
|
); |
|
101
|
|
|
$parent = $documentRepo->find($parentId); |
|
102
|
|
|
/*$dirList = explode('/', $documentPath); |
|
103
|
|
|
$dirList = array_filter($dirList); |
|
104
|
|
|
$len = count($dirList) + 1; |
|
105
|
|
|
$realDir = ''; |
|
106
|
|
|
for ($i = 1; $i < $len; $i++) { |
|
107
|
|
|
$realDir .= '/'.(isset($dirList[$i]) ? $dirList[$i] : ''); |
|
108
|
|
|
$parentId = \DocumentManager::get_document_id(['real_id'=> $courseId], $realDir); |
|
109
|
|
|
var_dump($parentId); |
|
110
|
|
|
if (!empty($parentId)) { |
|
111
|
|
|
} |
|
112
|
|
|
}*/ |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (null === $parent) { |
|
116
|
|
|
$parent = $course; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$document->setParent($parent); |
|
120
|
|
|
foreach ($items as $item) { |
|
121
|
|
|
$sessionId = $item['session_id']; |
|
122
|
|
|
$visibility = $item['visibility']; |
|
123
|
|
|
$userId = $item['insert_user_id']; |
|
124
|
|
|
$groupId = $item['to_group_id']; |
|
125
|
|
|
|
|
126
|
|
|
$newVisibility = ResourceLink::VISIBILITY_PENDING; |
|
127
|
|
|
switch ($visibility) { |
|
128
|
|
|
case 0: |
|
129
|
|
|
$newVisibility = ResourceLink::VISIBILITY_PENDING; |
|
130
|
|
|
break; |
|
131
|
|
|
case 1: |
|
132
|
|
|
$newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
|
133
|
|
|
break; |
|
134
|
|
|
case 2: |
|
135
|
|
|
$newVisibility = ResourceLink::VISIBILITY_DELETED; |
|
136
|
|
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (isset($userList[$userId])) { |
|
140
|
|
|
$user = $userList[$userId]; |
|
141
|
|
|
} else { |
|
142
|
|
|
$user = $userList[$userId] = $userRepo->find($userId); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (null === $user) { |
|
146
|
|
|
$user = $admin; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$session = null; |
|
150
|
|
|
if (!empty($sessionId)) { |
|
151
|
|
|
if (isset($sessionList[$sessionId])) { |
|
152
|
|
|
$session = $sessionList[$sessionId]; |
|
153
|
|
|
} else { |
|
154
|
|
|
$session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$group = null; |
|
159
|
|
|
if (!empty($groupId)) { |
|
160
|
|
|
if (isset($groupList[$groupId])) { |
|
161
|
|
|
$group = $groupList[$groupId]; |
|
162
|
|
|
} else { |
|
163
|
|
|
$group = $groupList[$groupId] = $groupRepo->find($groupId); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (null === $resourceNode) { |
|
168
|
|
|
$resourceNode = $documentRepo->addResourceNode($document, $user, $parent); |
|
169
|
|
|
$em->persist($resourceNode); |
|
170
|
|
|
} |
|
171
|
|
|
$document->addCourseLink($course, $session, $group, $newVisibility); |
|
172
|
|
|
$em->persist($document); |
|
173
|
|
|
$createNode = true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/document/'.$documentPath; |
|
177
|
|
|
if (!is_dir($filePath)) { |
|
178
|
|
|
$createNode = false; |
|
179
|
|
|
if (file_exists($filePath)) { |
|
180
|
|
|
$mimeType = mime_content_type($filePath); |
|
181
|
|
|
$file = new UploadedFile($filePath, basename($documentPath), $mimeType, null, true); |
|
182
|
|
|
if ($file) { |
|
183
|
|
|
$createNode = true; |
|
184
|
|
|
$documentRepo->addFile($document, $file); |
|
185
|
|
|
} else { |
|
186
|
|
|
$this->warnIf( |
|
187
|
|
|
true, |
|
188
|
|
|
'Cannot migrate doc #'.$documentData['iid'].' path: '.$documentPath.' ' |
|
189
|
|
|
); |
|
190
|
|
|
$createNode = false; |
|
191
|
|
|
} |
|
192
|
|
|
} else { |
|
193
|
|
|
$this->warnIf( |
|
194
|
|
|
true, |
|
195
|
|
|
'Cannot migrate doc #'.$documentData['iid'].' file not found: '.$documentPath |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
//var_dump($createNode,$documentPath, $documentData['iid'], file_exists($filePath)); |
|
201
|
|
|
$em->persist($document); |
|
202
|
|
|
$em->flush(); |
|
203
|
|
|
//$em->clear(); |
|
204
|
|
|
/*if ($createNode) { |
|
205
|
|
|
$em->persist($document); |
|
206
|
|
|
if (0 === $counter % $batchSize) { |
|
207
|
|
|
$em->flush(); |
|
208
|
|
|
$em->clear(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$counter++; |
|
212
|
|
|
} else { |
|
213
|
|
|
$em->clear(); |
|
214
|
|
|
}*/ |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$em->flush(); |
|
219
|
|
|
$em->clear(); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function down(Schema $schema): void |
|
224
|
|
|
{ |
|
225
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|