3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @author Evan Prodromou <evan@wikitravel.org>
27 * Because programmers assign to $wgHooks, we need to be very
28 * careful about its contents. So, there's a lot more error-checking
29 * in here than would normally be necessary.
31 function wfRunHooks($event, $args = null) {
34 $fname = 'wfRunHooks';
36 if (!is_array($wgHooks)) {
37 throw new MWException("Global hooks array is not an array!\n");
41 if (!array_key_exists($event, $wgHooks)) {
45 if (!is_array($wgHooks[$event])) {
46 throw new MWException("Hooks array for event '$event' is not an array!\n");
50 foreach ($wgHooks[$event] as $index => $hook) {
58 /* $hook can be: a function, an object, an array of $function and $data,
59 * an array of just a function, an array of object and method, or an
60 * array of object, method, and data.
63 if (is_array($hook)) {
64 if (count($hook) < 1) {
65 throw new MWException("Empty array in hooks for " . $event . "\n");
66 } else if (is_object($hook[0])) {
67 $object =& $wgHooks[$event][$index][0];
68 if (count($hook) < 2) {
69 $method = "on" . $event;
72 if (count($hook) > 2) {
77 } else if (is_string($hook[0])) {
79 if (count($hook) > 1) {
85 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
87 } else if (is_string($hook)) { # functions look like strings, too
89 } else if (is_object($hook)) {
90 $object =& $wgHooks[$event][$index];
91 $method = "on" . $event;
93 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
96 /* We put the first data element on, if needed. */
99 $hook_args = array_merge(array($data), $args);
105 if ( isset( $object ) ) {
106 $func = get_class( $object ) . '::' . $method;
110 wfProfileIn( $func );
111 if( isset( $object ) ) {
112 $retval = call_user_func_array(array(&$object, $method), $hook_args);
114 $retval = call_user_func_array($func, $hook_args);
116 wfProfileOut( $func );
118 /* String return is an error; false return means stop processing. */
120 if (is_string($retval)) {
122 $wgOut->showFatalError($retval);
124 } else if (!$retval) {