Skip to content
Snippets Groups Projects
UpdateAuthors.php 5.03 KiB
Newer Older
<?php

namespace SGalinski\SgNews\Updates;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

use SGalinski\SgNews\Domain\Model\News;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

/**
 * Migrate template db entries to the correct root pages
 */
class UpdateAuthors implements UpgradeWizardInterface {
	/**
	 * The wizard identifier
	 */
	const IDENTIFIER = 'tx_sgnews_update_authors';

	 * Retrieves the next site root in the page hierarchy from the current page
	 *
	 * @param int $currentPid
	 * @return int
	protected function getSiteRoot($currentPid): int {
		$rootLine = BackendUtility::BEgetRootLine((int) $currentPid);
		$siteRoot = ['uid' => 0];

		foreach ($rootLine as $page) {
			if ((int) $page['is_siteroot'] === 1) {
				$siteRoot = $page;
				break;
			}
		}

		return $siteRoot['uid'];
	}
	 * @return string
	public function getIdentifier(): string {
	 * @return string
	public function getTitle(): string {
		return 'Migrate all news authors to the new author table for each site root.';
	 * @return string
	public function getDescription(): string {
		return 'Create new author entries for the old author field of news pages';
	}
	/**
	 * @return bool
	 */
	public function executeUpdate(): bool {
		$newsQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
		$newsQueryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
		$newsEntries = $newsQueryBuilder->select('*')
			->from('pages')
			->where(
				$newsQueryBuilder->expr()->andX(
					$newsQueryBuilder->expr()->eq('doktype', News::DOK_TYPE_NEWS),
					$newsQueryBuilder->expr()->neq('author', $newsQueryBuilder->quote(''))
				)
			)
			->execute()->fetchAll();
		if (count($newsEntries) <= 0) {
			return TRUE;
		}
		foreach ($newsEntries as $news) {
			$siteRoot = $this->getSiteRoot($news['pid']);
			if ($siteRoot <= 0) {
				continue;
			}

			$authorQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
				->getQueryBuilderForTable('tx_sgnews_domain_model_author');
			$authorQueryBuilder->getRestrictions()->removeAll()->add(
				GeneralUtility::makeInstance(DeletedRestriction::class)
			);
			$author = $authorQueryBuilder->select('*')
				->from('tx_sgnews_domain_model_author')
				->where(
					$newsQueryBuilder->expr()->andX(
						$newsQueryBuilder->expr()->eq('pid', $siteRoot),
						$newsQueryBuilder->expr()->eq('name', $newsQueryBuilder->quote($news['author']))
					)
				)
				->setMaxResults(1)
				->execute()->fetchAll();
			if (!$author) {
				$authorQueryBuilder->insert('tx_sgnews_domain_model_author')
					->values([
						'name' => $news['author'],
						'path_segment' => rawurlencode($news['author']),
						'pid' => $siteRoot,
						'crdate' => time(),
						'tstamp' => time(),
					])->execute();
				$authorUid = (int) $authorQueryBuilder->getConnection()->lastInsertId();
			} else {
				$authorUid = $author[0]['uid'];
			}

			$newsQueryBuilder->update('pages')
				->set('tx_sgnews_news_author', $authorUid)
				->set('author', '')
				->where($newsQueryBuilder->expr()->eq('uid', $news['uid']))
				->execute();
		}

		return TRUE;
	}

	/**
	 * @return bool
	public function updateNecessary(): bool {
		$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
		$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
		$rowCount = $queryBuilder->select('*')
			->from('pages')
			->where(
				$queryBuilder->expr()->andX(
					$queryBuilder->expr()->eq('doktype', News::DOK_TYPE_NEWS),
					$queryBuilder->expr()->isNotNull('author')
				)
			)
			->execute()->rowCount();
		return $rowCount > 0;
	}
	/**
	 * @return array|string[]
	 */
	public function getPrerequisites(): array {
		return [
			DatabaseUpdatedPrerequisite::class
		];