Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
AdminApiController | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
clearCacheAction | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
getApiSpecFragment | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
6 | |||
getDefaultCachesToClear | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** |
4 | * Admin Api Controller |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) The National Library of Finland 2021. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Controller |
25 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | namespace VuFindApi\Controller; |
31 | |
32 | use Laminas\ServiceManager\ServiceLocatorInterface; |
33 | use VuFind\Cache\Manager as CacheManager; |
34 | |
35 | /** |
36 | * Admin Api Controller |
37 | * |
38 | * @category VuFind |
39 | * @package Controller |
40 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org Main Page |
43 | */ |
44 | class AdminApiController extends \VuFind\Controller\AbstractBase implements ApiInterface |
45 | { |
46 | use ApiTrait; |
47 | |
48 | /** |
49 | * Cache manager |
50 | * |
51 | * @var CacheManager |
52 | */ |
53 | protected $cacheManager; |
54 | |
55 | /** |
56 | * Constructor |
57 | * |
58 | * @param ServiceLocatorInterface $sm Service locator |
59 | * @param CacheManager $cm Cache manager |
60 | */ |
61 | public function __construct(ServiceLocatorInterface $sm, CacheManager $cm) |
62 | { |
63 | parent::__construct($sm); |
64 | $this->cacheManager = $cm; |
65 | } |
66 | |
67 | /** |
68 | * Permission required for the clear cache endpoint |
69 | * |
70 | * @var string |
71 | */ |
72 | protected $cacheAccessPermission = 'access.api.admin.cache'; |
73 | |
74 | /** |
75 | * Clear the cache |
76 | * |
77 | * @return \Laminas\Http\Response |
78 | */ |
79 | public function clearCacheAction() |
80 | { |
81 | $this->disableSessionWrites(); |
82 | $this->determineOutputMode(); |
83 | |
84 | if ($result = $this->isAccessDenied($this->cacheAccessPermission)) { |
85 | return $result; |
86 | } |
87 | |
88 | try { |
89 | $cacheList = $this->getRequest()->getQuery()->get('id') |
90 | ?: $this->getDefaultCachesToClear(); |
91 | foreach ((array)$cacheList as $id) { |
92 | $this->cacheManager->getCache($id)->flush(); |
93 | } |
94 | } catch (\Exception $e) { |
95 | return $this->output([], self::STATUS_ERROR, 500, $e->getMessage()); |
96 | } |
97 | |
98 | return $this->output([], self::STATUS_OK); |
99 | } |
100 | |
101 | /** |
102 | * Get API specification JSON fragment for services provided by the |
103 | * controller |
104 | * |
105 | * @return string |
106 | */ |
107 | public function getApiSpecFragment() |
108 | { |
109 | $spec = []; |
110 | if (!$this->isAccessDenied($this->cacheAccessPermission)) { |
111 | $defaultCaches = implode(',', $this->getDefaultCachesToClear()); |
112 | $spec['paths']['/admin/cache']['delete'] = [ |
113 | 'summary' => 'Clear caches', |
114 | 'description' => 'Flushes the specified caches', |
115 | 'parameters' => [ |
116 | [ |
117 | 'name' => 'id[]', |
118 | 'in' => 'query', |
119 | 'description' => 'Caches to clear. By default the following' |
120 | . " caches are cleared: $defaultCaches", |
121 | 'required' => false, |
122 | 'style' => 'form', |
123 | 'explode' => true, |
124 | 'schema' => [ |
125 | 'type' => 'array', |
126 | 'items' => [ |
127 | 'type' => 'string', |
128 | ], |
129 | ], |
130 | ], |
131 | ], |
132 | 'tags' => ['admin'], |
133 | 'responses' => [ |
134 | '200' => [ |
135 | 'description' => 'An OK response', |
136 | 'content' => [ |
137 | 'application/json' => [ |
138 | 'schema' => [ |
139 | '$ref' => '#/components/schemas/Success', |
140 | ], |
141 | ], |
142 | ], |
143 | ], |
144 | 'default' => [ |
145 | 'description' => 'Error', |
146 | 'content' => [ |
147 | 'application/json' => [ |
148 | 'schema' => [ |
149 | '$ref' => '#/components/schemas/Error', |
150 | ], |
151 | ], |
152 | ], |
153 | ], |
154 | ], |
155 | ]; |
156 | } |
157 | |
158 | return json_encode($spec); |
159 | } |
160 | |
161 | /** |
162 | * Get an array of caches to clear by default |
163 | * |
164 | * @return array |
165 | */ |
166 | protected function getDefaultCachesToClear(): array |
167 | { |
168 | $result = []; |
169 | foreach ($this->cacheManager->getNonPersistentCacheList() as $id) { |
170 | $cache = $this->cacheManager->getCache($id); |
171 | if ($cache instanceof \Laminas\Cache\Storage\FlushableInterface) { |
172 | $result[] = $id; |
173 | } |
174 | } |
175 | return $result; |
176 | } |
177 | } |