Problem/Motivation
Redirect::generateHash() is a public method and can therefor be used by extending modules, which makes sense and is helpful.
On the other hand, it should (eventually) ensure consistent handling.
/**
* Generates a unique hash for identification purposes.
*
* @param string $source_path
* Source path of the redirect.
* @param array $source_query
* Source query as an array.
* @param string $language
* Redirect language.
*
* @return string
* Base 64 hash.
*/
public static function generateHash($source_path, array $source_query, $language) {
$hash = [
'source' => mb_strtolower($source_path),
'language' => $language,
];
if (!empty($source_query)) {
$hash['source_query'] = $source_query;
}
redirect_sort_recursive($hash, 'ksort');
return Crypt::hashBase64(serialize($hash));
}
In cases like #3448469: "Delete redirects defined in the spreadsheet" misses to remove leading slash normalization a project could provide $source_path differently, with other or no preprocessing that currently happens internally in the Redirect module, like:
$this->redirect_source->path = rtrim($this->redirect_source->path, '/'); in the preSave() method.
I guess the method should ensure that the hash is always created on the same normalization of the path as it is saved in the database. For redirect entities that means for example:
- Without leading slash for internal URLs
- Without trailings slash
- Lowercase?
- ...?
so it can be used more safely and unified by the outside world.
Steps to reproduce
See above
Proposed resolution
Implement consistent handling and normalization for the parameters provided.
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork redirect-3448498
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
anybodyComment #5
anybodyComment #6
berdirMakes sense, should we make this an mb_strtolower() as it may contain special characters? Not sure if it matters, as long as the hash is consistent?
Comment #7
anybodyThanks @Berdir, yes, to be sure I did that.
Comment #9
berdirMerged.