UpdateScriptUtility.php 4.77 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Extensionmanager\Utility;

18
use TYPO3\CMS\Core\Core\Environment;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
21
22
23

/**
 * Utility to find and execute class.ext_update.php scripts of extensions
24
 * @internal This class is a specific ExtensionManager implementation and is not part of the Public TYPO3 API.
25
 */
26
27
28
29
30
31
32
33
34
35
36
37
class UpdateScriptUtility
{
    /**
     * Returns true, if ext_update class says it wants to run.
     *
     * @param string $extensionKey extension key
     * @return mixed NULL, if update is not available, else update script return
     */
    public function executeUpdateIfNeeded($extensionKey)
    {
        $className = $this->requireUpdateScript($extensionKey);
        $scriptObject = GeneralUtility::makeInstance($className);
38

39
40
41
42
43
        // old em always assumed the method exist, we do so too.
        // @TODO: Make this smart, let scripts implement interfaces
        // @TODO: With current ext_update construct it is impossible to enforce some type of return
        return $scriptObject->access() ? $scriptObject->main() : null;
    }
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    /**
     * Require update script.
     * Throws exception if update script does not exist, so checkUpdateScriptExists()
     * should be called before
     *
     * @param string $extensionKey
     * @return string Class name of update script
     * @throws ExtensionManagerException
     */
    protected function requireUpdateScript($extensionKey)
    {
        if (class_exists('ext_update', false)) {
            throw new ExtensionManagerException(
                'class ext_update for this run does already exist, requiring impossible',
                1359748085
            );
        }
62

63
64
65
66
67
68
69
70
71
        $className = $this->determineUpdateClassName($extensionKey);
        if ($className === '') {
            throw new ExtensionManagerException(
                'Requested update script of extension does not exist',
                1359747976
            );
        }
        return $className;
    }
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    /**
     * Checks if an update class file exists.
     *
     * Does not check if some update is needed.
     *
     * @param string $extensionKey Extension key
     * @return bool TRUE, if there is some update script and it needs to be executed
     */
    public function checkUpdateScriptExists($extensionKey)
    {
        $className = $this->determineUpdateClassName($extensionKey);
        if ($className !== '') {
            $updater = GeneralUtility::makeInstance($className);
            return $updater->access();
        }
        return false;
    }
90

91
92
93
94
95
96
97
98
99
    /**
     * Determine the real class name to use
     *
     * @param string $extensionKey
     * @return string Returns the final class name if an update script is present, otherwise empty string
     * @throws ExtensionManagerException If an update script is present but no ext_update class can be loaded
     */
    protected function determineUpdateClassName($extensionKey)
    {
100
        $updateScript = GeneralUtility::getFileAbsFileName('EXT:' . $extensionKey . '/class.ext_update.php');
101
102
103
        if (!file_exists($updateScript)) {
            return '';
        }
104

105
106
107
108
109
110
        // get script contents
        $scriptSourceCode = GeneralUtility::getUrl($updateScript);
        // check if it has a namespace
        if (!preg_match('/<\?php.*namespace\s+([^;]+);.*class/is', $scriptSourceCode, $matches)) {
            // if no, rename the class with a unique name
            $className = 'ext_update' . md5($extensionKey . $scriptSourceCode);
111
            $temporaryFileName = Environment::getVarPath() . '/transient/' . $className . '.php';
112
113
114
115
116
117
118
119
            if (!file_exists(GeneralUtility::getFileAbsFileName($temporaryFileName))) {
                $scriptSourceCode = preg_replace('/^\s*class\s+ext_update\s+/m', 'class ' . $className . ' ', $scriptSourceCode);
                GeneralUtility::writeFileToTypo3tempDir($temporaryFileName, $scriptSourceCode);
            }
            $updateScript = $temporaryFileName;
        } else {
            $className = $matches[1] . '\ext_update';
        }
120
        include_once $updateScript;
121
122
123
124
125
126
        if (!class_exists($className, false)) {
            throw new ExtensionManagerException(
                sprintf('class.ext_update.php of extension "%s" did not declare ext_update class', $extensionKey),
                1428176468
            );
        }
127

128
129
        return $className;
    }
130
}