/**
  * Migrate dam references to fal references
  *
  * @param \mysqli_result $result
  * @param string $table
  * @param string $type
  * @param array $fieldnameMapping Re-map fieldnames e.g.
  *    tx_damnews_dam_images => tx_falttnews_fal_images
  *
  * @return void
  */
 protected function migrateDamReferencesToFalReferences($result, $table, $type, $fieldnameMapping = array())
 {
     $counter = 0;
     $total = $this->database->sql_num_rows($result);
     $this->controller->infoMessage('Found ' . $total . ' ' . $table . ' records with a dam ' . $type);
     while ($record = $this->database->sql_fetch_assoc($result)) {
         $identifier = $this->getFullFileName($record);
         try {
             $fileObject = $this->storageObject->getFile($identifier);
         } catch (\Exception $e) {
             // If file is not found
             // getFile will throw an invalidArgumentException if the file
             // does not exist. Create an empty file to avoid this. This is
             // usefull in a development environment that has the production
             // database but not all the physical files.
             try {
                 GeneralUtility::mkdir_deep(PATH_site . $this->storageBasePath . dirname($identifier));
             } catch (\Exception $e) {
                 $this->controller->errorMessage('Unable to create directory: ' . PATH_site . $this->storageBasePath . $identifier);
                 continue;
             }
             $config = $this->controller->getConfiguration();
             if (isset($config['createMissingFiles']) && (int) $config['createMissingFiles']) {
                 $this->controller->infoMessage('Creating empty missing file: ' . PATH_site . $this->storageBasePath . $identifier);
                 try {
                     GeneralUtility::writeFile(PATH_site . $this->storageBasePath . $identifier, '');
                 } catch (\Exception $e) {
                     $this->controller->errorMessage('Unable to create file: ' . PATH_site . $this->storageBasePath . $identifier);
                     continue;
                 }
             } else {
                 $this->controller->errorMessage('File not found: ' . PATH_site . $this->storageBasePath . $identifier);
                 continue;
             }
             $fileObject = $this->storageObject->getFile($identifier);
         }
         if ($fileObject instanceof \TYPO3\CMS\Core\Resource\File) {
             if ($fileObject->isMissing()) {
                 $this->controller->warningMessage('FAL did not find any file resource for DAM record. DAM uid: ' . $record['uid'] . ': "' . $identifier . '"');
                 continue;
             }
             $record['uid_local'] = $fileObject->getUid();
             foreach ($fieldnameMapping as $old => $new) {
                 if ($record['ident'] === $old) {
                     $record['ident'] = $new;
                 }
             }
             $progress = number_format(100 * ($counter++ / $total), 1) . '% of ' . $total;
             if (!$this->doesFileReferenceExist($record)) {
                 $insertData = array('tstamp' => time(), 'crdate' => time(), 'cruser_id' => $GLOBALS['BE_USER']->user['uid'], 'uid_local' => $record['uid_local'], 'uid_foreign' => (int) $record['uid_foreign'], 'sorting' => (int) $record['sorting'], 'sorting_foreign' => (int) $record['sorting_foreign'], 'tablenames' => (string) $record['tablenames'], 'fieldname' => (string) $record['ident'], 'table_local' => 'sys_file', 'pid' => $record['item_pid'], 'l10n_diffsource' => (string) $record['l18n_diffsource']);
                 $this->database->exec_INSERTquery('sys_file_reference', $insertData);
                 $this->amountOfMigratedRecords++;
                 $this->controller->message($progress . ' Migrating relation for ' . (string) $record['tablenames'] . ' uid: ' . $record['item_uid'] . ' dam uid: ' . $record['dam_uid'] . ' to fal uid: ' . $record['uid_local']);
             } else {
                 $this->controller->message($progress . ' Reference already exists for uid: ' . (int) $record['item_uid']);
             }
         }
     }
     $this->database->sql_free_result($result);
 }
 /**
  * Processes the actual transformation from CSV to sys_file_references
  *
  * @param array $record
  * @param string $field
  * @return void
  */
 protected function migrateRecord(array $record, $field)
 {
     if ($field === 'fal_related_files') {
         $file = $record['file'];
     } else {
         $file = $record['image'];
     }
     if (!empty($file) && file_exists(PATH_site . 'uploads/tx_news/' . $file)) {
         GeneralUtility::upload_copy_move(PATH_site . 'uploads/tx_news/' . $file, $this->targetDirectory . $file);
         $fileObject = $this->storage->getFile(self::FOLDER_ContentUploads . '/' . $file);
         $this->fileRepository->add($fileObject);
         $dataArray = ['uid_local' => $fileObject->getUid(), 'tablenames' => 'tx_news_domain_model_news', 'fieldname' => $field, 'uid_foreign' => $record['newsUid'], 'table_local' => 'sys_file', 'cruser_id' => 999, 'pid' => $record['newsPid'], 'sorting_foreign' => $record['sorting'], 'title' => $record['title'], 'hidden' => $record['hidden']];
         if ($field === 'fal_media') {
             $description = [];
             if (!empty($record['caption'])) {
                 $description[] = $record['caption'];
             }
             if (!empty($record['description'])) {
                 $description[] = $record['description'];
             }
             $additionalData = ['description' => implode(LF . LF, $description), 'alternative' => $record['alt'], 'showinpreview' => $record['showinpreview']];
         } else {
             $additionalData = ['description' => $record['description']];
         }
         $dataArray += $additionalData;
         $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $dataArray);
     }
 }
    /**
     * Processes the actual transformation from CSV to sys_file_references
     *
     * @param array $record
     * @return void
     */
    protected function migrateRecord(array $record)
    {
        $collections = array();
        if (trim($record['select_key'])) {
            $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_collection', array('pid' => $record['pid'], 'title' => $record['select_key'], 'storage' => $this->storage->getUid(), 'folder' => ltrim('fileadmin/', $record['select_key'])));
            $collections[] = $GLOBALS['TYPO3_DB']->sql_insert_id();
        }
        $files = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $record['media'], TRUE);
        $descriptions = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('
', $record['imagecaption']);
        $titleText = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('
', $record['titleText']);
        $i = 0;
        foreach ($files as $file) {
            if (file_exists(PATH_site . 'uploads/media/' . $file)) {
                \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . 'uploads/media/' . $file, $this->targetDirectory . $file);
                $fileObject = $this->storage->getFile(self::FOLDER_ContentUploads . '/' . $file);
                $this->fileRepository->addToIndex($fileObject);
                $dataArray = array('uid_local' => $fileObject->getUid(), 'tablenames' => 'tt_content', 'uid_foreign' => $record['uid'], 'fieldname' => 'media', 'sorting_foreign' => $i);
                if (isset($descriptions[$i])) {
                    $dataArray['description'] = $descriptions[$i];
                }
                if (isset($titleText[$i])) {
                    $dataArray['alternative'] = $titleText[$i];
                }
                $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $dataArray);
                unlink(PATH_site . 'uploads/media/' . $file);
            }
            $i++;
        }
        $this->cleanRecord($record, $i, $collections);
    }
 /**
  * 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
  */
 public function performUpdate(array &$dbQueries, &$customMessages)
 {
     $this->init();
     if (!PATH_site) {
         throw new \Exception('PATH_site was undefined.');
     }
     $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/');
     $targetDirectory = '/_migrated/RTE/';
     $fullTargetDirectory = PATH_site . $fileadminDirectory . $targetDirectory;
     // Create the directory, if necessary
     if (!is_dir($fullTargetDirectory)) {
         \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($fullTargetDirectory);
     }
     $oldRecords = $this->findMagicImagesInOldLocation();
     foreach ($oldRecords as $refRecord) {
         // Is usually uploads/RTE_magicC_123423324.png.png
         $sourceFileName = $refRecord['ref_string'];
         // Absolute path/filename
         $fullSourceFileName = PATH_site . $refRecord['ref_string'];
         $targetFileName = $targetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']);
         // Full directory
         $fullTargetFileName = $fullTargetDirectory . \TYPO3\CMS\Core\Utility\PathUtility::basename($refRecord['ref_string']);
         // maybe the file has been moved previously
         if (!file_exists($fullTargetFileName)) {
             // 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(PATH_site . $sourceFileName)) {
                 $this->logger->notice('File ' . $sourceFileName . ' does not exist. Reference was not migrated.', array());
                 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
                 $message = sprintf($format, $sourceFileName, $refRecord['tablename'], $refRecord['recuid'], $refRecord['field']);
                 $customMessages .= PHP_EOL . $message;
                 continue;
             }
             rename($fullSourceFileName, $fullTargetFileName);
         }
         // Get the File object
         $file = $this->storage->getFile($targetFileName);
         if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
             // And now update the referencing field
             $targetFieldName = $refRecord['field'];
             $targetRecord = $this->db->exec_SELECTgetSingleRow('uid, ' . $targetFieldName, $refRecord['tablename'], 'uid=' . (int) $refRecord['recuid']);
             if ($targetRecord) {
                 // Replace the old filename with the new one, and add data-* attributes used by the RTE
                 $searchString = 'src="' . $sourceFileName . '"';
                 $replacementString = 'src="' . $fileadminDirectory . $targetFileName . '"';
                 $replacementString .= ' data-htmlarea-file-uid="' . $file->getUid() . '"';
                 $replacementString .= ' data-htmlarea-file-table="sys_file"';
                 $targetRecord[$targetFieldName] = str_replace($searchString, $replacementString, $targetRecord[$targetFieldName]);
                 // Update the record
                 $this->db->exec_UPDATEquery($refRecord['tablename'], 'uid=' . (int) $refRecord['recuid'], array($targetFieldName => $targetRecord[$targetFieldName]));
                 $queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery);
                 // Finally, update the sys_refindex table as well
                 $this->db->exec_UPDATEquery('sys_refindex', 'hash=' . $this->db->fullQuoteStr($refRecord['hash'], 'sys_refindex'), array('ref_table' => 'sys_file', 'softref_key' => 'rtehtmlarea_images', 'ref_uid' => $file->getUid(), 'ref_string' => $fileadminDirectory . $targetFileName));
                 $queries[] = str_replace(LF, ' ', $this->db->debug_lastBuiltQuery);
             }
         }
     }
     return TRUE;
 }
 /**
  * Migrate files to sys_file_references
  *
  * @param array $record
  * @param string $field
  * @return void
  */
 protected function migrateFiles(array $record, $field)
 {
     $filesList = $record['tx_jhopengraphprotocol_ogimage'];
     $files = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $filesList, TRUE);
     if ($files) {
         foreach ($files as $file) {
             if (file_exists(PATH_site . 'uploads/tx_jhopengraphprotocol/' . $file)) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . 'uploads/tx_jhopengraphprotocol/' . $file, $this->targetDirectory . $file);
                 $fileObject = $this->storage->getFile(self::FOLDER_ContentUploads . '/' . $file);
                 $this->fileRepository->add($fileObject);
                 $dataArray = array('uid_local' => $fileObject->getUid(), 'tablenames' => 'pages', 'fieldname' => $field, 'uid_foreign' => $record['uid'], 'table_local' => 'sys_file', 'cruser_id' => self::CruserId, 'pid' => $record['pid'], 'sorting_foreign' => $record['sorting'], 'title' => $record['title']);
                 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $dataArray);
             }
         }
     }
 }
 /**
  * Tries to fetch the file object corresponding to the given path.
  *
  * @param string $path Path to a file (starting with "fileadmin/")
  * @param array $reference Corresponding sys_refindex entry
  * @return null|\TYPO3\CMS\Core\Resource\FileInterface
  */
 protected function fetchReferencedFile($path, array $reference)
 {
     $fileObject = NULL;
     if (@file_exists(PATH_site . '/' . $path)) {
         try {
             $fileObject = $this->storage->getFile('/' . str_replace($this->fileAdminDir, '', $path));
         } catch (\TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException $notFoundException) {
             // This should really not happen, since we are testing existence of the file just before
             $this->errors[] = $path . ' not found (referenced in element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . ')';
         }
     } else {
         // Nothing to be done if file not found
         $this->errors[] = $path . ' not found (referenced in element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . ')';
     }
     return $fileObject;
 }
 /**
  * Tries to fetch the file object corresponding to the given path.
  *
  * @param string $path Path to a file (starting with "fileadmin/")
  * @param array $reference Corresponding sys_refindex entry
  * @return null|\TYPO3\CMS\Core\Resource\FileInterface
  */
 protected function fetchReferencedFile($path, array $reference)
 {
     $fileObject = NULL;
     if (@file_exists(PATH_site . '/' . $path)) {
         try {
             $fileObject = $this->storage->getFile('/' . str_replace($this->fileAdminDir, '', $path));
         } catch (\TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException $notFoundException) {
             // This should really not happen, since we are testing existence of the file just before
             $this->errors[] = $path . ' not found (referenced in element ' . $reference['recuid'] . ' of table ' . $reference['tablename'] . ' in field ' . $reference['field'] . ')';
         }
     } else {
         // Nothing to be done if file not found, but output errors with (page) pid in csv format for easier manual treatment
         $recpid = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('pid', $reference['tablename'], 'uid = ' . (int) $reference['recuid']);
         $this->errors[] = 'File not found (page / uid / path / table / field):,' . $recpid[0]['pid'] . ',' . $reference['recuid'] . ',' . $path . ',' . $reference['tablename'] . ',' . $reference['field'];
     }
     return $fileObject;
 }
 /**
  * Processes the actual transformation from CSV to sys_file_references
  *
  * @param array $source
  * @param array $destination
  * @param array $configuration
  *
  * @return void
  */
 protected function migrateFilesToFal(array $source, array $destination, array $configuration)
 {
     $path = PATH_site . $configuration['sourcePath'];
     $files = GeneralUtility::trimExplode(',', $source[$configuration['sourceField']], true);
     $i = 1;
     foreach ($files as $file) {
         if (file_exists($path . $file)) {
             GeneralUtility::upload_copy_move($path . $file, $this->targetDirectory . $file);
             /** @var \TYPO3\CMS\Core\Resource\File $fileObject */
             $fileObject = $this->storage->getFile(self::FILE_MIGRATION_FOLDER . $file);
             $this->fileIndexRepository->add($fileObject);
             $count = $this->database->exec_SELECTcountRows('*', 'sys_file_reference', 'tablenames = ' . $this->database->fullQuoteStr($configuration['destinationTable'], 'sys_file_reference') . ' AND fieldname = ' . $this->database->fullQuoteStr($configuration['destinationField'], 'sys_file_reference') . ' AND uid_local = ' . $fileObject->getUid() . ' AND uid_foreign = ' . $destination['uid']);
             if (!$count) {
                 $dataArray = array('uid_local' => $fileObject->getUid(), 'tablenames' => $configuration['destinationTable'], 'uid_foreign' => $destination['uid'], 'pid' => $source['pid'], 'fieldname' => $configuration['destinationField'], 'sorting_foreign' => $i, 'table_local' => 'sys_file');
                 $this->database->exec_INSERTquery('sys_file_reference', $dataArray);
             }
         }
         $i++;
     }
 }
 /**
  * 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)
 {
     $titleTextContents = [];
     $alternativeTextContents = [];
     $captionContents = [];
     $linkContents = [];
     $fieldItems = GeneralUtility::trimExplode(',', $row[$fieldname], true);
     if (empty($fieldItems) || is_numeric($row[$fieldname])) {
         return [];
     }
     if (isset($fieldConfiguration['titleTexts'])) {
         $titleTextField = $fieldConfiguration['titleTexts'];
         $titleTextContents = explode(LF, $row[$titleTextField]);
     }
     if (isset($fieldConfiguration['alternativeTexts'])) {
         $alternativeTextField = $fieldConfiguration['alternativeTexts'];
         $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
     }
     if (isset($fieldConfiguration['captions'])) {
         $captionField = $fieldConfiguration['captions'];
         $captionContents = explode(LF, $row[$captionField]);
     }
     if (isset($fieldConfiguration['links'])) {
         $linkField = $fieldConfiguration['links'];
         $linkContents = explode(LF, $row[$linkField]);
     }
     $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
     $queries = [];
     $i = 0;
     if (!PATH_site) {
         throw new \Exception('PATH_site was undefined.');
     }
     $storageUid = (int) $this->storage->getUid();
     foreach ($fieldItems as $item) {
         $fileUid = null;
         $sourcePath = PATH_site . $fieldConfiguration['sourcePath'] . $item;
         $targetDirectory = PATH_site . $fileadminDirectory . $fieldConfiguration['targetPath'];
         $targetPath = $targetDirectory . basename($item);
         // maybe the file was already moved, so check if the original file still exists
         if (file_exists($sourcePath)) {
             if (!is_dir($targetDirectory)) {
                 GeneralUtility::mkdir_deep($targetDirectory);
             }
             // see if the file already exists in the storage
             $fileSha1 = sha1_file($sourcePath);
             $existingFileRecord = $this->database->exec_SELECTgetSingleRow('uid', 'sys_file', 'sha1=' . $this->database->fullQuoteStr($fileSha1, 'sys_file') . ' AND storage=' . $storageUid);
             // the file exists, the file does not have to be moved again
             if (is_array($existingFileRecord)) {
                 $fileUid = $existingFileRecord['uid'];
             } else {
                 // just move the file (no duplicate)
                 rename($sourcePath, $targetPath);
             }
         }
         if ($fileUid === null) {
             // get the File object if it hasn't been fetched before
             try {
                 // 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.
                 /** @var File $file */
                 $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
                 $fileUid = $file->getUid();
             } catch (\InvalidArgumentException $e) {
                 // no file found, no reference can be set
                 $this->logger->notice('File ' . $fieldConfiguration['sourcePath'] . $item . ' does not exist. Reference was not migrated.', ['table' => $table, 'record' => $row, 'field' => $fieldname]);
                 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
                 $message = sprintf($format, $fieldConfiguration['sourcePath'] . $item, $table, $row['uid'], $fieldname);
                 $customMessages .= PHP_EOL . $message;
                 continue;
             }
         }
         if ($fileUid > 0) {
             $fields = ['fieldname' => $fieldname, 'table_local' => 'sys_file', 'pid' => $table === 'pages' ? $row['uid'] : $row['pid'], 'uid_foreign' => $row['uid'], 'uid_local' => $fileUid, 'tablenames' => $table, 'crdate' => time(), 'tstamp' => time(), 'sorting' => $i + 256, 'sorting_foreign' => $i];
             if (isset($titleTextField)) {
                 $fields['title'] = trim($titleTextContents[$i]);
             }
             if (isset($alternativeTextField)) {
                 $fields['alternative'] = trim($alternativeTextContents[$i]);
             }
             if (isset($captionField)) {
                 $fields['description'] = trim($captionContents[$i]);
             }
             if (isset($linkField)) {
                 $fields['link'] = trim($linkContents[$i]);
             }
             $this->database->exec_INSERTquery('sys_file_reference', $fields);
             $queries[] = str_replace(LF, ' ', $this->database->debug_lastBuiltQuery);
             ++$i;
         }
     }
     // Update referencing table's original field to now contain the count of references,
     // but only if all new references could be set
     if ($i === count($fieldItems)) {
         $this->database->exec_UPDATEquery($table, 'uid=' . $row['uid'], [$fieldname => $i]);
         $queries[] = str_replace(LF, ' ', $this->database->debug_lastBuiltQuery);
     } else {
         $this->recordOffset[$table]++;
     }
     return $queries;
 }
 protected function migrateField($table, $row, $fieldname, $fieldConfiguration)
 {
     $fieldItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row[$fieldname], TRUE);
     if (empty($fieldItems) || is_numeric($row[$fieldname])) {
         return array();
     }
     if (isset($fieldConfiguration['titleTexts'])) {
         $titleTextField = $fieldConfiguration['titleTexts'];
         $titleTextContents = explode(LF, $row[$titleTextField]);
     }
     if (isset($fieldConfiguration['alternativeTexts'])) {
         $alternativeTextField = $fieldConfiguration['alternativeTexts'];
         $alternativeTextContents = explode(LF, $row[$alternativeTextField]);
     }
     if (isset($fieldConfiguration['captions'])) {
         $captionField = $fieldConfiguration['captions'];
         $captionContents = explode(LF, $row[$captionField]);
     }
     if (isset($fieldConfiguration['links'])) {
         $linkField = $fieldConfiguration['links'];
         $linkContents = explode(LF, $row[$linkField]);
     }
     $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
     $queries = array();
     $i = 0;
     foreach ($fieldItems as $item) {
         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;
         if (!is_dir(dirname($targetPath))) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep(dirname($targetPath));
         }
         rename($sourcePath, $targetPath);
         // get the File object
         $file = $this->storage->getFile($fieldConfiguration['targetPath'] . $item);
         if ($file instanceof \TYPO3\CMS\Core\Resource\File) {
             $fields = array('fieldname' => $fieldname, 'table_local' => 'sys_file', 'uid_foreign' => $row['uid'], 'uid_local' => $file->getUid(), 'tablenames' => $table, 'crdate' => time(), 'tstamp' => time());
             if (isset($titleTextField)) {
                 $fields['title'] = trim($titleTextContents[$i]);
             }
             if (isset($alternativeTextField)) {
                 $fields['alternative'] = trim($alternativeTextContents[$i]);
             }
             if (isset($captionField)) {
                 $fields['description'] = trim($captionContents[$i]);
             }
             if (isset($linkField)) {
                 $fields['link'] = trim($linkContents[$i]);
             }
             $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $fields);
             $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
             ++$i;
         }
     }
     // Update referencing table's original field to now contain the count of references.
     $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . $row['uid'], array($fieldname => $i));
     $queries[] = str_replace(LF, ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
     return $queries;
 }
