Hooks.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * A tool for running hook functions.
  4. *
  5. * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. *
  21. * @author Evan Prodromou <evan@wikitravel.org>
  22. * @see hooks.txt
  23. * @file
  24. */
  25. /**
  26. * Hooks class.
  27. *
  28. * Used to supersede $wgHooks, because globals are EVIL.
  29. *
  30. * @since 1.18
  31. */
  32. class Hooks {
  33. /**
  34. * Array of events mapped to an array of callbacks to be run
  35. * when that event is triggered.
  36. */
  37. protected static $handlers = [];
  38. /**
  39. * Attach an event handler to a given hook.
  40. *
  41. * @param string $name Name of hook
  42. * @param callable $callback Callback function to attach
  43. *
  44. * @since 1.18
  45. */
  46. public static function register( $name, $callback ) {
  47. self::$handlers[$name][] = $callback;
  48. }
  49. /**
  50. * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
  51. * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
  52. *
  53. * @param string $name The name of the hook to clear.
  54. *
  55. * @since 1.21
  56. * @throws MWException If not in testing mode.
  57. * @codeCoverageIgnore
  58. */
  59. public static function clear( $name ) {
  60. if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
  61. throw new MWException( 'Cannot reset hooks in operation.' );
  62. }
  63. unset( self::$handlers[$name] );
  64. }
  65. /**
  66. * Returns true if a hook has a function registered to it.
  67. * The function may have been registered either via Hooks::register or in $wgHooks.
  68. *
  69. * @since 1.18
  70. *
  71. * @param string $name Name of hook
  72. * @return bool True if the hook has a function registered to it
  73. */
  74. public static function isRegistered( $name ) {
  75. global $wgHooks;
  76. return !empty( $wgHooks[$name] ) || !empty( self::$handlers[$name] );
  77. }
  78. /**
  79. * Returns an array of all the event functions attached to a hook
  80. * This combines functions registered via Hooks::register and with $wgHooks.
  81. *
  82. * @since 1.18
  83. *
  84. * @param string $name Name of the hook
  85. * @return array
  86. */
  87. public static function getHandlers( $name ) {
  88. global $wgHooks;
  89. if ( !self::isRegistered( $name ) ) {
  90. return [];
  91. } elseif ( !isset( self::$handlers[$name] ) ) {
  92. return $wgHooks[$name];
  93. } elseif ( !isset( $wgHooks[$name] ) ) {
  94. return self::$handlers[$name];
  95. } else {
  96. return array_merge( self::$handlers[$name], $wgHooks[$name] );
  97. }
  98. }
  99. /**
  100. * @param string $event Event name
  101. * @param array|callable $hook
  102. * @param array $args Array of parameters passed to hook functions
  103. * @param string|null $deprecatedVersion [optional]
  104. * @param string &$fname [optional] Readable name of hook [returned]
  105. * @return null|string|bool
  106. */
  107. private static function callHook( $event, $hook, array $args, $deprecatedVersion = null,
  108. &$fname = null
  109. ) {
  110. // Turn non-array values into an array. (Can't use casting because of objects.)
  111. if ( !is_array( $hook ) ) {
  112. $hook = [ $hook ];
  113. }
  114. if ( !array_filter( $hook ) ) {
  115. // Either array is empty or it's an array filled with null/false/empty.
  116. return null;
  117. }
  118. if ( is_array( $hook[0] ) ) {
  119. // First element is an array, meaning the developer intended
  120. // the first element to be a callback. Merge it in so that
  121. // processing can be uniform.
  122. $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
  123. }
  124. /**
  125. * $hook can be: a function, an object, an array of $function and
  126. * $data, an array of just a function, an array of object and
  127. * method, or an array of object, method, and data.
  128. */
  129. if ( $hook[0] instanceof Closure ) {
  130. $fname = "hook-$event-closure";
  131. $callback = array_shift( $hook );
  132. } elseif ( is_object( $hook[0] ) ) {
  133. $object = array_shift( $hook );
  134. $method = array_shift( $hook );
  135. // If no method was specified, default to on$event.
  136. if ( $method === null ) {
  137. $method = "on$event";
  138. }
  139. $fname = get_class( $object ) . '::' . $method;
  140. $callback = [ $object, $method ];
  141. } elseif ( is_string( $hook[0] ) ) {
  142. $fname = $callback = array_shift( $hook );
  143. } else {
  144. throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
  145. }
  146. // Run autoloader (workaround for call_user_func_array bug)
  147. // and throw error if not callable.
  148. if ( !is_callable( $callback ) ) {
  149. throw new MWException( 'Invalid callback ' . $fname . ' in hooks for ' . $event . "\n" );
  150. }
  151. // mark hook as deprecated, if deprecation version is specified
  152. if ( $deprecatedVersion !== null ) {
  153. wfDeprecated( "$event hook (used in $fname)", $deprecatedVersion );
  154. }
  155. // Call the hook.
  156. $hook_args = array_merge( $hook, $args );
  157. return call_user_func_array( $callback, $hook_args );
  158. }
  159. /**
  160. * Call hook functions defined in Hooks::register and $wgHooks.
  161. *
  162. * For the given hook event, fetch the array of hook events and
  163. * process them. Determine the proper callback for each hook and
  164. * then call the actual hook using the appropriate arguments.
  165. * Finally, process the return value and return/throw accordingly.
  166. *
  167. * For hook event that are not abortable through a handler's return value,
  168. * use runWithoutAbort() instead.
  169. *
  170. * @param string $event Event name
  171. * @param array $args Array of parameters passed to hook functions
  172. * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
  173. * @return bool True if no handler aborted the hook
  174. *
  175. * @throws Exception
  176. * @throws FatalError
  177. * @throws MWException
  178. * @since 1.22 A hook function is not required to return a value for
  179. * processing to continue. Not returning a value (or explicitly
  180. * returning null) is equivalent to returning true.
  181. */
  182. public static function run( $event, array $args = [], $deprecatedVersion = null ) {
  183. foreach ( self::getHandlers( $event ) as $hook ) {
  184. $retval = self::callHook( $event, $hook, $args, $deprecatedVersion );
  185. if ( $retval === null ) {
  186. continue;
  187. }
  188. // Process the return value.
  189. if ( is_string( $retval ) ) {
  190. // String returned means error.
  191. throw new FatalError( $retval );
  192. } elseif ( $retval === false ) {
  193. // False was returned. Stop processing, but no error.
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * Call hook functions defined in Hooks::register and $wgHooks.
  201. *
  202. * @param string $event Event name
  203. * @param array $args Array of parameters passed to hook functions
  204. * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
  205. * @return bool Always true
  206. * @throws MWException If a callback is invalid, unknown
  207. * @throws UnexpectedValueException If a callback returns an abort value.
  208. * @since 1.30
  209. */
  210. public static function runWithoutAbort( $event, array $args = [], $deprecatedVersion = null ) {
  211. foreach ( self::getHandlers( $event ) as $hook ) {
  212. $fname = null;
  213. $retval = self::callHook( $event, $hook, $args, $deprecatedVersion, $fname );
  214. if ( $retval !== null && $retval !== true ) {
  215. throw new UnexpectedValueException( "Invalid return from $fname for unabortable $event." );
  216. }
  217. }
  218. return true;
  219. }
  220. }