/** * @test * * @return void */ public function prepareSelectQueryCreateValidQuery() { $this->assertTrue($this->subject->admin_query("INSERT INTO {$this->testTable} ({$this->testField}) VALUES ('aTestValue')")); $preparedQuery = $this->subject->prepare_SELECTquery("{$this->testField},{$this->anotherTestField}", $this->testTable, 'id=:id', '', '', '', [':id' => 1]); $preparedQuery->execute(); $result = $preparedQuery->fetch(); $expectedResult = [$this->testField => 'aTestValue', $this->anotherTestField => null]; $this->assertSame($expectedResult, $result); }
/** * Create database if needed, save selected db name in configuration * * @return array<\TYPO3\CMS\Install\Status\StatusInterface> */ public function execute() { $result = array(); $this->initializeDatabaseConnection(); $postValues = $this->postValues['values']; $localConfigurationPathValuePairs = array(); /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */ $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class); if ($postValues['type'] === 'new') { $newDatabaseName = $postValues['new']; if ($this->isValidDatabaseName($newDatabaseName)) { $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8'); if ($createDatabaseResult) { $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName; } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class); $errorStatus->setTitle('Unable to create database'); $errorStatus->setMessage('Database with name ' . $newDatabaseName . ' could not be created.' . ' Either your database name contains a reserved keyword or your database' . ' user does not have sufficient permissions to create it.' . ' Please choose an existing (empty) database or contact administration.'); $result[] = $errorStatus; } } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class); $errorStatus->setTitle('Database name not valid'); $errorStatus->setMessage('Given database name must be shorter than fifty characters' . ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)' . ' and underscores (_).'); $result[] = $errorStatus; } } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) { // Only store database information when it's empty $this->databaseConnection->setDatabaseName($postValues['existing']); $this->databaseConnection->sql_select_db(); $existingTables = $this->databaseConnection->admin_get_tables(); $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress'); if (!$isInitialInstallation || empty($existingTables)) { $localConfigurationPathValuePairs['DB/database'] = $postValues['existing']; } } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class); $errorStatus->setTitle('No Database selected'); $errorStatus->setMessage('You must select a database.'); $result[] = $errorStatus; } if (!empty($localConfigurationPathValuePairs)) { $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs); } return $result; }
/** * Create database if needed, save selected db name in configuration * * @return array<\TYPO3\CMS\Install\Status\StatusInterface> */ public function execute() { $result = array(); $this->initializeDatabaseConnection(); $postValues = $this->postValues['values']; $localConfigurationPathValuePairs = array(); /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */ $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager'); if ($postValues['type'] === 'new') { $newDatabaseName = $postValues['new']; if (strlen($newDatabaseName) <= 50) { $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8'); if ($createDatabaseResult) { $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName; } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus'); $errorStatus->setTitle('Unable to create database'); $errorStatus->setMessage('Database with name ' . $newDatabaseName . ' could not be created.' . ' Either your database name contains special chars (only alphanumeric characters are allowed)' . ' or your database user probably does not have sufficient permissions to create it.' . ' Please choose an existing (empty) database or contact administration.'); $result[] = $errorStatus; } } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus'); $errorStatus->setTitle('Database name not valid'); $errorStatus->setMessage('Given database name must be shorter than fifty characters.'); $result[] = $errorStatus; } } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) { // Only store database information when it's empty $this->databaseConnection->setDatabaseName($postValues['existing']); $this->databaseConnection->sql_select_db(); $existingTables = $this->databaseConnection->admin_get_tables(); $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress'); if (!$isInitialInstallation || count($existingTables) === 0) { $localConfigurationPathValuePairs['DB/database'] = $postValues['existing']; } } else { /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */ $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus'); $errorStatus->setTitle('No Database selected'); $errorStatus->setMessage('You must select a database.'); $result[] = $errorStatus; } if (!empty($localConfigurationPathValuePairs)) { $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs); } return $result; }
/** * Performs the accordant updates. * * @param array &$dbQueries Queries done in this update * @param mixed &$customMessages Custom messages * @return boolean Whether everything went smoothly or not */ public function performUpdate(array &$dbQueries, &$customMessages) { $oldCategoryTableFields = $this->databaseConnection->admin_get_fields('tx_mooxnews_domain_model_category'); // A temporary migration column is needed in old category table. Add this when not already present if (!array_key_exists('migrate_sys_category_uid', $oldCategoryTableFields)) { $this->databaseConnection->admin_query("ALTER TABLE tx_mooxnews_domain_model_category ADD migrate_sys_category_uid int(11) DEFAULT '0' NOT NULL"); } // convert tx_mooxnews_domain_model_category records $this->migrateNewsCategoryRecords(); // set/update all relations $oldNewCategoryUidMapping = $this->getOldNewCategoryUidMapping(); $this->updateParentFieldOfMigratedCategories($oldNewCategoryUidMapping); $this->migrateCategoryMmRecords($oldNewCategoryUidMapping); $this->updateCategoryPermissionFields('be_groups', $oldNewCategoryUidMapping); $this->updateCategoryPermissionFields('be_users', $oldNewCategoryUidMapping); $this->migrateCategoryImages(); $this->markWizardAsDone(); return TRUE; }
/** * Retrieves the default character set of the database. * * @return string */ protected function getDefaultDatabaseCharset() { $result = $this->databaseConnection->admin_query('SHOW VARIABLES LIKE "character_set_database"'); $row = $this->databaseConnection->sql_fetch_assoc($result); $key = $row['Variable_name']; $value = $row['Value']; $databaseCharset = ''; if ($key == 'character_set_database') { $databaseCharset = $value; } return $databaseCharset; }
/** * Migrates old event categories to sys_categories if required * * @return void */ protected function migrateEventCategoriesToSysCategories() { // check if tx_sfeventmgt_domain_model_category still exists $oldCategoryTableFields = $this->databaseConnection->admin_get_fields('tx_sfeventmgt_domain_model_category'); if (count($oldCategoryTableFields) === 0) { $status = FlashMessage::NOTICE; $title = ''; $message = 'Old category table does not exist anymore so no update needed'; $this->messageArray[] = [$status, $title, $message]; return; } // check if there are categories present else no update is needed $oldCategoryCount = $this->databaseConnection->exec_SELECTcountRows('uid', 'tx_sfeventmgt_domain_model_category', 'deleted = 0'); if ($oldCategoryCount === 0) { $status = FlashMessage::NOTICE; $title = ''; $message = 'No categories found in old table, no update needed'; $this->messageArray[] = [$status, $title, $message]; return; } else { $status = FlashMessage::NOTICE; $title = ''; $message = 'Must migrate ' . $oldCategoryCount . ' categories.'; $this->messageArray[] = [$status, $title, $message]; } // A temporary migration column is needed in old category table. Add this when not already present if (!array_key_exists('migrate_sys_category_uid', $oldCategoryTableFields)) { $this->databaseConnection->admin_query("ALTER TABLE tx_sfeventmgt_domain_model_category ADD migrate_sys_category_uid int(11) DEFAULT '0' NOT NULL"); } // convert tx_sfeventmgt_domain_model_category records $this->migrateEventCategoryRecords(); // set/update all relations $oldNewCategoryUidMapping = $this->getOldNewCategoryUidMapping(); $this->updateParentFieldOfMigratedCategories($oldNewCategoryUidMapping); $this->migrateCategoryMmRecords($oldNewCategoryUidMapping); $this->updateFlexformCategories('sfeventmgt_pievent', $oldNewCategoryUidMapping, 'settings.category'); /** * Finished category migration */ $message = 'All categories are updated. Run <strong>DB compare</strong> in the install tool to remove the ' . 'now obsolete `tx_sfeventmgt_domain_model_category` and `tx_sfeventmgt_event_category_mm` tables and ' . 'run the <strong>DB check</strong> to update the reference index.'; $status = FlashMessage::OK; $title = 'Updated all categories!'; $this->messageArray[] = [$status, $title, $message]; }
/** * Returns TRUE if upgrade wizard for legacy EXT:eu_ldap records should be run. * * @return bool */ protected function checkEuLdap() { $table = 'tx_euldap_server'; $migrationField = 'tx_igldapssoauth_migrated'; // We check the database table itself and not whether EXT:eu_ldap is loaded // because it may have been deactivated since it is not incompatible $existingTables = $this->databaseConnection->admin_get_tables(); if (!isset($existingTables[$table])) { return FALSE; } // Ensure the column used to flag processed records is present $fields = $this->databaseConnection->admin_get_fields($table); if (!isset($fields[$migrationField])) { $alterTableQuery = 'ALTER TABLE ' . $table . ' ADD ' . $migrationField . ' tinyint(4) NOT NULL default \'0\''; // Method admin_query() will parse the query and make it compatible with DBAL, if needed $this->databaseConnection->admin_query($alterTableQuery); } $euLdapConfigurationRecords = $this->databaseConnection->exec_SELECTcountRows('*', $table, $migrationField . '=0'); return $euLdapConfigurationRecords > 0; }
/** * Updates realurl table structure. The code is copied almost 1:1 from * ExtensionManagerTables class. * * We ignore any errors because nothing can be done about those really. The * client will have to do database update anyway, so he will see all failed * queries. * * @return void */ protected function updateRealurlTableStructure() { $updateStatements = array(); // Get all necessary statements for ext_tables.sql file $rawDefinitions = file_get_contents(ExtensionManagementUtility::extPath('realurl', 'ext_tables.sql')); $sqlParser = GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService'); $fieldDefinitionsFromFile = $sqlParser->getFieldDefinitions_fileContent($rawDefinitions); if (count($fieldDefinitionsFromFile)) { $fieldDefinitionsFromCurrentDatabase = $sqlParser->getFieldDefinitions_database(); $diff = $sqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase); $updateStatements = $sqlParser->getUpdateSuggestions($diff); } foreach ((array) $updateStatements['add'] as $string) { $this->databaseConnection->admin_query($string); } foreach ((array) $updateStatements['change'] as $string) { $this->databaseConnection->admin_query($string); } foreach ((array) $updateStatements['create_table'] as $string) { $this->databaseConnection->admin_query($string); } }
/** * @test * * @return void */ public function sqlFreeResultReturnsTrueOnSuccess() { $this->assertTrue($this->subject->admin_query("INSERT INTO {$this->testTable} ({$this->testField}) VALUES ('baz')")); $res = $this->subject->admin_query("SELECT * FROM {$this->testTable} WHERE {$this->testField}='baz'"); $this->assertTrue($this->subject->sql_free_result($res)); }