sunnysideup /
silverstripe-migration-task
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Sunnysideup\MigrateData\Tasks; |
||||
| 4 | |||||
| 5 | use SilverStripe\Dev\BuildTask; |
||||
| 6 | use SilverStripe\ORM\DB; |
||||
| 7 | use Sunnysideup\Flush\FlushNow; |
||||
| 8 | |||||
| 9 | /** |
||||
| 10 | * Update all systems. |
||||
| 11 | * |
||||
| 12 | * Class UpdateSystemsWithProductCodeVariantKeywords |
||||
| 13 | */ |
||||
| 14 | class FixMissingFiles extends BuildTask |
||||
| 15 | { |
||||
| 16 | use FlushNow; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * @var string |
||||
| 20 | */ |
||||
| 21 | protected $title = 'Fix missing filename references in duplicate file records from SS3'; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * @var string |
||||
| 25 | */ |
||||
| 26 | protected $description = 'When duplicate records exist for the same file in SS3 the silverstripe |
||||
| 27 | data migration task will only update one. Causing the other files to "go missing". This task fixes that'; |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Fix broken file references and publish them. |
||||
| 31 | * |
||||
| 32 | * @param mixed $request |
||||
| 33 | */ |
||||
| 34 | public function run($request) |
||||
| 35 | { |
||||
| 36 | $sql = 'SELECT * FROM File WHERE ClassName != \'SilverStripe\\Assets\\Folder\' AND FileFilename is NULL'; |
||||
| 37 | $broken_rows = DB::query($sql); |
||||
| 38 | |||||
| 39 | foreach ($broken_rows as $row) { |
||||
| 40 | $sql = " |
||||
| 41 | SELECT * FROM File WHERE |
||||
| 42 | ClassName != 'SilverStripe\\Assets\\Folder' AND |
||||
| 43 | Filename = '" . $row['Filename'] . "' AND |
||||
| 44 | Name = '" . $row['Name'] . "' AND |
||||
| 45 | FileFilename is not NULL LIMIT 1 |
||||
| 46 | "; |
||||
| 47 | $result = DB::query($sql); |
||||
| 48 | $healthyRow = $result->first(); |
||||
| 49 | if ($healthyRow) { |
||||
|
0 ignored issues
–
show
|
|||||
| 50 | echo 'Fixing' . $healthyRow['Name']; |
||||
| 51 | $this->runUpdateQuery( |
||||
| 52 | 'UPDATE "' . 'File' . '" |
||||
| 53 | SET "' . 'File' . '"."' . 'FileHash' . '" = \'' . $healthyRow['FileHash'] . '\' |
||||
| 54 | WHERE ID = ' . $row['ID'], |
||||
| 55 | 2 |
||||
| 56 | ); |
||||
| 57 | |||||
| 58 | $this->runUpdateQuery( |
||||
| 59 | 'UPDATE "' . 'File' . '" |
||||
| 60 | SET "' . 'File' . '"."' . 'FileFilename' . '" = \'' . $healthyRow['FileFilename'] . '\' |
||||
| 61 | WHERE ID = ' . $row['ID'], |
||||
| 62 | 2 |
||||
| 63 | ); |
||||
| 64 | |||||
| 65 | $this->publishFile($row['ID']); |
||||
| 66 | } |
||||
| 67 | } |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * @param string $sqlQuery list of queries |
||||
| 72 | * @param int $indents what is this list about? |
||||
| 73 | */ |
||||
| 74 | protected function runUpdateQuery(string $sqlQuery, ?int $indents = 1) |
||||
| 75 | { |
||||
| 76 | $this->flushNow(str_replace('"', '`', $sqlQuery), 'created'); |
||||
| 77 | $prefix = str_repeat(' ... ', $indents); |
||||
|
0 ignored issues
–
show
It seems like
$indents can also be of type null; however, parameter $times of str_repeat() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 78 | |||||
| 79 | try { |
||||
| 80 | DB::query($sqlQuery); |
||||
| 81 | $this->flushNow($prefix . ' DONE ' . DB::affected_rows() . ' rows affected'); |
||||
| 82 | } catch (\Exception $exception) { |
||||
| 83 | $this->flushNow($prefix . "ERROR: Unable to run '{$sqlQuery}'", 'deleted'); |
||||
| 84 | $this->flushNow('' . $exception->getMessage() . '', 'deleted'); |
||||
| 85 | } |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Take a file record and publish it (enter it to File_Live). |
||||
| 90 | * |
||||
| 91 | * @param mixed $fileId |
||||
| 92 | */ |
||||
| 93 | protected function publishFile($fileId) |
||||
| 94 | { |
||||
| 95 | $sql = " |
||||
| 96 | SELECT * FROM File_Live WHERE ID = {$fileId} |
||||
| 97 | "; |
||||
| 98 | $result = DB::query($sql); |
||||
| 99 | |||||
| 100 | if (0 === $result->numRecords()) { |
||||
| 101 | $sql = " |
||||
| 102 | SELECT * FROM File WHERE ID = {$fileId} |
||||
| 103 | "; |
||||
| 104 | $file_record = DB::query($sql)->first(); |
||||
| 105 | |||||
| 106 | DB::query(' |
||||
| 107 | INSERT IGNORE INTO `File_Live` (`ID`, `ClassName`, `LastEdited`, `Created`, `Name`, `Title`, `ShowInSearch`, |
||||
| 108 | `CanViewType`, `ParentID`, `OwnerID`, `Version`, `CanEditType`, `FileHash`, `FileFilename`, `FileVariant`) |
||||
| 109 | VALUES (' . $file_record['ID'] . ", '" . str_replace('\\', '\\\\', $file_record['ClassName']) . "', '" . $file_record['LastEdited'] . "', |
||||
| 110 | '" . $file_record['Created'] . "', '" . $file_record['Name'] . "', '" . $file_record['Title'] . "', '" . $file_record['ShowInSearch'] . "', |
||||
| 111 | '" . $file_record['CanViewType'] . "', '" . $file_record['ParentID'] . "', '" . $file_record['OwnerID'] . "', '" . $file_record['Version'] . "', |
||||
| 112 | '" . $file_record['CanEditType'] . "', '" . $file_record['FileHash'] . "', '" . $file_record['FileFilename'] . "', NULL) |
||||
| 113 | "); |
||||
| 114 | |||||
| 115 | echo ' |
||||
| 116 | INSERT INTO `File_Live` (`ID`, `ClassName`, `LastEdited`, `Created`, `Name`, `Title`, `ShowInSearch`, |
||||
| 117 | `CanViewType`, `ParentID`, `OwnerID`, `Version`, `CanEditType`, `FileHash`, `FileFilename`, `FileVariant`) |
||||
| 118 | VALUES (' . $file_record['ID'] . ", '" . str_replace('\\', '\\\\', $file_record['ClassName']) . "', '" . $file_record['LastEdited'] . "', |
||||
| 119 | '" . $file_record['Created'] . "', '" . $file_record['Name'] . "', '" . $file_record['Title'] . "', '" . $file_record['ShowInSearch'] . "', |
||||
| 120 | '" . $file_record['CanViewType'] . "', '" . $file_record['ParentID'] . "', '" . $file_record['OwnerID'] . "', '" . $file_record['Version'] . "', |
||||
| 121 | '" . $file_record['CanEditType'] . "', '" . $file_record['FileHash'] . "', '" . $file_record['FileFilename'] . "', NULL) |
||||
| 122 | "; |
||||
| 123 | } |
||||
| 124 | } |
||||
| 125 | } |
||||
| 126 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.