Skip to content

Commit 06bd51b

Browse files
[TASK] Migrate to DBAL typo3 queries
1 parent b99742f commit 06bd51b

File tree

4 files changed

+116
-49
lines changed

4 files changed

+116
-49
lines changed

Classes/Controller/AdminController.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use BeechIt\NewsImporter\Domain\Model\ExtractedItem;
1111
use BeechIt\NewsImporter\Domain\Model\ImportSource;
1212
use BeechIt\NewsImporter\Domain\Repository\ImportSourceRepository;
13+
use BeechIt\NewsImporter\Exception\NewsItemNotFoundException;
1314
use BeechIt\NewsImporter\Service\ExtractorService;
1415
use BeechIt\NewsImporter\Service\ImportService;
1516
use TYPO3\CMS\Backend\Routing\UriBuilder;
@@ -180,12 +181,17 @@ public function showAction(ImportSource $importSource)
180181
$items = [];
181182
/** @var ExtractedItem $item */
182183
foreach ($extractedItems as $item) {
184+
$newsUid = false;
185+
try {
186+
$newsUid = $this->importService->getNewsItemUid($importSource->getPid(), $item->getGuid());
187+
} catch (NewsItemNotFoundException $e) {
188+
}
183189
$items[] = [
184190
'guid' => $item->getGuid(),
185191
'title' => $item->extractValue('title'),
186192
'link' => $item->extractValue('link'),
187193
'datetime' => $item->extractValue('datetime'),
188-
'newsUid' => $this->importService->alreadyImported($importSource->getPid(), $item->getGuid()),
194+
'newsUid' => $newsUid,
189195
];
190196
}
191197

