Issues (150)

bin/oneshot/fix-plain-name.php (1 issue)

Labels
Severity
1
#! /usr/bin/env php
2
<?php
3
4
/**
5
 * This one time use script will fix the field `card.plain_name` not being
6
 * filled in nor updated since the bug introduced in
7
 * ed5df283ec3ce40c806d6b66753d19a49b509d13 in v4.2.0 on 2024.04.09.
8
 *
9
 * We will only fix data since this bug was introduced. Because it seems that
10
 * there was a lot of changes in policies of how HTML tags are stripped from
11
 * name and how plain_name was generated and data have never been updated
12
 * accordingly.
13
 *
14
 * So we can't just check if plain_name !== what plain_name should
15
 * be, because it might have been generated correctly at the time but is no
16
 * longer correct now.
17
 *
18
 * So for the sake of not changing that was not changed before, we will only fix
19
 * data that was affected by this bug.
20
 */
21
22
declare(strict_types=1);
23
24
use Application\Utility;
0 ignored issues
show
The type Application\Utility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
require_once 'server/cli.php';
27
28
// We don't use CardRepository because we don't want Doctrine to update timestamps.
29
30
$connection = _em()->getConnection();
31
32
$sqlSelect = <<<SQL
33
        SELECT
34
            card.id,
35
            card.plain_name,
36
            card.name
37
        FROM card
38
        WHERE card.update_date >= '2024-04-09 00:00:00'
39
    SQL;
40
$results = $connection->executeQuery($sqlSelect)->fetchAllAssociative();
41
42
$total = 0;
43
$totalFailed = 0;
44
45
$sqlUpdate = <<<SQL
46
        UPDATE card
47
        SET plain_name = :plainName
48
        WHERE id = :id
49
    SQL;
50
51
foreach ($results as $result) {
52
    $fixedPlainName = Utility::richTextToPlainText($result['name']);
53
54
    if ($fixedPlainName !== $result['plain_name']) {
55
        $count = $connection->executeStatement(
56
            $sqlUpdate,
57
            [
58
                'id' => $result['id'],
59
                'plainName' => $fixedPlainName,
60
            ],
61
        );
62
63
        if ($count === 1) {
64
            echo "Card id {$result['id']} updated.\n    Name: {$result['name']}\n    Old plain name: {$result['plain_name']}\n    New plain name: $fixedPlainName\n\n";
65
            ++$total;
66
        } else {
67
            echo "Card id {$result['id']} failed to update.\n";
68
            ++$totalFailed;
69
        }
70
    }
71
}
72
73
echo "Total cards updated: $total\n";
74
echo "Total cards failed to update: $totalFailed\n";
75