repository.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * DokuWiki Plugin extension (Helper Component)
  4. *
  5. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  6. * @author Michael Hamann <michael@content-space.de>
  7. */
  8. use dokuwiki\Cache\Cache;
  9. use dokuwiki\HTTP\DokuHTTPClient;
  10. use dokuwiki\Extension\PluginController;
  11. /**
  12. * Class helper_plugin_extension_repository provides access to the extension repository on dokuwiki.org
  13. */
  14. class helper_plugin_extension_repository extends DokuWiki_Plugin
  15. {
  16. const EXTENSION_REPOSITORY_API = 'https://www.dokuwiki.org/lib/plugins/pluginrepo/api.php';
  17. private $loaded_extensions = array();
  18. private $has_access = null;
  19. /**
  20. * Initialize the repository (cache), fetches data for all installed plugins
  21. */
  22. public function init()
  23. {
  24. /* @var PluginController $plugin_controller */
  25. global $plugin_controller;
  26. if ($this->hasAccess()) {
  27. $list = $plugin_controller->getList('', true);
  28. $request_data = array('fmt' => 'php');
  29. $request_needed = false;
  30. foreach ($list as $name) {
  31. $cache = new Cache('##extension_manager##'.$name, '.repo');
  32. if (!isset($this->loaded_extensions[$name]) &&
  33. $this->hasAccess() &&
  34. !$cache->useCache(array('age' => 3600 * 24))
  35. ) {
  36. $this->loaded_extensions[$name] = true;
  37. $request_data['ext'][] = $name;
  38. $request_needed = true;
  39. }
  40. }
  41. if ($request_needed) {
  42. $httpclient = new DokuHTTPClient();
  43. $data = $httpclient->post(self::EXTENSION_REPOSITORY_API, $request_data);
  44. if ($data !== false) {
  45. $extensions = unserialize($data);
  46. foreach ($extensions as $extension) {
  47. $cache = new Cache('##extension_manager##'.$extension['plugin'], '.repo');
  48. $cache->storeCache(serialize($extension));
  49. }
  50. } else {
  51. $this->has_access = false;
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * If repository access is available
  58. *
  59. * @param bool $usecache use cached result if still valid
  60. * @return bool If repository access is available
  61. */
  62. public function hasAccess($usecache = true) {
  63. if ($this->has_access === null) {
  64. $cache = new Cache('##extension_manager###hasAccess', '.repo');
  65. if (!$cache->useCache(array('age' => 60*10, 'purge' => !$usecache))) {
  66. $httpclient = new DokuHTTPClient();
  67. $httpclient->timeout = 5;
  68. $data = $httpclient->get(self::EXTENSION_REPOSITORY_API.'?cmd=ping');
  69. if ($data !== false) {
  70. $this->has_access = true;
  71. $cache->storeCache(1);
  72. } else {
  73. $this->has_access = false;
  74. $cache->storeCache(0);
  75. }
  76. } else {
  77. $this->has_access = ($cache->retrieveCache(false) == 1);
  78. }
  79. }
  80. return $this->has_access;
  81. }
  82. /**
  83. * Get the remote data of an individual plugin or template
  84. *
  85. * @param string $name The plugin name to get the data for, template names need to be prefix by 'template:'
  86. * @return array The data or null if nothing was found (possibly no repository access)
  87. */
  88. public function getData($name)
  89. {
  90. $cache = new Cache('##extension_manager##'.$name, '.repo');
  91. if (!isset($this->loaded_extensions[$name]) &&
  92. $this->hasAccess() &&
  93. !$cache->useCache(array('age' => 3600 * 24))
  94. ) {
  95. $this->loaded_extensions[$name] = true;
  96. $httpclient = new DokuHTTPClient();
  97. $data = $httpclient->get(self::EXTENSION_REPOSITORY_API.'?fmt=php&ext[]='.urlencode($name));
  98. if ($data !== false) {
  99. $result = unserialize($data);
  100. if(count($result)) {
  101. $cache->storeCache(serialize($result[0]));
  102. return $result[0];
  103. }
  104. return array();
  105. } else {
  106. $this->has_access = false;
  107. }
  108. }
  109. if (file_exists($cache->cache)) {
  110. return unserialize($cache->retrieveCache(false));
  111. }
  112. return array();
  113. }
  114. /**
  115. * Search for plugins or templates using the given query string
  116. *
  117. * @param string $q the query string
  118. * @return array a list of matching extensions
  119. */
  120. public function search($q)
  121. {
  122. $query = $this->parseQuery($q);
  123. $query['fmt'] = 'php';
  124. $httpclient = new DokuHTTPClient();
  125. $data = $httpclient->post(self::EXTENSION_REPOSITORY_API, $query);
  126. if ($data === false) return array();
  127. $result = unserialize($data);
  128. $ids = array();
  129. // store cache info for each extension
  130. foreach ($result as $ext) {
  131. $name = $ext['plugin'];
  132. $cache = new Cache('##extension_manager##'.$name, '.repo');
  133. $cache->storeCache(serialize($ext));
  134. $ids[] = $name;
  135. }
  136. return $ids;
  137. }
  138. /**
  139. * Parses special queries from the query string
  140. *
  141. * @param string $q
  142. * @return array
  143. */
  144. protected function parseQuery($q)
  145. {
  146. $parameters = array(
  147. 'tag' => array(),
  148. 'mail' => array(),
  149. 'type' => array(),
  150. 'ext' => array()
  151. );
  152. // extract tags
  153. if (preg_match_all('/(^|\s)(tag:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
  154. foreach ($matches as $m) {
  155. $q = str_replace($m[2], '', $q);
  156. $parameters['tag'][] = $m[3];
  157. }
  158. }
  159. // extract author ids
  160. if (preg_match_all('/(^|\s)(authorid:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
  161. foreach ($matches as $m) {
  162. $q = str_replace($m[2], '', $q);
  163. $parameters['mail'][] = $m[3];
  164. }
  165. }
  166. // extract extensions
  167. if (preg_match_all('/(^|\s)(ext:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
  168. foreach ($matches as $m) {
  169. $q = str_replace($m[2], '', $q);
  170. $parameters['ext'][] = $m[3];
  171. }
  172. }
  173. // extract types
  174. if (preg_match_all('/(^|\s)(type:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
  175. foreach ($matches as $m) {
  176. $q = str_replace($m[2], '', $q);
  177. $parameters['type'][] = $m[3];
  178. }
  179. }
  180. // FIXME make integer from type value
  181. $parameters['q'] = trim($q);
  182. return $parameters;
  183. }
  184. }
  185. // vim:ts=4:sw=4:et: