- foreach ($wgHooks[$event] as $index => $hook) {
-
- $object = NULL;
- $method = NULL;
- $func = NULL;
- $data = NULL;
- $have_data = false;
-
- /* $hook can be: a function, an object, an array of $function and $data,
- * an array of just a function, an array of object and method, or an
- * array of object, method, and data.
- */
-
- if (is_array($hook)) {
- if (count($hook) < 1) {
- wfDebugDieBacktrace("Empty array in hooks for " . $event . "\n");
- } else if (is_object($hook[0])) {
- $object =& $wgHooks[$event][$index][0];
- if (count($hook) < 2) {
- $method = "on" . $event;
- } else {
- $method = $hook[1];
- if (count($hook) > 2) {
- $data = $hook[2];
+ /**
+ * Call hook functions defined in Hooks::register
+ *
+ * Because programmers assign to $wgHooks, we need to be very
+ * careful about its contents. So, there's a lot more error-checking
+ * in here than would normally be necessary.
+ *
+ * @param $event String: event name
+ * @param $args Array: parameters passed to hook functions
+ * @return Boolean
+ */
+ public static function run( $event, $args = array() ) {
+ global $wgHooks;
+
+ // Return quickly in the most common case
+ if ( !isset( self::$handlers[$event] ) && !isset( $wgHooks[$event] ) ) {
+ return true;
+ }
+
+ if ( !is_array( self::$handlers ) ) {
+ throw new MWException( "Local hooks array is not an array!\n" );
+ }
+
+ if ( !is_array( $wgHooks ) ) {
+ throw new MWException( "Global hooks array is not an array!\n" );
+ }
+
+ $new_handlers = (array) self::$handlers;
+ $old_handlers = (array) $wgHooks;
+
+ $hook_array = array_merge( $new_handlers, $old_handlers );
+
+ if ( !is_array( $hook_array[$event] ) ) {
+ throw new MWException( "Hooks array for event '$event' is not an array!\n" );
+ }
+
+ foreach ( $hook_array[$event] as $index => $hook ) {
+ $object = null;
+ $method = null;
+ $func = null;
+ $data = null;
+ $have_data = false;
+ $closure = false;
+ $badhookmsg = false;
+
+ /**
+ * $hook can be: a function, an object, an array of $function and
+ * $data, an array of just a function, an array of object and
+ * method, or an array of object, method, and data.
+ */
+ if ( is_array( $hook ) ) {
+ if ( count( $hook ) < 1 ) {
+ throw new MWException( 'Empty array in hooks for ' . $event . "\n" );
+ } elseif ( is_object( $hook[0] ) ) {
+ $object = $hook_array[$event][$index][0];
+ if ( $object instanceof Closure ) {
+ $closure = true;
+ if ( count( $hook ) > 1 ) {
+ $data = $hook[1];
+ $have_data = true;
+ }
+ } else {
+ if ( count( $hook ) < 2 ) {
+ $method = 'on' . $event;
+ } else {
+ $method = $hook[1];
+ if ( count( $hook ) > 2 ) {
+ $data = $hook[2];
+ $have_data = true;
+ }
+ }
+ }
+ } elseif ( is_string( $hook[0] ) ) {
+ $func = $hook[0];
+ if ( count( $hook ) > 1) {
+ $data = $hook[1];