[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / ExtensionManagerTables.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
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
17 /**
18 * Contains the update class for filling the basic repository record of the extension manager
19 *
20 * @author Georg Ringer <typo3@ringerge.org>
21 */
22 class ExtensionManagerTables extends AbstractUpdate {
23
24 /**
25 * @var string
26 */
27 protected $title = 'Add the default Extension Manager database tables';
28
29 /**
30 * @var NULL|\TYPO3\CMS\Install\Service\SqlSchemaMigrationService
31 */
32 protected $installToolSqlParser = NULL;
33
34 /**
35 * @return \TYPO3\CMS\Install\Service\SqlSchemaMigrationService
36 */
37 protected function getInstallToolSqlParser() {
38 if ($this->installToolSqlParser === NULL) {
39 $this->installToolSqlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
40 }
41
42 return $this->installToolSqlParser;
43 }
44
45 /**
46 * Gets all create, add and change queries from ext_tables.sql
47 *
48 * @return array
49 */
50 protected function getUpdateStatements() {
51 $updateStatements = array();
52
53 // Get all necessary statements for ext_tables.sql file
54 $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extensionmanager') . '/ext_tables.sql');
55 $fieldDefinitionsFromFile = $this->getInstallToolSqlParser()->getFieldDefinitions_fileContent($rawDefinitions);
56 if (count($fieldDefinitionsFromFile)) {
57 $fieldDefinitionsFromCurrentDatabase = $this->getInstallToolSqlParser()->getFieldDefinitions_database();
58 $diff = $this->getInstallToolSqlParser()->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
59 $updateStatements = $this->getInstallToolSqlParser()->getUpdateSuggestions($diff);
60 }
61
62 return $updateStatements;
63 }
64
65 /**
66 * Checks if an update is needed
67 *
68 * @param string &$description The description for the update
69 * @return boolean Whether an update is needed (TRUE) or not (FALSE)
70 */
71 public function checkForUpdate(&$description) {
72 $result = FALSE;
73 $description = 'Creates necessary database tables and adds static tables for the new Extension Manager.';
74
75 // First check necessary database update
76 $updateStatements = $this->getUpdateStatements();
77 if (empty($updateStatements)) {
78 // Check for repository database table
79 $databaseTables = $GLOBALS['TYPO3_DB']->admin_get_tables();
80 if (!isset($databaseTables['tx_extensionmanager_domain_model_repository'])) {
81 $result = TRUE;
82 } else {
83 // Get count of rows in repository database table
84 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', 'tx_extensionmanager_domain_model_repository');
85 if ($count === 0) {
86 $result = TRUE;
87 }
88 }
89 } else {
90 $result = TRUE;
91 }
92
93 return $result;
94 }
95
96 /**
97 * @param mixed &$customMessages Custom messages
98 *
99 * @return boolean
100 */
101 protected function hasError(&$customMessages) {
102 $result = FALSE;
103 if ($GLOBALS['TYPO3_DB']->sql_error()) {
104 $customMessages .= '<br /><br />SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
105 $result = TRUE;
106 }
107
108 return $result;
109 }
110
111 /**
112 * Performs the database update.
113 *
114 * @param array &$dbQueries Queries done in this update
115 * @param mixed &$customMessages Custom messages
116 * @return boolean Whether it worked (TRUE) or not (FALSE)
117 */
118 public function performUpdate(array &$dbQueries, &$customMessages) {
119 $result = FALSE;
120
121 // First perform all create, add and change queries
122 $updateStatements = $this->getUpdateStatements();
123 foreach ((array) $updateStatements['add'] as $string) {
124 $GLOBALS['TYPO3_DB']->admin_query($string);
125 $dbQueries[] = $string;
126 $result = ($result || $this->hasError($customMessages));
127 }
128 foreach ((array) $updateStatements['change'] as $string) {
129 $GLOBALS['TYPO3_DB']->admin_query($string);
130 $dbQueries[] = $string;
131 $result = ($result || $this->hasError($customMessages));
132 }
133 foreach ((array) $updateStatements['create_table'] as $string) {
134 $GLOBALS['TYPO3_DB']->admin_query($string);
135 $dbQueries[] = $string;
136 $result = ($result || $this->hasError($customMessages));
137 }
138
139 // Perform statis import anyway
140 $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extensionmanager') . 'ext_tables_static+adt.sql');
141 $statements = $this->getInstallToolSqlParser()->getStatementarray($rawDefinitions, 1);
142 foreach ($statements as $statement) {
143 if (trim($statement) !== '') {
144 $GLOBALS['TYPO3_DB']->admin_query($statement);
145 $dbQueries[] = $statement;
146 $result = ($result || $this->hasError($customMessages));
147 }
148 }
149
150 return !$result;
151 }
152
153 }