~ubuntu-branches/ubuntu/trusty/moodle/trusty

« back to all changes in this revision

Viewing changes to cache/classes/helper.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-01-21 13:40:52 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20140121134052-ym2qvsp2cd9vq0p6
Tags: 2.5.4-1
* New upstream release, fixing security issues:
  - MSA-14-0001 Config passwords visibility issue [CVE-2014-0008]
  - MSA-14-0002 Group constraints lacking in "login as" [CVE-2014-0009]
  - MSA-14-0003 CSRF vulnerability in profile fields [CVE-2014-0010]
* Move /var/lib/moodle directory into package.
* Revert back to bundled yui3. Unfortunately, version in Debian and
  of upstream are not compatible (closes: #735312).

Show diffs side-by-side

added added

removed removed

Lines of Context:
143
143
     *
144
144
     * @param array $stores
145
145
     * @param cache_definition $definition
146
 
     * @return array
 
146
     * @return cache_store[]
147
147
     */
148
148
    protected static function initialise_cachestore_instances(array $stores, cache_definition $definition) {
149
149
        $return = array();
382
382
    /**
383
383
     * Record a cache hit in the stats for the given store and definition.
384
384
     *
 
385
     * @internal
385
386
     * @param string $store
386
387
     * @param string $definition
 
388
     * @param int $hits The number of hits to record (by default 1)
387
389
     */
388
 
    public static function record_cache_hit($store, $definition) {
 
390
    public static function record_cache_hit($store, $definition, $hits = 1) {
389
391
        self::ensure_ready_for_stats($store, $definition);
390
 
        self::$stats[$definition][$store]['hits']++;
 
392
        self::$stats[$definition][$store]['hits'] += $hits;
391
393
    }
392
394
 
393
395
    /**
394
396
     * Record a cache miss in the stats for the given store and definition.
395
397
     *
 
398
     * @internal
396
399
     * @param string $store
397
400
     * @param string $definition
 
401
     * @param int $misses The number of misses to record (by default 1)
398
402
     */
399
 
    public static function record_cache_miss($store, $definition) {
 
403
    public static function record_cache_miss($store, $definition, $misses = 1) {
400
404
        self::ensure_ready_for_stats($store, $definition);
401
 
        self::$stats[$definition][$store]['misses']++;
 
405
        self::$stats[$definition][$store]['misses'] += $misses;
402
406
    }
403
407
 
404
408
    /**
405
409
     * Record a cache set in the stats for the given store and definition.
406
410
     *
 
411
     * @internal
407
412
     * @param string $store
408
413
     * @param string $definition
 
414
     * @param int $sets The number of sets to record (by default 1)
409
415
     */
410
 
    public static function record_cache_set($store, $definition) {
 
416
    public static function record_cache_set($store, $definition, $sets = 1) {
411
417
        self::ensure_ready_for_stats($store, $definition);
412
 
        self::$stats[$definition][$store]['sets']++;
 
418
        self::$stats[$definition][$store]['sets'] += $sets;
413
419
    }
414
420
 
415
421
    /**
462
468
        $class = $store['class'];
463
469
 
464
470
        // Found the store: is it ready?
 
471
        /* @var cache_store $instance */
465
472
        $instance = new $class($store['name'], $store['configuration']);
466
 
        if (!$instance->is_ready()) {
 
473
        if (!$instance::are_requirements_met() || !$instance->is_ready()) {
467
474
            unset($instance);
468
475
            return false;
469
476
        }
672
679
            }
673
680
        }
674
681
    }
 
682
 
 
683
    /**
 
684
     * Returns an array of stores that would meet the requirements for every definition.
 
685
     *
 
686
     * These stores would be 100% suitable to map as defaults for cache modes.
 
687
     *
 
688
     * @return array[] An array of stores, keys are the store names.
 
689
     */
 
690
    public static function get_stores_suitable_for_mode_default() {
 
691
        $factory = cache_factory::instance();
 
692
        $config = $factory->create_config_instance();
 
693
        $requirements = 0;
 
694
        foreach ($config->get_definitions() as $definition) {
 
695
            $definition = cache_definition::load($definition['component'].'/'.$definition['area'], $definition);
 
696
            $requirements = $requirements | $definition->get_requirements_bin();
 
697
        }
 
698
        $stores = array();
 
699
        foreach ($config->get_all_stores() as $name => $store) {
 
700
            if (!empty($store['features']) && ($store['features'] & $requirements)) {
 
701
                $stores[$name] = $store;
 
702
            }
 
703
        }
 
704
        return $stores;
 
705
    }
 
706
 
 
707
    /**
 
708
     * Returns stores suitable for use with a given definition.
 
709
     *
 
710
     * @param cache_definition $definition
 
711
     * @return cache_store[]
 
712
     */
 
713
    public static function get_stores_suitable_for_definition(cache_definition $definition) {
 
714
        $factory = cache_factory::instance();
 
715
        $stores = array();
 
716
        if ($factory->is_initialising() || $factory->stores_disabled()) {
 
717
            // No suitable stores here.
 
718
            return $stores;
 
719
        } else {
 
720
            $stores = self::get_cache_stores($definition);
 
721
            if (count($stores) === 0) {
 
722
                // No suitable stores we found for the definition. We need to come up with a sensible default.
 
723
                // If this has happened we can be sure that the user has mapped custom stores to either the
 
724
                // mode of the definition. The first alternative to try is the system default for the mode.
 
725
                // e.g. the default file store instance for application definitions.
 
726
                $config = $factory->create_config_instance();
 
727
                foreach ($config->get_stores($definition->get_mode()) as $name => $details) {
 
728
                    if (!empty($details['default'])) {
 
729
                        $stores[] = $factory->create_store_from_config($name, $details, $definition);
 
730
                        break;
 
731
                    }
 
732
                }
 
733
            }
 
734
        }
 
735
        return $stores;
 
736
    }
675
737
}