/** * Handle issue * * @param Tx_Smoothmigration_Domain_Model_Issue $issue * @return void */ protected function handleIssue(Tx_Smoothmigration_Domain_Model_Issue $issue) { if (is_string($issue->getLocationInfo())) { $locationInfo = unserialize($issue->getLocationInfo()); } else { $locationInfo = $issue->getLocationInfo(); } $this->messageService->message($locationInfo->getFilePath() . ':' . $locationInfo->getLineNumber() . ' [' . trim($locationInfo->getMatchedString()) . '] => '); if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage('already migrated', TRUE); return; } $newFileContent = ''; if (!file_exists($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_FOUND); $this->messageService->errorMessage('Error, file not found', TRUE); return; } if (!is_writable($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_WRITABLE); $this->messageService->errorMessage('Error, file not writable', TRUE); return; } $fileObject = new SplFileObject($locationInfo->getFilePath()); // If more than one line needs to be removed from the same file, only the // first would succeed as the line numbering will change after removing // the first line. Therefore we need to keep track of the number of lines // removed per file. We use this offset to read the file. $lineOffset = $this->locationCounters[$locationInfo->getFilePath()]; foreach ($fileObject as $lineNumber => $lineContent) { if ($lineNumber + 1 + $lineOffset != $locationInfo->getLineNumber()) { $newFileContent .= $lineContent; } else { $newLineContent = str_replace($locationInfo->getMatchedString(), LF, $lineContent); if ($newLineContent == $lineContent) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_CHANGED); $this->messageService->errorMessage($this->ll('migrationsstatus.4'), TRUE); return; } $newFileContent .= $newLineContent; } } file_put_contents($locationInfo->getFilePath(), $newFileContent); $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage('Succes', TRUE); if (!isset($this->locationCounters[$locationInfo->getFilePath()])) { $this->locationCounters[$locationInfo->getFilePath()] = 1; } else { $this->locationCounters[$locationInfo->getFilePath()]++; } }
/** * Handle issue * * @param Tx_Smoothmigration_Domain_Model_Issue $issue * @return void */ protected function handleIssue(Tx_Smoothmigration_Domain_Model_Issue $issue) { if (is_string($issue->getLocationInfo())) { $locationInfo = unserialize($issue->getLocationInfo()); } else { $locationInfo = $issue->getLocationInfo(); } if (is_string($issue->getAdditionalInformation())) { $additionalInformation = unserialize($issue->getAdditionalInformation()); } else { $additionalInformation = $issue->getAdditionalInformation(); } $allowedCollations = array('utf8_general_ci', 'utf8_unicode_ci', 'utf8_general_cs', 'utf8_unicode_cs', 'utf8_bin'); switch ($additionalInformation['type']) { case 'configuration': $this->messageService->message($this->ll('result.typo3-database-database-utf8.databaseServerSetting', array($additionalInformation['setting'], $additionalInformation['actualValue'], $additionalInformation['preferredValue']))); $this->messageService->warningMessage($this->ll('migration.manualInterventionNeeded'), TRUE); $this->messageService->message(); break; case 'databaseCollation': $this->messageService->message($this->ll('result.typo3-database-database-utf8.databaseCollation', array($additionalInformation['characterSet'], $additionalInformation['defaultCollation']))); $collation = 'utf8_general_ci'; if (in_array($additionalInformation['defaultCollation'], $allowedCollations)) { $collation = $additionalInformation['defaultCollation']; } if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage($this->ll('migration.alreadyMigrated'), TRUE); return; } $query = 'ALTER DATABASE CHARACTER SET utf8 COLLATE ' . $collation . ';'; $this->messageService->message($this->ll('migration.executingQuery', array($query))); $GLOBALS['TYPO3_DB']->sql_query($query); if ($sqlError = $GLOBALS['TYPO3_DB']->sql_error()) { $this->messageService->errorMessage($sqlError . LF, TRUE); } $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage($this->ll('migrationsstatus.1') . LF, TRUE); break; case 'tableCollation': $this->messageService->message($this->ll('result.typo3-database-database-utf8.databaseTableCollation', array($additionalInformation['tableName'], $additionalInformation['tableCollation'], 'utf8_general_ci'))); if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage($this->ll('migration.alreadyMigrated'), TRUE); return; } $query = 'ALTER TABLE `' . $additionalInformation['tableName'] . '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'; $this->messageService->message($this->ll('migration.executingQuery', array($query))); $GLOBALS['TYPO3_DB']->sql_query($query); if ($sqlError = $GLOBALS['TYPO3_DB']->sql_error()) { $this->messageService->errorMessage($sqlError . LF, TRUE); } else { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage($this->ll('migrationsstatus.1') . LF, TRUE); } break; case 'columnCollation': $this->messageService->message($this->ll('result.typo3-database-database-utf8.databaseColumnCollation', array($additionalInformation['tableName'], $additionalInformation['columnName'], $additionalInformation['characterSetName'], $additionalInformation['collationName']))); if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage($this->ll('migration.alreadyMigrated'), TRUE); return; } $query = 'ALTER TABLE `' . $additionalInformation['tableName'] . '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'; $this->messageService->message($this->ll('migration.executingQuery', array($query))); $GLOBALS['TYPO3_DB']->sql_query($query); if ($sqlError = $GLOBALS['TYPO3_DB']->sql_error()) { $this->messageService->errorMessage($sqlError . LF, TRUE); } else { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage($this->ll('migrationsstatus.1') . LF, TRUE); } break; default: } }
/** * Perform the actual replacement * * @param Tx_Smoothmigration_Domain_Model_Issue $issue * @param object $locationInfo * * @return boolean */ protected function performReplacement(Tx_Smoothmigration_Domain_Model_Issue $issue, $locationInfo) { $search = trim($locationInfo->getMatchedString()); $replacement = trim($search, ')') . ', \'' . $issue->getExtension() . '\')'; $this->messageService->message($locationInfo->getFilePath() . ' line: ' . $locationInfo->getLineNumber() . LF . 'Replacing [' . $search . '] =>' . ' [' . $replacement . ']'); if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage('already migrated', TRUE); return; } if (!file_exists($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_FOUND); $this->messageService->errorMessage('Error, file not found', TRUE); return; } if (!is_writable($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_WRITABLE); $this->messageService->errorMessage('Error, file not writable', TRUE); return; } $fileObject = new SplFileObject($locationInfo->getFilePath()); $newFileContent = ''; foreach ($fileObject as $lineNumber => $lineContent) { if ($lineNumber + 1 != $locationInfo->getLineNumber()) { $newFileContent .= $lineContent; } else { $newLineContent = str_replace($search, $replacement, $lineContent); if (!strstr($newLineContent, $replacement)) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_CHANGED); $this->messageService->errorMessage($this->ll('migrationsstatus.4'), TRUE); return; } $newFileContent .= $newLineContent; } } file_put_contents($locationInfo->getFilePath(), $newFileContent); $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage('Succes' . LF, TRUE); }
/** * * @param Tx_Smoothmigration_Domain_Model_Issue $issue * @param object $locationInfo * @param array $additionalInformation * @return void */ protected function performReplacement(Tx_Smoothmigration_Domain_Model_Issue $issue, $locationInfo, $additionalInformation) { $concatenator = '::'; if ($additionalInformation['replacementClass'] == '$GLOBALS[\'TYPO3_DB\']') { $concatenator = '->'; } // Some replacements are plain PHP functions if ($additionalInformation['replacementClass'] == '') { $concatenator = ''; } $this->messageService->message($locationInfo->getFilePath() . ' line: ' . $locationInfo->getLineNumber() . LF . 'Replacing [' . trim($locationInfo->getMatchedString()) . '] =>' . ' [' . $additionalInformation['replacementClass'] . $concatenator . $additionalInformation['replacementMethod'] . '(]'); if ($issue->getMigrationStatus() != 0) { $this->messageService->successMessage('already migrated', TRUE); return; } if (!file_exists($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_FOUND); $this->messageService->errorMessage('Error, file not found', TRUE); return; } if (!is_writable($locationInfo->getFilePath())) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_WRITABLE); $this->messageService->errorMessage('Error, file not writable', TRUE); return; } $fileObject = new SplFileObject($locationInfo->getFilePath()); $isRegexReplace = !empty($additionalInformation['regexSearch']) && !empty($additionalInformation['regexReplace']); if ($isRegexReplace) { // When we have a semi-colon, we should assume its a new line $linesToReplace = count(explode(';', $additionalInformation['regexSearch'])); } else { $linesToReplace = 1; } $contentBefore = ''; $contentToProcess = ''; $contentAfter = ''; foreach ($fileObject as $lineNumber => $lineContent) { if ($lineNumber + 1 < $locationInfo->getLineNumber()) { $contentBefore .= $lineContent; } elseif ($lineNumber + 1 < $locationInfo->getLineNumber() + $linesToReplace) { $contentToProcess .= $lineContent; } else { $contentAfter .= $lineContent; } } if ($isRegexReplace) { $newContent = preg_replace($additionalInformation['regexSearch'], $additionalInformation['regexReplace'], $contentToProcess); } else { $replacement = $additionalInformation['replacementClass'] . $concatenator . $additionalInformation['replacementMethod'] . '('; $newContent = str_replace($locationInfo->getMatchedString(), $replacement, $contentToProcess); } if ($newContent == $contentToProcess) { $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::ERROR_FILE_NOT_CHANGED); $this->messageService->errorMessage($this->ll('migrationsstatus.4'), TRUE); return; } file_put_contents($locationInfo->getFilePath(), $contentBefore . $newContent . $contentAfter); $issue->setMigrationStatus(Tx_Smoothmigration_Domain_Interface_Migration::SUCCESS); $this->messageService->successMessage('Succes' . LF, TRUE); }