Merge branch 'MDL-81063-main' of https://github.com/andrewnicols/moodle
[moodle.git] / admin / cli / alternative_component_cache.php
blob6b3d454c997123244054ae90b6b341f175600099
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 * This hack is intended for clustered sites that do not want
19 * to use shared cachedir for component cache.
21 * This file needs to be called after any change in PHP files in dataroot,
22 * that is before upgrade and install.
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
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);
35 require(__DIR__.'/../../config.php');
36 require_once($CFG->libdir.'/clilib.php');
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
46 array(
47 'h' => 'help'
51 if ($unrecognized) {
52 $unrecognized = implode("\n ", $unrecognized);
53 cli_error(get_string('cliunknowoption', 'admin', $unrecognized), 2);
56 if (!$options['rebuild'] and !$options['file'] and !$options['print']) {
57 $help =
58 "Create alternative component cache file
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
66 Example:
67 \$ php admin/cli/rebuild_alternative_component_cache.php --rebuild
70 echo $help;
71 exit(0);
74 error_reporting(E_ALL | E_STRICT);
75 ini_set('display_errors', 1);
77 $content = core_component::get_cache_content();
79 if ($options['print']) {
80 echo $content;
81 exit(0);
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);
90 $target = $CFG->alternative_component_cache;
91 } else {
92 $target = $options['file'];
95 if (!$target) {
96 fwrite(STDERR, "Invalid target file $target");
97 fwrite(STDERR, "\n");
98 exit(1);
101 $bytes = file_put_contents($target, $content);
103 if (!$bytes) {
104 fwrite(STDERR, "Error writing to $target");
105 fwrite(STDERR, "\n");
106 exit(1);
109 // Success.
110 echo "File $target was updated\n";
111 exit(0);