Skip to content

Commit

Permalink
Merge pull request #6006 from christianbeeznest/ofaj-22199-2
Browse files Browse the repository at this point in the history
Migration: Fix document path replacement by filtering with course ID - refs BT#22199
  • Loading branch information
NicoDucou authored Jan 2, 2025
2 parents 482ff70 + e956ffc commit c25d659
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/CoreBundle/Migrations/Schema/V200/Version20230913162700.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ private function updateContent($config, $courseDirectory, $courseId, $documentRe

private function updateHtmlContent($courseDirectory, $courseId, $documentRepo, $resourceNodeRepo): void
{
$sql = "SELECT iid, resource_node_id FROM c_document WHERE filetype = 'file'";
$sql = "SELECT iid, path, c_id, resource_node_id FROM c_document WHERE filetype = 'file'";
$result = $this->connection->executeQuery($sql);
$items = $result->fetchAllAssociative();

foreach ($items as $item) {
if ((int)$item['c_id'] !== (int)$courseId) {
continue;
}

/** @var CDocument $document */
$document = $documentRepo->find($item['iid']);
if (!$document) {
Expand All @@ -123,7 +127,7 @@ private function updateHtmlContent($courseDirectory, $courseId, $documentRepo, $

$filePath = $resourceFile->getTitle();
if ('text/html' === $resourceFile->getMimeType()) {
error_log('Verifying HTML file: '.$filePath);
error_log("[DEBUG] Processing file: $filePath for Document IID={$item['iid']} in Course ID={$item['c_id']}");

try {
$content = $resourceNodeRepo->getResourceNodeFileContent($resourceNode);
Expand All @@ -134,7 +138,7 @@ private function updateHtmlContent($courseDirectory, $courseId, $documentRepo, $
$documentRepo->update($document);
}
} catch (Exception $e) {
error_log("Error processing file $filePath: ".$e->getMessage());
error_log("[ERROR] Processing file $filePath failed for IID={$item['iid']} in Course ID={$item['c_id']}: " . $e->getMessage());
}
}
}
Expand All @@ -148,15 +152,15 @@ private function replaceOldURLsWithNew($itemDataText, $courseDirectory, $courseI

foreach ($matches[2] as $index => $fullUrl) {
$videoPath = parse_url($fullUrl, PHP_URL_PATH) ?: $fullUrl;
$fileName = basename($videoPath);
error_log("[DEBUG] Processing specific file: $fileName");
$actualCourseDirectory = $matches[5][$index];
if ($actualCourseDirectory !== $courseDirectory) {
$videoPath = preg_replace("/^\\/courses\\/$actualCourseDirectory\\//i", "/courses/$courseDirectory/", $videoPath);
}

$documentPath = str_replace('/courses/'.$courseDirectory.'/document/', '/', $videoPath);

$sql = "SELECT iid, path, resource_node_id FROM c_document WHERE c_id = $courseId AND path LIKE '$documentPath'";
$result = $this->connection->executeQuery($sql);
$sql = "SELECT iid, title, resource_node_id FROM c_document WHERE title = :title AND c_id = :courseId";
$result = $this->connection->executeQuery($sql, ['title' => $fileName, 'courseId' => $courseId]);
$documents = $result->fetchAllAssociative();

if (!empty($documents)) {
Expand Down Expand Up @@ -184,10 +188,13 @@ private function replaceDocumentLinks($documents, $documentRepo, $matches, $inde
if ($documentFile) {
$newUrl = $documentRepo->getResourceFileUrl($documentFile);
if (!empty($newUrl)) {
error_log("[DEBUG] Replacing old URL with new URL: $newUrl");
$patternForReplacement = '/'.preg_quote($matches[0][$index], '/').'/';
$replacement = $matches[1][$index].'="'.$newUrl.'"';
$contentText = preg_replace($patternForReplacement, $replacement, $contentText, 1);
}
} else {
error_log("[DEBUG] Document file not found for ResourceNodeID: $resourceNodeId");
}
}
}
Expand All @@ -196,22 +203,28 @@ private function createNewDocument($videoPath, $courseId)
{
try {
$documentRepo = $this->container->get(CDocumentRepository::class);
$kernel = $this->container->get('kernel');
$rootPath = $kernel->getProjectDir();
$appCourseOldPath = $rootPath.'/app'.$videoPath;
$title = basename($appCourseOldPath);

$courseRepo = $this->container->get(CourseRepository::class);
$course = $courseRepo->find($courseId);
if (!$course) {
throw new Exception("Course with ID $courseId not found.");
}

$existingDocument = $documentRepo->findResourceByTitleInCourse($title, $course);
if ($existingDocument) {
error_log("Document '$title' already exists for course {$course->getId()}. Skipping creation.");
$rootPath = $this->getUpdateRootPath();
$appCourseOldPath = $rootPath.'/app'.$videoPath;
$title = basename($appCourseOldPath);

return $existingDocument;
$sql = "SELECT * FROM c_document WHERE title = :title AND c_id = :courseId";
$stmt = $this->connection->prepare($sql);
$result = $stmt->executeQuery(['title' => $title, 'courseId' => $courseId]);
$existingDocument = $result->fetchAssociative();

if ($existingDocument) {
$document = $documentRepo->find($existingDocument['iid']);
if ($document) {
return $document;
} else {
throw new Exception("ResourceNode not found for resource_node_id " . $existingDocument['resource_node_id']);
}
}

if (file_exists($appCourseOldPath) && !is_dir($appCourseOldPath)) {
Expand All @@ -234,7 +247,7 @@ private function createNewDocument($videoPath, $courseId)

return $document;
}
$generalCoursesPath = $this->getUpdateRootPath().'/app/courses/';
$generalCoursesPath = $rootPath.'/app/courses/';
$foundPath = $this->recursiveFileSearch($generalCoursesPath, $title);
if ($foundPath) {
$document = new CDocument();
Expand Down

0 comments on commit c25d659

Please sign in to comment.