aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/cache.php
blob: 329aab7a968a1ce98ce8c2b55d588233ff4b575e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
 * cache.php
 * Controller for managing system cache.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Thomas Hackl <studip@thomas-hackl.name>
 * @copyright   2020 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       5.0
 */

class Admin_CacheController extends AuthenticatedController
{

    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        // Check permissions to be on this site
        if (!$GLOBALS['perm']->have_perm('root')) {
            throw new AccessDeniedException();
        }
        PageLayout::setTitle(_('Cache'));
        Navigation::activateItem('/admin/config/cache');

        $this->enabled = $GLOBALS['CACHING_ENABLE'];

        $this->sidebar = Sidebar::get();
        $views = new ViewsWidget();
        $views->addLink(
            _('Verwaltung'),
            $this->url_for('admin/cache/settings')
        )->setActive($action === 'settings');

        if ($this->enabled) {
            $views->addLink(
                _('Statistiken'),
                $this->url_for('admin/cache/stats')
            )->setActive($action === 'stats');
        }

        $this->sidebar->addWidget($views);

        if ($this->enabled) {
            $actions = new ActionsWidget();
            $actions->addLink(
                _('Cache leeren'),
                $this->url_for('admin/cache/flush'),
                Icon::create('decline'),
                ['data-confirm' => _('Soll der gesamte Inhalt des Caches wirklich gelöscht werden?')]
            );
            $this->sidebar->addWidget($actions);
        }
    }

    /**
     * Show all available cache types.
     */
    public function settings_action()
    {
        if ($this->enabled) {
            $this->types = CacheType::findAndMapBySQL(function (CacheType $type) {
                return $type->toArray();
            }, "1 ORDER BY `cache_id`");

            $currentCache = Config::get()->SYSTEMCACHE;
            $currentCacheClass = CacheType::findOneByClass_name($currentCache['type']);
            $this->cache = $currentCacheClass->class_name;
            $this->config = $currentCacheClass->class_name::getConfig();
        } else {
            PageLayout::postWarning(
                _('Caching ist systemweit ausgeschaltet, daher kann hier nichts konfiguriert werden.'));
        }
    }

    /**
     * Fetches necessary configuration for given cache type.
     *
     * @param string $className
     */
    public function get_config_action($className)
    {
        $type = CacheType::findOneByClass_name($className);

        $this->render_json($type->class_name::getConfig());
    }

    /**
     * Stores cache settings to global config.
     */
    public function store_settings_action()
    {
        // Take the whole Request object as array ...
        $request = Request::getInstance()->getIterator()->getArrayCopy();

        // ... remove cachetype entry as this is saved separately ...
        unset($request['cachetype']);

        // ... and use the rest of the request as cache config.
        $settings = [
            'type' => Request::get('cachetype'),
            'config' => $request
        ];

        // Store settings to global config.
        if (Config::get()->store('SYSTEMCACHE', $settings)) {
            PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));
            StudipCacheFactory::unconfigure();
        }

        $this->relocate('admin/cache/settings');
    }

    /**
     * Flush all cache content.
     */
    public function flush_action()
    {
        $cache = StudipCacheFactory::getCache();
        $cache->flush();

        PageLayout::postSuccess(_('Die Inhalte des Caches wurden gelöscht.'));

        $this->relocate('admin/cache/settings');
    }

    /**
     * Show cache statistics.
     */
    public function stats_action()
    {
        $cache = StudipCacheFactory::getCache();

        $this->stats = $cache->getStats();
    }
}