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>
26 function _wfInvokeInternalGoop($event, $hook) {
33 if (is_array($hook)) {
34 if (count($hook) < 1) {
35 throw new MWException("Empty array in hooks for " . $event . "\n");
36 } else if (is_object($hook[0])) {
38 if (count($hook) < 2) {
39 $method = "on" . $event;
42 if (count($hook) > 2) {
47 } else if (is_string($hook[0])) {
49 if (count($hook) > 1) {
55 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
57 } else if (is_string($hook)) { # functions look like strings, too
59 } else if (is_object($hook)) {
61 $method = "on" . $event;
63 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
66 if ( isset( $object ) ) {
67 $func = get_class( $object ) . '::' . $method;
68 $callback = array( $object, $method );
69 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
70 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos +
2 ) );
75 return array($callback, $func, $data);
78 /* Return a string describing the hook for debugging purposes. */
79 function wfFormatInvocation($event, $hook, $args = array()) {
80 list($callback, $func, $data) = _wfInvokeInternalGoop($event, $hook, $args);
82 if( is_array( $callback ) ) {
83 if( is_object( $callback[0] ) ) {
84 $prettyClass = get_class( $callback[0] );
86 $prettyClass = strval( $callback[0] );
88 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
90 $prettyFunc = strval( $callback );
97 * Invoke a function dynamically.
98 * $event is the name of the invocation; this is used if the hook specifies
99 * an object but no method name; 'on$event' is then invoked on the object.
100 * $hook can be: a function, an object, an array of $function and $data,
101 * an array of just a function, an array of object and method, or an
102 * array of object, method, and data.
103 * If arguments are provided both as part of the hook itself, and when
104 * calling wfCallFancyCallback, the two arrays are merged.
106 function wfInvoke($event, $hook, $args = array()) {
107 list($callback, $func, $data) = _wfInvokeInternalGoop($event, $hook, $args);
109 /* We put the first data element on, if needed. */
111 $hook_args = array_merge(array($data), $args);
116 // Run autoloader (workaround for call_user_func_array bug)
117 is_callable( $callback );
120 wfProfileIn( $func );
121 $retval = call_user_func_array( $callback, $hook_args );
122 wfProfileOut( $func );
128 * Because programmers assign to $wgHooks, we need to be very
129 * careful about its contents. So, there's a lot more error-checking
130 * in here than would normally be necessary.
132 function wfRunHooks($event, $args = array()) {
136 if (!is_array($wgHooks)) {
137 throw new MWException("Global hooks array is not an array!\n");
141 if (!array_key_exists($event, $wgHooks)) {
145 if (!is_array($wgHooks[$event])) {
146 throw new MWException("Hooks array for event '$event' is not an array!\n");
150 foreach ($wgHooks[$event] as $index => $hook) {
152 $retval = wfInvoke($event, $hook, $args);
154 /* String return is an error; false return means stop processing. */
156 if (is_string($retval)) {
158 $wgOut->showFatalError($retval);
160 } elseif( $retval === null ) {
161 $prettyFunc = wfFormatInvocation($event, $hook, $args);
162 throw new MWException( "Detected bug in an extension! " .
163 "Hook $prettyFunc failed to return a value; " .
164 "should return true to continue hook processing or false to abort." );
165 } else if (!$retval) {