Skip to content

Commit 978faee

Browse files
authored
Implement #140 (#148)
* Implement #140 Get installed tesseract languages from backend Signed-off-by: Robin Windey <ro.windey@gmail.com> * Fix OcrBackendInfoServiceTest for #140 Signed-off-by: Robin Windey <ro.windey@gmail.com> * Introduce specific CommandException Signed-off-by: Robin Windey <ro.windey@gmail.com> Signed-off-by: Robin Windey <ro.windey@gmail.com>
1 parent 169b0dd commit 978faee

27 files changed

+992
-126
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Build artifact
22

33
on:
4+
pull_request:
45
workflow_dispatch:
56

67
env:
@@ -36,5 +37,5 @@ jobs:
3637
- name: Upload artifacts
3738
uses: actions/upload-artifact@v1
3839
with:
39-
name: ${{ env.APP_NAME }}.tar.gz
40-
path: ${{ env.APP_NAME }}/build/artifacts/appstore/${{ env.APP_NAME }}.tar.gz
40+
name: ${{ env.APP_NAME }}.tar.gz
41+
path: ${{ env.APP_NAME }}/build/artifacts/appstore/${{ env.APP_NAME }}.tar.gz

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ appstore:
180180
--exclude="../$(app_name)/*.json" \
181181
--exclude="../$(app_name)/*.lock" \
182182
--exclude="../$(app_name)/*.cov" \
183+
--exclude="../$(app_name)/psalm.xml" \
183184
../$(app_name) \
184185

185186
.PHONY: test

appinfo/routes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
return [
2929
'routes' => [
30-
['name' => 'GlobalSettings#getGlobalSettings', 'url' => '/globalsettings', 'verb' => 'GET'],
31-
['name' => 'GlobalSettings#setGlobalSettings', 'url' => '/globalsettings', 'verb' => 'PUT']
30+
['name' => 'GlobalSettings#getGlobalSettings', 'url' => '/globalSettings', 'verb' => 'GET'],
31+
['name' => 'GlobalSettings#setGlobalSettings', 'url' => '/globalSettings', 'verb' => 'PUT'],
32+
['name' => 'OcrBackendInfo#getInstalledLanguages', 'url' => '/ocrBackendInfo/installedLangs', 'verb' => 'GET']
3233
]
3334
];

lib/AppInfo/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
use OCA\WorkflowOcr\Service\EventService;
3939
use OCA\WorkflowOcr\Service\GlobalSettingsService;
4040
use OCA\WorkflowOcr\Service\IGlobalSettingsService;
41+
use OCA\WorkflowOcr\Service\IOcrBackendInfoService;
4142
use OCA\WorkflowOcr\Service\IOcrService;
43+
use OCA\WorkflowOcr\Service\OcrBackendInfoService;
4244
use OCA\WorkflowOcr\Service\OcrService;
4345
use OCA\WorkflowOcr\Wrapper\CommandWrapper;
4446
use OCA\WorkflowOcr\Wrapper\Filesystem;
@@ -76,6 +78,7 @@ public function register(IRegistrationContext $context): void {
7678
$context->registerServiceAlias(IFilesystem::class, Filesystem::class);
7779
$context->registerServiceAlias(IGlobalSettingsService::class, GlobalSettingsService::class);
7880
$context->registerServiceAlias(IEventService::class, EventService::class);
81+
$context->registerServiceAlias(IOcrBackendInfoService::class, OcrBackendInfoService::class);
7982

8083
// BUG #43
8184
$context->registerService(ICommand::class, function () {

lib/Controller/ControllerBase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Robin Windey <ro.windey@gmail.com>
7+
*
8+
* @author Robin Windey <ro.windey@gmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\WorkflowOcr\Controller;
28+
29+
use OCP\AppFramework\Controller;
30+
use OCP\AppFramework\Http\JSONResponse;
31+
32+
abstract class ControllerBase extends Controller {
33+
protected function tryExecute(callable $function) : JSONResponse {
34+
try {
35+
$result = $function();
36+
return new JSONResponse($result);
37+
} catch (\Throwable $e) {
38+
return new JSONResponse(['error' => $e->getMessage()], 500);
39+
}
40+
}
41+
}

lib/Controller/GlobalSettingsController.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828

2929
use OCA\WorkflowOcr\Model\GlobalSettings;
3030
use OCA\WorkflowOcr\Service\IGlobalSettingsService;
31-
use OCP\AppFramework\Controller;
3231
use OCP\AppFramework\Http\JSONResponse;
3332
use OCP\IRequest;
3433

3534
/**
3635
* This is the backend API controller for the Admin.vue component.
3736
*/
38-
class GlobalSettingsController extends Controller {
37+
class GlobalSettingsController extends ControllerBase {
3938
/** @var IGlobalSettingsService */
4039
private $globalSettingsService;
4140

@@ -66,13 +65,4 @@ public function setGlobalSettings(array $globalSettings) : JSONResponse {
6665
return $this->globalSettingsService->getGlobalSettings();
6766
});
6867
}
69-
70-
private function tryExecute(callable $function) : JSONResponse {
71-
try {
72-
$result = $function();
73-
return new JSONResponse($result);
74-
} catch (\Throwable $e) {
75-
return new JSONResponse(['error' => $e->getMessage()], 500);
76-
}
77-
}
7868
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Robin Windey <ro.windey@gmail.com>
7+
*
8+
* @author Robin Windey <ro.windey@gmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\WorkflowOcr\Controller;
28+
29+
use OCA\WorkflowOcr\Service\IOcrBackendInfoService;
30+
use OCP\AppFramework\Http\JSONResponse;
31+
use OCP\IRequest;
32+
33+
/**
34+
* This is the backend API controller which provides informations about the OCR backend system.
35+
*/
36+
class OcrBackendInfoController extends ControllerBase {
37+
/** @var IOcrBackendInfoService */
38+
private $ocrBackendInfoService;
39+
40+
public function __construct($AppName, IRequest $request, IOcrBackendInfoService $ocrBackendInfoService) {
41+
parent::__construct($AppName, $request);
42+
$this->ocrBackendInfoService = $ocrBackendInfoService;
43+
}
44+
45+
/**
46+
* @return JSONResponse
47+
*/
48+
public function getInstalledLanguages() : JSONResponse {
49+
return $this->tryExecute(function () {
50+
return $this->ocrBackendInfoService->getInstalledLanguages();
51+
});
52+
}
53+
}

lib/Exception/CommandException.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Exception;
25+
26+
use Exception;
27+
28+
class CommandException extends Exception {
29+
public function __construct(string $message, string $command) {
30+
$this->message = "The command '$command' produced an error: $message";
31+
}
32+
}

lib/Helper/ISidecarFileAccessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface ISidecarFileAccessor {
2727
/**
2828
* Creates a new temporary sidecar file for OCR text content.
2929
* If a file was already created, the path to the existing file is returned.
30-
*
30+
*
3131
* @return string|bool Path to the sidecar file or false if the file could not be created
3232
*/
3333
public function getOrCreateSidecarFile();
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)