Exemple #11
0
 /**
  * Migrates a single field.
  *
  * @param array $row
  * @param string $customMessages
  * @param array $dbQueries
  *
  * @throws \Exception
  */
 protected function migrateField($row, &$customMessages, &$dbQueries)
 {
     $fieldItems = GeneralUtility::trimExplode(',', $row[$this->fieldToMigrate], true);
     if (empty($fieldItems) || is_numeric($row[$this->fieldToMigrate])) {
         return;
     }
     $fileadminDirectory = rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') . '/';
     $i = 0;
     if (!PATH_site) {
         throw new \Exception('PATH_site was undefined.', 1476107387);
     }
     $storageUid = (int) $this->storage->getUid();
     $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
     foreach ($fieldItems as $item) {
         $fileUid = null;
         $sourcePath = PATH_site . $this->sourcePath . $item;
         $targetDirectory = PATH_site . $fileadminDirectory . $this->targetPath;
         $targetPath = $targetDirectory . basename($item);
         // maybe the file was already moved, so check if the original file still exists
         if (file_exists($sourcePath)) {
             if (!is_dir($targetDirectory)) {
                 GeneralUtility::mkdir_deep($targetDirectory);
             }
             // see if the file already exists in the storage
             $fileSha1 = sha1_file($sourcePath);
             $queryBuilder = $connectionPool->getQueryBuilderForTable('sys_file');
             $queryBuilder->getRestrictions()->removeAll();
             $existingFileRecord = $queryBuilder->select('uid')->from('sys_file')->where($queryBuilder->expr()->eq('sha1', $queryBuilder->createNamedParameter($fileSha1, \PDO::PARAM_STR)), $queryBuilder->expr()->eq('storage', $queryBuilder->createNamedParameter($storageUid, \PDO::PARAM_INT)))->execute()->fetch();
             // the file exists, the file does not have to be moved again
             if (is_array($existingFileRecord)) {
                 $fileUid = $existingFileRecord['uid'];
             } else {
                 // just move the file (no duplicate)
                 rename($sourcePath, $targetPath);
             }
         }
         if ($fileUid === null) {
             // get the File object if it hasn't been fetched before
             try {
                 // 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.
                 $file = $this->storage->getFile($this->targetPath . $item);
                 $fileUid = $file->getUid();
             } catch (\InvalidArgumentException $e) {
                 // no file found, no reference can be set
                 $this->logger->notice('File ' . $this->sourcePath . $item . ' does not exist. Reference was not migrated.', ['table' => $this->table, 'record' => $row, 'field' => $this->fieldToMigrate]);
                 $format = 'File \'%s\' does not exist. Referencing field: %s.%d.%s. The reference was not migrated.';
                 $message = sprintf($format, $this->sourcePath . $item, $this->table, $row['uid'], $this->fieldToMigrate);
                 $customMessages .= PHP_EOL . $message;
                 continue;
             }
         }
         if ($fileUid > 0) {
             $fields = ['fieldname' => $this->fieldToMigrate, 'table_local' => 'sys_file', 'pid' => $this->table === 'pages' ? $row['uid'] : $row['pid'], 'uid_foreign' => $row['uid'], 'uid_local' => $fileUid, 'tablenames' => $this->table, 'crdate' => time(), 'tstamp' => time(), 'sorting' => $i + 256, 'sorting_foreign' => $i];
             $queryBuilder = $connectionPool->getQueryBuilderForTable('sys_file_reference');
             $queryBuilder->insert('sys_file_reference')->values($fields)->execute();
             $dbQueries[] = str_replace(LF, ' ', $queryBuilder->getSQL());
             ++$i;
         }
     }
     // Update referencing table's original field to now contain the count of references,
     // but only if all new references could be set
     if ($i === count($fieldItems)) {
         $queryBuilder = $connectionPool->getQueryBuilderForTable($this->table);
         $queryBuilder->update($this->table)->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($row['uid'], \PDO::PARAM_INT)))->set($this->fieldToMigrate, $i)->execute();
         $dbQueries[] = str_replace(LF, ' ', $queryBuilder->getSQL());
     } else {
         $this->recordOffset[$this->table]++;
     }
 }