Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save christophlehmann/fb6a7241c4d0afb6f1eabe8067b09071 to your computer and use it in GitHub Desktop.
Save christophlehmann/fb6a7241c4d0afb6f1eabe8067b09071 to your computer and use it in GitHub Desktop.
Repair wrong chashes in tx_realurl_urldata table
<?php
// Add Command controllers
if (TYPO3_MODE === 'BE') {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = \Lemming\Extension\Command\CacheHashCommandController::class;
}
<?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));
}
}
@christophlehmann
Copy link
Author

Possibly needed while updating from 7.6 to 8.7

@dmitryd
Copy link

dmitryd commented Dec 12, 2018

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