Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
  19   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20   */
  21  
  22  define('CLI_SCRIPT', true);
  23  
  24  require(__DIR__.'/../../config.php');
  25  
  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'
  36  ];
  37  
  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 = [];
  45  
  46  foreach ($jsondata as $data) {
  47      $category = $data['category'];
  48      $unified = $data['unified'];
  49  
  50      if ($category === 'Component') {
  51          continue;
  52      }
  53  
  54      if (!in_array($category, $categories)) {
  55          $categories[] = $category;
  56      }
  57  
  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']
  64          ];
  65          continue;
  66      }
  67  
  68      if (!isset($emojibycategory[$category])) {
  69          $emojibycategory[$category] = [
  70              'name' => $category,
  71              'emojis' => []
  72          ];
  73      }
  74  
  75      $emojibycategory[$category]['emojis'][] = [
  76          'sortorder' => (int) $data['sort_order'],
  77          'unified' => $unified,
  78          'shortnames' => [$data['short_name']]
  79      ];
  80  }
  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");
  86  }
  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");
  91  }
  92  
  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);
 101  
 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']]);
 109              }
 110              unset($emoji['sortorder']);
 111              return $emoji;
 112          }, $category['emojis']);
 113          return $category;
 114      }, $emojibycategory);
 115  }
 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;
 121  });
 122  
 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'];
 130      }
 131      $carry[$shortname] = $unified;
 132      return $carry;
 133  }, []);
 134  
 135  $loader = new \Mustache_Loader_ArrayLoader([
 136      'data.js' => file_get_contents('./data.js.mustache')
 137  ]);
 138  $mustache = new \core\output\mustache_engine(['loader' => $loader]);
 139  
 140  echo $mustache->render('data.js', [
 141      'byCategory' => json_encode($emojibycategory, JSON_PRETTY_PRINT),
 142      'byShortName' => json_encode($emojibyshortname, JSON_PRETTY_PRINT)
 143  ]);