Commit 36846001 authored by Dainis Abols's avatar Dainis Abols
Browse files

TYPO3 v10 Compatability changes

parent b38dece4
...@@ -11,7 +11,7 @@ use TYPO3\CMS\Core\FormProtection\FormProtectionFactory; ...@@ -11,7 +11,7 @@ use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
use TYPO3\CMS\Core\Page\PageRenderer; use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Lang\LanguageService; use TYPO3\CMS\Core\Localization\LanguageService;
/** /**
* Class FrontController * Class FrontController
...@@ -66,8 +66,6 @@ class FrontController extends ActionController ...@@ -66,8 +66,6 @@ class FrontController extends ActionController
*/ */
public function __construct() public function __construct()
{ {
parent::__construct();
if (!$this->pageUid) { if (!$this->pageUid) {
$this->pageUid = $_REQUEST['id']; $this->pageUid = $_REQUEST['id'];
} }
...@@ -237,7 +235,7 @@ class FrontController extends ActionController ...@@ -237,7 +235,7 @@ class FrontController extends ActionController
*/ */
protected function getLanguageService() protected function getLanguageService()
{ {
return $GLOBALS['LANG'] ?? new LanguageService(); return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
} }
/** /**
......
...@@ -17,7 +17,7 @@ use TYPO3\CMS\Core\Utility\HttpUtility; ...@@ -17,7 +17,7 @@ use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Lang\LanguageService; use TYPO3\CMS\Core\Localization\LanguageService;
/** /**
* Class MusicController * Class MusicController
...@@ -79,8 +79,6 @@ class MusicController extends ActionController ...@@ -79,8 +79,6 @@ class MusicController extends ActionController
*/ */
public function __construct() public function __construct()
{ {
parent::__construct();
if (!$this->pageUid) { if (!$this->pageUid) {
$this->pageUid = $_REQUEST['id']; $this->pageUid = $_REQUEST['id'];
} }
...@@ -154,7 +152,14 @@ class MusicController extends ActionController ...@@ -154,7 +152,14 @@ class MusicController extends ActionController
$songs = $this->songRepository->findAll(); $songs = $this->songRepository->findAll();
if (count($songs)) { if (count($songs)) {
$returnUrl = 'index.php?route=/web/LuNabamusicMusic/&id='.$this->pageUid.$this->getToken(); $routeBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
$returnUrl = (string)$routeBuilder->buildUriFromRoutePath(
'/web/LuNabamusicMusic/', [
'id' => $this->pageUid,
'token' => $this->getToken(true),
]
);
foreach ($songs as $item) { foreach ($songs as $item) {
// Load extra data for songs // Load extra data for songs
$item->loadAdditionalInfo(); $item->loadAdditionalInfo();
...@@ -244,16 +249,20 @@ class MusicController extends ActionController ...@@ -244,16 +249,20 @@ class MusicController extends ActionController
$routeBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class); $routeBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
// Create redirect url // Create redirect url
$returnUrl = 'index.php?route=/web/LuNabamusicMusic/&id='.$this->pageUid.$this->getToken(); $returnUrl = $routeBuilder->buildUriFromRoutePath('/web/LuNabamusicMusic/', [
$url = $routeBuilder->buildUriFromRoute( 'id'=> $this->pageUid,
'token' => $this->getToken(true)
]);
$url = $routeBuilder->buildUriFromRoute(
'record_edit', [ 'record_edit', [
'edit['.$table.'][0]' => 'new', 'edit['.$table.']['.$this->pageUid.']' => 'new',
'returnUrl' => $returnUrl, 'returnUrl' => (string)$returnUrl,
] ]
); );
// Redirect // Redirect
HttpUtility::redirect($url); HttpUtility::redirect((string)$url);
} }
/** /**
...@@ -263,7 +272,7 @@ class MusicController extends ActionController ...@@ -263,7 +272,7 @@ class MusicController extends ActionController
{ {
// Preset used variables // Preset used variables
$buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar(); $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
$iconFactory = new IconFactory(); $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$uriBuilder = $this->objectManager->get(UriBuilder::class); $uriBuilder = $this->objectManager->get(UriBuilder::class);
// Set basic uri // Set basic uri
...@@ -294,7 +303,7 @@ class MusicController extends ActionController ...@@ -294,7 +303,7 @@ class MusicController extends ActionController
*/ */
protected function getLanguageService() protected function getLanguageService()
{ {
return $GLOBALS['LANG'] ?? new LanguageService(); return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
} }
/** /**
......
...@@ -6,6 +6,7 @@ use Lu\LuNabamusic\Domain\Model\Song; ...@@ -6,6 +6,7 @@ use Lu\LuNabamusic\Domain\Model\Song;
use Lu\LuNabamusic\Domain\Model\Vote; use Lu\LuNabamusic\Domain\Model\Vote;
use Lu\LuNabamusic\Domain\Repository\SongRepository; use Lu\LuNabamusic\Domain\Repository\SongRepository;
use Lu\LuNabamusic\Helpers\DataHelper; use Lu\LuNabamusic\Helpers\DataHelper;
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
use TYPO3\CMS\Backend\Template\Components\ButtonBar; use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore; use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
use TYPO3\CMS\Backend\View\BackendTemplateView; use TYPO3\CMS\Backend\View\BackendTemplateView;
...@@ -19,7 +20,7 @@ use TYPO3\CMS\Core\Utility\HttpUtility; ...@@ -19,7 +20,7 @@ use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Lang\LanguageService; use TYPO3\CMS\Core\Localization\LanguageService;
/** /**
* Class MusicController * Class MusicController
...@@ -88,8 +89,6 @@ class VotesController extends ActionController ...@@ -88,8 +89,6 @@ class VotesController extends ActionController
*/ */
public function __construct() public function __construct()
{ {
parent::__construct();
if (!$this->pageUid) { if (!$this->pageUid) {
$this->pageUid = $_REQUEST['id'] ?? $GLOBALS['TSFE']->id; $this->pageUid = $_REQUEST['id'] ?? $GLOBALS['TSFE']->id;
} }
...@@ -164,11 +163,10 @@ class VotesController extends ActionController ...@@ -164,11 +163,10 @@ class VotesController extends ActionController
// Redirect back to listing // Redirect back to listing
if ($isAdmin) { if ($isAdmin) {
$returnUrl = 'index.php?route=/web/LuNabamusicMusic/&id='.$this->pageUid.$this->getToken(); $this->redirectToModule();
HttpUtility::redirect($returnUrl);
} else { } else {
// Redirect back to FE page // Redirect back to FE page
$RedirectPageId = $this->settings['afterVoteContent']; $RedirectPageId = empty($this->settings['afterVoteContent']) ? $this->pageUid : $this->settings['afterVoteContent'];
$returnUrl = $this->controllerContext->getUriBuilder()->setCreateAbsoluteUri(true)->reset() $returnUrl = $this->controllerContext->getUriBuilder()->setCreateAbsoluteUri(true)->reset()
->setTargetPageUid($RedirectPageId)->buildFrontendUri(); ->setTargetPageUid($RedirectPageId)->buildFrontendUri();
HttpUtility::redirect($returnUrl); HttpUtility::redirect($returnUrl);
...@@ -194,8 +192,20 @@ class VotesController extends ActionController ...@@ -194,8 +192,20 @@ class VotesController extends ActionController
$VoteModel->delete(); $VoteModel->delete();
} }
// Redirect back to listing // Redirect back
$returnUrl = 'index.php?route=/web/LuNabamusicMusic/&id='.$this->pageUid.$this->getToken(); $this->redirectToModule();
}
/**
* Redirects back to module
*/
private function redirectToModule()
{
$routeBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
$returnUrl = $routeBuilder->buildUriFromRoutePath('/web/LuNabamusicMusic/', [
'id'=> $this->pageUid,
'token' => $this->getToken(true)
]);
HttpUtility::redirect($returnUrl); HttpUtility::redirect($returnUrl);
} }
...@@ -206,7 +216,7 @@ class VotesController extends ActionController ...@@ -206,7 +216,7 @@ class VotesController extends ActionController
*/ */
protected function getLanguageService() protected function getLanguageService()
{ {
return $GLOBALS['LANG'] ?? new LanguageService(); return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
} }
/** /**
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Lu\LuNabamusic\Domain\Model; namespace Lu\LuNabamusic\Domain\Model;
use DateTime; use DateTime;
use HDNET\Calendarize\Domain\Model\Event;
use Lu\LuNabamusic\Helpers\DataHelper; use Lu\LuNabamusic\Helpers\DataHelper;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
......
...@@ -6,6 +6,7 @@ use Lu\LuNabamusic\Interfaces\TableView; ...@@ -6,6 +6,7 @@ use Lu\LuNabamusic\Interfaces\TableView;
use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
...@@ -188,17 +189,17 @@ class Song extends AbstractEntity implements TableView ...@@ -188,17 +189,17 @@ class Song extends AbstractEntity implements TableView
/** /**
* Return UID * Return UID
* *
* @return int * @return int|null
*/ */
public function getUid() public function getUid(): ?int
{ {
return $this->uid; return $this->uid;
} }
/** /**
* @return mixed * @return int|null
*/ */
public function getPid() public function getPid(): ?int
{ {
return $this->pid; return $this->pid;
} }
...@@ -240,18 +241,9 @@ class Song extends AbstractEntity implements TableView ...@@ -240,18 +241,9 @@ class Song extends AbstractEntity implements TableView
*/ */
public function getMusicFile() public function getMusicFile()
{ {
// Fetch music file ... ABSURD! $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
$response = $queryBuilder->select('music_file')
->from($this->tableName)
->where('uid='.$this->uid)
->execute();
while ($row = $response->fetch()) {
$result = $row['music_file'];
}
return file_exists( Environment::getPublicPath().'/'.$result) ? $result : null; return $fileRepository->findByRelation($this->tableName, 'music_file', $this->uid)[0];
} }
/** /**
......
...@@ -106,7 +106,7 @@ class DataHelper ...@@ -106,7 +106,7 @@ class DataHelper
/** /**
* Retrieve LanguageService * Retrieve LanguageService
* *
* @return mixed|\TYPO3\CMS\Lang\LanguageService * @return mixed|\TYPO3\CMS\Core\Localization\LanguageService
*/ */
private function getLanguageService() private function getLanguageService()
{ {
......
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace Lu\LuNabamusic\Updates;
use Doctrine\DBAL\DBALException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Resource\StorageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Install\Updates\ChattyInterface;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
/**
* Upgrade wizard which goes through all files
* and creates sys_file records as well as sys_file_reference records for each hit.
*/
class FileMigrationWizard implements UpgradeWizardInterface, ChattyInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var ResourceStorage
*/
protected $storage;
/**
* Table to migrate records from
*
* @var string
*/
protected $table = 'tx_lunabamusic_domain_model_song';
/**
* Table field holding the migration to be
*
* @var string
*/
protected $fieldToMigrate = 'music_file';
/**
* target folder after migration
* Relative to fileadmin
*
* @var string
*/
protected $targetPath = '_migrated/';
/**
* @return string Unique identifier of this updater
*/
public function getIdentifier(): string
{
return 'nabaFileMigration';
}
/**
* @return string Title of this updater
*/
public function getTitle(): string
{
return 'Migrate all NABA file relations to sys_file_references';
}
/**
* @return string Longer description of this updater
*/
public function getDescription(): string
{
return 'This update wizard goes through all files that are referenced in the'
. ' backend_layout.icon field and adds the files to the FAL File Index.'
. ' It also moves the files from uploads/ to the fileadmin/_migrated/ path.';
}
/**
* @return bool True if there are records to update
*/
public function updateNecessary(): bool
{
return !empty($this->getRecordsFromTable());
}
/**
* @return string[] All new fields and tables must exist
*/
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class
];
}
/**
* @param OutputInterface $output
*/
public function setOutput(OutputInterface $output): void
{
$this->output = $output;
}
/**
* Performs the configuration update.
*
* @return bool
*/
public function executeUpdate(): bool
{
$result = true;
try {
$storages = GeneralUtility::makeInstance(StorageRepository::class)->findAll();
$this->storage = $storages[0];
$records = $this->getRecordsFromTable();
foreach ($records as $record) {
$this->migrateField($record);
}
} catch (\Exception $e) {
// If something goes wrong, migrateField() logs an error
$result = false;
}
return $result;
}
/**
* Get records from table where the field to migrate is not empty (NOT NULL and != '')
* and also not numeric (which means that it is migrated)
*
* @return array
* @throws \RuntimeException
*/
protected function getRecordsFromTable()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll();
try {
return $queryBuilder
->select('uid', 'pid', $this->fieldToMigrate)
->from($this->table)
->where(
$queryBuilder->expr()->isNotNull($this->fieldToMigrate),
$queryBuilder->expr()->neq(
$this->fieldToMigrate,
$queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
),
$queryBuilder->expr()->comparison(
'CAST(CAST(' . $queryBuilder->quoteIdentifier($this->fieldToMigrate) . ' AS DECIMAL) AS CHAR)',
ExpressionBuilder::NEQ,
'CAST(' . $queryBuilder->quoteIdentifier($this->fieldToMigrate) . ' AS CHAR)'
)
)
->orderBy('uid')
->execute()
->fetchAll();
} catch (DBALException $e) {
throw new \RuntimeException(
'Database query failed. Error was: ' . $e->getPrevious()->getMessage(),
1511950673
);
}
}
/**
* Migrates a single field.
*
* @param array $row
* @throws \Exception
*/
protected function migrateField($row)
{
$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;
$storageUid = (int)$this->storage->getUid();
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
foreach ($fieldItems as $item) {
$fileUid = null;
$sourcePath = Environment::getPublicPath() . '/' . $item;
$strippedPath = str_replace("fileadmin/user_upload/", "", $item);
$targetDirectory = Environment::getPublicPath() . '/' . $fileadminDirectory . $this->targetPath;
$targetPath = $targetDirectory . $strippedPath;
// Remove file name
$temp = explode("/", $strippedPath);
array_pop($temp);
$subdir = implode("/", $temp);
$subdir = $targetDirectory . $subdir;
// maybe the file was already moved, so check if the original file still exists
if (file_exists($sourcePath) && !is_dir($sourcePath)) {
if (!is_dir($targetDirectory)) {
GeneralUtility::mkdir_deep($targetDirectory);
}
// Create subdirectory
if (!is_dir($subdir)) {
GeneralUtility::mkdir_deep($subdir);
}
// 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.
/** @var File $file */
$file = $this->storage->getFile($this->targetPath . $strippedPath);
$fileUid = $file->getUid();