UpdateExtensionListTask.php 10.1 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Task;

flyguide's avatar
flyguide committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Update extension list task
 */
31
class UpdateExtensionListTask extends \T3o\TerFe2\Task\AbstractTask
32
33
34
35
36
37
38
39
40
41
42
43
44
{

    /**
     * @var string
     */
    public $providerName = 'extensionmanager';

    /**
     * @var boolean
     */
    public $createExtensions = TRUE;

    /**
45
     * @var \T3o\TerFe2\Provider\ProviderManager
46
47
48
49
     */
    protected $providerManager;

    /**
50
     * @var \T3o\TerFe2\Object\ObjectBuilder
51
52
53
54
     */
    protected $objectBuilder;

    /**
55
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
56
57
58
59
     */
    protected $persistenceManager;

    /**
60
     * @var \T3o\TerFe2\Domain\Repository\ExtensionRepository
61
62
63
64
     */
    protected $extensionRepository;

    /**
65
     * @var \T3o\TerFe2\Domain\Repository\AuthorRepository
66
67
68
69
     */
    protected $authorRepository;

    /**
70
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
71
72
73
74
75
76
77
78
79
80
81
     */
    protected $ownerRepository;


    /**
     * Initialize task
     *
     * @return void
     */
    public function initializeTask()
    {
82
83
        $this->providerManager = $this->objectManager->get(\T3o\TerFe2\Provider\ProviderManager::class);
        $this->objectBuilder = $this->objectManager->get(\T3o\TerFe2\Object\ObjectBuilder::class);
84
        $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
85
86
        $this->extensionRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\ExtensionRepository::class);
        $this->authorRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\AuthorRepository::class);
