Skip to content

Commit 62bb068

Browse files
committed
minor #23766 Consistently use 7 chars of sha256 for hash-based id generation (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- Consistently use 7 chars of sha256 for hash-based id generation | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This prevents generating over long service ids, and for filesystem-related changes, makes the Windows 258 chars limit farther. Commits ------- bc22cdd034 Consistently use 7 chars of sha256 for hash-based id generation
2 parents a13d23e + 0b12512 commit 62bb068

File tree

7 files changed

+13
-11
lines changed

7 files changed

+13
-11
lines changed

Dumper/XliffFileDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain,
8585
foreach ($messages->all($domain) as $source => $target) {
8686
$translation = $dom->createElement('trans-unit');
8787

88-
$translation->setAttribute('id', md5($source));
88+
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
8989
$translation->setAttribute('resname', $source);
9090

9191
$s = $translation->appendChild($dom->createElement('source'));
@@ -145,7 +145,7 @@ private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain,
145145

146146
foreach ($messages->all($domain) as $source => $target) {
147147
$translation = $dom->createElement('unit');
148-
$translation->setAttribute('id', md5($source));
148+
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
149149

150150
$segment = $translation->appendChild($dom->createElement('segment'));
151151

Tests/Dumper/FileDumperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function testDump()
2828
$dumper->dump($catalogue, array('path' => $tempDir));
2929

3030
$this->assertFileExists($tempDir.'/messages.en.concrete');
31+
32+
@unlink($tempDir.'/messages.en.concrete');
3133
}
3234

3335
/**

Tests/fixtures/resources-2.0-clean.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
33
<file id="messages.en_US">
4-
<unit id="acbd18db4cc2f85cedef654fccc4a4d8">
4+
<unit id="LCa0a2j">
55
<segment>
66
<source>foo</source>
77
<target>bar</target>
88
</segment>
99
</unit>
10-
<unit id="3c6e0b8a9c15224a8228b9a98ca1531d">
10+
<unit id="LHDhK3o">
1111
<segment>
1212
<source>key</source>
1313
<target order="1"></target>
1414
</segment>
1515
</unit>
16-
<unit id="18e6a493872558d949b4c16ea1fa6ab6">
16+
<unit id="2DA_bnh">
1717
<segment>
1818
<source>key.with.cdata</source>
1919
<target><![CDATA[<source> & <target>]]></target>

Tests/fixtures/resources-clean.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
<tool tool-id="symfony" tool-name="Symfony"/>
66
</header>
77
<body>
8-
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
8+
<trans-unit id="LCa0a2j" resname="foo">
99
<source>foo</source>
1010
<target>bar</target>
1111
<note priority="1" from="bar">baz</note>
1212
</trans-unit>
13-
<trans-unit id="3c6e0b8a9c15224a8228b9a98ca1531d" resname="key">
13+
<trans-unit id="LHDhK3o" resname="key">
1414
<source>key</source>
1515
<target></target>
1616
<note>baz</note>
1717
<note>qux</note>
1818
</trans-unit>
19-
<trans-unit id="18e6a493872558d949b4c16ea1fa6ab6" resname="key.with.cdata">
19+
<trans-unit id="2DA_bnh" resname="key.with.cdata">
2020
<source>key.with.cdata</source>
2121
<target><![CDATA[<source> & <target>]]></target>
2222
</trans-unit>

Tests/fixtures/resources-target-attributes.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<tool tool-id="symfony" tool-name="Symfony"/>
66
</header>
77
<body>
8-
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
8+
<trans-unit id="LCa0a2j" resname="foo">
99
<source>foo</source>
1010
<target state="needs-translation">bar</target>
1111
</trans-unit>

Tests/fixtures/resources-tool-info.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<tool tool-id="foo" tool-name="foo" tool-version="0.0" tool-company="Foo"/>
66
</header>
77
<body>
8-
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
8+
<trans-unit id="LCa0a2j" resname="foo">
99
<source>foo</source>
1010
<target>bar</target>
1111
</trans-unit>

Translator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ private function getFallbackContent(MessageCatalogue $catalogue)
363363

364364
private function getCatalogueCachePath($locale)
365365
{
366-
return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
366+
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
367367
}
368368

369369
private function doLoadCatalogue($locale)

0 commit comments

Comments
 (0)