[BUGFIX] SQL error in install tool if no root page available
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / TceformsUpdateWizard.php
index aeaba61..38990af 100644 (file)
@@ -4,7 +4,7 @@ namespace TYPO3\CMS\Install\Updates;
 /***************************************************************
  *  Copyright notice
  *
 /***************************************************************
  *  Copyright notice
  *
- *  (c) 2011 Ingmar Schlecht <ingmar@typo3.org>
+ *  (c) 2011-2013 Ingmar Schlecht <ingmar@typo3.org>
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -23,15 +23,16 @@ namespace TYPO3\CMS\Install\Updates;
  *
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
  *
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Upgrade wizard which goes through all files referenced in the tt_content.image filed
  * and creates sys_file records as well as sys_file_reference records for the individual usages.
  *
 /**
  * Upgrade wizard which goes through all files referenced in the tt_content.image filed
  * and creates sys_file records as well as sys_file_reference records for the individual usages.
  *
- * @package TYPO3
  * @author Ingmar Schlecht <ingmar@typo3.org>
  * @author Ingmar Schlecht <ingmar@typo3.org>
- * @license http://www.gnu.org/copyleft/gpl.html
  */
  */
-class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
+class TceformsUpdateWizard extends AbstractUpdate {
 
        /**
         * @var string
 
        /**
         * @var string
@@ -43,6 +44,20 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
         */
        protected $storage;
 
         */
        protected $storage;
 
+       /**
+        * @var \TYPO3\CMS\Core\Log\Logger
+        */
+       protected $logger;
+
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
+               $logManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager');
+               $this->logger = $logManager->getLogger(__CLASS__);
+       }
+
        /**
         * Initialize the storage repository.
         */
        /**
         * Initialize the storage repository.
         */
