Issues (202)

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
class RemoveSiteFromScheduler implements Migration {
36
37
    /**
38
     * Called by the extension manager to determine if the update menu entry
39
     * should by showed.
40
     *
41
     * @return bool
42
     */
43
    public function isNeeded()
44
    {
45
        $taskRows = $this->getTasksWithAssignedSite();
46
        $legacySchedulerTaskCount = $taskRows->rowCount();
47
48
        return $legacySchedulerTaskCount > 0;
49
    }
50
51
    /**
52
     * Main update function called by the extension manager.
53
     *
54
     * @return string
55
     */
56
    public function process()
57
    {
58
        $taskRows = $this->getTasksWithAssignedSite();
59
        $legacySchedulerTasks = $taskRows->fetchAll();
60
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable("tx_scheduler_task");
61
62
        $status = FlashMessage::OK;
63
        $title = 'Remove site from scheduler task';
64
        $failedTaskCount = 0;
65
        $migratedTaskCount = 0;
66
        foreach ($legacySchedulerTasks as $legacySchedulerTask) {
67
            try {
68
                $uid = $legacySchedulerTask['uid'];
69
                $task = unserialize($legacySchedulerTask['serialized_task_object']);
70
                $task->setRootPageId($task->getSite()->getRootPageId());
71
                $updatedTask = serialize($task);
72
                $updatedRows = $queryBuilder->update('tx_scheduler_task')
73
                    ->where($queryBuilder->expr()->eq('uid', $uid))
74
                    ->set('serialized_task_object', $updatedTask)
75
                    ->execute();
76
77
                $migratedTaskCount += $updatedRows;
78
            } catch (\Exception $e) {
79
                $failedTaskCount++;
80
                $status = FlashMessage::ERROR;
81
            }
82
        }
83
84
        $message = 'Migrated ' . (int)$migratedTaskCount . ' scheduler tasks! Update of ' . (int)$failedTaskCount . ' failed!';
85
        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...
86
    }
87
88
    /**
89
     * @return \Doctrine\DBAL\Driver\Statement|int
90
     */
91
    protected function getTasksWithAssignedSite()
92
    {
93
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable("tx_scheduler_task");
94
        $taskRows = $queryBuilder
95
            ->select('uid', 'serialized_task_object')
96
            ->from("tx_scheduler_task")
97
            ->where(
98
                $queryBuilder->expr()->andX(
99
                    $queryBuilder->expr()->like('serialized_task_object', "'%ApacheSolrForTypo3%'"),
100
                    $queryBuilder->expr()->like('serialized_task_object', "'%site\";O:28:\"%'")
101
                )
102
            )->execute();
103
104
        return $taskRows;
105
    }
106
}
107