Skip to content

Commit a2fa11e

Browse files
authored
Merge pull request dokuwiki#4450 from dokuwiki/extension-api
Add remote API to extension manager
2 parents c84b525 + 1d94f4f commit a2fa11e

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\extension;
4+
5+
use dokuwiki\Remote\Response\ApiResponse;
6+
7+
class ExtensionApiResponse extends ApiResponse
8+
{
9+
protected Extension $extension;
10+
11+
/** @var string The type of this extension ("plugin" or "template") */
12+
public $type;
13+
14+
/** @var string The id of this extension (templates are prefixed with "template") */
15+
public $id;
16+
17+
/** @var string The base name of this extension */
18+
public $base;
19+
20+
/** @var string The display name of this extension */
21+
public $name;
22+
23+
/** @var string The installed version/date of this extension */
24+
public $version;
25+
26+
/** @var string The author of this extension */
27+
public $author;
28+
29+
/** @var string The description of this extension */
30+
public $description;
31+
32+
/** @var bool Whether this extension is installed */
33+
public $isInstalled;
34+
35+
/** @var bool Whether this extension is enabled */
36+
public $isEnabled;
37+
38+
/** @var bool Whether an update is available */
39+
public $updateAvailable;
40+
41+
/** @var bool Whether this extension is bundled with DokuWiki */
42+
public $isBundled;
43+
44+
/** @var bool Whether this extension is under git control */
45+
public $isGitControlled;
46+
47+
/** @var string[] Notices for this extension */
48+
public $notices;
49+
50+
/** @var string Documentation URL for this extension */
51+
public $url;
52+
53+
/** @var string[] The component types this plugin provides */
54+
public $componentTypes;
55+
56+
/** @var string The last available remote update date */
57+
public $lastUpdate;
58+
59+
/** @var string The download URL for this extension */
60+
public string $downloadURL;
61+
62+
/**
63+
* Constructor
64+
*
65+
* @param Extension $extension The extension to create the response for
66+
*/
67+
public function __construct(Extension $extension)
68+
{
69+
$this->extension = $extension;
70+
$this->type = $extension->getType();
71+
$this->id = $extension->getId();
72+
$this->base = $extension->getBase();
73+
$this->name = $extension->getDisplayName();
74+
$this->version = $extension->getInstalledVersion();
75+
$this->author = $extension->getAuthor();
76+
$this->description = $extension->getDescription();
77+
$this->isInstalled = $extension->isInstalled();
78+
$this->isEnabled = $extension->isEnabled();
79+
$this->updateAvailable = $extension->isUpdateAvailable();
80+
$this->isBundled = $extension->isBundled();
81+
$this->isGitControlled = $extension->isGitControlled();
82+
$this->componentTypes = $extension->getComponentTypes();
83+
$this->lastUpdate = $extension->getLastUpdate();
84+
$this->url = $extension->getURL();
85+
$this->downloadURL = $extension->getDownloadURL();
86+
87+
// Add notices
88+
$this->notices = array_merge(...array_values(Notice::list($extension)));
89+
}
90+
91+
/** @inheritdoc */
92+
public function __toString()
93+
{
94+
return $this->extension->getId();
95+
}
96+
}

lib/plugins/extension/remote.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
use dokuwiki\Extension\RemotePlugin;
4+
use dokuwiki\plugin\extension\Extension;
5+
use dokuwiki\plugin\extension\ExtensionApiResponse;
6+
use dokuwiki\plugin\extension\Installer;
7+
use dokuwiki\plugin\extension\Local;
8+
use dokuwiki\plugin\extension\Repository;
9+
use dokuwiki\Remote\AccessDeniedException;
10+
11+
/**
12+
* DokuWiki Plugin extension (Remote Component)
13+
*
14+
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
15+
* @author Andreas Gohr <[email protected]>
16+
*/
17+
class remote_plugin_extension extends RemotePlugin
18+
{
19+
/**
20+
* List installed extensions
21+
*
22+
* This lists all installed extensions. The list is not sorted in any way.
23+
*
24+
* @return ExtensionApiResponse[] The list of installed extensions and their details
25+
*/
26+
public function list()
27+
{
28+
if (!auth_isadmin()) {
29+
throw new AccessDeniedException('Only admins are allowed to access extensions', 114);
30+
}
31+
32+
$extensions = (new Local())->getExtensions();
33+
Repository::getInstance()->initExtensions(array_keys($extensions));
34+
35+
return array_values(
36+
array_map(
37+
static fn($extension) => new ExtensionApiResponse($extension),
38+
$extensions
39+
)
40+
);
41+
}
42+
43+
/**
44+
* Search for extensions in the repository
45+
*
46+
* @param string $query The keyword(s) to search for
47+
* @param int $max Maximum number of results (default 10)
48+
* @return ExtensionApiResponse[] List of matching extensions
49+
*/
50+
public function search($query, $max = 10)
51+
{
52+
if (!auth_isadmin()) {
53+
throw new AccessDeniedException('Only admins are allowed to access extensions', 114);
54+
}
55+
56+
$repo = Repository::getInstance();
57+
$result = $repo->searchExtensions($query);
58+
59+
if ($max > 0) {
60+
$result = array_slice($result, 0, $max);
61+
}
62+
63+
return array_values(
64+
array_map(
65+
static fn($extension) => new ExtensionApiResponse($extension),
66+
$result
67+
)
68+
);
69+
}
70+
71+
/**
72+
* Enable a specific extension
73+
*
74+
* @param string $extension Extension ID to enable
75+
* @return bool Success status
76+
*/
77+
public function enable($extension)
78+
{
79+
if (!auth_isadmin()) {
80+
throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
81+
}
82+
83+
$ext = Extension::createFromId($extension);
84+
$ext->enable();
85+
return true;
86+
}
87+
88+
/**
89+
* Disable a specific extension
90+
*
91+
* @param string $extension Extension ID to disable
92+
* @return bool Success status
93+
*/
94+
public function disable($extension)
95+
{
96+
if (!auth_isadmin()) {
97+
throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
98+
}
99+
100+
$ext = Extension::createFromId($extension);
101+
$ext->disable();
102+
return true;
103+
}
104+
105+
/**
106+
* Install a specific extension
107+
*
108+
* This will also install dependencies, so more than the given extension may be installed.
109+
*
110+
* @param string $extension Extension ID or download URL
111+
* @return string[] List of installed extensions
112+
*/
113+
public function install($extension)
114+
{
115+
if (!auth_isadmin()) {
116+
throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
117+
}
118+
119+
$installer = new Installer(true);
120+
$installer->installFromId($extension);
121+
122+
return array_keys(
123+
array_filter(
124+
$installer->getProcessed(),
125+
static fn($status) => (
126+
$status == Installer::STATUS_INSTALLED || $status == Installer::STATUS_UPDATED
127+
)
128+
)
129+
);
130+
}
131+
132+
/**
133+
* Uninstall a specific extension
134+
*
135+
* @param string $extension Extension ID to uninstall
136+
* @return bool Success status
137+
*/
138+
public function uninstall($extension)
139+
{
140+
if (!auth_isadmin()) {
141+
throw new AccessDeniedException('Only admins are allowed to manage extensions', 114);
142+
}
143+
144+
$ext = Extension::createFromId($extension);
145+
$installer = new Installer();
146+
$installer->uninstall($ext);
147+
return true;
148+
}
149+
}

0 commit comments

Comments
 (0)