87
        $this->ownerRepository = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository::class);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

        // Set registry name to current provider name
        $this->registry->setName(get_class($this) . '_' . $this->providerName);
    }


    /**
     * Execute the task
     *
     * @param integer $lastRun Timestamp of the last run
     * @param integer $offset Starting point
     * @param integer $count Element count to process at once
     * @return boolean TRUE on success
     */
    protected function executeTask($lastRun, $offset, $count)
    {
        // Check static setup
        if (empty($this->setup['settings.'])) {
106
            throw new \Exception('Please include static setup "TER Frontend - Default Configuration (ter_fe2)" on root page');
107
108
109
110
        }

        // Check storage page
        if (!$this->storagePageConfigured()) {
111
            throw new \Exception('Please configure "plugin.tx_terfe2.persistence.storagePid" in TypoScript setup');
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        }

        // Get extension structure from provider
        $provider = $this->providerManager->getProvider($this->providerName);
        $extensions = $provider->getExtensions($lastRun, $offset, $count);
        if (empty($extensions)) {
            return FALSE;
        }

        // Build extensions...
        foreach ($extensions as $extensionRow) {
            $this->createOrUpdateExtension($extensionRow);
        }

        return TRUE;
    }


    /**
     * Create or update an extension
     *
     * @param array $extensionRow Extension row
     * @return void
     */
    protected function createOrUpdateExtension(array $extensionRow)
    {
        $modified = FALSE;

        if (empty($extensionRow['ext_key'])) {
141
            \T3o\TerFe2\Utility\Log::addMessage('Extension key was empty for extension "' . $extensionRow['title'] . '"', 'ter_fe2', 2);
142
143
144
145
146
147
148
149
150
151
152
            return;
        }

        // Get extension model
        if ($this->extensionRepository->countByExtKey($extensionRow['ext_key'])) {
            $extension = $this->extensionRepository->findOneByExtKey($extensionRow['ext_key']);
            if ($extensionRow['flattr_username'] !== '') {
                $extension->setFlattrUsername($extensionRow['flattr_username']);
                $modified = TRUE;
            }
        } else if (!empty($this->createExtensions)) {
153
            $extension = $this->objectBuilder->create('\T3o\TerFe2\Domain\Model\Extension', $extensionRow);
154
155
156
157
            $extension->setLastUpload(new DateTime());
            $extension->setLastMaintained(new DateTime());
            $modified = TRUE;
        } else {
158
            \T3o\TerFe2\Utility\Log::addMessage('Extension "' . $extensionRow['ext_key'] . '" not found and not created', 'ter_fe2', 2);
159
160
161
162
163
164
165
166
167
168
169
            return;
        }

        // Versions
        foreach ($extensionRow['versions'] as $versionRow) {
            // Version already exists, so do nothing here
            if ($this->extensionRepository->countByExtKeyAndVersionNumber($extensionRow['ext_key'], $versionRow['version_number'])) {
                continue;
            }

            // Extension model does not exist, so do nothing
170
            if (!$extension or !($extension instanceof \T3o\TerFe2\Domain\Model\Extension)) {
171
                $extension = $this->extensionRepository->findOneByExtKey($extensionRow['ext_key']);
172
                if (!($extension instanceof \T3o\TerFe2\Domain\Model\Extension)) {
173
174
175
176
                    continue;
                }
            }

177
            $version = $this->objectBuilder->create('\T3o\TerFe2\Domain\Model\Version', $versionRow);
178
179
180
181
182
183
            $version->setExtension($extension);
            $version->setExtensionProvider($this->providerName);
            $modified = TRUE;

            // Relations
            foreach ($versionRow['relations'] as $relationRow) {
184
                $relation = $this->objectBuilder->create('\T3o\TerFe2\Domain\Model\Relation', $relationRow);
185
186
                if (strtolower($relationRow['relation_key']) != 'typo3') {
                    $relatedExtension = $this->extensionRepository->findOneByExtKey($relationRow['relation_key']);
187
                    if ($relatedExtension instanceof \T3o\TerFe2\Domain\Model\Extension) {
188
189
190
191
192
193
194
195
196
197
198
199
                        $relation->setRelatedExtension($relatedExtension);
                    }
                }
                $version->addSoftwareRelation($relation);
            }

            // Author
            if (!empty($versionRow['author'])) {
                $authorRow = $versionRow['author'];
                if ($this->authorRepository->findByAuthorData($authorRow)->count() == 1) {
                    $author = $this->authorRepository->findByAuthorData($authorRow)->getFirst();
                } else {
200
                    $author = $this->objectBuilder->create('\T3o\TerFe2\Domain\Model\Author', $authorRow);
201
202
203
204
205
206
207
208
209
210
211
212
                    $this->persistenceManager->getSession()->registerReconstitutedObject($author);
                }
                if ($frontendUser = $this->ownerRepository->findOneByUsername(trim($authorRow['username']))) {
                    $author->setFrontendUser($frontendUser);
                }
                $version->setAuthor($author);
            }

            $extension->addVersion($version);
        }

        // Persist objects
213
        if ($modified and $extension instanceof \T3o\TerFe2\Domain\Model\Extension) {
214
215
216
217
            $this->persistenceManager->getSession()->registerReconstitutedObject($extension);
            $this->persistenceManager->persistAll();

            // update the EXT:solr Index Queue
218
219
            if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('solr')) {
                $indexQueue = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_solr_indexqueue_Queue');
220
221
222
223
224
225
226
227
228
229
230
231
232
                $indexQueue->updateItem('tx_terfe2_domain_model_extension', $extension->getUid());
            }
        }
    }


    /**
     * Check whether a storage page is configured or not
     *
     * @return TRUE if a storage page was found
     */
    protected function storagePageConfigured()
    {
233
        $setup = \T3o\TerFe2\Utility\TypoScript::getSetup('config.tx_extbase.persistence');
234
        $setup = \TYPO3\CMS\Extbase\Utility\ArrayUtility::arrayMergeRecursiveOverrule($setup, $this->setup['persistence.'], FALSE, FALSE);
235
236
237
        if (!empty($setup['storagePid'])) {
            return TRUE;
        }
238
        if (!empty($setup['classes.']['\T3o\TerFe2\Domain\Model\Extension.']['newRecordStoragePid'])) {
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
            return TRUE;
        }
        return FALSE;
    }


    /**
     * Returns additional information
     *
     * @return string
     */
    public function getAdditionalInformation()
    {
        // Get title
        $title = ucfirst($this->providerName);
        if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$this->providerName]['title'])) {
            $title = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ter_fe2']['extensionProviders'][$this->providerName]['title'];
256
            $title = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($title, '');
257
258
259
        }

        // Load registry
260
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
261
        $registry = $objectManager->get(\T3o\TerFe2\Persistence\Registry::class);
262
263
264
265
266
267
268
269
        $registry->setName(get_class($this) . '_' . $this->providerName);

        // Get process information
        $lastRun = (int)$registry->get('lastRun');
        $offset = (int)$registry->get('offset');

        return ' Provider: ' . $title . ' | Offset: ' . $offset . ' ';
    }
flyguide's avatar
flyguide committed
270
271

}