MDL-71116 core_badges: Backpack URLs with more than 50 chars
[moodle.git] / lib / emoji-data / generate_emoji_data.php
blob61d71fed6643a36c11b2ae4a92e6e87bd022b607
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 & Emotion',
28 'People & Body',
29 'Animals & Nature',
30 'Food & Drink',
31 'Travel & Places',
32 'Activities',
33 'Objects',
34 'Symbols',
35 'Flags'
38 // Source: https://github.com/iamcal/emoji-data
39 $rawdata = file_get_contents('./emoji_pretty.json');
40 $jsondata = json_decode($rawdata, true);
41 $emojibycategory = [];
42 $obsoletes = [];
43 // Emoji categories used in the emoji-data library.
44 $categories = [];
46 foreach ($jsondata as $data) {
47 $category = $data['category'];
48 $unified = $data['unified'];
50 if ($category === 'Skin Tones') {
51 continue;
54 if (!in_array($category, $categories)) {
55 $categories[] = $category;
58 if (!empty($data['obsoleted_by'])) {
59 // Skip any obsolete emojis. We'll merge these short names into the
60 // newer emoji later on.
61 $obsoletes[] = [
62 'shortname' => $data['short_name'],
63 'by' => $data['obsoleted_by']
65 continue;
68 if (!isset($emojibycategory[$category])) {
69 $emojibycategory[$category] = [
70 'name' => $category,
71 'emojis' => []
75 $emojibycategory[$category]['emojis'][] = [
76 'sortorder' => (int) $data['sort_order'],
77 'unified' => $unified,
78 'shortnames' => [$data['short_name']]
81 // Detect any category changes.
82 // Some emoji categories from the emoji-data library are missing.
83 if ($missingcategories = array_diff($categories, $categorysortorder)) {
84 die("The following categories are missing: " . implode(', ', $missingcategories) .
85 ". For more details on how to properly fix this issue, please see /lib/emoji-data/readme_moodle.txt");
87 // Some emoji categories are not being used anymore in the emoji-data library.
88 if ($unusedcategories = array_diff($categorysortorder, $categories)) {
89 die("The following categories are no longer used: " . implode(', ', $unusedcategories) .
90 ". For more details on how to properly fix this issue, please see /lib/emoji-data/readme_moodle.txt");
93 $emojibycategory = array_values($emojibycategory);
94 // Sort the emojis within each category into the order specified in the raw data.
95 $emojibycategory = array_map(function($category) {
96 usort($category['emojis'], function($a, $b) {
97 return $a['sortorder'] <=> $b['sortorder'];
98 });
99 return $category;
100 }, $emojibycategory);
102 // Add the short names for the obsoleted emojis into the list of short names
103 // of the newer emoji.
104 foreach ($obsoletes as $obsolete) {
105 $emojibycategory = array_map(function($category) use ($obsolete) {
106 $category['emojis'] = array_map(function($emoji) use ($obsolete) {
107 if ($obsolete['by'] == $emoji['unified']) {
108 $emoji['shortnames'] = array_merge($emoji['shortnames'], [$obsolete['shortname']]);
110 unset($emoji['sortorder']);
111 return $emoji;
112 }, $category['emojis']);
113 return $category;
114 }, $emojibycategory);
116 // Sort the emoji categories into the correct order.
117 usort($emojibycategory, function($a, $b) use ($categorysortorder) {
118 $aindex = array_search($a['name'], $categorysortorder);
119 $bindex = array_search($b['name'], $categorysortorder);
120 return $aindex <=> $bindex;
123 $emojibyshortname = array_reduce($jsondata, function($carry, $data) {
124 $unified = null;
125 $shortname = $data['short_name'];
126 if (!empty($data['obsoleted_by'])) {
127 $unified = $data['obsoleted_by'];
128 } else {
129 $unified = $data['unified'];
131 $carry[$shortname] = $unified;
132 return $carry;
133 }, []);
135 $loader = new \Mustache_Loader_ArrayLoader([
136 'data.js' => file_get_contents('./data.js.mustache')
138 $mustache = new \core\output\mustache_engine(['loader' => $loader]);
140 echo $mustache->render('data.js', [
141 'byCategory' => json_encode($emojibycategory, JSON_PRETTY_PRINT),
142 'byShortName' => json_encode($emojibyshortname, JSON_PRETTY_PRINT)