SamplePlugin.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * A sample plugin to show best practices for GNU social plugins
  18. *
  19. * @package GNU social
  20. * @author Brion Vibber <brionv@status.net>
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. // This check helps protect against security problems;
  26. // your code file can't be executed directly from the web.
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Sample plugin main class
  30. *
  31. * Each plugin requires a main class to interact with the StatusNet system.
  32. *
  33. * The main class usually extends the Plugin class that comes with StatusNet.
  34. *
  35. * The class has standard-named methods that will be called when certain events
  36. * happen in the code base. These methods have names like 'onX' where X is an
  37. * event name (see EVENTS.txt for the list of available events). Event handlers
  38. * have pre-defined arguments, based on which event they're handling. A typical
  39. * event handler:
  40. *
  41. * function onSomeEvent($paramA, &$paramB)
  42. * {
  43. * if ($paramA == 'jed') {
  44. * throw new Exception(sprintf(_m("Invalid parameter %s"), $paramA));
  45. * }
  46. * $paramB = 'spock';
  47. * return true;
  48. * }
  49. *
  50. * Event handlers must return a boolean value. If they return false, all other
  51. * event handlers for this event (in other plugins) will be skipped, and in some
  52. * cases the default processing for that event would be skipped. This is great for
  53. * replacing the default action of an event.
  54. *
  55. * If the handler returns true, processing of other event handlers and the default
  56. * processing will continue. This is great for extending existing functionality.
  57. *
  58. * If the handler throws an exception, processing will stop, and the exception's
  59. * error will be shown to the user.
  60. *
  61. * To install a plugin (like this one), site admins add the following code to
  62. * their config.php file:
  63. *
  64. * addPlugin('Sample');
  65. *
  66. * Plugins must be installed in one of the following directories:
  67. *
  68. * local/plugins/{$pluginclass}.php
  69. * local/plugins/{$name}/{$pluginclass}.php
  70. * local/{$pluginclass}.php
  71. * local/{$name}/{$pluginclass}.php
  72. * plugins/{$pluginclass}.php
  73. * plugins/{$name}/{$pluginclass}.php
  74. *
  75. * Here, {$name} is the name of the plugin, like 'Sample', and {$pluginclass} is
  76. * the name of the main class, like 'SamplePlugin'. Plugins that are part of the
  77. * main StatusNet distribution go in 'plugins' and third-party or local ones go
  78. * in 'local'.
  79. *
  80. * Simple plugins can be implemented as a single plugin. Others are more complex
  81. * and require additional plugins; these should use their own directory, like
  82. * 'local/plugins/{$name}/'. All files related to the plugin, including images,
  83. * JavaScript, CSS, external libraries or PHP plugins should go in the plugin
  84. * directory.
  85. *
  86. * @category Sample
  87. * @package GNU social
  88. * @author Brion Vibber <brionv@status.net>
  89. * @author Evan Prodromou <evan@status.net>
  90. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  91. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  92. */
  93. class SamplePlugin extends Plugin
  94. {
  95. // Versions start at 0.1.0 in Semver
  96. const PLUGIN_VERSION = '0.1.0';
  97. /**
  98. * Plugins are configured using public instance attributes. To set
  99. * their values, site administrators use this syntax:
  100. *
  101. * addPlugin('Sample', ['attr1' => 'foo', 'attr2' => 'bar']);
  102. *
  103. * The same plugin class can be initialized multiple times with different
  104. * arguments:
  105. *
  106. * addPlugin('EmailNotify', ['sendTo' => 'evan@status.net']);
  107. * addPlugin('EmailNotify', ['sendTo' => 'brionv@status.net']);
  108. *
  109. */
  110. public $attr1 = null;
  111. public $attr2 = null;
  112. /**
  113. * Initializer for this plugin
  114. *
  115. * Plugins overload this method to do any initialization they need,
  116. * like connecting to remote servers or creating paths or so on.
  117. *
  118. * @return bool hook value; true means continue processing, false means stop.
  119. */
  120. public function initialize(): bool
  121. {
  122. return true;
  123. }
  124. /**
  125. * Cleanup for this plugin
  126. *
  127. * Plugins overload this method to do any cleanup they need,
  128. * like disconnecting from remote servers or deleting temp files or so on.
  129. *
  130. * @return bool hook value; true means continue processing, false means stop.
  131. */
  132. public function cleanup(): bool
  133. {
  134. return true;
  135. }
  136. /**
  137. * Database schema setup
  138. *
  139. * Plugins can add their own tables to the StatusNet database. Plugins
  140. * should use StatusNet's schema interface to add or delete tables. The
  141. * ensureTable() method provides an easy way to ensure a table's structure
  142. * and availability.
  143. *
  144. * By default, the schema is checked every time StatusNet is run (say, when
  145. * a Web page is hit). Admins can configure their systems to only check the
  146. * schema when the checkschema.php script is run, greatly improving performance.
  147. * However, they need to remember to run that script after installing or
  148. * upgrading a plugin!
  149. *
  150. * @return bool hook value; true means continue processing, false means stop.
  151. * @see Schema
  152. * @see ColumnDef
  153. */
  154. public function onCheckSchema(): bool
  155. {
  156. $schema = Schema::get();
  157. // For storing user-submitted flags on profiles
  158. $schema->ensureTable('user_greeting_count', User_greeting_count::schemaDef());
  159. return true;
  160. }
  161. /**
  162. * Map URLs to actions
  163. *
  164. * This event handler lets the plugin map URLs on the site to actions (and
  165. * thus an action handler class). Note that the action handler class for an
  166. * action will be named 'FoobarAction', where action = 'foobar'. The class
  167. * must be loaded in the onAutoload() method.
  168. *
  169. * @param URLMapper $m path-to-action mapper
  170. *
  171. * @return bool hook value; true means continue processing, false means stop.
  172. * @throws Exception If it can't connect our required routes
  173. */
  174. public function onRouterInitialized(URLMapper $m): bool
  175. {
  176. $m->connect(
  177. 'main/hello',
  178. ['action' => 'hello']
  179. );
  180. return true;
  181. }
  182. /**
  183. * Modify the default menu to link to our custom action
  184. *
  185. * Using event handlers, it's possible to modify the default UI for pages
  186. * almost without limit. In this method, we add a menu item to the default
  187. * primary menu for the interface to link to our action.
  188. *
  189. * The Action class provides a rich set of events to hook, as well as output
  190. * methods.
  191. *
  192. * @param Action $action The current action handler. Use this to
  193. * do any output.
  194. *
  195. * @return bool hook value; true means continue processing, false means stop.
  196. *
  197. * @throws Exception
  198. * @see Action
  199. */
  200. public function onEndPrimaryNav(Action $action): bool
  201. {
  202. // common_local_url() gets the correct URL for the action name
  203. // we provide
  204. $action->menuItem(
  205. common_local_url('hello'),
  206. // TRANS: Menu item in sample plugin.
  207. _m('Hello'),
  208. // TRANS: Menu item title in sample plugin.
  209. _m('A warm greeting'),
  210. false,
  211. 'nav_hello'
  212. );
  213. return true;
  214. }
  215. /**
  216. * Plugin version information/meta-data
  217. *
  218. * @param array $versions
  219. * @return bool hook true
  220. * @throws Exception
  221. */
  222. public function onPluginVersion(array &$versions): bool
  223. {
  224. $versions[] = [
  225. 'name' => 'Sample',
  226. 'version' => self::PLUGIN_VERSION,
  227. 'author' => 'Brion Vibber, Evan Prodromou',
  228. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/Sample',
  229. 'rawdescription' =>
  230. // TRANS: Plugin description.
  231. _m('A sample plugin to show basics of development for new hackers.')
  232. ];
  233. return true;
  234. }
  235. }