Passed
Push — master ( 719760...59ab2e )
by Angel Fernando Quiroz
18:35
created

Version20230913162700::replaceOldURLsWithNew()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 40
rs 8.5866
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\Course;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CourseBundle\Repository\CDocumentRepository;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20230913162700 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Replace old document path by resource file path';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $container = $this->getContainer();
25
        $em = $this->getEntityManager();
26
27
        /** @var Connection $connection */
28
        $connection = $em->getConnection();
29
        $kernel = $container->get('kernel');
30
        $rootPath = $kernel->getProjectDir();
31
32
        /** @var CDocumentRepository $documentRepo */
33
        $documentRepo = $container->get(CDocumentRepository::class);
34
35
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
36
37
        /** @var Course $course */
38
        foreach ($q->toIterable() as $course) {
39
            $courseId = $course->getId();
40
            $courseDirectory = $course->getDirectory();
41
42
            if (empty($courseDirectory)) {
43
                continue;
44
            }
45
46
            // Tool intro
47
            $sql = "SELECT * FROM c_tool_intro WHERE c_id = {$courseId} ORDER BY iid";
48
            $result = $connection->executeQuery($sql);
49
            $items = $result->fetchAllAssociative();
50
            if (!empty($items)) {
51
                foreach ($items as $itemData) {
52
                    $originalIntroText = $itemData['intro_text'];
53
                    if (!empty($originalIntroText)) {
54
                        $updatedIntroText = $this->replaceOldURLsWithNew($originalIntroText, $courseDirectory, $courseId, $connection, $documentRepo);
55
                        if ($originalIntroText !== $updatedIntroText) {
56
                            $sql = 'UPDATE c_tool_intro SET intro_text = :newIntroText WHERE iid = :introId';
57
                            $params = [
58
                                'newIntroText' => $updatedIntroText,
59
                                'introId' => $itemData['iid'],
60
                            ];
61
                            $connection->executeQuery($sql, $params);
62
                        }
63
                    }
64
                }
65
            }
66
67
            // Course description
68
            $sql = "SELECT * FROM c_course_description WHERE c_id = {$courseId} ORDER BY iid";
69
            $result = $connection->executeQuery($sql);
70
            $items = $result->fetchAllAssociative();
71
            if (!empty($items)) {
72
                foreach ($items as $itemData) {
73
                    $originalContent = $itemData['content'];
74
                    if (!empty($originalContent)) {
75
                        $updatedContent = $this->replaceOldURLsWithNew($originalContent, $courseDirectory, $courseId, $connection, $documentRepo);
76
                        if ($originalContent !== $updatedContent) {
77
                            $sql = 'UPDATE c_course_description SET content = :newContent WHERE iid = :id';
78
                            $params = [
79
                                'newContent' => $updatedContent,
80
                                'id' => $itemData['iid'],
81
                            ];
82
                            $connection->executeQuery($sql, $params);
83
                        }
84
                    }
85
                }
86
            }
87
88
            // Quiz
89
            $sql = "SELECT * FROM c_quiz WHERE c_id = {$courseId} ORDER BY iid";
90
            $result = $connection->executeQuery($sql);
91
            $items = $result->fetchAllAssociative();
92
            if (!empty($items)) {
93
                foreach ($items as $itemData) {
94
                    $originalDescription = $itemData['description'];
95
                    if (!empty($originalDescription)) {
96
                        $updatedDescription = $this->replaceOldURLsWithNew($originalDescription, $courseDirectory, $courseId, $connection, $documentRepo);
97
                        if ($originalDescription !== $updatedDescription) {
98
                            $sql = 'UPDATE c_quiz SET description = :newDescription WHERE iid = :id';
99
                            $params = [
100
                                'newDescription' => $updatedDescription,
101
                                'id' => $itemData['iid'],
102
                            ];
103
                            $connection->executeQuery($sql, $params);
104
                        }
105
                    }
106
107
                    $originalText = $itemData['text_when_finished'];
108
                    if (!empty($originalText)) {
109
                        $updatedText = $this->replaceOldURLsWithNew($originalText, $courseDirectory, $courseId, $connection, $documentRepo);
110
                        if ($originalText !== $updatedText) {
111
                            $sql = 'UPDATE c_quiz SET text_when_finished = :newText WHERE iid = :id';
112
                            $params = [
113
                                'newText' => $updatedText,
114
                                'id' => $itemData['iid'],
115
                            ];
116
                            $connection->executeQuery($sql, $params);
117
                        }
118
                    }
119
                }
120
            }
121
122
            // Quiz question
123
            $sql = "SELECT * FROM c_quiz_question WHERE c_id = {$courseId} ORDER BY iid";
124
            $result = $connection->executeQuery($sql);
125
            $items = $result->fetchAllAssociative();
126
            if (!empty($items)) {
127
                foreach ($items as $itemData) {
128
                    $originalDescription = $itemData['description'];
129
                    if (!empty($originalDescription)) {
130
                        $updatedDescription = $this->replaceOldURLsWithNew($originalDescription, $courseDirectory, $courseId, $connection, $documentRepo);
131
                        if ($originalDescription !== $updatedDescription) {
132
                            $sql = 'UPDATE c_quiz_question SET description = :newDescription WHERE iid = :id';
133
                            $params = [
134
                                'newDescription' => $updatedDescription,
135
                                'id' => $itemData['iid'],
136
                            ];
137
                            $connection->executeQuery($sql, $params);
138
                        }
139
                    }
140
141
                    $originalQuestion = $itemData['question'];
142
                    if (!empty($originalQuestion)) {
143
                        $updatedQuestion = $this->replaceOldURLsWithNew($originalQuestion, $courseDirectory, $courseId, $connection, $documentRepo);
144
                        if ($originalQuestion !== $updatedQuestion) {
145
                            $sql = 'UPDATE c_quiz_question SET question = :newQuestion WHERE iid = :id';
146
                            $params = [
147
                                'newQuestion' => $updatedQuestion,
148
                                'id' => $itemData['iid'],
149
                            ];
150
                            $connection->executeQuery($sql, $params);
151
                        }
152
                    }
153
                }
154
            }
155
156
            // Quiz answer
157
            $sql = "SELECT * FROM c_quiz_answer WHERE c_id = {$courseId} ORDER BY iid";
158
            $result = $connection->executeQuery($sql);
159
            $items = $result->fetchAllAssociative();
160
            if (!empty($items)) {
161
                foreach ($items as $itemData) {
162
                    $originalAnswer = $itemData['answer'];
163
                    if (!empty($originalAnswer)) {
164
                        $updatedAnswer = $this->replaceOldURLsWithNew($originalAnswer, $courseDirectory, $courseId, $connection, $documentRepo);
165
                        if ($originalAnswer !== $updatedAnswer) {
166
                            $sql = 'UPDATE c_quiz_answer SET answer = :newAnswer WHERE iid = :id';
167
                            $params = [
168
                                'newAnswer' => $updatedAnswer,
169
                                'id' => $itemData['iid'],
170
                            ];
171
                            $connection->executeQuery($sql, $params);
172
                        }
173
                    }
174
175
                    $originalComment = $itemData['comment'];
176
                    if (!empty($originalComment)) {
177
                        $updatedComment = $this->replaceOldURLsWithNew($originalComment, $courseDirectory, $courseId, $connection, $documentRepo);
178
                        if ($originalComment !== $updatedComment) {
179
                            $sql = 'UPDATE c_quiz_answer SET comment = :newComment WHERE iid = :id';
180
                            $params = [
181
                                'newComment' => $updatedComment,
182
                                'id' => $itemData['iid'],
183
                            ];
184
                            $connection->executeQuery($sql, $params);
185
                        }
186
                    }
187
                }
188
            }
189
190
            // Student publication
191
            $sql = "SELECT * FROM c_student_publication WHERE c_id = {$courseId} ORDER BY iid";
192
            $result = $connection->executeQuery($sql);
193
            $items = $result->fetchAllAssociative();
194
            if (!empty($items)) {
195
                foreach ($items as $itemData) {
196
                    $originalWorkDescription = $itemData['description'];
197
                    if (!empty($originalWorkDescription)) {
198
                        $updatedWorkDescription = $this->replaceOldURLsWithNew($originalWorkDescription, $courseDirectory, $courseId, $connection, $documentRepo);
199
                        if ($originalWorkDescription !== $updatedWorkDescription) {
200
                            $sql = 'UPDATE c_student_publication SET description = :newDescription WHERE iid = :id';
201
                            $params = [
202
                                'newDescription' => $updatedWorkDescription,
203
                                'id' => $itemData['iid'],
204
                            ];
205
                            $connection->executeQuery($sql, $params);
206
                        }
207
                    }
208
                }
209
            }
210
211
            // Student publication comment
212
            $sql = "SELECT * FROM c_student_publication_comment WHERE c_id = {$courseId} ORDER BY iid";
213
            $result = $connection->executeQuery($sql);
214
            $items = $result->fetchAllAssociative();
215
            if (!empty($items)) {
216
                foreach ($items as $itemData) {
217
                    $originalWorkComment = $itemData['comment'];
218
                    if (!empty($originalWorkComment)) {
219
                        $updatedWorkComment = $this->replaceOldURLsWithNew($originalWorkComment, $courseDirectory, $courseId, $connection, $documentRepo);
220
                        if ($originalWorkComment !== $updatedWorkComment) {
221
                            $sql = 'UPDATE c_student_publication_comment SET comment = :newComment WHERE iid = :id';
222
                            $params = [
223
                                'newComment' => $updatedWorkComment,
224
                                'id' => $itemData['iid'],
225
                            ];
226
                            $connection->executeQuery($sql, $params);
227
                        }
228
                    }
229
                }
230
            }
231
232
            // Forum category
233
            $sql = "SELECT * FROM c_forum_category WHERE c_id = {$courseId} ORDER BY iid";
234
            $result = $connection->executeQuery($sql);
235
            $items = $result->fetchAllAssociative();
236
            if (!empty($items)) {
237
                foreach ($items as $itemData) {
238
                    $originalCatComment = $itemData['cat_comment'];
239
                    if (!empty($originalCatComment)) {
240
                        $updatedCatComment = $this->replaceOldURLsWithNew($originalCatComment, $courseDirectory, $courseId, $connection, $documentRepo);
241
                        if ($originalCatComment !== $updatedCatComment) {
242
                            $sql = 'UPDATE c_forum_category SET cat_comment = :newComment WHERE iid = :id';
243
                            $params = [
244
                                'newComment' => $updatedCatComment,
245
                                'id' => $itemData['iid'],
246
                            ];
247
                            $connection->executeQuery($sql, $params);
248
                        }
249
                    }
250
                }
251
            }
252
253
            // Forum
254
            $sql = "SELECT * FROM c_forum_forum WHERE c_id = {$courseId} ORDER BY iid";
255
            $result = $connection->executeQuery($sql);
256
            $items = $result->fetchAllAssociative();
257
            if (!empty($items)) {
258
                foreach ($items as $itemData) {
259
                    $originalForumComment = $itemData['forum_comment'];
260
                    if (!empty($originalForumComment)) {
261
                        $updatedForumComment = $this->replaceOldURLsWithNew($originalForumComment, $courseDirectory, $courseId, $connection, $documentRepo);
262
                        if ($originalForumComment !== $updatedForumComment) {
263
                            $sql = 'UPDATE c_forum_forum SET forum_comment = :newComment WHERE iid = :id';
264
                            $params = [
265
                                'newComment' => $updatedForumComment,
266
                                'id' => $itemData['iid'],
267
                            ];
268
                            $connection->executeQuery($sql, $params);
269
                        }
270
                    }
271
                }
272
            }
273
274
            // Forum post
275
            $sql = "SELECT * FROM c_forum_post WHERE c_id = {$courseId} ORDER BY iid";
276
            $result = $connection->executeQuery($sql);
277
            $items = $result->fetchAllAssociative();
278
            if (!empty($items)) {
279
                foreach ($items as $itemData) {
280
                    $originalPostText = $itemData['post_text'];
281
                    if (!empty($originalPostText)) {
282
                        $updatedPostText = $this->replaceOldURLsWithNew($originalPostText, $courseDirectory, $courseId, $connection, $documentRepo);
283
                        if ($originalPostText !== $updatedPostText) {
284
                            $sql = 'UPDATE c_forum_post SET post_text = :newText WHERE iid = :id';
285
                            $params = [
286
                                'newText' => $updatedPostText,
287
                                'id' => $itemData['iid'],
288
                            ];
289
                            $connection->executeQuery($sql, $params);
290
                        }
291
                    }
292
                }
293
            }
294
295
            // Glossary
296
            $sql = "SELECT * FROM c_glossary WHERE c_id = {$courseId} ORDER BY iid";
297
            $result = $connection->executeQuery($sql);
298
            $items = $result->fetchAllAssociative();
299
            if (!empty($items)) {
300
                foreach ($items as $itemData) {
301
                    $originalGlossaryDescription = $itemData['description'];
302
                    if (!empty($originalGlossaryDescription)) {
303
                        $updatedGlossaryDescription = $this->replaceOldURLsWithNew($originalGlossaryDescription, $courseDirectory, $courseId, $connection, $documentRepo);
304
                        if ($originalGlossaryDescription !== $updatedGlossaryDescription) {
305
                            $sql = 'UPDATE c_glossary SET description = :newDescription WHERE iid = :id';
306
                            $params = [
307
                                'newDescription' => $updatedGlossaryDescription,
308
                                'id' => $itemData['iid'],
309
                            ];
310
                            $connection->executeQuery($sql, $params);
311
                        }
312
                    }
313
                }
314
            }
315
316
            // Survey
317
            $sql = "SELECT * FROM c_survey WHERE c_id = {$courseId} ORDER BY iid";
318
            $result = $connection->executeQuery($sql);
319
            $items = $result->fetchAllAssociative();
320
            if (!empty($items)) {
321
                foreach ($items as $itemData) {
322
                    $originalSurveyTitle = $itemData['title'];
323
                    if (!empty($originalSurveyTitle)) {
324
                        $updatedSurveyTitle = $this->replaceOldURLsWithNew($originalSurveyTitle, $courseDirectory, $courseId, $connection, $documentRepo);
325
                        if ($originalSurveyTitle !== $updatedSurveyTitle) {
326
                            $sql = 'UPDATE c_survey SET title = :newTitle WHERE iid = :id';
327
                            $params = [
328
                                'newTitle' => $updatedSurveyTitle,
329
                                'id' => $itemData['iid'],
330
                            ];
331
                            $connection->executeQuery($sql, $params);
332
                        }
333
                    }
334
335
                    $originalSurveySubTitle = $itemData['subtitle'];
336
                    if (!empty($originalSurveySubTitle)) {
337
                        $updatedSurveySubTitle = $this->replaceOldURLsWithNew($originalSurveySubTitle, $courseDirectory, $courseId, $connection, $documentRepo);
338
                        if ($originalSurveySubTitle !== $updatedSurveySubTitle) {
339
                            $sql = 'UPDATE c_survey SET subtitle = :newSubtitle WHERE iid = :id';
340
                            $params = [
341
                                'newSubtitle' => $updatedSurveySubTitle,
342
                                'id' => $itemData['iid'],
343
                            ];
344
                            $connection->executeQuery($sql, $params);
345
                        }
346
                    }
347
                }
348
            }
349
350
            // Survey question
351
            $sql = "SELECT * FROM c_survey_question WHERE c_id = {$courseId} ORDER BY iid";
352
            $result = $connection->executeQuery($sql);
353
            $items = $result->fetchAllAssociative();
354
            if (!empty($items)) {
355
                foreach ($items as $itemData) {
356
                    $originalSurveyQuestion = $itemData['survey_question'];
357
                    if (!empty($originalSurveyQuestion)) {
358
                        $updatedSurveyQuestion = $this->replaceOldURLsWithNew($originalSurveyQuestion, $courseDirectory, $courseId, $connection, $documentRepo);
359
                        if ($originalSurveyQuestion !== $updatedSurveyQuestion) {
360
                            $sql = 'UPDATE c_survey_question SET survey_question = :newQuestion WHERE iid = :id';
361
                            $params = [
362
                                'newQuestion' => $updatedSurveyQuestion,
363
                                'id' => $itemData['iid'],
364
                            ];
365
                            $connection->executeQuery($sql, $params);
366
                        }
367
                    }
368
369
                    $originalSurveyQuestionComment = $itemData['survey_question_comment'];
370
                    if (!empty($originalSurveyQuestionComment)) {
371
                        $updatedSurveyQuestionComment = $this->replaceOldURLsWithNew($originalSurveyQuestionComment, $courseDirectory, $courseId, $connection, $documentRepo);
372
                        if ($originalSurveyQuestionComment !== $updatedSurveyQuestionComment) {
373
                            $sql = 'UPDATE c_survey_question SET survey_question_comment = :newComment WHERE iid = :id';
374
                            $params = [
375
                                'newComment' => $updatedSurveyQuestionComment,
376
                                'id' => $itemData['iid'],
377
                            ];
378
                            $connection->executeQuery($sql, $params);
379
                        }
380
                    }
381
                }
382
            }
383
384
            // Survey question option
385
            $sql = "SELECT * FROM c_survey_question_option WHERE c_id = {$courseId} ORDER BY iid";
386
            $result = $connection->executeQuery($sql);
387
            $items = $result->fetchAllAssociative();
388
            if (!empty($items)) {
389
                foreach ($items as $itemData) {
390
                    $originalOptionText = $itemData['option_text'];
391
                    if (!empty($originalOptionText)) {
392
                        $updatedOptionText = $this->replaceOldURLsWithNew($originalOptionText, $courseDirectory, $courseId, $connection, $documentRepo);
393
                        if ($originalOptionText !== $updatedOptionText) {
394
                            $sql = 'UPDATE c_survey_question_option SET option_text = :newText WHERE iid = :id';
395
                            $params = [
396
                                'newText' => $updatedOptionText,
397
                                'id' => $itemData['iid'],
398
                            ];
399
                            $connection->executeQuery($sql, $params);
400
                        }
401
                    }
402
                }
403
            }
404
        }
