Automatically generated installer lang files
[moodle.git] / lib / emoji-data / generate_emoji_data.php
blobbc7f85df1901920c099229cadd2d61b50e804974
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 define('CLI_SCRIPT', true);
24 require(__DIR__.'/../../config.php');
26 $categorysortorder = [
27 'Smileys & People',
28 'Animals & Nature',
29 'Food & Drink',
30 'Travel & Places',
31 'Activities',
32 'Objects',
33 'Symbols',
34 'Flags'
37 // Source: https://github.com/iamcal/emoji-data
38 $rawdata = file_get_contents('./emoji_pretty.json');
39 $jsondata = json_decode($rawdata, true);
40 $emojibycategory = [];
41 $obsoletes = [];
43 foreach ($jsondata as $data) {
44 $category = $data['category'];
45 $unified = $data['unified'];
47 if ($category === 'Skin Tones') {
48 continue;
51 if (!empty($data['obsoleted_by'])) {
52 // Skip any obsolete emojis. We'll merge these short names into the
53 // newer emoji later on.
54 $obsoletes[] = [
55 'shortname' => $data['short_name'],
56 'by' => $data['obsoleted_by']
58 continue;
61 if (!isset($emojibycategory[$category])) {
62 $emojibycategory[$category] = [
63 'name' => $category,
64 'emojis' => []
68 $emojibycategory[$category]['emojis'][] = [
69 'sortorder' => (int) $data['sort_order'],
70 'unified' => $unified,
71 'shortnames' => [$data['short_name']]
75 $emojibycategory = array_values($emojibycategory);
76 // Sort the emojis within each category into the order specified in the raw data.
77 $emojibycategory = array_map(function($category) {
78 usort($category['emojis'], function($a, $b) {
79 return $a['sortorder'] <=> $b['sortorder'];
80 });
81 return $category;
82 }, $emojibycategory);
84 // Add the short names for the obsoleted emojis into the list of short names
85 // of the newer emoji.
86 foreach ($obsoletes as $obsolete) {
87 $emojibycategory = array_map(function($category) use ($obsolete) {
88 $category['emojis'] = array_map(function($emoji) use ($obsolete) {
89 if ($obsolete['by'] == $emoji['unified']) {
90 $emoji['shortnames'] = array_merge($emoji['shortnames'], [$obsolete['shortname']]);
92 unset($emoji['sortorder']);
93 return $emoji;
94 }, $category['emojis']);
95 return $category;
96 }, $emojibycategory);
98 // Sort the emoji categories into the correct order.
99 usort($emojibycategory, function($a, $b) use ($categorysortorder) {
100 $aindex = array_search($a['name'], $categorysortorder);
101 $bindex = array_search($b['name'], $categorysortorder);
102 return $aindex <=> $bindex;
105 $emojibyshortname = array_reduce($jsondata, function($carry, $data) {
106 $unified = null;
107 $shortname = $data['short_name'];
108 if (!empty($data['obsoleted_by'])) {
109 $unified = $data['obsoleted_by'];
110 } else {
111 $unified = $data['unified'];
113 $carry[$shortname] = $unified;
114 return $carry;
115 }, []);
117 $loader = new \Mustache_Loader_ArrayLoader([
118 'data.js' => file_get_contents('./data.js.mustache')
120 $mustache = new \core\output\mustache_engine(['loader' => $loader]);
122 echo $mustache->render('data.js', [
123 'byCategory' => json_encode($emojibycategory, JSON_PRETTY_PRINT),
124 'byShortName' => json_encode($emojibyshortname, JSON_PRETTY_PRINT)