Merge remote-tracking branch 'origin/master'

pull/5838/head
Angel Fernando Quiroz Campos 11 months ago
commit 2a455bf26a
No known key found for this signature in database
GPG Key ID: B284841AE3E562CD
  1. 51
      src/CoreBundle/Migrations/Schema/V200/Version20240924120200.php

@ -7,13 +7,17 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Doctrine\DBAL\Schema\Schema;
use Exception;
final class Version20240924120200 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Update HTML content blocks to replace old CKEditor image paths with new ones and convert .gif references to .png';
return 'Update HTML content blocks to replace old CKEditor image paths with new ones and convert .gif references to .png, including HTML files in the document repository';
}
public function up(Schema $schema): void
@ -39,6 +43,8 @@ final class Version20240924120200 extends AbstractMigrationChamilo
foreach ($updateConfigurations as $config) {
$this->updateContent($config);
}
$this->updateHtmlFiles();
}
private function updateContent(array $config): void
@ -52,7 +58,7 @@ final class Version20240924120200 extends AbstractMigrationChamilo
foreach ($items as $item) {
$originalText = $item[$field];
if (!empty($originalText)) {
if (is_string($originalText) && trim($originalText) !== '') {
$updatedText = $this->replaceGifWithPng($originalText);
if ($originalText !== $updatedText) {
$updateSql = "UPDATE {$config['table']} SET {$field} = :newText WHERE iid = :id";
@ -63,6 +69,47 @@ final class Version20240924120200 extends AbstractMigrationChamilo
}
}
private function updateHtmlFiles(): void
{
$sql = "SELECT iid, resource_node_id FROM c_document WHERE filetype = 'file'";
$result = $this->connection->executeQuery($sql);
$items = $result->fetchAllAssociative();
$documentRepo = $this->container->get(CDocumentRepository::class);
$resourceNodeRepo = $this->container->get(ResourceNodeRepository::class);
foreach ($items as $item) {
/** @var CDocument $document */
$document = $documentRepo->find($item['iid']);
if (!$document) {
continue;
}
$resourceNode = $document->getResourceNode();
if (!$resourceNode || !$resourceNode->hasResourceFile()) {
continue;
}
$resourceFile = $resourceNode->getResourceFiles()->first();
if (!$resourceFile || $resourceFile->getMimeType() !== 'text/html') {
continue;
}
try {
$content = $resourceNodeRepo->getResourceNodeFileContent($resourceNode);
if (is_string($content) && trim($content) !== '') {
$updatedContent = $this->replaceGifWithPng($content);
if ($content !== $updatedContent) {
$documentRepo->updateResourceFileContent($document, $updatedContent);
$documentRepo->update($document);
}
}
} catch (Exception $e) {
// error_log("Error processing file for document ID {$item['iid']}: " . $e->getMessage());
}
}
}
private function replaceGifWithPng(string $content): string
{
$pattern = '/(src=["\'])(https?:\/\/[^\/]+\/)?(\/?web\/assets\/ckeditor\/plugins\/smiley\/images\/([a-zA-Z0-9_\-]+))\.(gif|png)(["\'])/i';

Loading…
Cancel
Save