Passed
Push — dependabot/npm_and_yarn/microm... ( e84ba6...f2f212 )
by
unknown
10:03
created

Version20241010111200::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 27
rs 9.6666
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 Chamilo\CoreBundle\Repository\ResourceNodeRepository;
11
use Chamilo\CourseBundle\Entity\CDocument;
12
use Chamilo\CourseBundle\Repository\CDocumentRepository;
13
use Doctrine\DBAL\Schema\Schema;
14
use Exception;
15
16
final class Version20241010111200 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Update document and link URLs in HTML content blocks and replace old legacy paths with new resource paths';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $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

25
        $this->entityManager->/** @scrutinizer ignore-call */ 
26
                              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...
26
27
        // Define the content fields to update
28
        $updateConfigurations = [
29
            ['table' => 'c_tool_intro', 'field' => 'intro_text'],
30
            ['table' => 'c_course_description', 'field' => 'content'],
31
            ['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']],
32
            ['table' => 'c_quiz_question', 'fields' => ['description', 'question']],
33
            ['table' => 'c_quiz_answer', 'fields' => ['answer', 'comment']],
34
            ['table' => 'c_student_publication', 'field' => 'description'],
35
            ['table' => 'c_student_publication_comment', 'field' => 'comment'],
36
            ['table' => 'c_forum_post', 'field' => 'post_text'],
37
            ['table' => 'c_glossary', 'field' => 'description'],
38
            ['table' => 'c_survey', 'fields' => ['title', 'subtitle']],
39
            ['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']],
40
            ['table' => 'c_survey_question_option', 'field' => 'option_text'],
41
        ];
42
43
        $documentRepo = $this->container->get(CDocumentRepository::class);
0 ignored issues
show
Bug introduced by
The method get() 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

43
        /** @scrutinizer ignore-call */ 
44
        $documentRepo = $this->container->get(CDocumentRepository::class);

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...
44
45
        foreach ($updateConfigurations as $config) {
46
           $this->updateContent($config, $documentRepo);
47
        }
48
49
        $this->updateDocumentLinks();
50
    }
51
52
    /**
53
     * Updates content fields with new URLs.
54
     */
55
    private function updateContent(array $config, $documentRepo): void
56
    {
57
        $fields = isset($config['field']) ? [$config['field']] : $config['fields'] ?? [];
58
        foreach ($fields as $field) {
59
            $sql = "SELECT iid, {$field} FROM {$config['table']}";
60
            $result = $this->connection->executeQuery($sql);
61
            $items = $result->fetchAllAssociative();
62
            foreach ($items as $item) {
63
                $originalText = $item[$field];
64
                if (is_string($originalText) && trim($originalText) !== '') {
65
                    $updatedText = $this->replaceOldURLs($originalText, $documentRepo);
66
                    if ($originalText !== $updatedText) {
67
                        $updateSql = "UPDATE {$config['table']} SET {$field} = :newText WHERE iid = :id";
68
                        $this->connection->executeQuery($updateSql, ['newText' => $updatedText, 'id' => $item['iid']]);
69
                    }
70
                }
71
            }
72
        }
73
    }
74
75
    /**
76
     * Updates HTML content in document files by replacing old URLs with new resource paths.
77
     */
78
    private function updateDocumentLinks(): void
79
    {
80
        $sql = "SELECT iid, resource_node_id FROM c_document WHERE filetype = 'file'";
81
        $result = $this->connection->executeQuery($sql);
82
        $items = $result->fetchAllAssociative();
83
84
        $documentRepo = $this->container->get(CDocumentRepository::class);
85
        $resourceNodeRepo = $this->container->get(ResourceNodeRepository::class);
86
87
        foreach ($items as $item) {
88
            /** @var CDocument $document */
89
            $document = $documentRepo->find($item['iid']);
90
            if (!$document) {
91
                continue;
92
            }
93
94
            $resourceNode = $document->getResourceNode();
95
            if (!$resourceNode || !$resourceNode->hasResourceFile()) {
96
                continue;
97
            }
98
99
            $resourceFile = $resourceNode->getResourceFiles()->first();
100
            if ($resourceFile && $resourceFile->getMimeType() === 'text/html') {
101
                try {
102
                    $content = $resourceNodeRepo->getResourceNodeFileContent($resourceNode);
103
                    if (is_string($content) && trim($content) !== '') {
104
                        $updatedContent = $this->replaceOldURLs($content, $documentRepo);
105
                        if ($content !== $updatedContent) {
106
                            $documentRepo->updateResourceFileContent($document, $updatedContent);
107
                            $documentRepo->update($document);
108
                        }
109
                    }
110
                } catch (Exception $e) {
111
                    // error_log("Error processing file for document ID {$item['iid']}: " . $e->getMessage());
112
                }
113
            }
114
        }
115
    }
116
117
    /**
118
     * Replace old URLs with new Vue.js resource paths.
119
     */
120
    private function replaceOldURLs(string $content, $documentRepo): string
121
    {
122
        // Replace document URLs
123
        $content = $this->replaceOldDocumentURLs($content, $documentRepo);
124
        // Replace link URLs
125
        return $this->replaceOldLinkURLs($content);
126
    }
127
128
    /**
129
     * Replace old document URLs with the new relative paths.
130
     */
131
    private function replaceOldDocumentURLs(string $content, $documentRepo): string
