PluginTrait.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace dokuwiki\Extension;
  3. /**
  4. * Provides standard DokuWiki plugin behaviour
  5. */
  6. trait PluginTrait
  7. {
  8. protected $localised = false; // set to true by setupLocale() after loading language dependent strings
  9. protected $lang = array(); // array to hold language dependent strings, best accessed via ->getLang()
  10. protected $configloaded = false; // set to true by loadConfig() after loading plugin configuration variables
  11. protected $conf = array(); // array to hold plugin settings, best accessed via ->getConf()
  12. /**
  13. * @see PluginInterface::getInfo()
  14. */
  15. public function getInfo()
  16. {
  17. $parts = explode('_', get_class($this));
  18. $info = DOKU_PLUGIN . '/' . $parts[2] . '/plugin.info.txt';
  19. if (file_exists($info)) return confToHash($info);
  20. msg(
  21. 'getInfo() not implemented in ' . get_class($this) . ' and ' . $info . ' not found.<br />' .
  22. 'Verify you\'re running the latest version of the plugin. If the problem persists, send a ' .
  23. 'bug report to the author of the ' . $parts[2] . ' plugin.', -1
  24. );
  25. return array(
  26. 'date' => '0000-00-00',
  27. 'name' => $parts[2] . ' plugin',
  28. );
  29. }
  30. /**
  31. * @see PluginInterface::isSingleton()
  32. */
  33. public function isSingleton()
  34. {
  35. return true;
  36. }
  37. /**
  38. * @see PluginInterface::loadHelper()
  39. */
  40. public function loadHelper($name, $msg = true)
  41. {
  42. $obj = plugin_load('helper', $name);
  43. if (is_null($obj) && $msg) msg("Helper plugin $name is not available or invalid.", -1);
  44. return $obj;
  45. }
  46. // region introspection methods
  47. /**
  48. * @see PluginInterface::getPluginType()
  49. */
  50. public function getPluginType()
  51. {
  52. list($t) = explode('_', get_class($this), 2);
  53. return $t;
  54. }
  55. /**
  56. * @see PluginInterface::getPluginName()
  57. */
  58. public function getPluginName()
  59. {
  60. list(/* $t */, /* $p */, $n) = sexplode('_', get_class($this), 4, '');
  61. return $n;
  62. }
  63. /**
  64. * @see PluginInterface::getPluginComponent()
  65. */
  66. public function getPluginComponent()
  67. {
  68. list(/* $t */, /* $p */, /* $n */, $c) = sexplode('_', get_class($this), 4, '');
  69. return $c;
  70. }
  71. // endregion
  72. // region localization methods
  73. /**
  74. * @see PluginInterface::getLang()
  75. */
  76. public function getLang($id)
  77. {
  78. if (!$this->localised) $this->setupLocale();
  79. return (isset($this->lang[$id]) ? $this->lang[$id] : '');
  80. }
  81. /**
  82. * @see PluginInterface::locale_xhtml()
  83. */
  84. public function locale_xhtml($id)
  85. {
  86. return p_cached_output($this->localFN($id));
  87. }
  88. /**
  89. * @see PluginInterface::localFN()
  90. */
  91. public function localFN($id, $ext = 'txt')
  92. {
  93. global $conf;
  94. $plugin = $this->getPluginName();
  95. $file = DOKU_CONF . 'plugin_lang/' . $plugin . '/' . $conf['lang'] . '/' . $id . '.' . $ext;
  96. if (!file_exists($file)) {
  97. $file = DOKU_PLUGIN . $plugin . '/lang/' . $conf['lang'] . '/' . $id . '.' . $ext;
  98. if (!file_exists($file)) {
  99. //fall back to english
  100. $file = DOKU_PLUGIN . $plugin . '/lang/en/' . $id . '.' . $ext;
  101. }
  102. }
  103. return $file;
  104. }
  105. /**
  106. * @see PluginInterface::setupLocale()
  107. */
  108. public function setupLocale()
  109. {
  110. if ($this->localised) return;
  111. global $conf, $config_cascade; // definitely don't invoke "global $lang"
  112. $path = DOKU_PLUGIN . $this->getPluginName() . '/lang/';
  113. $lang = array();
  114. // don't include once, in case several plugin components require the same language file
  115. @include($path . 'en/lang.php');
  116. foreach ($config_cascade['lang']['plugin'] as $config_file) {
  117. if (file_exists($config_file . $this->getPluginName() . '/en/lang.php')) {
  118. include($config_file . $this->getPluginName() . '/en/lang.php');
  119. }
  120. }
  121. if ($conf['lang'] != 'en') {
  122. @include($path . $conf['lang'] . '/lang.php');
  123. foreach ($config_cascade['lang']['plugin'] as $config_file) {
  124. if (file_exists($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php')) {
  125. include($config_file . $this->getPluginName() . '/' . $conf['lang'] . '/lang.php');
  126. }
  127. }
  128. }
  129. $this->lang = $lang;
  130. $this->localised = true;
  131. }
  132. // endregion
  133. // region configuration methods
  134. /**
  135. * @see PluginInterface::getConf()
  136. */
  137. public function getConf($setting, $notset = false)
  138. {
  139. if (!$this->configloaded) {
  140. $this->loadConfig();
  141. }
  142. if (isset($this->conf[$setting])) {
  143. return $this->conf[$setting];
  144. } else {
  145. return $notset;
  146. }
  147. }
  148. /**
  149. * @see PluginInterface::loadConfig()
  150. */
  151. public function loadConfig()
  152. {
  153. global $conf;
  154. $defaults = $this->readDefaultSettings();
  155. $plugin = $this->getPluginName();
  156. foreach ($defaults as $key => $value) {
  157. if (isset($conf['plugin'][$plugin][$key])) continue;
  158. $conf['plugin'][$plugin][$key] = $value;
  159. }
  160. $this->configloaded = true;
  161. $this->conf =& $conf['plugin'][$plugin];
  162. }
  163. /**
  164. * read the plugin's default configuration settings from conf/default.php
  165. * this function is automatically called through getConf()
  166. *
  167. * @return array setting => value
  168. */
  169. protected function readDefaultSettings()
  170. {
  171. $path = DOKU_PLUGIN . $this->getPluginName() . '/conf/';
  172. $conf = array();
  173. if (file_exists($path . 'default.php')) {
  174. include($path . 'default.php');
  175. }
  176. return $conf;
  177. }
  178. // endregion
  179. // region output methods
  180. /**
  181. * @see PluginInterface::email()
  182. */
  183. public function email($email, $name = '', $class = '', $more = '')
  184. {
  185. if (!$email) return $name;
  186. $email = obfuscate($email);
  187. if (!$name) $name = $email;
  188. $class = "class='" . ($class ? $class : 'mail') . "'";
  189. return "<a href='mailto:$email' $class title='$email' $more>$name</a>";
  190. }
  191. /**
  192. * @see PluginInterface::external_link()
  193. */
  194. public function external_link($link, $title = '', $class = '', $target = '', $more = '')
  195. {
  196. global $conf;
  197. $link = htmlentities($link);
  198. if (!$title) $title = $link;
  199. if (!$target) $target = $conf['target']['extern'];
  200. if ($conf['relnofollow']) $more .= ' rel="nofollow"';
  201. if ($class) $class = " class='$class'";
  202. if ($target) $target = " target='$target'";
  203. if ($more) $more = " " . trim($more);
  204. return "<a href='$link'$class$target$more>$title</a>";
  205. }
  206. /**
  207. * @see PluginInterface::render_text()
  208. */
  209. public function render_text($text, $format = 'xhtml')
  210. {
  211. return p_render($format, p_get_instructions($text), $info);
  212. }
  213. // endregion
  214. }