|
1
|
|
|
#! /usr/bin/env php |
|
2
|
|
|
<?php |
|
3
|
|
|
|
|
4
|
|
|
declare(strict_types=1); |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A script to clean product formatted content in DB. |
|
8
|
|
|
*/ |
|
9
|
|
|
use Application\Model\Product; |
|
10
|
|
|
use Application\Utility; |
|
11
|
|
|
|
|
12
|
|
|
require_once 'server/cli.php'; |
|
13
|
|
|
|
|
14
|
|
|
function cleanRichText(string $description): string |
|
15
|
|
|
{ |
|
16
|
|
|
$description = Utility::sanitizeRichText($description); |
|
17
|
|
|
$replacements = [ |
|
18
|
|
|
' target="_blank"' => '', |
|
19
|
|
|
'<p class="surligner">Vous êtes sur la version papier</p>' => '', |
|
20
|
|
|
'<p class="surligner">Vous êtes sur la version papier </p>' => '', |
|
21
|
|
|
'<p class="surligner">Vous êtes à la version papier</p>' => '', |
|
22
|
|
|
'<p>Vous êtes sur la version papier </p>' => '', |
|
23
|
|
|
'<p class="surligner">Vous êtes sur la version numérique</p>' => '', |
|
24
|
|
|
'<p>Aller à la version numérique</p>' => '', |
|
25
|
|
|
'<p>Aller à la version papier</p>' => '', |
|
26
|
|
|
'</p>060-n60-des-monnaies-pour-une-prosperite-sans-croissance-version-numerique.html" target="_blank">Aller à la version numérique</a></p>' => '</p>', |
|
27
|
|
|
]; |
|
28
|
|
|
$description = str_replace(array_keys($replacements), array_values($replacements), $description); |
|
29
|
|
|
|
|
30
|
|
|
$pregReplacements = [ |
|
31
|
|
|
'~<p><a href="[^"]*">Aller à la version numérique</a></p>~' => '', |
|
32
|
|
|
'~<p class="petit"><a href="[^"]*">Aller à la version numérique</a></p>~' => '', |
|
33
|
|
|
'~<a href="[^"]*" target="_blank">Aller à la version numérique</a></p>~' => '', |
|
34
|
|
|
'~ style="[^"]*"~' => '', |
|
35
|
|
|
'~ class="[^"]*"~' => '', |
|
36
|
|
|
'~href="https://www\.larevuedurable\.com/fr/[^/]+/(\d+)-[^/]+\.html"~' => 'href="/larevuedurable/article/$1"', |
|
37
|
|
|
]; |
|
38
|
|
|
$description = preg_replace(array_keys($pregReplacements), array_values($pregReplacements), $description); |
|
39
|
|
|
|
|
40
|
|
|
return $description; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function cleanOneTable(string $table, array $fields): void |
|
44
|
|
|
{ |
|
45
|
|
|
$connection = _em()->getConnection(); |
|
46
|
|
|
$qb = $connection->createQueryBuilder() |
|
47
|
|
|
->from($table) |
|
48
|
|
|
->addSelect('id') |
|
49
|
|
|
->orderBy('id'); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($fields as $name => $f) { |
|
52
|
|
|
$qb->addSelect($name); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$products = $qb->fetchAllAssociative(); |
|
56
|
|
|
|
|
57
|
|
|
$count = 0; |
|
58
|
|
|
$total = count($products); |
|
59
|
|
|
foreach ($products as $i => $row) { |
|
60
|
|
|
if ($i === 0 || $i % 200 === 0) { |
|
61
|
|
|
echo $i . '/' . $total . PHP_EOL; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Apply cleaning |
|
65
|
|
|
$newRow = $row; |
|
66
|
|
|
foreach ($fields as $name => $f) { |
|
67
|
|
|
$newRow[$name] = $f($newRow[$name]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($newRow !== $row) { |
|
71
|
|
|
unset($newRow['id']); |
|
72
|
|
|
$count += $connection->update($table, $newRow, ['id' => $row['id']]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
echo ' |
|
77
|
|
|
Updated records in table ' . $table . ': ' . $count . ' |
|
78
|
|
|
'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function cleanFormattedContent(): void |
|
82
|
|
|
{ |
|
83
|
|
|
cleanOneTable('product', [ |
|
84
|
|
|
'description' => fn ($v) => cleanRichText($v), |
|
85
|
|
|
'content' => fn ($v) => strip_tags($v), |
|
86
|
|
|
]); |
|
87
|
|
|
|
|
88
|
|
|
cleanOneTable('news', [ |
|
89
|
|
|
'description' => fn ($v) => strip_tags($v), |
|
90
|
|
|
'content' => fn ($v) => cleanRichText($v), |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
cleanFormattedContent(); |
|
95
|
|
|
|