diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | lib/plugins/extension/README | 27 | ||||
-rw-r--r-- | lib/plugins/extension/helper/extension.php | 461 | ||||
-rw-r--r-- | lib/plugins/extension/helper/repository.php | 112 | ||||
-rw-r--r-- | lib/plugins/extension/plugin.info.txt | 7 |
5 files changed, 608 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore index e83fed24f..64816ba38 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ !/lib/plugins/authplain !/lib/plugins/acl !/lib/plugins/config +!/lib/plugins/extension !/lib/plugins/info !/lib/plugins/plugin !/lib/plugins/popularity diff --git a/lib/plugins/extension/README b/lib/plugins/extension/README new file mode 100644 index 000000000..5eefe924d --- /dev/null +++ b/lib/plugins/extension/README @@ -0,0 +1,27 @@ +extension Plugin for DokuWiki + +Extension manager + +All documentation for this plugin can be found at +https://www.dokuwiki.org/plugin:extension + +If you install this plugin manually, make sure it is installed in +lib/plugins/extension/ - if the folder is called different it +will not work! + +Please refer to http://www.dokuwiki.org/plugins for additional info +on how to install plugins in DokuWiki. + +---- +Copyright (C) Michael Hamann <michael@content-space.de> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +See the COPYING file in your DokuWiki folder for details diff --git a/lib/plugins/extension/helper/extension.php b/lib/plugins/extension/helper/extension.php new file mode 100644 index 000000000..b8981cf91 --- /dev/null +++ b/lib/plugins/extension/helper/extension.php @@ -0,0 +1,461 @@ +<?php +/** + * DokuWiki Plugin extension (Helper Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Hamann <michael@content-space.de> + */ + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Class helper_plugin_extension_extension represents a single extension (plugin or template) + */ +class helper_plugin_extension_extension extends DokuWiki_Plugin { + private $name; + private $is_template; + private $localInfo; + private $remoteInfo; + private $managerData; + /** @var helper_plugin_extension_repository $repository */ + private $repository = null; + + /** + * @return bool false, this component is not a singleton + */ + public function isSingleton() { + return false; + } + + /** + * Set the name of the extension this instance shall represents, triggers loading the local and remote data + * + * @param string $name The base name of the extension + * @param bool $is_template If the extension is a template + * @return bool If some (local or remote) data was found + */ + public function setExtension($name, $is_template) { + $this->name = $name; + $this->is_template = $is_template; + $this->localInfo = array(); + $this->managerData = array(); + $this->remoteInfo = array(); + + if ($this->isInstalled()) { + if ($this->isTemplate()) { + $infopath = $this->getInstallDir().'/template.info.txt'; + } else { + $infopath = $this->getInstallDir().'/plugin.info.txt'; + } + if (is_readable($infopath)) { + $this->localInfo = confToHash($infopath); + } + + $this->readManagerData(); + } + + if ($this->repository == null) { + $this->repository = $this->loadHelper('extension_repository'); + } + + $this->remoteInfo = $this->repository->getData(($this->isTemplate() ? 'template:' : '').$this->getBase()); + } + + /** + * If the extension is installed locally + * + * @return bool If the extension is installed locally + */ + public function isInstalled() { + return is_dir($this->getInstallDir()); + } + + /** + * If the extension is enabled + * + * @return bool If the extension is enabled + */ + public function isEnabled() { + /* @var Doku_Plugin_Controller $plugin_controller */ + global $plugin_controller; + return !$plugin_controller->isdisabled($this->name); + } + + /** + * If the extension should be updated, i.e. if an updated version is available + * + * @return bool If an update is available + */ + public function updateAvailable() { + $lastupdate = $this->getLastUpdate(); + if ($lastupdate === false) return false; + return $this->getInstalledVersion() < $this->getLastUpdate(); + } + + /** + * If the extension is a template + * + * @return bool If this extension is a template + */ + public function isTemplate() { + return $this->is_template; + } + + // Data from plugin.info.txt/template.info.txt or the repo when not available locally + /** + * Get the basename of the extension + * + * @return string The basename + */ + public function getBase() { + return $this->name; + } + + /** + * Get the display name of the extension + * + * @return string The display name + */ + public function getName() { + if (isset($this->localInfo['name'])) return $this->localInfo['name']; + if (isset($this->remoteInfo['name'])) return $this->remoteInfo['name']; + return $this->name; + } + + /** + * Get the author name of the extension + * + * @return string The name of the author + */ + public function getAuthor() { + if (isset($this->localInfo['author'])) return $this->localInfo['author']; + if (isset($this->remoteInfo['author'])) return $this->remoteInfo['author']; + return $this->getLang('unknownauthor'); + } + + /** + * Get the email of the author of the extension if there is any + * + * @return string|bool The email address or false if there is none + */ + public function getEmail() { + // email is only in the local data + if (isset($this->localInfo['email'])) return $this->localInfo['email']; + return false; + } + + /** + * Get the email id, i.e. the md5sum of the email + * + * @return string|bool The md5sum of the email if there is any, false otherwise + */ + public function getEmailID() { + if (isset($this->remoteInfo['emailid'])) return $this->remoteInfo['emailid']; + if (isset($this->localInfo['email'])) return md5($this->localInfo['email']); + return false; + } + + /** + * Get the description of the extension + * + * @return string The description + */ + public function getDescription() { + if (isset($this->localInfo['desc'])) return $this->localInfo['desc']; + if (isset($this->remoteInfo['description'])) return $this->remoteInfo['description']; + return ''; + } + + /** + * Get the URL of the extension, usually a page on dokuwiki.org + * + * @return string The URL + */ + public function getURL() { + if (isset($this->localInfo['url'])) return $this->localInfo['url']; + return 'https://www.dokuwiki.org/plugin:'.$this->name; + } + + /** + * Get the installed version of the extension + * + * @return string|bool The version, usually in the form yyyy-mm-dd if there is any + */ + public function getInstalledVersion() { + if (isset($this->localInfo['date'])) return $this->localInfo['date']; + if ($this->isInstalled()) return $this->getLang('unknownversion'); + return false; + } + + /** + * Get the names of the dependencies of this extension + * + * @return array The base names of the dependencies + */ + public function getDependencies() { + if (isset($this->remoteInfo['dependencies'])) return $this->remoteInfo['dependencies']; + return array(); + } + + /** + * Get the names of all conflicting extensions + * + * @return array The names of the conflicting extensions + */ + public function getConflicts() { + if (isset($this->remoteInfo['conflicts'])) return $this->remoteInfo['dependencies']; + return array(); + } + + /** + * Get the names of similar extensions + * + * @return array The names of similar extensions + */ + public function getSimilarExtensions() { + if (isset($this->remoteInfo['similar'])) return $this->remoteInfo['similar']; + return array(); + } + + /** + * Get the names of the tags of the extension + * + * @return array The names of the tags of the extension + */ + public function getTags() { + if (isset($this->remoteInfo['tags'])) return $this->remoteInfo['tags']; + return array(); + } + + /** + * Get the text of the security warning if there is any + * + * @return string|bool The security warning if there is any, false otherwise + */ + public function getSecurityWarning() { + if (isset($this->remoteInfo['securitywarning'])) return $this->remoteInfo['securitywarning']; + return false; + } + + /** + * Get the text of the security issue if there is any + * + * @return string|bool The security issue if there is any, false otherwise + */ + public function getSecurityIssue() { + if (isset($this->remoteInfo['securityissue'])) return $this->remoteInfo['securityissue']; + return false; + } + + /** + * Get the URL of the screenshot of the extension if there is any + * + * @return string|bool The screenshot URL if there is any, false otherwise + */ + public function getScreenshotURL() { + if (isset($this->remoteInfo['screenshoturl'])) return $this->remoteInfo['screenshoturl']; + return false; + } + + /** + * Get the last used download URL of the extension if there is any + * + * @return string|bool The previously used download URL, false if the extension has been installed manually + */ + public function getLastDownloadURL() { + if (isset($this->managerData['downloadurl'])) return $this->managerData['downloadurl']; + return false; + } + + /** + * Get the download URL of the extension if there is any + * + * @return string|bool The download URL if there is any, false otherwise + */ + public function getDownloadURL() { + if (isset($this->remoteInfo['downloadurl'])) return $this->remoteInfo['downloadurl']; + return false; + } + + /** + * Get the bug tracker URL of the extension if there is any + * + * @return string|bool The bug tracker URL if there is any, false otherwise + */ + public function getBugtrackerURL() { + if (isset($this->remoteInfo['bugtracker'])) return $this->remoteInfo['bugtracker']; + return false; + } + + /** + * Get the URL of the source repository if there is any + * + * @return string|bool The URL of the source repository if there is any, false otherwise + */ + public function getSourcerepoURL() { + if (isset($this->remoteInfo['sourcerepo'])) return $this->remoteInfo['sourcerepo']; + return false; + } + + /** + * Get the donation URL of the extension if there is any + * + * @return string|bool The donation URL if there is any, false otherwise + */ + public function getDonationURL() { + if (isset($this->remoteInfo['donationurl'])) return $this->remoteInfo['donationurl']; + return false; + } + + /** + * Get the extension type(s) + * + * @return array The type(s) as array of strings + */ + public function getTypes() { + if (isset($this->remoteInfo['types'])) return explode(', ', $this->remoteInfo['types']); + if ($this->isTemplate()) return array(32 => 'template'); + return array(); + } + + /** + * Get a list of all DokuWiki versions this extension is compatible with + * + * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit) + */ + public function getCompatibleVersions() { + if (isset($this->remoteInfo['compatible'])) return $this->remoteInfo['compatible']; + return array(); + } + + /** + * Get the date of the last available update + * + * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise + */ + public function getLastUpdate() { + if (isset($this->remoteInfo['lastupdate'])) return $this->remoteInfo['lastupdate']; + return false; + } + + /** + * Get the base path of the extension + * + * @return string The base path of the extension + */ + public function getInstallDir() { + if ($this->isTemplate()) { + return basename(tpl_incdir()).$this->name; + } else { + return DOKU_PLUGIN.$this->name; + } + } + + /** + * The type of extension installation + * + * @return string One of "none", "manual", "git" or "automatic" + */ + public function getInstallType() { + if (!$this->isInstalled()) return 'none'; + if (!empty($this->managerData)) return 'automatic'; + if (is_dir($this->getInstallDir().'/.git')) return 'git'; + return 'manual'; + } + + /** + * If the extension can probably be installed/updated or uninstalled + * + * @return bool|string True or one of "nourl", "noparentperms" (template/plugin install path not writable), "noperms" (extension itself not writable) + */ + public function canModify() { + } + + /** + * Install or update the extension + * + * @return bool|string True or an error message + */ + public function installOrUpdate() { + } + + /** + * Uninstall the extension + * + * @return bool|string True or an error message + */ + public function uninstall() { + } + + /** + * Enable the extension + * + * @return bool|string True or an error message + */ + public function enable() { + if ($this->isTemplate()) return $this->getLang('notimplemented'); + /* @var Doku_Plugin_Controller $plugin_controller */ + global $plugin_controller; + if (!$this->isInstalled()) return $this->getLang('notinstalled'); + if (!$this->isEnabled()) return $this->getLang('alreadyenabled'); + if ($plugin_controller->enable($this->name)) { + return true; + } else { + return $this->getLang('pluginlistsaveerror'); + } + } + + /** + * Disable the extension + * + * @return bool|string True or an error message + */ + public function disable() { + if ($this->isTemplate()) return $this->getLang('notimplemented'); + + /* @var Doku_Plugin_Controller $plugin_controller */ + global $plugin_controller; + if (!$this->isInstalled()) return $this->getLang('notinstalled'); + if (!$this->isEnabled()) return $this->getLang('alreadydisabled'); + if ($plugin_controller->disable($this->name)) { + return true; + } else { + return $this->getLang('pluginlistsaveerror'); + } + } + + /** + * Read the manager.dat file + */ + protected function readManagerData() { + $managerpath = $this->getInstallDir().'/manager.dat'; + if (is_readable($managerpath)) { + $file = @file($managerpath); + if(!empty($file)) { + foreach($file as $line) { + list($key, $value) = explode('=', trim($line, PHP_EOL), 2); + $key = trim($key); + $value = trim($value); + // backwards compatible with old plugin manager + if($key == 'url') $key = 'downloadurl'; + $this->managerData[$key] = $value; + } + } + } + } + + /** + * Write the manager.data file + */ + protected function writeManagerData() { + $managerpath = $this->getInstallDir().'/manager.dat'; + $data = ''; + foreach ($this->managerData as $k => $v) { + $data .= $k.'='.$v.DOKU_LF; + } + io_saveFile($managerpath, $data); + } +} + +// vim:ts=4:sw=4:et: diff --git a/lib/plugins/extension/helper/repository.php b/lib/plugins/extension/helper/repository.php new file mode 100644 index 000000000..37b9bc02c --- /dev/null +++ b/lib/plugins/extension/helper/repository.php @@ -0,0 +1,112 @@ +<?php +/** + * DokuWiki Plugin extension (Helper Component) + * + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html + * @author Michael Hamann <michael@content-space.de> + */ + +if (!defined('EXTENSION_REPOSITORY_API_ENDPOINT')) + define('EXTENSION_REPSITORY_API', 'http://www.dokuwiki.org/lib/plugins/pluginrepo/api.php'); + +// must be run within Dokuwiki +if(!defined('DOKU_INC')) die(); + +/** + * Class helper_plugin_extension_repository provides access to the extension repository on dokuwiki.org + */ +class helper_plugin_extension_repository extends DokuWiki_Plugin { + private $loaded_extensions = array(); + private $has_access = null; + /** + * Initialize the repository (cache), fetches data for all installed plugins + */ + public function init() { + /* @var Doku_Plugin_Controller $plugin_controller */ + global $plugin_controller; + if ($this->hasAccess()) { + $list = $plugin_controller->getList('', true); + $request_data = array('fmt' => 'php'); + $request_needed = false; + foreach ($list as $name) { + $cache = new cache('##extension_manager##'.$name, 'repo'); + $result = null; + if (!isset($this->loaded_extensions[$name]) && $this->hasAccess() && !$cache->useCache(array('age' => 3600 * 24))) { + $this->loaded_extensions[$name] = true; + $request_data['ext'][] = $name; + $request_needed = true; + } + } + + if ($request_needed) { + $httpclient = new DokuHTTPClient(); + $data = $httpclient->post(EXTENSION_REPSITORY_API, $request_data); + if ($data !== false) { + $extensions = unserialize($data); + foreach ($extensions as $extension) { + $cache = new cache('##extension_manager##'.$extension['plugin'], 'repo'); + $cache->storeCache(serialize($extension)); + } + } else { + $this->has_access = false; + } + } + } + } + + /** + * If repository access is available + * + * @return bool If repository access is available + */ + public function hasAccess() { + if ($this->has_access === null) { + $cache = new cache('##extension_manager###hasAccess', 'repo'); + $result = null; + if (!$cache->useCache(array('age' => 3600 * 24))) { + $httpclient = new DokuHTTPClient(); + $httpclient->timeout = 5; + $data = $httpclient->get(EXTENSION_REPSITORY_API.'?cmd=ping'); + if ($data !== false) { + $this->has_access = true; + $cache->storeCache(1); + } else { + $this->has_access = false; + $cache->storeCache(0); + } + } else { + $this->has_access = ($cache->retrieveCache(false) == 1); + } + } + return $this->has_access; + } + + /** + * Get the remote data of an individual plugin or template + * + * @param string $name The plugin name to get the data for, template names need to be prefix by 'template:' + * @return array The data or null if nothing was found (possibly no repository access) + */ + public function getData($name) { + $cache = new cache('##extension_manager##'.$name, 'repo'); + $result = null; + if (!isset($this->loaded_extensions[$name]) && $this->hasAccess() && !$cache->useCache(array('age' => 3600 * 24))) { + $this->loaded_extensions[$name] = true; + $httpclient = new DokuHTTPClient(); + $data = $httpclient->get(EXTENSION_REPSITORY_API.'?fmt=php&ext[]='.urlencode($name)); + if ($data !== false) { + $result = unserialize($data); + $cache->storeCache(serialize($result[0])); + return $result[0]; + } else { + $this->has_access = false; + } + } + if (file_exists($cache->cache)) { + return unserialize($cache->retrieveCache(false)); + } + return array(); + } +} + +// vim:ts=4:sw=4:et: diff --git a/lib/plugins/extension/plugin.info.txt b/lib/plugins/extension/plugin.info.txt new file mode 100644 index 000000000..3c4469ad7 --- /dev/null +++ b/lib/plugins/extension/plugin.info.txt @@ -0,0 +1,7 @@ +base extension +author Michael Hamann +email michael@content-space.de +date 2013-08-01 +name extension plugin +desc Extension manager +url https://www.dokuwiki.org/plugin:extension |