SamplePlugin.php 8.5 KB

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