Last active
December 12, 2018 12:05
-
-
Save christophlehmann/fb6a7241c4d0afb6f1eabe8067b09071 to your computer and use it in GitHub Desktop.
Repair wrong chashes in tx_realurl_urldata table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add Command controllers | |
if (TYPO3_MODE === 'BE') { | |
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = \Lemming\Extension\Command\CacheHashCommandController::class; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Lemming\Extension\Command; | |
use TYPO3\CMS\Core\Database\DatabaseConnection; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Frontend\Page\CacheHashCalculator; | |
class CacheHashCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController | |
{ | |
const TABLE = 'tx_realurl_urldata'; | |
/** | |
* Repair CHashes | |
* | |
*/ | |
public function repairCacheHashesCommand() | |
{ | |
/** @var DatabaseConnection $databaseConnection */ | |
$databaseConnection = $GLOBALS['TYPO3_DB']; | |
$entries = $databaseConnection->exec_SELECTgetRows( | |
'*', | |
self::TABLE, | |
'request_variables LIKE "%cHash%"' | |
); | |
if ($entries == null) { | |
$this->outputLine('No entries found'); | |
exit; | |
} | |
$this->outputLine(sprintf('Found %s entries with a cHash', count($entries))); | |
/** @var CacheHashCalculator $calculator */ | |
$calculator = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class); | |
$repaired = 0; | |
foreach ($entries as $entry) { | |
$this->outputLine(sprintf('Updating entry %d', $i)); | |
$requestVariables = json_decode($entry['request_variables'], true); | |
$requestVariablesAsString = http_build_query($requestVariables); | |
$cHash = $requestVariables['cHash']; | |
try { | |
$newCHash = $calculator->generateForParameters($requestVariablesAsString); | |
} catch (\Exception $exception) { | |
$this->outputLine('Error while generating new cHash for entry:'); | |
var_dump($entry); | |
} | |
if (isset($newCHash) && $newCHash != $cHash) { | |
$newRequestVariables = $requestVariables; | |
$newRequestVariables['cHash'] = $newCHash; | |
$newRequestVariablesField = json_encode($newRequestVariables); | |
$newOriginalUrlField = str_replace('cHash=' . $cHash, 'cHash=' . $newCHash, $entry['original_url']); | |
$newOriginalUrlHashField = crc32($newOriginalUrlField); | |
$databaseConnection->exec_UPDATEquery( | |
self::TABLE, | |
'uid=' . $entry['uid'], | |
[ | |
'original_url' => $newOriginalUrlField, | |
'original_url_hash' => $newOriginalUrlHashField, | |
'request_variables' => $newRequestVariablesField, | |
] | |
); | |
$repaired++; | |
} | |
} | |
$this->outputLine(sprintf('Repaired %s entries', $repaired)); | |
} | |
} |
Needed only if your 7.6 did not include id to cHash but your 8.7 does ($GLOBALS['TYPO3_CONF_VARS']['FE']['cHashIncludePageId']
in one installation is different from the other). Otherwise it is not needed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possibly needed while updating from 7.6 to 8.7