Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * This hack is intended for clustered sites that do not want
  19   * to use shared cachedir for component cache.
  20   *
  21   * This file needs to be called after any change in PHP files in dataroot,
  22   * that is before upgrade and install.
  23   *
  24   * @package   core
  25   * @copyright 2013 Petr Skoda (skodak)  {@link http://skodak.org}
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  
  30  define('CLI_SCRIPT', true);
  31  define('ABORT_AFTER_CONFIG', true); // We need just the values from config.php.
  32  define('CACHE_DISABLE_ALL', true); // This prevents reading of existing caches.
  33  define('IGNORE_COMPONENT_CACHE', true);
  34  
  35  require(__DIR__.'/../../config.php');
  36  require_once($CFG->libdir.'/clilib.php');
  37  
  38  // Now get cli options.
  39  list($options, $unrecognized) = cli_get_params(
  40      array(
  41          'file'    => false,
  42          'rebuild' => false,
  43          'print'   => false,
  44          'help'    => false
  45      ),
  46      array(
  47          'h' => 'help'
  48      )
  49  );
  50  
  51  if ($unrecognized) {
  52      $unrecognized = implode("\n  ", $unrecognized);
  53      cli_error(get_string('cliunknowoption', 'admin', $unrecognized), 2);
  54  }
  55  
  56  if (!$options['rebuild'] and !$options['file'] and !$options['print']) {
  57      $help =
  58  "Create alternative component cache file
  59  
  60  Options:
  61  -h, --help            Print out this help
  62  --rebuild             Rebuild \$CFG->alternative_component_cache file
  63  --file=filepath       Save component cache to file
  64  --print               Print component cache file content
  65  
  66  Example:
  67  \$ php admin/cli/rebuild_alternative_component_cache.php --rebuild
  68  ";
  69  
  70      echo $help;
  71      exit(0);
  72  }
  73  
  74  error_reporting(E_ALL | E_STRICT);
  75  ini_set('display_errors', 1);
  76  
  77  $content = core_component::get_cache_content();
  78  
  79  if ($options['print']) {
  80      echo $content;
  81      exit(0);
  82  }
  83  
  84  if ($options['rebuild']) {
  85      if (empty($CFG->alternative_component_cache)) {
  86          fwrite(STDERR, 'config.php does not contain $CFG->alternative_component_cache setting');
  87          fwrite(STDERR, "\n");
  88          exit(2);
  89      }
  90      $target = $CFG->alternative_component_cache;
  91  } else {
  92      $target = $options['file'];
  93  }
  94  
  95  if (!$target) {
  96      fwrite(STDERR, "Invalid target file $target");
  97      fwrite(STDERR, "\n");
  98      exit(1);
  99  }
 100  
 101  $bytes = file_put_contents($target, $content);
 102  
 103  if (!$bytes) {
 104      fwrite(STDERR, "Error writing to $target");
 105      fwrite(STDERR, "\n");
 106      exit(1);
 107  }
 108  
 109  // Success.
 110  echo "File $target was updated\n";
 111  exit(0);