Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Cache configuration reader
-
 
19
 *
-
 
20
 * This file is part of Moodle's cache API, affectionately called MUC.
-
 
21
 * It contains the components that are requried in order to use caching.
-
 
22
 *
-
 
23
 * @package    core
16
 
24
 * @category   cache
-
 
25
 * @copyright  2012 Sam Hemelryk
-
 
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
Línea 27... Línea 17...
27
 */
17
namespace core_cache;
Línea 28... Línea 18...
28
 
18
 
29
defined('MOODLE_INTERNAL') || die();
19
use core_cache\exception\cache_exception;
30
 
20
 
31
/**
21
/**
32
 * Cache configuration reader.
22
 * Cache configuration reader.
33
 *
23
 *
34
 * This class is used to interact with the cache's configuration.
24
 * This class is used to interact with the cache's configuration.
35
 * The configuration is stored in the Moodle data directory.
25
 * The configuration is stored in the Moodle data directory.
36
 *
26
 *
37
 * @package    core
27
 * @package    core_cache
38
 * @category   cache
28
 * @category   cache
39
 * @copyright  2012 Sam Hemelryk
29
 * @copyright  2012 Sam Hemelryk
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
41
 */
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
class cache_config {
31
 */
43
 
32
class config {
44
    /**
33
    /**
45
     * The configured stores
34
     * The configured stores
Línea 46... Línea 35...
46
     * @var array
35
     * @var array
47
     */
36
     */
48
    protected $configstores = array();
37
    protected $configstores = [];
49
 
38
 
50
    /**
39
    /**
Línea 51... Línea 40...
51
     * The configured mode mappings
40
     * The configured mode mappings
52
     * @var array
41
     * @var array
53
     */
42
     */
54
    protected $configmodemappings = array();
43
    protected $configmodemappings = [];
55
 
44
 
Línea 56... Línea 45...
56
    /**
45
    /**
57
     * The configured definitions as picked up from cache.php files
46
     * The configured definitions as picked up from cache.php files
58
     * @var array
47
     * @var array
59
     */
48
     */
60
    protected $configdefinitions = array();
49
    protected $configdefinitions = [];
Línea 61... Línea 50...
61
 
50
 
62
    /**
51
    /**
63
     * The definition mappings that have been configured.
52
     * The definition mappings that have been configured.
64
     * @var array
53
     * @var array
65
     */
54
     */
Línea 66... Línea 55...
66
    protected $configdefinitionmappings = array();
55
    protected $configdefinitionmappings = [];
67
 
56
 
68
    /**
57
    /**
69
     * An array of configured cache lock instances.
58
     * An array of configured cache lock instances.
70
     * @var array
59
     * @var array
Línea 71... Línea 60...
71
     */
60
     */
72
    protected $configlocks = array();
61
    protected $configlocks = [];
73
 
62
 
74
    /**
63
    /**
75
     * The site identifier used when the cache config was last saved.
64
     * The site identifier used when the cache config was last saved.
76
     * @var string
65
     * @var string
Línea 77... Línea 66...
77
     */
66
     */
78
    protected $siteidentifier = null;
67
    protected $siteidentifier = null;
79
 
68
 
80
    /**
69
    /**
81
     * Please use cache_config::instance to get an instance of the cache config that is ready to be used.
70
     * Please use config::instance to get an instance of the cache config that is ready to be used.
82
     */
71
     */
83
    public function __construct() {
72
    public function __construct() {
84
        // Nothing to do here but look pretty.
73
        // Nothing to do here but look pretty.
85
    }
74
    }
Línea 86... Línea 75...
86
 
75
 
87
    /**
76
    /**
Línea 113... Línea 102...
113
        global $CFG;
102
        global $CFG;
114
        if (!empty($CFG->altcacheconfigpath)) {
103
        if (!empty($CFG->altcacheconfigpath)) {
115
            $path = $CFG->altcacheconfigpath;
104
            $path = $CFG->altcacheconfigpath;
116
            if (is_dir($path) && is_writable($path)) {
105
            if (is_dir($path) && is_writable($path)) {
117
                // Its a writable directory, thats fine.
106
                // Its a writable directory, thats fine.
118
                return $path.'/cacheconfig.php';
107
                return $path . '/cacheconfig.php';
119
            } else if (is_writable(dirname($path)) && (!file_exists($path) || is_writable($path))) {
108
            } else if (is_writable(dirname($path)) && (!file_exists($path) || is_writable($path))) {
120
                // Its a file, either it doesn't exist and the directory is writable or the file exists and is writable.
109
                // Its a file, either it doesn't exist and the directory is writable or the file exists and is writable.
121
                return $path;
110
                return $path;
122
            }
111
            }
123
        }
112
        }
124
        // Return the default location within dataroot.
113
        // Return the default location within dataroot.
125
        return $CFG->dataroot.'/muc/config.php';
114
        return $CFG->dataroot . '/muc/config.php';
126
    }
115
    }
Línea 127... Línea 116...
127
 
116
 
128
    /**
117
    /**
129
     * Loads the configuration file and parses its contents into the expected structure.
118
     * Loads the configuration file and parses its contents into the expected structure.
Línea 136... Línea 125...
136
 
125
 
137
        if ($configuration === false) {
126
        if ($configuration === false) {
138
            $configuration = $this->include_configuration();
127
            $configuration = $this->include_configuration();
Línea 139... Línea 128...
139
        }
128
        }
140
 
129
 
141
        $this->configstores = array();
130
        $this->configstores = [];
142
        $this->configdefinitions = array();
131
        $this->configdefinitions = [];
143
        $this->configlocks = array();
132
        $this->configlocks = [];
Línea 144... Línea 133...
144
        $this->configmodemappings = array();
133
        $this->configmodemappings = [];
145
        $this->configdefinitionmappings = array();
134
        $this->configdefinitionmappings = [];
146
 
135
 
147
        $siteidentifier = 'unknown';
136
        $siteidentifier = 'unknown';
Línea 172... Línea 161...
172
            }
161
            }
173
            $this->configlocks[$name] = $conf;
162
            $this->configlocks[$name] = $conf;
174
        }
163
        }
Línea 175... Línea 164...
175
 
164
 
176
        // Filter the stores.
165
        // Filter the stores.
177
        $availableplugins = cache_helper::early_get_cache_plugins();
166
        $availableplugins = helper::early_get_cache_plugins();
178
        foreach ($configuration['stores'] as $store) {
167
        foreach ($configuration['stores'] as $store) {
179
            if (!is_array($store) || !array_key_exists('name', $store) || !array_key_exists('plugin', $store)) {
168
            if (!is_array($store) || !array_key_exists('name', $store) || !array_key_exists('plugin', $store)) {
180
                // Not a valid instance configuration.
169
                // Not a valid instance configuration.
181
                debugging('Invalid cache store in config. Missing name or plugin.', DEBUG_DEVELOPER);
170
                debugging('Invalid cache store in config. Missing name or plugin.', DEBUG_DEVELOPER);
182
                continue;
171
                continue;
183
            }
172
            }
184
            $plugin = $store['plugin'];
173
            $plugin = $store['plugin'];
185
            $class = 'cachestore_'.$plugin;
174
            $class = 'cachestore_' . $plugin;
186
            $exists = array_key_exists($plugin, $availableplugins);
175
            $exists = array_key_exists($plugin, $availableplugins);
187
            if (!$exists) {
176
            if (!$exists) {
188
                // Not a valid plugin, or has been uninstalled, just skip it an carry on.
177
                // Not a valid plugin, or has been uninstalled, just skip it an carry on.
189
                debugging('Invalid cache store in config. Not an available plugin.', DEBUG_DEVELOPER);
178
                debugging('Invalid cache store in config. Not an available plugin.', DEBUG_DEVELOPER);
190
                continue;
179
                continue;
191
            }
180
            }
192
            $file = $CFG->dirroot.'/cache/stores/'.$plugin.'/lib.php';
181
            $file = $CFG->dirroot . '/cache/stores/' . $plugin . '/lib.php';
193
            if (!class_exists($class) && file_exists($file)) {
182
            if (!class_exists($class) && file_exists($file)) {
194
                require_once($file);
183
                require_once($file);
195
            }
184
            }
196
            if (!class_exists($class)) {
185
            if (!class_exists($class)) {
197
                continue;
186
                continue;
198
            }
187
            }
199
            if (!array_key_exists('cache_store', class_parents($class))) {
188
            if (!array_key_exists(store::class, class_parents($class))) {
200
                continue;
189
                continue;
201
            }
190
            }
202
            if (!array_key_exists('configuration', $store) || !is_array($store['configuration'])) {
191
            if (!array_key_exists('configuration', $store) || !is_array($store['configuration'])) {
203
                $store['configuration'] = array();
192
                $store['configuration'] = [];
204
            }
193
            }
205
            $store['class'] = $class;
194
            $store['class'] = $class;
206
            $store['default'] = !empty($store['default']);
195
            $store['default'] = !empty($store['default']);
207
            if (!array_key_exists('lock', $store) || !array_key_exists($store['lock'], $this->configlocks)) {
196
            if (!array_key_exists('lock', $store) || !array_key_exists($store['lock'], $this->configlocks)) {
Línea 224... Línea 213...
224
            if (array_key_exists($id, $this->configdefinitions)) {
213
            if (array_key_exists($id, $this->configdefinitions)) {
225
                debugging('Duplicate cache definition detected. This should never happen.', DEBUG_DEVELOPER);
214
                debugging('Duplicate cache definition detected. This should never happen.', DEBUG_DEVELOPER);
226
                continue;
215
                continue;
227
            }
216
            }
228
            $conf['mode'] = (int)$conf['mode'];
217
            $conf['mode'] = (int)$conf['mode'];
229
            if ($conf['mode'] < cache_store::MODE_APPLICATION || $conf['mode'] > cache_store::MODE_REQUEST) {
218
            if ($conf['mode'] < store::MODE_APPLICATION || $conf['mode'] > store::MODE_REQUEST) {
230
                // Invalid cache mode used for the definition.
219
                // Invalid cache mode used for the definition.
231
                continue;
220
                continue;
232
            }
221
            }
233
            if ($conf['mode'] === cache_store::MODE_SESSION || $conf['mode'] === cache_store::MODE_REQUEST) {
222
            if ($conf['mode'] === store::MODE_SESSION || $conf['mode'] === store::MODE_REQUEST) {
234
                // We force this for session and request caches.
223
                // We force this for session and request caches.
235
                // They are only allowed to use the default as we don't want people changing them.
224
                // They are only allowed to use the default as we don't want people changing them.
236
                $conf['sharingoptions'] = cache_definition::SHARING_DEFAULT;
225
                $conf['sharingoptions'] = definition::SHARING_DEFAULT;
237
                $conf['selectedsharingoption'] = cache_definition::SHARING_DEFAULT;
226
                $conf['selectedsharingoption'] = definition::SHARING_DEFAULT;
238
                $conf['userinputsharingkey'] = '';
227
                $conf['userinputsharingkey'] = '';
239
            } else {
228
            } else {
240
                // Default the sharing option as it was added for 2.5.
229
                // Default the sharing option as it was added for 2.5.
241
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
230
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
242
                if (!isset($conf['sharingoptions'])) {
231
                if (!isset($conf['sharingoptions'])) {
243
                    $conf['sharingoptions'] = cache_definition::SHARING_DEFAULTOPTIONS;
232
                    $conf['sharingoptions'] = definition::SHARING_DEFAULTOPTIONS;
244
                }
233
                }
245
                // Default the selected sharing option as it was added for 2.5.
234
                // Default the selected sharing option as it was added for 2.5.
246
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
235
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
247
                if (!isset($conf['selectedsharingoption'])) {
236
                if (!isset($conf['selectedsharingoption'])) {
248
                    $conf['selectedsharingoption'] = cache_definition::SHARING_DEFAULT;
237
                    $conf['selectedsharingoption'] = definition::SHARING_DEFAULT;
249
                }
238
                }
250
                // Default the user input sharing key as it was added for 2.5.
239
                // Default the user input sharing key as it was added for 2.5.
251
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
240
                // This can be removed sometime after 2.5 is the minimum version someone can upgrade from.
252
                if (!isset($conf['userinputsharingkey'])) {
241
                if (!isset($conf['userinputsharingkey'])) {
253
                    $conf['userinputsharingkey'] = '';
242
                    $conf['userinputsharingkey'] = '';
Línea 297... Línea 286...
297
                $mapping['sort'] = 0;
286
                $mapping['sort'] = 0;
298
            }
287
            }
299
            $this->configdefinitionmappings[] = $mapping;
288
            $this->configdefinitionmappings[] = $mapping;
300
        }
289
        }
Línea 301... Línea 290...
301
 
290
 
302
        usort($this->configmodemappings, array($this, 'sort_mappings'));
291
        usort($this->configmodemappings, [$this, 'sort_mappings']);
Línea 303... Línea 292...
303
        usort($this->configdefinitionmappings, array($this, 'sort_mappings'));
292
        usort($this->configdefinitionmappings, [$this, 'sort_mappings']);
304
 
293
 
Línea 305... Línea 294...
305
        return true;
294
        return true;
Línea 336... Línea 325...
336
 
325
 
337
        if (!is_array($configuration)) {
326
        if (!is_array($configuration)) {
338
            throw new cache_exception('Invalid cache configuration file');
327
            throw new cache_exception('Invalid cache configuration file');
339
        }
328
        }
340
        if (!array_key_exists('stores', $configuration) || !is_array($configuration['stores'])) {
329
        if (!array_key_exists('stores', $configuration) || !is_array($configuration['stores'])) {
341
            $configuration['stores'] = array();
330
            $configuration['stores'] = [];
342
        }
331
        }
343
        if (!array_key_exists('modemappings', $configuration) || !is_array($configuration['modemappings'])) {
332
        if (!array_key_exists('modemappings', $configuration) || !is_array($configuration['modemappings'])) {
344
            $configuration['modemappings'] = array();
333
            $configuration['modemappings'] = [];
345
        }
334
        }
346
        if (!array_key_exists('definitions', $configuration) || !is_array($configuration['definitions'])) {
335
        if (!array_key_exists('definitions', $configuration) || !is_array($configuration['definitions'])) {
347
            $configuration['definitions'] = array();
336
            $configuration['definitions'] = [];
348
        }
337
        }
349
        if (!array_key_exists('definitionmappings', $configuration) || !is_array($configuration['definitionmappings'])) {
338
        if (!array_key_exists('definitionmappings', $configuration) || !is_array($configuration['definitionmappings'])) {
350
            $configuration['definitionmappings'] = array();
339
            $configuration['definitionmappings'] = [];
351
        }
340
        }
352
        if (!array_key_exists('locks', $configuration) || !is_array($configuration['locks'])) {
341
        if (!array_key_exists('locks', $configuration) || !is_array($configuration['locks'])) {
353
            $configuration['locks'] = array();
342
            $configuration['locks'] = [];
Línea 354... Línea 343...
354
        }
343
        }
355
 
344
 
Línea 399... Línea 388...
399
     *
388
     *
400
     * @param string $storename
389
     * @param string $storename
401
     * @return array Associative array of definitions, id=>definition
390
     * @return array Associative array of definitions, id=>definition
402
     */
391
     */
403
    public function get_definitions_by_store($storename) {
392
    public function get_definitions_by_store($storename) {
404
        $definitions = array();
393
        $definitions = [];
Línea 405... Línea 394...
405
 
394
 
406
        // This function was accidentally made static at some stage in the past.
395
        // This function was accidentally made static at some stage in the past.
407
        // It was converted to an instance method but to be backwards compatible
396
        // It was converted to an instance method but to be backwards compatible
408
        // we must step around this in code.
397
        // we must step around this in code.
409
        if (!isset($this)) {
398
        if (!isset($this)) {
410
            $config = cache_config::instance();
399
            $config = self::instance();
411
        } else {
400
        } else {
412
            $config = $this;
401
            $config = $this;
Línea 413... Línea 402...
413
        }
402
        }
Línea 418... Línea 407...
418
            return false;
407
            return false;
419
        }
408
        }
Línea 420... Línea 409...
420
 
409
 
421
        $defmappings = $config->get_definition_mappings();
410
        $defmappings = $config->get_definition_mappings();
422
        // Create an associative array for the definition mappings.
411
        // Create an associative array for the definition mappings.
423
        $thedefmappings = array();
412
        $thedefmappings = [];
424
        foreach ($defmappings as $defmapping) {
413
        foreach ($defmappings as $defmapping) {
425
            $thedefmappings[$defmapping['definition']] = $defmapping;
414
            $thedefmappings[$defmapping['definition']] = $defmapping;
Línea 426... Línea 415...
426
        }
415
        }
427
 
416
 
428
        // Search for matches in default mappings.
417
        // Search for matches in default mappings.
429
        $defs = $config->get_definitions();
418
        $defs = $config->get_definitions();
430
        foreach($config->get_mode_mappings() as $modemapping) {
419
        foreach ($config->get_mode_mappings() as $modemapping) {
431
            if ($modemapping['store'] !== $storename) {
420
            if ($modemapping['store'] !== $storename) {
432
                continue;
421
                continue;
433
            }
422
            }
434
            foreach($defs as $id => $definition) {
423
            foreach ($defs as $id => $definition) {
435
                if ($definition['mode'] !== $modemapping['mode']) {
424
                if ($definition['mode'] !== $modemapping['mode']) {
436
                    continue;
425
                    continue;
437
                }
426
                }
Línea 441... Línea 430...
441
                }
430
                }
442
                $definitions[$id] = $definition;
431
                $definitions[$id] = $definition;
443
            }
432
            }
444
        }
433
        }
