Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.33% covered (danger)
16.33%
8 / 49
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DevtoolsController
16.33% covered (danger)
16.33%
8 / 49
16.67% covered (danger)
16.67%
1 / 6
207.81
0.00% covered (danger)
0.00%
0 / 1
 getQueryBuilder
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 deminifyAction
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
72
 homeAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 iconAction
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 languageAction
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 permissionsAction
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * Development Tools Controller
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2011.
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Controller
25 * @author   Mark Triggs <vufind-tech@lists.sourceforge.net>
26 * @author   Chris Hallberg <challber@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/indexing:alphabetical_heading_browse Wiki
29 */
30
31namespace VuFindDevTools\Controller;
32
33use VuFind\I18n\Locale\LocaleSettings;
34use VuFind\I18n\Translator\Loader\ExtendedIni;
35use VuFind\Role\PermissionManager;
36use VuFind\Role\PermissionProvider\PluginManager as PermissionProviderPluginManager;
37use VuFind\Role\PermissionProvider\SessionKey;
38use VuFind\Search\Results\PluginManager as ResultsManager;
39use VuFindDevTools\LanguageHelper;
40
41use function is_callable;
42
43/**
44 * Development Tools Controller
45 *
46 * @category VuFind
47 * @package  Controller
48 * @author   Mark Triggs <vufind-tech@lists.sourceforge.net>
49 * @author   Chris Hallberg <challber@villanova.edu>
50 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
51 * @link     https://vufind.org/wiki/indexing:alphabetical_heading_browse Wiki
52 */
53class DevtoolsController extends \VuFind\Controller\AbstractBase
54{
55    /**
56     * Fetch the query builder for the specified search backend. Return null if
57     * unavailable.
58     *
59     * @param string $id Backend ID
60     *
61     * @return object
62     */
63    protected function getQueryBuilder($id)
64    {
65        $command = new \VuFindSearch\Command\GetQueryBuilderCommand($id);
66        try {
67            $this->getService(\VuFindSearch\Service::class)->invoke($command);
68        } catch (\Exception $e) {
69            return null;
70        }
71        return $command->getResult();
72    }
73
74    /**
75     * Deminify action
76     *
77     * @return \Laminas\View\Model\ViewModel
78     */
79    public function deminifyAction()
80    {
81        $min = trim($this->params()->fromPost('min'));
82        $view = $this->createViewModel();
83        if (!empty($min)) {
84            $view->min = unserialize($min);
85        }
86        if (isset($view->min) && $view->min) {
87            $view->results = $view->min->deminify(
88                $this->getService(ResultsManager::class)
89            );
90        }
91        if (isset($view->results) && $view->results) {
92            $params = $view->results->getParams();
93            $view->query = $params->getQuery();
94            if (is_callable([$params, 'getBackendParameters'])) {
95                $view->backendParams = $params->getBackendParameters()
96                    ->getArrayCopy();
97            }
98            if ($builder = $this->getQueryBuilder($params->getSearchClassId())) {
99                $view->queryParams = $builder->build($view->query)->getArrayCopy();
100            }
101        }
102        return $view;
103    }
104
105    /**
106     * Home action
107     *
108     * @return \Laminas\View\Model\ViewModel
109     */
110    public function homeAction()
111    {
112        return $this->createViewModel();
113    }
114
115    /**
116     * Icon action
117     *
118     * @return array
119     */
120    public function iconAction()
121    {
122        $config = $this->getService(\VuFindTheme\ThemeInfo::class)
123            ->getMergedConfig('icons');
124        $aliases = array_keys($config['aliases'] ?? []);
125        sort($aliases);
126        return compact('aliases');
127    }
128
129    /**
130     * Language action
131     *
132     * @return array
133     */
134    public function languageAction()
135    {
136        // Test languages with no local overrides and no fallback:
137        $loader = new ExtendedIni([APPLICATION_PATH . '/languages']);
138        $langs = $this->getService(LocaleSettings::class)
139            ->getEnabledLocales();
140        $helper = new LanguageHelper($loader, $langs);
141        return $helper->getAllDetails(
142            $this->params()->fromQuery('main', 'en'),
143            (bool)$this->params()->fromQuery('includeOptional', 1)
144        );
145    }
146
147    /**
148     * Permissions action
149     *
150     * @return array
151     */
152    public function permissionsAction()
153    {
154        // Handle demo session key setting/unsetting:
155        $set = $this->params()->fromQuery('setSessionKey');
156        $unset = $this->params()->fromQuery('unsetSessionKey');
157        if ($set || $unset) {
158            $provider = $this->getService(PermissionProviderPluginManager::class)->get(SessionKey::class);
159            $method = $set ? 'setSessionValue' : 'unsetSessionValue';
160            $provider->$method('demo_key');
161            return $this->redirect()->toRoute('devtools-permissions');
162        }
163
164        // Retrieve full permission list:
165        $manager = $this->getService(PermissionManager::class);
166        $permissions = [];
167        foreach ($manager->getAllConfiguredPermissions() as $permission) {
168            $permissions[$permission] = $manager->isAuthorized($permission);
169        }
170        ksort($permissions);
171        return compact('permissions');
172    }
173}