TYPO3 CMS  TYPO3_7-6
UpdateScriptUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
31  public function executeUpdateIfNeeded($extensionKey)
32  {
33  $className = $this->requireUpdateScript($extensionKey);
34  $scriptObject = GeneralUtility::makeInstance($className);
35 
36  // old em always assumed the method exist, we do so too.
37  // @TODO: Make this smart, let scripts implement interfaces
38  // @TODO: With current ext_update construct it is impossible to enforce some type of return
39  return $scriptObject->access() ? $scriptObject->main() : null;
40  }
41 
51  protected function requireUpdateScript($extensionKey)
52  {
53  if (class_exists('ext_update', false)) {
54  throw new ExtensionManagerException(
55  'class ext_update for this run does already exist, requiring impossible',
56  1359748085
57  );
58  }
59 
60  $className = $this->determineUpdateClassName($extensionKey);
61  if ($className === '') {
62  throw new ExtensionManagerException(
63  'Requested update script of extension does not exist',
64  1359747976
65  );
66  }
67  return $className;
68  }
69 
78  public function checkUpdateScriptExists($extensionKey)
79  {
80  $className = $this->determineUpdateClassName($extensionKey);
81  if ($className !== '') {
82  $updater = GeneralUtility::makeInstance($className);
83  return $updater->access();
84  }
85  return false;
86  }
87 
95  protected function determineUpdateClassName($extensionKey)
96  {
97  $updateScript = GeneralUtility::getFileAbsFileName(
98  'EXT:' . $extensionKey . '/class.ext_update.php',
99  false
100  );
101  if (!file_exists($updateScript)) {
102  return '';
103  }
104 
105  // get script contents
106  $scriptSourceCode = GeneralUtility::getUrl($updateScript);
107  // check if it has a namespace
108  if (!preg_match('/<\?php.*namespace\s+([^;]+);.*class/is', $scriptSourceCode, $matches)) {
109  // if no, rename the class with a unique name
110  $className = 'ext_update' . md5($extensionKey . $scriptSourceCode);
111  $temporaryFileName = PATH_site . 'typo3temp/ExtensionManager/UpdateScripts/' . $className . '.php';
112  if (!file_exists(GeneralUtility::getFileAbsFileName($temporaryFileName))) {
113  $scriptSourceCode = preg_replace('/^\s*class\s+ext_update\s+/m', 'class ' . $className . ' ', $scriptSourceCode);
114  GeneralUtility::writeFileToTypo3tempDir($temporaryFileName, $scriptSourceCode);
115  }
116  $updateScript = $temporaryFileName;
117  } else {
118  $className = $matches[1] . '\ext_update';
119  }
120  include_once $updateScript;
121  if (!class_exists($className, false)) {
122  throw new ExtensionManagerException(
123  sprintf('class.ext_update.php of extension "%s" did not declare ext_update class', $extensionKey),
124  1428176468
125  );
126  }
127 
128  return $className;
129  }
130 }
static writeFileToTypo3tempDir($filepath, $content)
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)