Passed
Pull Request — master (#5817)
by
unknown
07:57
created

Version20240924120200::replaceGifWithPng()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20240924120200 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Update HTML content blocks to replace old CKEditor image paths with new ones and convert .gif references to .png';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $this->entityManager->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $this->entityManager->/** @scrutinizer ignore-call */ 
22
                              clear();

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.

Loading history...
22
23
        // Define the content fields to update
24
        $updateConfigurations = [
25
            ['table' => 'c_tool_intro', 'field' => 'intro_text'],
26
            ['table' => 'c_course_description', 'field' => 'content'],
27
            ['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']],
28
            ['table' => 'c_quiz_question', 'fields' => ['description', 'question']],
29
            ['table' => 'c_quiz_answer', 'fields' => ['answer', 'comment']],
30
            ['table' => 'c_student_publication', 'field' => 'description'],
31
            ['table' => 'c_student_publication_comment', 'field' => 'comment'],
32
            ['table' => 'c_forum_post', 'field' => 'post_text'],
33
            ['table' => 'c_glossary', 'field' => 'description'],
34
            ['table' => 'c_survey', 'fields' => ['title', 'subtitle']],
35
            ['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']],
36
            ['table' => 'c_survey_question_option', 'field' => 'option_text'],
37
        ];
38
39
        foreach ($updateConfigurations as $config) {
40
            $this->updateContent($config);
41
        }
42
    }
43
44
    private function updateContent(array $config): void
45
    {
46
        $fields = isset($config['field']) ? [$config['field']] : $config['fields'] ?? [];
47
48
        foreach ($fields as $field) {
49
            $sql = "SELECT iid, {$field} FROM {$config['table']}";
50
            $result = $this->connection->executeQuery($sql);
51
            $items = $result->fetchAllAssociative();
52
53
            foreach ($items as $item) {
54
                $originalText = $item[$field];
55
                if (!empty($originalText)) {
56
                    $updatedText = $this->replaceGifWithPng($originalText);
57
                    if ($originalText !== $updatedText) {
58
                        $updateSql = "UPDATE {$config['table']} SET {$field} = :newText WHERE iid = :id";
59
                        $this->connection->executeQuery($updateSql, ['newText' => $updatedText, 'id' => $item['iid']]);
60
                    }
61
                }
62
            }
63
        }
64
    }
65
66
    private function replaceGifWithPng(string $content): string
67
    {
68
        $pattern = '/(src=["\'])(https?:\/\/[^\/]+\/)?(\/?web\/assets\/ckeditor\/plugins\/smiley\/images\/([a-zA-Z0-9_\-]+))\.(gif|png)(["\'])/i';
69
70
        $content = preg_replace_callback($pattern, function ($matches) {
71
            $prefix = $matches[1];
72
            $filename = $matches[4];
73
            $extension = 'png';
74
75
            return "{$prefix}/img/legacy/{$filename}.{$extension}{$matches[6]}";
76
        }, $content);
77
78
        return $content;
79
    }
80
}
81