aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/extension/remote.php
blob: 4eb84a82966b6623480c79c283e01ab76cc819e1 (plain) (blame)
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
143
144
145
146
147
148
149
<?php

use dokuwiki\Extension\RemotePlugin;
use dokuwiki\plugin\extension\Extension;
use dokuwiki\plugin\extension\ExtensionApiResponse;
use dokuwiki\plugin\extension\Installer;
use dokuwiki\plugin\extension\Local;
use dokuwiki\plugin\extension\Repository;
use dokuwiki\Remote\AccessDeniedException;

/**
 * DokuWiki Plugin extension (Remote Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author Andreas Gohr <andi@splitbrain.org>
 */
class remote_plugin_extension extends RemotePlugin
{
    /**
     * List installed extensions
     *
     * This lists all installed extensions. The list is not sorted in any way.
     *
     * @return ExtensionApiResponse[] The list of installed extensions and their details
     */
    public function list()
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to access extensions', 114);
        }

        $extensions = (new Local())->getExtensions();
        Repository::getInstance()->initExtensions(array_keys($extensions));

        return array_values(
            array_map(
                static fn($extension) => new ExtensionApiResponse($extension),
                $extensions
            )
        );
    }

    /**
     * Search for extensions in the repository
     *
     * @param string $query The keyword(s) to search for
     * @param int $max Maximum number of results (default 10)
     * @return ExtensionApiResponse[] List of matching extensions
     */
    public function search($query, $max = 10)
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to access extensions', 114);
        }

        $repo = Repository::getInstance();
        $result = $repo->searchExtensions($query);

        if ($max > 0) {
            $result = array_slice($result, 0, $max);
        }

        return array_values(
            array_map(
                static fn($extension) => new ExtensionApiResponse($extension),
                $result
            )
        );
    }

    /**
     * Enable a specific extension
     *
     * @param string $extension Extension ID to enable
     * @return bool Success status
     */
    public function enable($extension)
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
        }

        $ext = Extension::createFromId($extension);
        $ext->enable();
        return true;
    }

    /**
     * Disable a specific extension
     *
     * @param string $extension Extension ID to disable
     * @return bool Success status
     */
    public function disable($extension)
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
        }

        $ext = Extension::createFromId($extension);
        $ext->disable();
        return true;
    }

    /**
     * Install a specific extension
     *
     * This will also install dependencies, so more than the given extension may be installed.
     *
     * @param string $extension Extension ID or download URL
     * @return string[] List of installed extensions
     */
    public function install($extension)
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
        }

        $installer = new Installer(true);
        $installer->installFromId($extension);

        return array_keys(
            array_filter(
                $installer->getProcessed(),
                static fn($status) => (
                    $status == Installer::STATUS_INSTALLED || $status == Installer::STATUS_UPDATED
                )
            )
        );
    }

    /**
     * Uninstall a specific extension
     *
     * @param string $extension Extension ID to uninstall
     * @return bool Success status
     */
    public function uninstall($extension)
    {
        if (!auth_isadmin()) {
            throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
        }

        $ext = Extension::createFromId($extension);
        $installer = new Installer();
        $installer->uninstall($ext);
        return true;
    }
}