From 98bd9ddfcf535b5e2c1cabf2724d9db819ac326b Mon Sep 17 00:00:00 2001 From: christianbeeznest <84335353+christianbeeznest@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:18:28 -0500 Subject: [PATCH] Revert "Internal: Migration: Remove unnecessary cid parameters from URLs" --- .../Schema/V200/Version20240429140100.php | 101 ------------------ 1 file changed, 101 deletions(-) delete mode 100644 src/CoreBundle/Migrations/Schema/V200/Version20240429140100.php diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20240429140100.php b/src/CoreBundle/Migrations/Schema/V200/Version20240429140100.php deleted file mode 100644 index a064613b9c..0000000000 --- a/src/CoreBundle/Migrations/Schema/V200/Version20240429140100.php +++ /dev/null @@ -1,101 +0,0 @@ -connection->beginTransaction(); - try { - $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); - /** @var Course $course */ - foreach ($q->toIterable() as $course) { - $courseId = $course->getId(); - $this->processTextFields('c_tool_intro', 'intro_text'); - $this->processTextFields('c_course_description', 'content'); - $this->processTextFields('c_quiz', 'description'); - $this->processTextFields('c_quiz', 'text_when_finished'); - $this->processTextFields('c_quiz_question', 'description'); - $this->processTextFields('c_quiz_question', 'question'); - $this->processTextFields('c_quiz_answer', 'answer'); - $this->processTextFields('c_quiz_answer', 'comment'); - $this->processTextFields('c_student_publication', 'description'); - $this->processTextFields('c_student_publication_comment', 'comment'); - $this->processTextFields('c_forum_category', 'cat_comment'); - $this->processTextFields('c_forum_forum', 'forum_comment'); - $this->processTextFields('c_forum_post', 'post_text'); - $this->processTextFields('c_glossary', 'description'); - $this->processTextFields('c_survey', 'title'); - $this->processTextFields('c_survey_question', 'survey_question'); - $this->processTextFields('c_survey_question_option', 'option_text'); - } - $this->connection->commit(); - } catch (Exception $e) { - $this->connection->rollBack(); - throw new Exception("Database error: " . $e->getMessage()); - } - } - - private function processTextFields(string $tableName, string $fieldName) - { - $sql = "SELECT iid, {$fieldName} FROM {$tableName}"; - $result = $this->connection->executeQuery($sql); - $items = $result->fetchAllAssociative(); - - foreach ($items as $item) { - $originalText = $item[$fieldName]; - if ($originalText === null) continue; - - $updatedText = $this->removeCidParameter($originalText); - - if ($originalText !== $updatedText) { - $sqlUpdate = "UPDATE {$tableName} SET {$fieldName} = :updatedText WHERE iid = :iid"; - $this->connection->executeQuery($sqlUpdate, ['updatedText' => $updatedText, 'iid' => $item['iid']]); - error_log($tableName, $item['iid'], $originalText, $updatedText); - } - } - } - - private function removeCidParameter(?string $text): string - { - if ($text === null) return ''; - - $pattern = '/(\/r\/document\/files\/[\w-]+\/view)(\?|\&)(cid=\d+)/'; - - $text = preg_replace_callback( - $pattern, - function ($matches) { - $url = $matches[1]; - $queryDelimiter = $matches[2]; - $newQuery = str_replace($matches[3], '', $queryDelimiter); - $newQuery = trim($newQuery, '?&'); - return $url . ($newQuery ? '?' . $newQuery : ''); - }, - $text - ); - - $text = str_replace('?&', '?', $text); - $text = str_replace('&&', '&', $text); - $text = rtrim($text, '?'); - $text = rtrim($text, '&'); - - return $text; - } -}