|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2022 Robin Windey <ro.windey@gmail.com> |
| 7 | + * |
| 8 | + * @license GNU AGPL version 3 or any later version |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OCA\WorkflowOcr\Migration; |
| 25 | + |
| 26 | +use Closure; |
| 27 | +use Exception; |
| 28 | +use OCP\DB\ISchemaWrapper; |
| 29 | +use OCP\IDBConnection; |
| 30 | +use OCP\Migration\IOutput; |
| 31 | +use OCP\Migration\SimpleMigrationStep; |
| 32 | + |
| 33 | +class Version2404Date20220903071748 extends SimpleMigrationStep { |
| 34 | + |
| 35 | + /** @var IDBConnection */ |
| 36 | + private $db; |
| 37 | + |
| 38 | + public function __construct(IDBConnection $db) { |
| 39 | + $this->db = $db; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritDoc} |
| 44 | + */ |
| 45 | + public function name(): string { |
| 46 | + return 'migrate lang codes'; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritDoc} |
| 51 | + */ |
| 52 | + public function description(): string { |
| 53 | + return 'Execute migration of language codes towards tesseract langugage codes (e.g. deu instead of de)'; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 60 | + // 'id' and new 'operation' value will be stored here |
| 61 | + $datasetsToMigrate = $this->getDatasetsToMigrate(); |
| 62 | + $this->updateDatabase($datasetsToMigrate); |
| 63 | + |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + private function getDatasetsToMigrate() : array { |
| 68 | + $langMapping = [ |
| 69 | + 'de' => 'deu', |
| 70 | + 'en' => 'eng', |
| 71 | + 'fr' => 'fra', |
| 72 | + 'it' => 'ita', |
| 73 | + 'es' => 'spa', |
| 74 | + 'pt' => 'por', |
| 75 | + 'ru' => 'rus', |
| 76 | + 'chi' => 'chi_sim' |
| 77 | + ]; |
| 78 | + |
| 79 | + $builder = $this->db->getQueryBuilder(); |
| 80 | + |
| 81 | + $ocrFlowOperations = $builder->select('id', 'operation') |
| 82 | + ->from('flow_operations') |
| 83 | + ->where($builder->expr()->eq('class', $builder->createNamedParameter('OCA\WorkflowOcr\Operation'))) |
| 84 | + ->executeQuery(); |
| 85 | + |
| 86 | + $datasetsToMigrate = []; |
| 87 | + |
| 88 | + try { |
| 89 | + while ($row = $ocrFlowOperations->fetch()) { |
| 90 | + $workflowSettings = json_decode($row['operation'], true); |
| 91 | + $foundMapping = false; |
| 92 | + $newLangArr = []; |
| 93 | + $languagesArr = $workflowSettings['languages']; |
| 94 | + |
| 95 | + // Check if we need to migrate the languages code. |
| 96 | + // If yes, we have to regenerate the whole 'operation' string. |
| 97 | + foreach ($languagesArr as $existingLang) { |
| 98 | + if (array_key_exists($existingLang, $langMapping)) { |
| 99 | + $newLangArr[] = $langMapping[$existingLang]; |
| 100 | + $foundMapping = true; |
| 101 | + continue; |
| 102 | + } |
| 103 | + $newLangArr[] = $existingLang; |
| 104 | + } |
| 105 | + |
| 106 | + if ($foundMapping) { |
| 107 | + $workflowSettings['languages'] = $newLangArr; |
| 108 | + $datasetsToMigrate[] = [ |
| 109 | + 'id' => $row['id'], |
| 110 | + 'operation' => json_encode($workflowSettings) |
| 111 | + ]; |
| 112 | + } |
| 113 | + } |
| 114 | + } finally { |
| 115 | + $ocrFlowOperations->closeCursor(); |
| 116 | + } |
| 117 | + |
| 118 | + return $datasetsToMigrate; |
| 119 | + } |
| 120 | + |
| 121 | + private function updateDatabase(array $datasetsToMigrate) : void { |
| 122 | + $this->db->beginTransaction(); |
| 123 | + |
| 124 | + try { |
| 125 | + $builder = $this->db->getQueryBuilder(); |
| 126 | + $builder->update('flow_operations') |
| 127 | + ->set('operation', $builder->createParameter('operation')) |
| 128 | + ->where($builder->expr()->eq('id', $builder->createParameter('id'))); |
| 129 | + |
| 130 | + foreach ($datasetsToMigrate as $dataset) { |
| 131 | + $builder->setParameter('id', $dataset['id']); |
| 132 | + $builder->setParameter('operation', $dataset['operation']); |
| 133 | + $builder->executeStatement(); |
| 134 | + } |
| 135 | + } catch (Exception $e) { |
| 136 | + $this->db->rollBack(); |
| 137 | + throw $e; |
| 138 | + } |
| 139 | + |
| 140 | + $this->db->commit(); |
| 141 | + } |
| 142 | +} |
0 commit comments