[TASK] Prevent database error for upgrade wizard
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / LanguageIsoCodeUpdate.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 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
17
18 /**
19 * Update sys_language records to use the newly created
20 * field language_isocode, if they have used the now deprecated
21 * static_lang_isocode
22 */
23 class LanguageIsoCodeUpdate extends AbstractUpdate {
24
25 /**
26 * @var string
27 */
28 protected $title = 'Update sys_language records to use new ISO 639-1 letter-code field';
29
30 /**
31 * Checks if an update is needed
32 *
33 * @param string &$description The description for the update
34 * @return bool Whether an update is needed (TRUE) or not (FALSE)
35 */
36 public function checkForUpdate(&$description) {
37 if ($this->isWizardDone() || !ExtensionManagementUtility::isLoaded('static_info_tables')) {
38 return FALSE;
39 }
40
41 $emptyValue = $this->getDatabaseConnection()->fullQuoteStr('', 'sys_language');
42 $migratableLanguageRecords = $this->getDatabaseConnection()->exec_SELECTcountRows('uid', 'sys_language', 'language_isocode=' . $emptyValue . ' AND CAST(static_lang_isocode AS CHAR) != ' . $emptyValue);
43 if ($migratableLanguageRecords === 0) {
44 return FALSE;
45 }
46
47 $description = 'The sys_language records have a new iso code field which removes the dependency of the TYPO3 CMS Core to the extension "static_info_tables". This upgrade wizard migrates the data of the existing "static_lang_isocode" field to the new DB field.';
48
49 return TRUE;
50 }
51
52 /**
53 * Performs the database update if the old field "static_lang_isocode"
54 * is in use and populates the new field "language_isocode" with the
55 * data of the old relation.
56 *
57 * @param array &$databaseQueries Queries done in this update
58 * @param mixed &$customMessages Custom messages
59 * @return bool
60 */
61 public function performUpdate(array &$databaseQueries, &$customMessages) {
62 $emptyValue = $this->getDatabaseConnection()->fullQuoteStr('', 'sys_language');
63 $migrateableLanguageRecords = $this->getDatabaseConnection()->exec_SELECTgetRows('uid,static_lang_isocode', 'sys_language', 'language_isocode=' . $emptyValue . ' AND CAST(static_lang_isocode AS CHAR) != ' . $emptyValue);
64 if (!empty($migrateableLanguageRecords)) {
65 foreach ($migrateableLanguageRecords as $languageRecord) {
66 $staticLanguageRecord = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'static_languages', 'uid=' . (int)$languageRecord['static_lang_isocode']);
67 if (!empty($staticLanguageRecord['lg_iso_2'])) {
68 $this->getDatabaseConnection()->exec_UPDATEquery(
69 'sys_language',
70 'uid=' . (int)$languageRecord['uid'],
71 array(
72 'language_isocode' => strtolower($staticLanguageRecord['lg_iso_2'])
73 )
74 );
75 $databaseQueries[] = $this->getDatabaseConnection()->debug_lastBuiltQuery;
76 }
77 }
78 }
79
80 $this->markWizardAsDone();
81 return TRUE;
82 }
83
84 }