@@ -197,6 +203,7 @@ public function showAction(ImportSource $importSource)
197203
* @param string $guid
198204
* @throws StopActionException
199205
* @throws UnsupportedRequestTypeException
206+
* @throws \BeechIt\NewsImporter\Exception\NewsItemNotFoundException
200207
*/
201208
public function importAction(ImportSource $importSource, $guid)
202209
{
@@ -207,8 +214,7 @@ public function importAction(ImportSource $importSource, $guid)
207214
foreach ($extractedItems as $item) {
208215
if ($item->getGuid() === $guid) {
209216
$this->importService->importItem($importSource, $item);
210-
$itemUid = $this->importService->alreadyImported($importSource->getPid(), $guid);
211-
217+
$itemUid = $this->importService->getNewsItemUid($importSource->getPid(), $guid);
212218
$this->uriBuilder->reset()->setCreateAbsoluteUri(true);
213219
if (GeneralUtility::getIndpEnv('TYPO3_SSL')) {
214220
$this->uriBuilder->setAbsoluteUriScheme('https');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* This source file is proprietary of Beech Applications bv.
4+
* Created by: Ruud Silvrants
5+
* Date: 11/05/2022
6+
* All code (c) Beech Applications bv. all rights reserverd
7+
*/
8+
9+
namespace BeechIt\NewsImporter\Exception;
10+
11+
/**
12+
* Class AccountAdminNotFoundException
13+
*/
14+
class NewsItemNotFoundException extends \Exception
15+
{
16+
public const MESSAGE = 'NewsItem not found by guid: %s';
17+
18+
public function __construct(string $guid, $code = 0, \Throwable $previous = null)
19+
{
20+
$message = sprintf(self::MESSAGE, $guid);
21+
parent::__construct($message, $code, $previous);
22+
}
23+
}

Classes/Service/ImportService.php

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
* Date: 12-06-2015
88
* All code (c) Beech Applications B.V. all rights reserved
99
*/
10+
1011
use BeechIt\NewsImporter\Domain\Model\ExtractedItem;
1112
use BeechIt\NewsImporter\Domain\Model\ImportSource;
13+
use BeechIt\NewsImporter\Exception\NewsItemNotFoundException;
1214
use GeorgRinger\News\Domain\Service\NewsImportService;
1315
use TYPO3\CMS\Backend\Utility\BackendUtility;
16+
use TYPO3\CMS\Core\Database\ConnectionPool;
17+
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
18+
use TYPO3\CMS\Core\Html\RteHtmlParser;
1419
use TYPO3\CMS\Core\Resource\DuplicationBehavior;
1520
use TYPO3\CMS\Core\Resource\ResourceFactory;
1621
use TYPO3\CMS\Core\SingletonInterface;
@@ -25,7 +30,8 @@ class ImportService implements SingletonInterface
2530
/**
2631
* @var NewsImportService
2732
*/
28-
protected $newsImportService;
33+
protected NewsImportService $newsImportService;
34+
2935
public function __construct(NewsImportService $newsImportService)
3036
{
3137
$this->newsImportService = $newsImportService;
@@ -37,7 +43,7 @@ public function __construct(NewsImportService $newsImportService)
3743
* @param ImportSource $importSource
3844
* @param ExtractedItem $item
3945
*/
40-
public function importItem(ImportSource $importSource, ExtractedItem $item)
46+
public function importItem(ImportSource $importSource, ExtractedItem $item): void
4147
{
4248
$data = $item->toArray();
4349
$data['pid'] = $importSource->getStoragePid();
@@ -62,15 +68,57 @@ public function importItem(ImportSource $importSource, ExtractedItem $item)
6268
* @param string $guid
6369
* @return bool
6470
*/
65-
public function alreadyImported($pid, $guid)
71+
public function alreadyImported(int $pid, string $guid): bool
6672
{
67-
$guid = $this->getDatabaseConnection()->fullQuoteStr($guid, 'tx_news_domain_model_news');
68-
$record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
69-
'uid',
70-
'tx_news_domain_model_news',
71-
'deleted=0 AND pid=' . (int)$pid . ' AND import_source = \'ext:news_importer\' AND import_id=' . $guid
72-
);
73-
return $record ? $record['uid'] : false;
73+
$newsItemImported = false;
74+
try {
75+
$this->getNewsItemUid($pid, $guid);
76+
$newsItemImported = true;
77+
} catch (NewsItemNotFoundException $e) {
78+
}
79+
return $newsItemImported;
80+
}
81+
82+
/**
83+
* @param int $pid
84+
* @param string $guid
85+
* @return int
86+
* @throws NewsItemNotFoundException
87+
*/
88+
public function getNewsItemUid(int $pid, string $guid): int
89+
{
90+
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_news_domain_model_news');
91+
$queryBuilder
92+
->getRestrictions()
93+
->removeAll()
94+
->add(GeneralUtility::makeInstance(DeletedRestriction::class));
95+
$firstRecord = $queryBuilder->select('uid')
96+
->from('tx_news_domain_model_news')
97+
->where(
98+
$queryBuilder->expr()->eq(
99+
'pid',
100+
$queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
101+
)
102+
)
103+
->andWhere(
104+
$queryBuilder->expr()->eq(
105+
'import_source',
106+
$queryBuilder->createNamedParameter('ext:news_importer', \PDO::PARAM_STR)
107+
)
108+
)
109+
->andWhere(
110+
$queryBuilder->expr()->eq(
111+
'import_id',
112+
$queryBuilder->createNamedParameter($guid, \PDO::PARAM_STR)
113+
)
114+
)
115+
->execute()
116+
->fetchAssociative();
117+
118+
if ($firstRecord === false) {
119+
throw new NewsItemNotFoundException($guid);
120+
}
121+
return $firstRecord['uid'];
74122
}
75123

76124
/**
@@ -83,7 +131,7 @@ public function alreadyImported($pid, $guid)
83131
* @param array $searchFields
84132
* @return bool
85133
*/
86-
public function matchFilter(ExtractedItem $item, $filterWords, array $searchFields = ['title', 'bodytext'])
134+
public function matchFilter(ExtractedItem $item, $filterWords, array $searchFields = ['title', 'bodytext']): bool
87135
{
88136
if (empty($searchFields)) {
89137
return true;
@@ -105,8 +153,9 @@ public function matchFilter(ExtractedItem $item, $filterWords, array $searchFiel
105153
*
106154
* @param string $text
107155
* @param int $pid
156+
* @return mixed
108157
*/
109-
protected function cleanBodyText($text, $pid)
158+
protected function cleanBodyText(string $text, int $pid)
110159
{
111160
static $rteHtmlParsers;
112161

@@ -115,17 +164,14 @@ protected function cleanBodyText($text, $pid)
115164
$rteHtmlParsers = [];
116165
}
117166
/** @var $htmlParser \TYPO3\CMS\Core\Html\RteHtmlParser */
118-
$rteHtmlParsers[$pid] = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\RteHtmlParser');
119-
$rteHtmlParsers[$pid]->init('tx_news_domain_model_news:bodytext', $pid);
167+
$rteHtmlParsers[$pid] = GeneralUtility::makeInstance(RteHtmlParser::class);
120168
}
121169

122170
// Perform transformation
123171
$tsConfig = BackendUtility::getPagesTSconfig($pid);
124-
return $rteHtmlParsers[$pid]->RTE_transform(
172+
return $rteHtmlParsers[$pid]->transformTextForPersistence(
125173
trim($text),
126-
['rte_transform' => ['parameters' => ['flag=rte_disabled', 'mode=ts_css']]],
127-
'db',
128-
$tsConfig['RTE.']['default.']
174+
$tsConfig['RTE.']['default.']['proc.'] ?? []
129175
);
130176
}
131177

@@ -188,12 +234,4 @@ protected function processMedia(array $data, ImportSource $importSource)
188234
}
189235
return $media;
190236
}
191-
192-
/**
193-
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
194-
*/
195-
protected function getDatabaseConnection()
196-
{
197-
return $GLOBALS['TYPO3_DB'];
198-
}
199237
}

composer.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "beechit/news-importer",
3-
"type": "typo3-cms-extension",
4-
"description": "\"News importer\": Import news feeds into your TYPO3 CMS",
5-
"license": [
6-
"GPL-2.0+"
7-
],
8-
"require": {
9-
"typo3/cms-core": ">= 7.6.0",
10-
"querypath/querypath": "^3"
11-
},
12-
"autoload": {
13-
"psr-4": {
14-
"BeechIt\\NewsImporter\\": "Classes"
15-
}
16-
},
17-
"extra": {
18-
"typo3/cms": {
19-
"extension-key": "news_importer"
20-
}
21-
}
2+
"name": "beechit/news-importer",
3+
"type": "typo3-cms-extension",
4+
"description": "\"News importer\": Import news feeds into your TYPO3 CMS",
5+
"license": [
6+
"GPL-2.0+"
7+
],
8+
"require": {
9+
"typo3/cms-core": ">= 7.6.0",
10+
"querypath/querypath": "^3"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"BeechIt\\NewsImporter\\": "Classes"
15+
}
16+
},
17+
"extra": {
18+
"typo3/cms": {
19+
"extension-key": "news_importer"
20+
}
21+
}
2222
}

0 commit comments

Comments
 (0)