132
    {
133
        $pattern = '/(href|src)=["\'](https?:\/\/[^\/]+)?\/main\/document\/(document\.php|show_content\.php|showinframes\.php)\?([^"\']*)["\']/i';
134
135
        return preg_replace_callback($pattern, function ($matches) use ($documentRepo) {
136
            $attribute = $matches[1];
137
            $params = str_replace('&amp;', '&', $matches[4]);
138
139
            parse_str($params, $parsedParams);
140
            $documentId = $parsedParams['id'] ?? null;
141
            $courseId = $parsedParams['cid'] ?? null;
142
            $sessionId = $parsedParams['id_session'] ?? $parsedParams['sid'] ?? '0';
143
            $groupId = $parsedParams['gidReq'] ?? $parsedParams['gid'] ?? '0';
144
145
            if (!$courseId && isset($parsedParams['cidReq'])) {
146
                $courseCode = $parsedParams['cidReq'];
147
                $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1';
148
                $stmt = $this->connection->executeQuery($sql, ['code' => $courseCode]);
149
                $course = $stmt->fetch();
150
151
                if ($course) {
152
                    $courseId = $course['id'];
153
                }
154
            }
155
156
            if ($documentId && $courseId) {
157
                $sql = "SELECT iid, filetype, resource_node_id FROM c_document WHERE iid = :documentId";
158
                $result = $this->connection->executeQuery($sql, ['documentId' => $documentId]);
159
                $documents = $result->fetchAllAssociative();
160
161
                if (!empty($documents)) {
162
                    $documentData = $documents[0];
163
                    if ($documentData['filetype'] === 'folder') {
164
                        $newUrl = $this->generateFolderUrl((int)$documentData['resource_node_id'], (int)$courseId, $sessionId, $groupId);
165
                    } else {
166
                        $document = $documentRepo->find($documentId);
167
                        $newUrl = $documentRepo->getResourceFileUrl($document);
168
                    }
169
170
                    return sprintf('%s="%s"', $attribute, $newUrl);
171
                }
172
            } elseif ($courseId) {
173
                $sql = "SELECT resource_node_id FROM course WHERE id = :courseId";
174
                $result = $this->connection->executeQuery($sql, ['courseId' => $courseId]);
175
                $course = $result->fetch();
176
177
                if ($course && isset($course['resource_node_id'])) {
178
                    $newUrl = $this->generateFolderUrl((int)$course['resource_node_id'], (int)$courseId, $sessionId, $groupId);
179
180
                    return sprintf('%s="%s"', $attribute, $newUrl);
181
                }
182
            }
183
184
            return $matches[0];
185
        }, $content);
186
    }
187
188
    /**
189
     * Replace old link URLs.
190
     */
191
    private function replaceOldLinkURLs(string $content): string
192
    {
193
        $pattern = '/(href|src)=["\'](https?:\/\/[^\/]+)?\/main\/link\/link\.php\?([^"\']*)["\']/i';
194
195
        return preg_replace_callback($pattern, function ($matches) {
196
            $attribute = $matches[1];
197
            $params = str_replace('&amp;', '&', $matches[3]);
198
            parse_str($params, $parsedParams);
199
200
            $courseId = isset($parsedParams['cid']) ? (int)$parsedParams['cid'] : null;
201
            $sessionId = $parsedParams['id_session'] ?? $parsedParams['sid'] ?? '0';
202
            $groupId = $parsedParams['gidReq'] ?? $parsedParams['gid'] ?? '0';
203
204
            if (!$courseId && isset($parsedParams['cidReq'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $courseId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
205
                $courseCode = $parsedParams['cidReq'];
206
                $courseId = $this->getCourseIdFromCode($courseCode);
207
            }
208
209
            if ($courseId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $courseId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
210
                $sql = "SELECT resource_node_id FROM course WHERE id = :courseId";
211
                $result = $this->connection->executeQuery($sql, ['courseId' => $courseId]);
212
                $course = $result->fetch();
213
214
                if ($course && isset($course['resource_node_id'])) {
215
                    $newUrl = $this->generateLinkUrl((int)$course['resource_node_id'], (int)$courseId, $sessionId, $groupId);
216
217
                    return sprintf('%s="%s"', $attribute, $newUrl);
218
                }
219
            }
220
221
            return $matches[0];
222
        }, $content);
223
    }
224
225
    /**
226
     * Generate the relative URL for link-type documents.
227
     */
228
    private function generateLinkUrl(int $resourceNodeId, int $courseId, string $sessionId, string $groupId): string
229
    {
230
        return sprintf("/resources/links/%d/?cid=%d&sid=%s&gid=%s", $resourceNodeId, $courseId, $sessionId, $groupId);
231
    }
232
233
    /**
234
     * Retrieves the course ID from the course code.
235
     * Returns null if the course is not found.
236
     */
237
    private function getCourseIdFromCode(string $courseCode): ?int
238
    {
239
        $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1';
240
        $stmt = $this->connection->executeQuery($sql, ['code' => $courseCode]);
241
        $course = $stmt->fetch();
242
243
        return $course ? (int)$course['id'] : null;
244
    }
245
246
    /**
247
     * Generate the relative URL for folder-type documents using resource_node_id.
248
     */
249
    private function generateFolderUrl(int $resourceNodeId, int $courseId, string $sessionId, string $groupId): string
250
    {
251
        return sprintf("/resources/document/%d/?cid=%d&sid=%s&gid=%s", $resourceNodeId, $courseId, $sessionId, $groupId);
252
    }
253
}
254