@@ -56,25 +71,24 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
        /**
         * Checks if an update is needed
         *
        /**
         * Checks if an update is needed
         *
-        * @param       string          &$description: The description for the update
-        * @return      boolean         TRUE if an update is needed, FALSE otherwise
+        * @param string &$description The description for the update
+        * @return boolean TRUE if an update is needed, FALSE otherwise
         */
        public function checkForUpdate(&$description) {
                $description = 'This update wizard goes through all files that are referenced in the tt_content.image and pages.media / pages_language_overlay.media filed and adds the files to the new File Index.<br />It also moves the files from uploads/ to the fileadmin/_migrated/ path.<br /><br />This update wizard can be called multiple times in case it didn\'t finish after running once.';
         */
        public function checkForUpdate(&$description) {
                $description = 'This update wizard goes through all files that are referenced in the tt_content.image and pages.media / pages_language_overlay.media filed and adds the files to the new File Index.<br />It also moves the files from uploads/ to the fileadmin/_migrated/ path.<br /><br />This update wizard can be called multiple times in case it didn\'t finish after running once.';
-               // make this wizard always available
+               // Make this wizard always available
                return TRUE;
        }
 
        /**
         * Performs the database update.
         *
                return TRUE;
        }
 
        /**
         * Performs the database update.
         *
-        * @param       array           &$dbQueries: queries done in this update
-        * @param       mixed           &$customMessages: custom messages
-        * @return      boolean         TRUE on success, FALSE on error
+        * @param array &$dbQueries Queries done in this update
+        * @param mixed &$customMessages Custom messages
+        * @return boolean TRUE on success, FALSE on error
         */
         */
-       public function performUpdate(&$dbQueries, &$customMessages) {
+       public function performUpdate(array &$dbQueries, &$customMessages) {
                $this->init();
                $this->init();
-               // Function below copied from sysext/install/updates/class.tx_coreupdates_imagelink.php
                $tables = array(
                        'tt_content' => array(
                                'image' => array(
                $tables = array(
                        'tt_content' => array(
                                'image' => array(
@@ -111,19 +125,18 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
                }
                $result = TRUE;
                if ($this->versionNumber >= 6000000) {
                }
                $result = TRUE;
                if ($this->versionNumber >= 6000000) {
-                       /** TODO
-
-                       - for each table:
-                       - get records from table
-                       - for each record:
-                       - for each field:
-                       - migrate field */
+                       // @todo
+                       // - for each table:
+                       // - get records from table
+                       // - for each record:
+                       // - for each field:
+                       // - migrate field
                        foreach ($tables as $table => $tableConfiguration) {
                                $fieldsToMigrate = array_keys($tableConfiguration);
                                $fieldsToGet = array();
                                // find all additional fields we should get from the database
                                foreach ($tableConfiguration as $field => $fieldConfiguration) {
                        foreach ($tables as $table => $tableConfiguration) {
                                $fieldsToMigrate = array_keys($tableConfiguration);
                                $fieldsToGet = array();
                                // find all additional fields we should get from the database
                                foreach ($tableConfiguration as $field => $fieldConfiguration) {
-                                       $fieldKey = ($table . ':') . $field;
+                                       $fieldKey = $table . ':' . $field;
                                        if (array_search($fieldKey, $finishedFields) !== FALSE) {
                                                // this field was already migrated
                                                continue;
                                        if (array_search($fieldKey, $finishedFields) !== FALSE) {
                                                // this field was already migrated
                                                continue;
@@ -147,7 +160,7 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
                                $records = $this->getRecordsFromTable($table, $fieldsToGet);
                                foreach ($records as $record) {
                                        foreach ($fieldsToMigrate as $field) {
                                $records = $this->getRecordsFromTable($table, $fieldsToGet);
                                foreach ($records as $record) {
                                        foreach ($fieldsToMigrate as $field) {
-                                               $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field]));
+                                               $dbQueries = array_merge($this->migrateField($table, $record, $field, $tableConfiguration[$field], $customMessages));
                                        }
                                }
                        }
                                        }
                                }
                        }
@@ -157,13 +170,31 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
                return $result;
        }
 
                return $result;
        }
 
+       /**
+        * Get records from table
+        *
+        * @param string $table
+        * @param array $relationFields
+        * @return array
+        */
        protected function getRecordsFromTable($table, $relationFields) {
                $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
                $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
                return $records;
        }
 
        protected function getRecordsFromTable($table, $relationFields) {
                $fields = implode(',', array_merge($relationFields, array('uid', 'pid')));
                $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, '');
                return $records;
        }
 
-       protected function migrateField($table, $row, $fieldname, $fieldConfiguration) {
+       /**
+        * Migrates a single field.
+        *
+        * @param string $table
+        * @param array $row
+        * @param string $fieldname
+        * @param array $fieldConfiguration
+        * @param string $customMessages
+        * @return array A list of performed database queries
+        * @throws \Exception
+        */
+       protected function migrateField($table, $row, $fieldname, $fieldConfiguration, &$customMessages) {
                $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
                if (empty($fieldItems) || is_numeric($row[$fieldname])) {
                        return array();
                $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
                if (empty($fieldItems) || is_numeric($row[$fieldname])) {
                        return array();
@@ -191,9 +222,26 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
                        if (!PATH_site) {
                                throw new \Exception('PATH_site was undefined.');
                        }
                        if (!PATH_site) {
                                throw new \Exception('PATH_site was undefined.');
                        }
-                       // copy file
-                       $sourcePath = (PATH_site . $fieldConfiguration['sourcePath']) . $item;
-                       $targetPath = ((PATH_site . $fileadminDirectory) . $fieldConfiguration['targetPath']) . $item;
+
+                       $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
+                       $targetPath = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'] . $item;
+
+                       // if the source file does not exist, we should just continue, but leave a message in the docs;
+                       // ideally, the user would be informed after the update as well.
+                       if (!file_exists($sourcePath)) {
+                               $this->logger->notice('File ' . $fieldConfiguration['sourcePath'] . $item . ' does not exist. Reference was not migrated.', array('table' => $table, 'record' => $row, 'field' => $fieldname));
+
+                               $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
+                               $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Messaging\FlashMessage',
+                                       sprintf($format, $fieldConfiguration['sourcePath'] . $item, $table, $row['uid'], $fieldname),
+                                       '', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
+                               );
+                               /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $message */
+                               $customMessages .= '<br />' . $message->render();
+
+                               continue;
+                       }
+
                        if (!is_dir(dirname($targetPath))) {
                                \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
                        }
                        if (!is_dir(dirname($targetPath))) {
                                \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
                        }
@@ -205,6 +253,9 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
                                        // TODO add sorting/sorting_foreign
                                        'fieldname' => $fieldname,
                                        'table_local' => 'sys_file',
                                        // TODO add sorting/sorting_foreign
                                        'fieldname' => $fieldname,
                                        'table_local' => 'sys_file',
+                                       // the sys_file_reference record should always placed on the same page
+                                       // as the record to link to, see issue #46497
+                                       'pid' => ($table === 'pages' ? $row['uid'] : $row['pid']),
                                        'uid_foreign' => $row['uid'],
                                        'uid_local' => $file->getUid(),
                                        'tablenames' => $table,
                                        'uid_foreign' => $row['uid'],
                                        'uid_local' => $file->getUid(),
                                        'tablenames' => $table,
@@ -236,5 +287,4 @@ class TceformsUpdateWizard extends \TYPO3\CMS\Install\Updates\AbstractUpdate {
 
 }
 
 
 }
 
-
 ?>
\ No newline at end of file
 ?>
\ No newline at end of file