TYPO3 CMS  TYPO3_6-2
UpdateScriptUtility.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
33  public function executeUpdateIfNeeded($extensionKey) {
34  $className = $this->requireUpdateScript($extensionKey);
35  $scriptObject = GeneralUtility::makeInstance($className);
36 
37  // old em always assumed the method exist, we do so too.
38  // @TODO: Make this smart, let scripts implement interfaces
39  // @TODO: With current ext_update construct it is impossible to enforce some type of return
40  return $scriptObject->access() ? $scriptObject->main() : NULL;
41  }
42 
52  protected function requireUpdateScript($extensionKey) {
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  $className = $this->determineUpdateClassName($extensionKey);
80  if ($className !== '') {
81  $updater = GeneralUtility::makeInstance($className);
82  return $updater->access();
83  }
84  return FALSE;
85  }
86 
94  protected function determineUpdateClassName($extensionKey) {
95  $updateScript = GeneralUtility::getFileAbsFileName(
96  'EXT:' . $extensionKey . '/class.ext_update.php',
97  FALSE
98  );
99  if (!file_exists($updateScript)) {
100  return '';
101  }
102 
103  // get script contents
104  $scriptSourceCode = GeneralUtility::getUrl($updateScript);
105  // check if it has a namespace
106  if (!preg_match('/<\?php.*namespace\s+([^;]+);.*class/is', $scriptSourceCode, $matches)) {
107  // if no, rename the class with a unique name
108  $className = 'ext_update' . md5($extensionKey . $scriptSourceCode);
109  $temporaryFileName = PATH_site . 'typo3temp/ExtensionManager/UpdateScripts/' . $className . '.php';
110  if (!file_exists(GeneralUtility::getFileAbsFileName($temporaryFileName))) {
111  $scriptSourceCode = preg_replace('/^\s*class\s+ext_update\s+/m', 'class ' . $className . ' ', $scriptSourceCode);
112  GeneralUtility::writeFileToTypo3tempDir($temporaryFileName, $scriptSourceCode);
113  }
114  $updateScript = $temporaryFileName;
115  } else {
116  $className = $matches[1] . '\ext_update';
117  }
118  include_once $updateScript;
119  if (!class_exists($className, FALSE)) {
120  throw new ExtensionManagerException(
121  sprintf('class.ext_update.php of extension "%s" did not declare ext_update class', $extensionKey),
122  1428176468
123  );
124  }
125 
126  return $className;
127  }
128 }
static writeFileToTypo3tempDir($filepath, $content)
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)