11<?php
2+
23namespace BeechIt \NewsImporter \Command ;
34
45/*
1516/**
1617 * Class ImportNewsCommand controller
1718 */
18- class ImportNewsCommandController extends \TYPO3 \CMS \Extbase \Mvc \Controller \CommandController {
19-
20- /**
21- * Define maximum line length of the terminal
22- */
23- const MAXIMUM_LINE_LENGTH = 29 ;
24-
25- /**
26- * @var array
27- */
28- protected $ settings ;
29-
30- /**
31- * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
32- * @inject
33- */
34- protected $ configurationManager ;
35-
36- /**
37- * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
38- * @inject
39- */
40- protected $ persistenceManager ;
41-
42- /**
43- * @var \BeechIt\NewsImporter\Domain\Repository\ImportSourceRepository
44- * @inject
45- */
46- protected $ importSourceRepository ;
47-
48- /**
49- * @var \BeechIt\NewsImporter\Service\ExtractorService
50- * @inject
51- */
52- protected $ extractorService ;
53-
54- /**
55- * @var \BeechIt\NewsImporter\Service\ImportService
56- * @inject
57- */
58- protected $ importService ;
59-
60- /**
61- * Call command
62- */
63- protected function callCommandMethod () {
64- $ this ->settings = $ this ->configurationManager ->getConfiguration (
65- ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS , 'newsImporter '
66- );
67- /** @var StorageRepository $storageRepository */
68- $ storageRepository = $ this ->objectManager ->get (StorageRepository::class);
69- foreach ($ storageRepository ->findAll () as $ storage ) {
70- $ storage ->setEvaluatePermissions (FALSE );
71- }
72- parent ::callCommandMethod ();
73- }
74-
75- /**
76- * Get status of all defined remotes (last run datetime)
77- */
78- public function statusCommand () {
79- $ this ->outputDashedLine ();
80- $ remotes = $ this ->importSourceRepository ->findAll ();
81- /** @var ImportSource $remote */
82- foreach ($ remotes as $ remote ) {
83- $ lastRun = '' ;
84- if ($ remote ->getDisableAutoImport ()) {
85- $ lastRun = ' - auto import is disabled! ' ;
86- } elseif ($ remote ->getLastRun ()) {
87- $ lastRun = ' - last run: ' . $ remote ->getLastRun ()->format ('Y-m-d H:i:s ' );
88- }
89- $ this ->outputLine ('[ ' . $ remote ->getUid () . '] ' . $ remote ->getUrl () . $ lastRun );
90- }
91- if ($ remotes ->count () === 0 ) {
92- $ this ->outputLine ('No remotes found! ' );
93- }
94- $ this ->outputDashedLine ();
95- $ this ->outputLine ('settings: ' . ($ this ->settings ? print_r ($ this ->settings , 1 ) : 'NO TYPOSCRIPT SETTINGS ' ));
96- $ this ->outputDashedLine ();
97- }
19+ class ImportNewsCommandController extends \TYPO3 \CMS \Extbase \Mvc \Controller \CommandController
20+ {
21+
22+ /**
23+ * Define maximum line length of the terminal
24+ */
25+ const MAXIMUM_LINE_LENGTH = 29 ;
26+
27+ /**
28+ * @var array
29+ */
30+ protected $ settings ;
31+
32+ /**
33+ * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
34+ * @inject
35+ */
36+ protected $ configurationManager ;
37+
38+ /**
39+ * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
40+ * @inject
41+ */
42+ protected $ persistenceManager ;
43+
44+ /**
45+ * @var \BeechIt\NewsImporter\Domain\Repository\ImportSourceRepository
46+ * @inject
47+ */
48+ protected $ importSourceRepository ;
49+
50+ /**
51+ * @var \BeechIt\NewsImporter\Service\ExtractorService
52+ * @inject
53+ */
54+ protected $ extractorService ;
55+
56+ /**
57+ * @var \BeechIt\NewsImporter\Service\ImportService
58+ * @inject
59+ */
60+ protected $ importService ;
61+
62+ /**
63+ * Call command
64+ */
65+ protected function callCommandMethod ()
66+ {
67+ $ this ->settings = $ this ->configurationManager ->getConfiguration (
68+ ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS , 'newsImporter '
69+ );
70+ /** @var StorageRepository $storageRepository */
71+ $ storageRepository = $ this ->objectManager ->get (StorageRepository::class);
72+ foreach ($ storageRepository ->findAll () as $ storage ) {
73+ $ storage ->setEvaluatePermissions (false );
74+ }
75+ parent ::callCommandMethod ();
76+ }
77+
78+ /**
79+ * Get status of all defined remotes (last run datetime)
80+ */
81+ public function statusCommand ()
82+ {
83+ $ this ->outputDashedLine ();
84+ $ remotes = $ this ->importSourceRepository ->findAll ();
85+ /** @var ImportSource $remote */
86+ foreach ($ remotes as $ remote ) {
87+ $ lastRun = '' ;
88+ if ($ remote ->getDisableAutoImport ()) {
89+ $ lastRun = ' - auto import is disabled! ' ;
90+ } elseif ($ remote ->getLastRun ()) {
91+ $ lastRun = ' - last run: ' . $ remote ->getLastRun ()->format ('Y-m-d H:i:s ' );
92+ }
93+ $ this ->outputLine ('[ ' . $ remote ->getUid () . '] ' . $ remote ->getUrl () . $ lastRun );
94+ }
95+ if ($ remotes ->count () === 0 ) {
96+ $ this ->outputLine ('No remotes found! ' );
97+ }
98+ $ this ->outputDashedLine ();
99+ $ this ->outputLine ('settings: ' . ($ this ->settings ? print_r ($ this ->settings , 1 ) : 'NO TYPOSCRIPT SETTINGS ' ));
100+ $ this ->outputDashedLine ();
101+ }
98102
99103 /**
100104 * Run importer
@@ -103,102 +107,108 @@ public function statusCommand() {
103107 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
104108 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
105109 */
106- public function runCommand ($ limit = 1 ) {
107-
108- $ importSources = $ this ->importSourceRepository ->findSourcesToImport ($ limit );
109- $ importReport = [];
110- if (isset ($ this ->settings ['filter ' ]['searchFields ' ]) && is_array ($ this ->settings ['filter ' ]['searchFields ' ])) {
111- $ searchFields = $ this ->settings ['filter ' ]['searchFields ' ];
112- } else {
113- $ searchFields = ['title ' , 'bodytext ' ];
114- }
115-
116- $ this ->outputLine ();
117-
118- /** @var ImportSource $importSource */
119- foreach ($ importSources as $ importSource ) {
120- $ this ->outputLine ($ importSource ->getTitle ());
121- $ this ->outputDashedLine ();
122-
123- $ this ->extractorService ->setSource ($ importSource ->getUrl ());
124- $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
125- $ items = $ this ->extractorService ->getItems ();
126-
127- foreach ($ items as $ item ) {
128- if ($ this ->importService ->alreadyImported ($ importSource ->getStoragePid (), $ item ->getGuid ())) {
129- $ this ->outputLine ('Already imported: ' . $ item ->getGuid ());
130- } elseif ($ importSource ->getFilterWords () && !$ this ->importService ->matchFilter ($ item , $ importSource ->getFilterWords (), $ searchFields )) {
131- $ this ->outputLine ('Skipped: ' . $ item ->getGuid () . '; Filter mismatch ' );
132- } else {
133- $ this ->importService ->importItem ($ importSource , $ item );
134- $ this ->outputLine ('Imported: ' . $ item ->getGuid ());
135- $ importReport [] = $ item ->extractValue ('title ' ) . '; ' . $ item ->getGuid ();
136- }
137- }
138-
139- if (!$ items ) {
140- $ this ->outputLine ('No items found ' );
141- }
142- $ importSource ->setLastRun (new \DateTime ());
143- $ this ->importSourceRepository ->update ($ importSource );
144- $ this ->persistenceManager ->persistAll ();
145- }
146-
147- if ($ importReport !== [] && !empty ($ this ->settings ['notification ' ]['recipients ' ])) {
148- /** @var MailMessage $message */
149- $ message = GeneralUtility::makeInstance ('TYPO3 \\CMS \\Core \\Mail \\MailMessage ' );
150- $ message ->setTo ($ this ->settings ['notification ' ]['recipients ' ])
151- ->setSubject ($ this ->settings ['notification ' ]['subject ' ] ?: 'New items imported ' );
152- if ($ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromAddress ' ]) {
153- $ message ->setFrom ($ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromAddress ' ], $ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromName ' ] ?: NULL );
154- }
155- $ message ->setBody (
156- vsprintf (
157- $ this ->settings ['notification ' ]['body ' ] ?: 'Imported %1$d items: %2$s ' ,
158- [
159- count ($ importReport ),
160- PHP_EOL . PHP_EOL . implode (PHP_EOL , $ importReport )
110+ public function runCommand ($ limit = 1 )
111+ {
112+
113+ $ importSources = $ this ->importSourceRepository ->findSourcesToImport ($ limit );
114+ $ importReport = [];
115+ if (isset ($ this ->settings ['filter ' ]['searchFields ' ]) && is_array ($ this ->settings ['filter ' ]['searchFields ' ])) {
116+ $ searchFields = $ this ->settings ['filter ' ]['searchFields ' ];
117+ } else {
118+ $ searchFields = ['title ' , 'bodytext ' ];
119+ }
120+
121+ $ this ->outputLine ();
122+
123+ /** @var ImportSource $importSource */
124+ foreach ($ importSources as $ importSource ) {
125+ $ this ->outputLine ($ importSource ->getTitle ());
126+ $ this ->outputDashedLine ();
127+
128+ $ this ->extractorService ->setSource ($ importSource ->getUrl ());
129+ $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
130+ $ items = $ this ->extractorService ->getItems ();
131+
132+ foreach ($ items as $ item ) {
133+ if ($ this ->importService ->alreadyImported ($ importSource ->getStoragePid (), $ item ->getGuid ())) {
134+ $ this ->outputLine ('Already imported: ' . $ item ->getGuid ());
135+ } elseif ($ importSource ->getFilterWords () && !$ this ->importService ->matchFilter ($ item ,
136+ $ importSource ->getFilterWords (), $ searchFields )) {
137+ $ this ->outputLine ('Skipped: ' . $ item ->getGuid () . '; Filter mismatch ' );
138+ } else {
139+ $ this ->importService ->importItem ($ importSource , $ item );
140+ $ this ->outputLine ('Imported: ' . $ item ->getGuid ());
141+ $ importReport [] = $ item ->extractValue ('title ' ) . '; ' . $ item ->getGuid ();
142+ }
143+ }
144+
145+ if (!$ items ) {
146+ $ this ->outputLine ('No items found ' );
147+ }
148+ $ importSource ->setLastRun (new \DateTime ());
149+ $ this ->importSourceRepository ->update ($ importSource );
150+ $ this ->persistenceManager ->persistAll ();
151+ }
152+
153+ if ($ importReport !== [] && !empty ($ this ->settings ['notification ' ]['recipients ' ])) {
154+ /** @var MailMessage $message */
155+ $ message = GeneralUtility::makeInstance ('TYPO3 \\CMS \\Core \\Mail \\MailMessage ' );
156+ $ message ->setTo ($ this ->settings ['notification ' ]['recipients ' ])
157+ ->setSubject ($ this ->settings ['notification ' ]['subject ' ] ?: 'New items imported ' );
158+ if ($ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromAddress ' ]) {
159+ $ message ->setFrom ($ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromAddress ' ],
160+ $ GLOBALS ['TYPO3_CONF_VARS ' ]['MAIL ' ]['defaultMailFromName ' ] ?: null );
161+ }
162+ $ message ->setBody (
163+ vsprintf (
164+ $ this ->settings ['notification ' ]['body ' ] ?: 'Imported %1$d items: %2$s ' ,
165+ [
166+ count ($ importReport ),
167+ PHP_EOL . PHP_EOL . implode (PHP_EOL , $ importReport )
161168 ]
162- )
163- );
164- $ message ->send ();
165- }
166- }
167-
168- /**
169- * Test import source by counting found items and displaying data of first item
170- *
171- * @param ImportSource $importSource
172- */
173- public function testSourceCommand (ImportSource $ importSource ) {
174- $ this ->outputLine ('Fetch: ' . $ importSource ->getUrl ());
175- $ this ->outputDashedLine ();
176-
177- $ this ->extractorService ->setSource ($ importSource ->getUrl ());
178- $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
179- $ items = $ this ->extractorService ->getItems ();
180-
181- $ this ->outputLine ('Found ' . count ($ items ) . ' items ' );
182- $ this ->outputDashedLine ();
183-
184- if (count ($ items )) {
185- $ this ->outputLine ('GUID: ' . $ items [0 ]->getGuid ());
186- $ this ->outputDashedLine ();
187- $ this ->outputLine (print_r ($ items [0 ]->toArray (), 1 ));
188- }
189- }
190-
191- /**
192- * @param string $char
193- */
194- protected function outputDashedLine ($ char = '- ' ) {
195- $ this ->outputLine (str_repeat ($ char , self ::MAXIMUM_LINE_LENGTH ));
196- }
197-
198- /**
199- * @return \TYPO3\CMS\Core\Database\DatabaseConnection
200- */
201- protected function getDatabaseConnection () {
202- return $ GLOBALS ['TYPO3_DB ' ];
203- }
169+ )
170+ );
171+ $ message ->send ();
172+ }
173+ }
174+
175+ /**
176+ * Test import source by counting found items and displaying data of first item
177+ *
178+ * @param ImportSource $importSource
179+ */
180+ public function testSourceCommand (ImportSource $ importSource )
181+ {
182+ $ this ->outputLine ('Fetch: ' . $ importSource ->getUrl ());
183+ $ this ->outputDashedLine ();
184+
185+ $ this ->extractorService ->setSource ($ importSource ->getUrl ());
186+ $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
187+ $ items = $ this ->extractorService ->getItems ();
188+
189+ $ this ->outputLine ('Found ' . count ($ items ) . ' items ' );
190+ $ this ->outputDashedLine ();
191+
192+ if (count ($ items )) {
193+ $ this ->outputLine ('GUID: ' . $ items [0 ]->getGuid ());
194+ $ this ->outputDashedLine ();
195+ $ this ->outputLine (print_r ($ items [0 ]->toArray (), 1 ));
196+ }
197+ }
198+
199+ /**
200+ * @param string $char
201+ */
202+ protected function outputDashedLine ($ char = '- ' )
203+ {
204+ $ this ->outputLine (str_repeat ($ char , self ::MAXIMUM_LINE_LENGTH ));
205+ }
206+
207+ /**
208+ * @return \TYPO3\CMS\Core\Database\DatabaseConnection
209+ */
210+ protected function getDatabaseConnection ()
211+ {
212+ return $ GLOBALS ['TYPO3_DB ' ];
213+ }
204214}
0 commit comments