x Sorry, these patches are not available anymore due to data migration. Please run a fresh inspection.
Failed Conditions
Push — master ( 5f60a5...9b80eb )
by Rafael
21:42
created

Classes/Migrations/RemoveSiteFromScheduler.php (1 issue)

1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Migrations;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Database\ConnectionPool;
29
use TYPO3\CMS\Core\Messaging\FlashMessage;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Removes the Site property from the SchedulerTask and set the rootPageId property.
34
 *
35
 * @package ApacheSolrForTypo3\Solr\Migrations
36
 */
37
class RemoveSiteFromScheduler implements Migration {
38
39
    /**
40
     * Called by the extension manager to determine if the update menu entry
41
     * should by showed.
42
     *
43
     * @return bool
44
     */
45
    public function isNeeded()
46
    {
47
        $taskRows = $this->getTasksWithAssignedSite();
48
        $legacySchedulerTaskCount = $taskRows->rowCount();
49
50
        return $legacySchedulerTaskCount > 0;
51
    }
52
53
    /**
54
     * Main update function called by the extension manager.
55
     *
56
     * @return string
57
     */
58
    public function process()
59
    {
60
        $taskRows = $this->getTasksWithAssignedSite();
61
        $legacySchedulerTasks = $taskRows->fetchAll();
62
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable("tx_scheduler_task");
63
64
        $status = FlashMessage::OK;
65
        $title = 'Remove site from scheduler task';
66
        $failedTaskCount = 0;
67
        $migratedTaskCount = 0;
68
        foreach ($legacySchedulerTasks as $legacySchedulerTask) {
69
            try {
70
                $uid = $legacySchedulerTask['uid'];
71
                $task = unserialize($legacySchedulerTask['serialized_task_object']);
72
                $task->setRootPageId($task->getSite()->getRootPageId());
73
                $updatedTask = serialize($task);
74
                $updatedRows = $queryBuilder->update('tx_scheduler_task')
75
                    ->where($queryBuilder->expr()->eq('uid', $uid))
76
                    ->set('serialized_task_object', $updatedTask)
77
                    ->execute();
78
79
                $migratedTaskCount += $updatedRows;
80
            } catch (\Exception $e) {
81
                $failedTaskCount++;
82
                $status = FlashMessage::ERROR;
83
            }
84
        }
85
86
        $message = 'Migrated ' . (int)$migratedTaskCount . ' scheduler tasks! Update of ' . (int)$failedTaskCount . ' failed!';
87
        return [$status, $title, $message];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($status, $title, $message) returns the type array<integer,integer|string> which is incompatible with the documented return type string.
Loading history...
88
    }
89
90
    /**
91
     * @return \Doctrine\DBAL\Driver\Statement|int
92
     */
93
    protected function getTasksWithAssignedSite()
94
    {
95
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable("tx_scheduler_task");
96
        $taskRows = $queryBuilder
97
            ->select('uid', 'serialized_task_object')
98
            ->from("tx_scheduler_task")
99
            ->where(
100
                $queryBuilder->expr()->andX(
101
                    $queryBuilder->expr()->like('serialized_task_object', "'%ApacheSolrForTypo3%'"),
102
                    $queryBuilder->expr()->like('serialized_task_object', "'%site\";O:28:\"%'")
103
                )
104
            )->execute();
105
106
        return $taskRows;
107
    }
108
}