405
    }
406
407
    private function replaceOldURLsWithNew($itemDataText, $courseDirectory, $courseId, $connection, $documentRepo): null|array|string
408
    {
409
        $contentText = $itemDataText;
410
411
        $pattern = '/(src|href)=(["\'])(\/courses\/'.preg_quote($courseDirectory, '/').'\/[^"\']+\.\w+)\2/i';
412
        preg_match_all($pattern, $contentText, $matches);
413
        $videosSrcPath = $matches[3];
414
415
        if (!empty($videosSrcPath)) {
416
            foreach ($videosSrcPath as $index => $videoPath) {
417
                $documentPath = str_replace('/courses/'.$courseDirectory.'/document/', '/', $videoPath);
418
                $sql = "SELECT iid, path, resource_node_id
419
                        FROM c_document
420
                        WHERE
421
                              c_id = $courseId AND
422
                              path LIKE '$documentPath'
423
                ";
424
                $result = $connection->executeQuery($sql);
425
                $documents = $result->fetchAllAssociative();
426
427
                if (!empty($documents)) {
428
                    foreach ($documents as $documentData) {
429
                        $resourceNodeId = (int) $documentData['resource_node_id'];
430
                        $documentFile = $documentRepo->getResourceFromResourceNode($resourceNodeId);
431
                        if ($documentFile) {
432
                            $newUrl = $documentRepo->getResourceFileUrl($documentFile);
433
                            if (!empty($newUrl)) {
434
                                $patternForReplacement = '/'.$matches[1][$index].'=(["\'])'.preg_quote($videoPath, '/').'\1/i';
435
                                $replacement = $matches[1][$index].'=$1'.$newUrl.'$1';
436
                                $contentText = preg_replace($patternForReplacement, $replacement, $contentText);
437
                                error_log('$documentPath ->'.$documentPath);
438
                                error_log('newUrl ->'.$newUrl);
439
                            }
440
                        }
441
                    }
442
                }
443
            }
444
        }
445
446
        return $contentText; // Return the updated content text.
447
    }
448
}
449