first commit
This commit is contained in:
215
bundles/External/ZNZBundle/Resources/script/ZNZFixBlocksScript.php
vendored
Normal file
215
bundles/External/ZNZBundle/Resources/script/ZNZFixBlocksScript.php
vendored
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace External\ZNZBundle\Resources\script;
|
||||
|
||||
use KupShop\AdminBundle\Util\Script\Script;
|
||||
use Query\Operator;
|
||||
|
||||
class ZNZFixBlocksScript extends Script
|
||||
{
|
||||
protected static $name = '[ZNZ] Fix blocks';
|
||||
protected static $defaultParameters = [
|
||||
'strongTexts' => false,
|
||||
'unnecessaryTitles' => false,
|
||||
];
|
||||
|
||||
protected function run(array $arguments)
|
||||
{
|
||||
foreach ($arguments as $method => $run) {
|
||||
if (!$run) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$methodName = 'fix_'.ucfirst($method);
|
||||
|
||||
if (!method_exists($this, $methodName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->log('Running "'.$method.'"...');
|
||||
|
||||
$this->{$methodName}();
|
||||
|
||||
$this->log('Done "'.$method.'"');
|
||||
}
|
||||
|
||||
$this->fix_StrongTexts();
|
||||
}
|
||||
|
||||
public function fix_UnnecessaryTitles(): void
|
||||
{
|
||||
$stats = [
|
||||
'defaultUpdated' => 0,
|
||||
'translatedUpdated' => 0,
|
||||
];
|
||||
|
||||
$filterSpec = Operator::andX(
|
||||
Operator::orX(
|
||||
Operator::like(['content' => '%POPIS%']),
|
||||
Operator::like(['content' => '%<h2%']),
|
||||
Operator::like(['content' => '%font size="5"%']),
|
||||
),
|
||||
Operator::like(['json_content' => '%legacy%'])
|
||||
);
|
||||
|
||||
$qb = sqlQueryBuilder()
|
||||
->select('id, content, json_content')
|
||||
->from('blocks')
|
||||
->where($filterSpec);
|
||||
|
||||
$config = \HTMLPurifier_Config::createDefault();
|
||||
$config->set('AutoFormat.AutoParagraph', true);
|
||||
$config->set('AutoFormat.RemoveEmpty', true);
|
||||
|
||||
$purifier = new \HTMLPurifier($config);
|
||||
|
||||
$prepareBlockData = function (array $item) use ($purifier) {
|
||||
$lines = explode(PHP_EOL, $item['content']);
|
||||
|
||||
$firstLineRemoved = false;
|
||||
foreach ($lines as $index => $line) {
|
||||
if ($index === 0 && count($lines) > 1
|
||||
&& (str_contains($line, 'font size="5"') || str_contains($line, '<h2'))) {
|
||||
unset($lines[$index]);
|
||||
$firstLineRemoved = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($index === 1 && $firstLineRemoved && str_contains($line, 'ml')) {
|
||||
unset($lines[$index]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str_contains(mb_strtolower($line), '<strong>popis</strong>')) {
|
||||
unset($lines[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
// odebrat br na zacatku textu, ktere tam jsou ted zbytecne
|
||||
foreach ($lines as $index => $line) {
|
||||
if (preg_match('/^<br(( )?\/)?>$/', trim($line))) {
|
||||
unset($lines[$index]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// jakmile jednou podminka nebude platit, tak prestavam
|
||||
break;
|
||||
}
|
||||
|
||||
$content = implode(PHP_EOL, $lines);
|
||||
$content = $purifier->purify($content);
|
||||
|
||||
$json = json_decode($item['json_content'], true);
|
||||
$json[0]['settings']['html'] = $content;
|
||||
|
||||
return [
|
||||
'content' => $content,
|
||||
'json_content' => json_encode($json),
|
||||
];
|
||||
};
|
||||
|
||||
// base language
|
||||
foreach ($qb->execute() as $item) {
|
||||
$update = $prepareBlockData($item);
|
||||
|
||||
sqlQueryBuilder()
|
||||
->update('blocks')
|
||||
->directValues($update)
|
||||
->where(Operator::equals(['id' => $item['id']]))
|
||||
->execute();
|
||||
|
||||
$stats['defaultUpdated']++;
|
||||
}
|
||||
|
||||
$qb = sqlQueryBuilder()
|
||||
->select('id, content, json_content')
|
||||
->from('blocks_translations')
|
||||
->where($filterSpec);
|
||||
|
||||
// translations
|
||||
foreach ($qb->execute() as $item) {
|
||||
$update = $prepareBlockData($item);
|
||||
|
||||
sqlQueryBuilder()
|
||||
->update('blocks_translations')
|
||||
->directValues($update)
|
||||
->where(Operator::equals(['id' => $item['id']]))
|
||||
->execute();
|
||||
|
||||
$stats['translatedUpdated']++;
|
||||
}
|
||||
|
||||
foreach ($stats as $name => $value) {
|
||||
$this->log("{$name}: {$value}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixes the strong texts in the 'blocks' and 'blocks_translations' tables.
|
||||
*
|
||||
* This method searches for invalid strong text syntax, represented by two asterisks (**), in the 'content' column
|
||||
* of the 'blocks' and 'blocks_translations' tables. It replaces the invalid syntax with the correct HTML strong tag.
|
||||
* The method updates the 'content' and 'json_content' columns in both tables to reflect the changes.
|
||||
*/
|
||||
public function fix_StrongTexts(): void
|
||||
{
|
||||
$qb = sqlQueryBuilder()
|
||||
->select('id, content, json_content')
|
||||
->from('blocks')
|
||||
->where("content REGEXP '\\\\*\\\\*.+\\\\*\\\\*'");
|
||||
|
||||
$updateBlock = function (array $block) {
|
||||
$removeInvalidSyntax = fn (string $content) => preg_replace_callback('/\*\*(.+?)\*\*/', fn ($x) => '<strong>'.trim($x[1]).'</strong>', $content);
|
||||
|
||||
$block['content'] = $removeInvalidSyntax($block['content']);
|
||||
|
||||
$data = json_decode($block['json_content'], true) ?? [];
|
||||
if (($data[0]['type'] ?? null) === 'legacy') {
|
||||
$data[0]['settings']['html'] = $removeInvalidSyntax($data[0]['settings']['html']);
|
||||
}
|
||||
|
||||
$block['json_content'] = json_encode($data);
|
||||
|
||||
return $block;
|
||||
};
|
||||
|
||||
foreach ($qb->execute() as $item) {
|
||||
$block = $updateBlock($item);
|
||||
|
||||
sqlQueryBuilder()
|
||||
->update('blocks')
|
||||
->directValues(
|
||||
[
|
||||
'content' => $block['content'],
|
||||
'json_content' => $block['json_content'],
|
||||
]
|
||||
)
|
||||
->where(Operator::equals(['id' => $item['id']]))
|
||||
->execute();
|
||||
}
|
||||
|
||||
$qbTranslations = sqlQueryBuilder()
|
||||
->select('id, content, json_content')
|
||||
->from('blocks_translations')
|
||||
->where("content REGEXP '\\\\*\\\\*.+\\\\*\\\\*'");
|
||||
|
||||
foreach ($qbTranslations->execute() as $item) {
|
||||
$block = $updateBlock($item);
|
||||
|
||||
sqlQueryBuilder()
|
||||
->update('blocks_translations')
|
||||
->directValues(
|
||||
[
|
||||
'content' => $block['content'],
|
||||
'json_content' => $block['json_content'],
|
||||
]
|
||||
)
|
||||
->where(Operator::equals(['id' => $item['id']]))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ZNZFixBlocksScript::class;
|
||||
Reference in New Issue
Block a user