Skip to content

[Intl] Allow compressing emoji and data maps #50055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/intl-data-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ jobs:

- name: Run intl-data tests
run: ./phpunit --group intl-data -v

- name: Test with compressed data
run: |
[ -f src/Symfony/Component/Intl/Resources/data/locales/en.php ]
[ ! -f src/Symfony/Component/Intl/Resources/data/locales/en.php.gz ]
src/Symfony/Component/Intl/Resources/bin/compress
[ ! -f src/Symfony/Component/Intl/Resources/data/locales/en.php ]
[ -f src/Symfony/Component/Intl/Resources/data/locales/en.php.gz ]
./phpunit src/Symfony/Component/Intl
1 change: 1 addition & 0 deletions src/Symfony/Component/Intl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add the special `strip` locale to `EmojiTransliterator` to strip all emojis from a string
* Add `compress` script to compress the `Resources/data` directory when disk space matters

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Intl\Data\Bundle\Reader;

use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
use Symfony\Component\Intl\Util\GzipStreamWrapper;

/**
* Reads .php resource bundles.
Expand All @@ -31,6 +32,10 @@ public function read(string $path, string $locale): mixed
throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
}

if (is_file($fileName.'.gz')) {
return GzipStreamWrapper::require($fileName.'.gz');
}

if (!is_file($fileName)) {
throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
}
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Intl/Resources/bin/compress
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

if ('cli' !== PHP_SAPI) {
throw new Exception('This script must be run from the command line.');
}
if (!extension_loaded('zlib')) {
throw new Exception('This script requires the zlib extension.');
}

foreach (glob(dirname(__DIR__).'/data/*/*.php') as $file) {
if ('meta.php' === basename($file)) {
continue;
}

$data = file_get_contents($file);
file_put_contents('compress.zlib://'.$file.'.gz', $data);

unlink($file.(filesize($file.'.gz') >= strlen($data) ? '.gz' : ''));
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Intl/Resources/emoji/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
Builder::cleanTarget();
$emojisCodePoints = Builder::getEmojisCodePoints();
Builder::saveRules(Builder::buildRules($emojisCodePoints));
Builder::saveRules(Builder::buildStripRules($emojisCodePoints));
Builder::saveRules(Builder::buildGitHubRules($emojisCodePoints));
Builder::saveRules(Builder::buildSlackRules($emojisCodePoints));
Builder::saveRules(Builder::buildStripRules($emojisCodePoints));

final class Builder
{
Expand Down Expand Up @@ -178,7 +178,7 @@ public static function buildStripRules(array $emojisCodePoints): iterable
$maps[$codePointsCount][$emoji] = '';
}

return ['strip' => self::createRules($maps)];
return ['emoji-strip' => self::createRules($maps)];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those changes are not directly related, although I found them while running the script

}

public static function cleanTarget(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Intl\Transliterator;

use Symfony\Component\Intl\Util\GzipStreamWrapper;

if (!class_exists(\Transliterator::class)) {
throw new \LogicException(sprintf('You cannot use the "%s\EmojiTransliterator" class as the "intl" extension is not installed. See https://php.net/intl.', __NAMESPACE__));
} else {
Expand Down Expand Up @@ -40,7 +42,8 @@ public static function create(string $id, int $direction = self::FORWARD): self
$id = self::REVERSEABLE_IDS[$id];
}

if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file(\dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php")) {
$file = \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/', $id) || !is_file($file) && !is_file($file .= '.gz')) {
\Transliterator::create($id); // Populate intl_get_error_*()

throw new \IntlException(intl_get_error_message(), intl_get_error_code());
Expand All @@ -57,7 +60,7 @@ public static function create(string $id, int $direction = self::FORWARD): self
$instance = unserialize(sprintf('O:%d:"%s":1:{s:2:"id";s:%d:"%s";}', \strlen(self::class), self::class, \strlen($id), $id));
}

$instance->map = $maps[$id] ??= require \dirname(__DIR__)."/Resources/data/transliterator/emoji/{$id}.php";
$instance->map = $maps[$id] ??= str_ends_with($file, '.gz') ? GzipStreamWrapper::require($file) : require $file;

return $instance;
}
Expand Down Expand Up @@ -86,7 +89,9 @@ public static function listIDs(): array
}

foreach (scandir(\dirname(__DIR__).'/Resources/data/transliterator/emoji/') as $file) {
if (str_ends_with($file, '.php')) {
if (str_ends_with($file, '.php.gz')) {
$ids[] = substr($file, 0, -7);
} elseif (str_ends_with($file, '.php')) {
$ids[] = substr($file, 0, -4);
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/Symfony/Component/Intl/Util/GzipStreamWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl\Util;

/**
* @internal
*/
class GzipStreamWrapper
{
/** @var resource|null */
public $context;

/** @var resource */
private $handle;
private string $path;

public static function require(string $path): array
{
if (!\extension_loaded('zlib')) {
throw new \LogicException(sprintf('The "zlib" extension is required to load the "%s/%s" map, please enable it in your php.ini file.', basename(\dirname($path)), basename($path)));
}

if (!\function_exists('opcache_is_script_cached') || !@opcache_is_script_cached($path)) {
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class);
}

return require $path;
}

public function stream_open(string $path, string $mode): bool
{
stream_wrapper_restore('file');
$this->path = $path;

return false !== $this->handle = fopen('compress.zlib://'.$path, $mode);
}

public function stream_read(int $count): string|false
{
return fread($this->handle, $count);
}

public function stream_eof(): bool
{
return feof($this->handle);
}

public function stream_set_option(int $option, int $arg1, int $arg2): bool
{
return match ($option) {
\STREAM_OPTION_BLOCKING => stream_set_blocking($this->handle, $arg1),
\STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->handle, $arg1, $arg2),
\STREAM_OPTION_WRITE_BUFFER => 0 === stream_set_write_buffer($this->handle, $arg2),
default => false,
};
}

public function stream_stat(): array|false
{
if (!$stat = stat($this->path)) {
return false;
}

$h = fopen($this->path, 'r');
fseek($h, -4, \SEEK_END);
$size = unpack('V', fread($h, 4));
fclose($h);

$stat[7] = $stat['size'] = end($size);

return $stat;
}
}