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.
   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   * Build and store theme CSS.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__.'/../../config.php');
  29  require_once("$CFG->libdir/clilib.php");
  30  require_once("$CFG->libdir/csslib.php");
  31  require_once("$CFG->libdir/outputlib.php");
  32  
  33  $longparams = [
  34      'themes'    => null,
  35      'direction' => null,
  36      'help'      => false,
  37      'verbose'   => false
  38  ];
  39  
  40  $shortmappings = [
  41      't' => 'themes',
  42      'd' => 'direction',
  43      'h' => 'help',
  44      'v' => 'verbose'
  45  ];
  46  
  47  // Get CLI params.
  48  list($options, $unrecognized) = cli_get_params($longparams, $shortmappings);
  49  
  50  if ($unrecognized) {
  51      $unrecognized = implode("\n  ", $unrecognized);
  52      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  53  }
  54  
  55  if ($options['help']) {
  56      echo
  57  "Compile the CSS for one or more installed themes.
  58  Existing CSS caches will replaced.
  59  By default all themes will be recompiled unless otherwise specified.
  60  
  61  Options:
  62  -t, --themes    A comma separated list of themes to be compiled
  63  -d, --direction Only compile a single direction (either ltr or rtl)
  64  -v, --verbose   Print info comments to stdout
  65  -h, --help      Print out this help
  66  
  67  Example:
  68  \$ sudo -u www-data /usr/bin/php admin/cli/build_theme_css.php --themes=boost --direction=ltr
  69  ";
  70  
  71      die;
  72  }
  73  
  74  if (empty($options['verbose'])) {
  75      $trace = new null_progress_trace();
  76  } else {
  77      $trace = new text_progress_trace();
  78  }
  79  
  80  cli_heading('Build theme css');
  81  
  82  // Determine which themes we need to build.
  83  $themenames = [];
  84  if (is_null($options['themes'])) {
  85      $trace->output('No themes specified. Finding all installed themes.');
  86      $themenames = array_keys(core_component::get_plugin_list('theme'));
  87  } else {
  88      if (is_string($options['themes'])) {
  89          $themenames = explode(',', $options['themes']);
  90      } else {
  91          cli_error('--themes must be a comma separated list of theme names');
  92      }
  93  }
  94  
  95  $trace->output('Checking that each theme is correctly installed...');
  96  $themeconfigs = [];
  97  foreach ($themenames as $themename) {
  98      if (is_null(theme_get_config_file_path($themename))) {
  99          cli_error("Unable to find theme config for {$themename}");
 100      }
 101  
 102      // Load the config for the theme.
 103      $themeconfigs[] = theme_config::load($themename);
 104  }
 105  
 106  $directions = ['ltr', 'rtl'];
 107  
 108  if (!is_null($options['direction'])) {
 109      if (!in_array($options['direction'], $directions)) {
 110           cli_error("--direction must be either ltr or rtl");
 111      }
 112  
 113      $directions = [$options['direction']];
 114  }
 115  
 116  $trace->output('Building CSS for themes: ' . implode(', ', $themenames));
 117  theme_build_css_for_themes($themeconfigs, $directions);
 118  
 119  exit(0);