Skip to content

Commit cf03df6

Browse files
[BUGFIX] various bugfixes (#26)
* [WIP] fixes * [TASK] ci fix * [TASK] add php-version 8.2 to github workflow * [WIP] intercept errors when API connection fails, logging, messages * [TASK] bump version
1 parent 0869a16 commit cf03df6

File tree

18 files changed

+307
-103
lines changed

18 files changed

+307
-103
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
matrix:
2222
php-version:
2323
- 8.1
24+
- 8.2
2425
typoscript-lint:
2526
name: "TypoScript linter"
2627
runs-on: ubuntu-20.04
@@ -80,6 +81,7 @@ jobs:
8081
- "php:sniff"
8182
php-version:
8283
- 8.1
84+
- 8.2
8385
xliff-lint:
8486
name: "Xliff linter"
8587
runs-on: ubuntu-20.04
@@ -115,12 +117,12 @@ jobs:
115117
run: |
116118
composer require --no-progress typo3/minimal:"$TYPO3"
117119
composer show
118-
- if: "matrix.composer-dependencies == 'lowest'"
120+
- if: matrix.composer-dependencies == 'lowest'
119121
name: "Install lowest dependencies with composer"
120122
run: |
121123
composer update --no-ansi --no-interaction --no-progress --with-dependencies --prefer-lowest
122124
composer show
123-
- if: "matrix.composer-dependencies == 'highest'"
125+
- if: matrix.composer-dependencies == 'highest'
124126
name: "Install highest dependencies with composer"
125127
run: |
126128
composer update --no-ansi --no-interaction --no-progress --with-dependencies
@@ -135,5 +137,6 @@ jobs:
135137
- lowest
136138
php-version:
137139
- 8.1
140+
- 8.2
138141
typo3-version:
139142
- ^12.4

Classes/Command/UpdateQbankFileStatusCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
115115

116116
continue;
117117
}
118+
} catch (\Throwable $th) {
119+
$io->writeln(
120+
sprintf(
121+
'QBank file [%s] was not found: "%s"',
122+
$file['tx_qbank_id'],
123+
$th->getMessage()
124+
)
125+
);
126+
break;
118127
}
119128

120129
$remoteUpdate = (int)$media->getUpdated()->getTimestamp();