Línea 445... Línea 434...
445
 
434
 
446
        // Search for matches in the custom definitions mapping
435
        // Search for matches in the custom definitions mapping.
447
        foreach ($defmappings as $defmapping) {
436
        foreach ($defmappings as $defmapping) {
448
            if ($defmapping['store'] !== $storename) {
437
            if ($defmapping['store'] !== $storename) {
449
                continue;
438
                continue;
450
            }
439
            }
Línea 458... Línea 447...
458
    }
447
    }
Línea 459... Línea 448...
459
 
448
 
460
    /**
449
    /**
461
     * Returns all of the stores that are suitable for the given mode and requirements.
450
     * Returns all of the stores that are suitable for the given mode and requirements.
462
     *
451
     *
463
     * @param int $mode One of cache_store::MODE_*
452
     * @param int $mode One of store::MODE_*
464
     * @param int $requirements The requirements of the cache as a binary flag
453
     * @param int $requirements The requirements of the cache as a binary flag
465
     * @return array An array of suitable stores.
454
     * @return array An array of suitable stores.
466
     */
455
     */
467
    public function get_stores($mode, $requirements = 0) {
456
    public function get_stores($mode, $requirements = 0) {
468
        $stores = array();
457
        $stores = [];
469
        foreach ($this->configstores as $name => $store) {
458
        foreach ($this->configstores as $name => $store) {
470
            // If the mode is supported and all of the requirements are provided features.
459
            // If the mode is supported and all of the requirements are provided features.
471
            if (($store['modes'] & $mode) && ($store['features'] & $requirements) === $requirements) {
460
            if (($store['modes'] & $mode) && ($store['features'] & $requirements) === $requirements) {
472
                $stores[$name] = $store;
461
                $stores[$name] = $store;
Línea 476... Línea 465...
476
    }
465
    }
Línea 477... Línea 466...
477
 
466
 
478
    /**
467
    /**
479
     * Gets all of the stores that are to be used for the given definition.
468
     * Gets all of the stores that are to be used for the given definition.
480
     *
469
     *
481
     * @param cache_definition $definition
470
     * @param definition $definition
482
     * @return array
471
     * @return array<store>
483
     */
472
     */
484
    public function get_stores_for_definition(cache_definition $definition) {
473
    public function get_stores_for_definition(definition $definition) {
485
        // Check if MUC has been disabled.
474
        // Check if MUC has been disabled.
486
        $factory = cache_factory::instance();
475
        $factory = factory::instance();
487
        if ($factory->stores_disabled()) {
476
        if ($factory->stores_disabled()) {
488
            // Yip its been disabled.
477
            // Yip its been disabled.
489
            // To facilitate this we are going to always return an empty array of stores to use.
478
            // To facilitate this we are going to always return an empty array of stores to use.
490
            // This will force all cache instances to use the cachestore_dummy.
479
            // This will force all cache instances to use the cachestore_dummy.
491
            // MUC will still be used essentially so that code using it will still continue to function but because no cache stores
480
            // MUC will still be used essentially so that code using it will still continue to function but because no cache stores
492
            // are being used interaction with MUC will be purely based around a static var.
481
            // are being used interaction with MUC will be purely based around a static var.
493
            return array();
482
            return [];
Línea 494... Línea 483...
494
        }
483
        }
495
 
484
 
496
        $availablestores = $this->get_stores($definition->get_mode(), $definition->get_requirements_bin());
485
        $availablestores = $this->get_stores($definition->get_mode(), $definition->get_requirements_bin());
Línea 497... Línea 486...
497
        $stores = array();
486
        $stores = [];
498
        $id = $definition->get_id();
487
        $id = $definition->get_id();
499
 
488
 
Línea 592... Línea 581...
592
            }
581
            }
593
        }
582
        }
594
        throw new cache_exception('ex_nodefaultlock');
583
        throw new cache_exception('ex_nodefaultlock');
595
    }
584
    }
596
}
585
}
-
 
586
 
-
 
587
// Alias this class to the old name.
-
 
588
// This file will be autoloaded by the legacyclasses autoload system.
-
 
589
// In future all uses of this class will be corrected and the legacy references will be removed.
-
 
590
class_alias(config::class, \cache_config::class);