Skip to content

Commit

Permalink
Fix article extra unserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
zkabic committed Aug 1, 2023
1 parent 1809d02 commit 78ddc32
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
34 changes: 29 additions & 5 deletions src/SWP/Bundle/CoreBundle/Command/FillArticleExtraTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$results = $query->fetchAll();

foreach ($results as $result) {
$legacyExtra = unserialize($result['extra']);
$legacyExtra = $this->unserializeExtraField($result['extra']);
if (empty($legacyExtra)) {
++$totalArticlesProcessed;
continue;
Expand All @@ -76,6 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$extra = ArticleExtraTextField::newFromValue($key, (string)$extraItem);
}
$extra->setArticle($article);
$this->entityManager->persist($extra);
}

$this->entityManager->persist($extra);
Expand All @@ -85,16 +86,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->entityManager->clear();
}
++$totalArticlesProcessed;
if (0 == ($totalArticlesProcessed % $batchSize)) {
$this->entityManager->flush();
$this->entityManager->clear();
}
}

if ($totalArticlesProcessed >= $totalArticles) {
$isProcessing = false;
// flush remaining entities in queue and break loop
if ($totalArticlesProcessed === $totalArticles) {
$this->entityManager->flush();
$this->entityManager->clear();
break;
}
}
return 0;
}

$this->entityManager->flush();
/**
* @param string $data
* @return mixed
*/
private function unserializeExtraField(string $data)
{
$unserialized = @unserialize($data);
if ($unserialized) {
return $unserialized;
}

return 0;
$callback = function ($matches) {
$matches[2] = trim(preg_replace('/\s\s+/', ' ', $matches[2]));
return 's:' . mb_strlen($matches[2]) . ':"' . $matches[2] . '";';
};

$fixedData = preg_replace_callback('!s:(\d+):"(.*?)";!s', $callback, $data);
return unserialize($fixedData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function postUp(Schema $schema): void
$results = $query->fetchAll();

foreach ($results as $result) {
$legacyExtra = unserialize($result['extra']);
$legacyExtra = $this->unserializeExtraField($result['extra']);
if (empty($legacyExtra)) {
++$totalArticlesProcessed;
continue;
Expand All @@ -97,25 +97,41 @@ public function postUp(Schema $schema): void
if (is_array($extraItem)) {
$extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
} else {
$extra = ArticleExtraTextField::newFromValue($key, (string)$extraItem);
$extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem);
}
$extra->setArticle($article);
$entityManager->persist($extra);
}

$entityManager->persist($extra);

if (0 === ($totalArticlesProcessed % $batchSize)) {
++$totalArticlesProcessed;
if (0 == ($totalArticlesProcessed % $batchSize)) {
$entityManager->flush();
$entityManager->clear();
}
++$totalArticlesProcessed;
}

// flush remaining entities in queue and break loop
if ($totalArticlesProcessed === $totalArticles) {
$entityManager->flush();
$entityManager->clear();
break;
}
}
}

$entityManager->flush();
private function unserializeExtraField(string $data)
{
$data = @unserialize($data);
if ($data) {
return $data;
}

$callback = function ($matches) {
$matches[2] = trim(preg_replace('/\s\s+/', ' ', $matches[2]));
return 's:' . mb_strlen($matches[2]) . ':"' . $matches[2] . '";';
};

$data = preg_replace_callback('!s:(\d+):"(.*?)";!s', $callback, $data);
return @unserialize($data);
}
}

0 comments on commit 78ddc32

Please sign in to comment.