|
| 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