Classes/Controller/ManagementController.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use TYPO3\CMS\Core\Imaging\Icon;
3434
use TYPO3\CMS\Core\Imaging\IconFactory;
3535
use TYPO3\CMS\Core\Localization\LanguageService;
36+
use TYPO3\CMS\Core\Log\LogManager;
3637
use TYPO3\CMS\Core\Messaging\AbstractMessage;
3738
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
3839
use TYPO3\CMS\Core\Page\PageRenderer;
@@ -90,7 +91,24 @@ public function initializeAction(): void
9091
*/
9192
protected function initializeView(): void
9293
{
94+
$apiStatus = [];
95+
try {
96+
$checkStatus = $this->qbankService->fetchMediaProperties();
97+
} catch (\Throwable $th) {
98+
$apiStatus = [
99+
'errorMessage' => $th->getMessage(),
100+
'errorCode' => $th->getCode(),
101+
];
102+
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
103+
$logger->error(
104+
sprintf(
105+
'Failed to connect to QBank API: "%s"',
106+
$th->getMessage()
107+
)
108+
);
109+
}
93110
$this->moduleTemplate->assignMultiple([
111+
'apiStatus' => $apiStatus,
94112
'dateFormat' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
95113
'timeFormat' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
96114
'dateTimeFormat' =>
@@ -192,7 +210,11 @@ protected function overviewAction(): ResponseInterface
192210
{
193211
$this->generateDropdownMenu('overview');
194212
$this->generateButtons('overview');
195-
$properties = $this->qbankService->fetchMediaProperties();
213+
try {
214+
$properties = $this->qbankService->fetchMediaProperties();
215+
} catch (\Throwable $th) {
216+
$properties = [];
217+
}
196218
$this->moduleTemplate->assign('properties', $properties);
197219
return $this->moduleTemplate->renderResponse('Management/Overview');
198220
}
@@ -206,9 +228,14 @@ protected function mappingsAction(): ResponseInterface
206228
$this->generateButtons('mappings');
207229
$mappingRepository = GeneralUtility::makeInstance(MappingRepository::class);
208230
$mappings = $mappingRepository->findAll();
231+
try {
232+
$mediaProperties = $this->qbankService->fetchMediaProperties();
233+
} catch (\Throwable $th) {
234+
$mediaProperties = [];
235+
}
209236
$this->moduleTemplate->assignMultiple([
210237
'mappings' => $mappings,
211-
'mediaProperties' => $this->qbankService->fetchMediaProperties(),
238+
'mediaProperties' => $mediaProperties,
212239
'fileProperties' => PropertyUtility::getFileProperties(),
213240
]);
214241
return $this->moduleTemplate->renderResponse('Management/Mappings');

Classes/Hook/MediaUsageReporterDataHandlerHook.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Pixelant\Qbank\Hook;
66

77
use Pixelant\Qbank\Service\QbankService;
8+
use Pixelant\Qbank\Utility\MessageUtility;
89
use TYPO3\CMS\Core\DataHandling\DataHandler;
10+
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
911
use TYPO3\CMS\Core\Utility\GeneralUtility;
1012
use TYPO3\CMS\Core\Utility\MathUtility;
1113

@@ -26,9 +28,20 @@ public function processDatamap_afterAllOperations(DataHandler $dataHandler): voi
2628
foreach ($dataHandler->datamap['sys_file_reference'] ?? [] as $id => $record) {
2729
// Only process new records
2830
if (!MathUtility::canBeInterpretedAsInteger($id)) {
29-
GeneralUtility::makeInstance(QbankService::class)->reportMediaUsageInFileReference(
30-
$dataHandler->substNEWwithIDs[$id]
31-
);
31+
try {
32+
GeneralUtility::makeInstance(QbankService::class)->reportMediaUsageInFileReference(
33+
$dataHandler->substNEWwithIDs[$id]
34+
);
35+
} catch (\Throwable $th) {
36+
MessageUtility::enqueueMessage(
37+
sprintf(
38+
'Media usage was not reported to QBank: "%s"',
39+
$th->getMessage()
40+
),
41+
'Connection to QBank API failed',
42+
ContextualFeedbackSeverity::ERROR
43+
);
44+
}
3245
}
3346
}
3447
}
@@ -52,22 +65,33 @@ public function processCmdmap_preProcess(
5265
DataHandler $dataHandler
5366
): void {
5467
if ($table === 'sys_file_reference') {
55-
switch ($command) {
56-
case 'move':
57-
/** @var QbankService $qbankService */
58-
$qbankService = GeneralUtility::makeInstance(QbankService::class);
59-
$qbankService->removeMediaUsageInFileReference($id);
60-
$qbankService->reportMediaUsageInFileReference($id);
68+
try {
69+
switch ($command) {
70+
case 'move':
71+
/** @var QbankService $qbankService */
72+
$qbankService = GeneralUtility::makeInstance(QbankService::class);
73+
$qbankService->removeMediaUsageInFileReference($id);
74+
$qbankService->reportMediaUsageInFileReference($id);
6175

62-
break;
63-
case 'delete':
64-
GeneralUtility::makeInstance(QbankService::class)->removeMediaUsageInFileReference($id);
76+
break;
77+
case 'delete':
78+
GeneralUtility::makeInstance(QbankService::class)->removeMediaUsageInFileReference($id);
6579

66-
break;
67-
case 'undelete':
68-
GeneralUtility::makeInstance(QbankService::class)->reportMediaUsageInFileReference($id);
80+
break;
81+
case 'undelete':
82+
GeneralUtility::makeInstance(QbankService::class)->reportMediaUsageInFileReference($id);
6983

70-
break;
84+
break;
85+
}
86+
} catch (\Throwable $th) {
87+
MessageUtility::enqueueMessage(
88+
sprintf(
89+
'Media usage was not reported to QBank: "%s"',
90+
$th->getMessage()
91+
),
92+
'Connection to QBank API failed',
93+
ContextualFeedbackSeverity::ERROR
94+
);
7195
}
7296
}
7397
}

Classes/Repository/AbstractRepository.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Pixelant\Qbank\Utility\QbankUtility;
88
use QBNK\QBank\API\QBankApi;
9+
use TYPO3\CMS\Core\Log\LogManager;
910
use TYPO3\CMS\Core\SingletonInterface;
11+
use TYPO3\CMS\Core\Utility\GeneralUtility;
1012

1113
class AbstractRepository implements SingletonInterface
1214
{
@@ -17,6 +19,16 @@ class AbstractRepository implements SingletonInterface
1719

1820
public function __construct()
1921
{
20-
$this->api = QbankUtility::getApi();
22+
try {
23+
$this->api = QbankUtility::getApi();
24+
} catch (\Throwable $th) {
25+
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
26+
$logger->error(
27+
sprintf(
28+
'Failed to connect to QBank API: "%s"',
29+
$th->getMessage()
30+
)
31+
);
32+
}
2133
}
2234
}

Classes/Repository/MediaUsageRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function findByQbankAndLocalId(int $qbankId, string $localId): array
8181

8282
$result = [];
8383
foreach ($usages as $usage) {
84-
if ($usage->getContext()['localID'] === $localId) {
84+
if (($usage->getContext()['localID'] ?? '') === $localId) {
8585
$result[] = $usage;
8686
}
8787
}
@@ -103,7 +103,7 @@ public function findByQbankId(int $qbankId): array
103103
return $this->api->media()->listUsages($qbankId);
104104
}
105105

106-
return $this->api->media()->listUsages($qbankId, $sessionSourceId);
106+
return $this->api->media()->listUsages($qbankId, (string)$sessionSourceId);
107107
}
108108

109109
/**

Classes/Service/QbankService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Pixelant\Qbank\Service\Event\FilePropertyChangeEvent;
2121
use Pixelant\Qbank\Service\Event\FileReferenceUrlEvent;
2222
use Pixelant\Qbank\Service\Event\ResolvePageTitleEvent;
23+
use Pixelant\Qbank\Utility\MessageUtility;
2324
use Pixelant\Qbank\Utility\PropertyUtility;
2425
use Pixelant\Qbank\Utility\QbankUtility;
2526
use QBNK\QBank\API\Exception\RequestException;
@@ -37,6 +38,7 @@
3738
use TYPO3\CMS\Core\Resource\ResourceFactory;
3839
use TYPO3\CMS\Core\SingletonInterface;
3940
use TYPO3\CMS\Core\Site\SiteFinder;
41+
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
4042
use TYPO3\CMS\Core\Utility\GeneralUtility;
4143
use TYPO3\CMS\Core\Utility\StringUtility;
4244

@@ -252,6 +254,17 @@ public function synchronizeMetadata(int $fileId): void
252254
1625149218
253255
);
254256
}
257+
} catch (\Throwable $th) {
258+
MessageUtility::enqueueMessage(
259+
sprintf(
260+
'Could not synchronize metadata for file [%s]: "%s"',
261+
$fileId,
262+
$th->getMessage()
263+
),
264+
'QBank',
265+
ContextualFeedbackSeverity::ERROR
266+
);
267+
return;
255268
}
256269

257270
$metaDataMappings = GeneralUtility::makeInstance(MappingRepository::class)->findAllAsKeyValuePairs(false);

Classes/Utility/MessageUtility.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pixelant\Qbank\Utility;
6+
7+
use TYPO3\CMS\Core\Context\Context;
8+
use TYPO3\CMS\Core\Log\LogManager;
9+
use TYPO3\CMS\Core\Messaging\FlashMessage;
10+
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
11+
use TYPO3\CMS\Core\Messaging\FlashMessageService;
12+
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
13+
use TYPO3\CMS\Core\Utility\GeneralUtility;
14+
15+
/**
16+
* Convenience methods relating to TYPO3 messaging.
17+
*/
18+
class MessageUtility
19+
{
20+
public static function enqueueMessage(
21+
string $message,
22+
string $title = '',
23+
ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::OK
24+
): void {
25+
$context = GeneralUtility::makeInstance(Context::class);
26+
$beUserId = $context->getPropertyFromAspect('backend.user', 'id');
27+
28+
if ($beUserId > 0) {
29+
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
30+
$notificationQueue = $flashMessageService->getMessageQueueByIdentifier(
31+
FlashMessageQueue::NOTIFICATION_QUEUE
32+
);
33+
$flashMessage = GeneralUtility::makeInstance(
34+
FlashMessage::class,
35+
$message,
36+
$title,
37+
$severity,
38+
true
39+
);
40+
$notificationQueue->enqueue($flashMessage);
41+
}
42+
43+
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
44+
$logger->error($message . PHP_EOL . $title);
45+
}
46+
}

Classes/Utility/PropertyUtility.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818
class PropertyUtility
1919
{
20-
protected static $filePropertyTypeConverterCache = [];
21-
2220
/**
2321
* Returns the file properties array from $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['qbank']['fileProperties'].
2422
*
@@ -29,12 +27,10 @@ public static function getFileProperties(): array
2927
$properties = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['qbank']['fileProperties'] ?? [];
3028

3129
foreach ($properties as $key => &$property) {
32-
if (is_callable($property['label'])) {
33-
$property['label'] = $property['label']() ?? $key;
34-
}
35-
3630
if (!isset($property['label'])) {
37-
$property['label'] = $key;
31+
$property['label'] = $GLOBALS['TCA']['sys_file']['columns'][$key]['label']
32+
?? $GLOBALS['TCA']['sys_file_metadata']['columns'][$key]['label']
33+
?? $key;
3834
}
3935
}
4036

@@ -50,10 +46,6 @@ public static function getFileProperties(): array
5046
*/
5147
public static function getTypeConverterForFileProperty(string $filePropertyName): TypeConverterInterface
5248
{
53-
if (isset(self::$filePropertyTypeConverterCache[$filePropertyName])) {
54-
return self::$filePropertyTypeConverterCache[$filePropertyName];
55-
}
56-
5749
$propertyConfiguration = self::getFileProperties()[$filePropertyName] ?? null;
5850

5951
if (!isset($propertyConfiguration['typeConverter'])) {

Resources/Private/Language/locallang_mod_qbank.xlf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<trans-unit id="target_property">
2222
<source>TYPO3 property</source>
2323
</trans-unit>
24+
<trans-unit id="api_error">
25+
<source>Connection to QBank API failed</source>
26+
</trans-unit>
2427
</body>
2528
</file>
2629
</xliff>

0 commit comments

Comments
 (0)