Update documentation:
[lhc/web/wiklou.git] / includes / Hooks.php
1 <?php
2 /**
3 * Hooks.php -- a tool for running hook functions
4 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
5 *
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.
10 *
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.
15 *
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
19 *
20 * @author Evan Prodromou <evan@wikitravel.org>
21 * @see hooks.txt
22 * @file
23 */
24
25
26 /**
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.
30 */
31 function wfRunHooks($event, $args = array()) {
32
33 global $wgHooks;
34
35 if (!is_array($wgHooks)) {
36 throw new MWException("Global hooks array is not an array!\n");
37 return false;
38 }
39
40 if (!array_key_exists($event, $wgHooks)) {
41 return true;
42 }
43
44 if (!is_array($wgHooks[$event])) {
45 throw new MWException("Hooks array for event '$event' is not an array!\n");
46 return false;
47 }
48
49 foreach ($wgHooks[$event] as $index => $hook) {
50
51 $object = NULL;
52 $method = NULL;
53 $func = NULL;
54 $data = NULL;
55 $have_data = false;
56 $closure = false;
57
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.
61 */
62
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 ( $object instanceof Closure ) {
69 $closure = true;
70 if ( count( $hook ) > 1 ) {
71 $data = $hook[1];
72 $have_data = true;
73 }
74 } else {
75 if ( count( $hook ) < 2 ) {
76 $method = "on" . $event;
77 } else {
78 $method = $hook[1];
79 if ( count( $hook ) > 2 ) {
80 $data = $hook[2];
81 $have_data = true;
82 }
83 }
84 }
85 } else if ( is_string( $hook[0] ) ) {
86 $func = $hook[0];
87 if ( count( $hook ) > 1) {
88 $data = $hook[1];
89 $have_data = true;
90 }
91 } else {
92 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
93 }
94 } else if ( is_string( $hook ) ) { # functions look like strings, too
95 $func = $hook;
96 } else if ( is_object( $hook ) ) {
97 $object = $wgHooks[$event][$index];
98 if ( $object instanceof Closure ) {
99 $closure = true;
100 } else {
101 $method = "on" . $event;
102 }
103 } else {
104 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
105 }
106
107 /* We put the first data element on, if needed. */
108
109 if ( $have_data ) {
110 $hook_args = array_merge(array($data), $args);
111 } else {
112 $hook_args = $args;
113 }
114
115 if ( $closure ) {
116 $callback = $object;
117 $func = "hook-$event-closure";
118 } elseif ( isset( $object ) ) {
119 $func = get_class( $object ) . '::' . $method;
120 $callback = array( $object, $method );
121 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
122 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
123 } else {
124 $callback = $func;
125 }
126
127 // Run autoloader (workaround for call_user_func_array bug)
128 is_callable( $callback );
129
130 /* Call the hook. */
131 wfProfileIn( $func );
132 $retval = call_user_func_array( $callback, $hook_args );
133 wfProfileOut( $func );
134
135 /* String return is an error; false return means stop processing. */
136
137 if ( is_string( $retval ) ) {
138 global $wgOut;
139 $wgOut->showFatalError( $retval );
140 return false;
141 } elseif( $retval === null ) {
142 if ( $closure ) {
143 $prettyFunc = "$event closure";
144 } elseif( is_array( $callback ) ) {
145 if( is_object( $callback[0] ) ) {
146 $prettyClass = get_class( $callback[0] );
147 } else {
148 $prettyClass = strval( $callback[0] );
149 }
150 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
151 } else {
152 $prettyFunc = strval( $callback );
153 }
154 throw new MWException( "Detected bug in an extension! " .
155 "Hook $prettyFunc failed to return a value; " .
156 "should return true to continue hook processing or false to abort." );
157 } else if ( !$retval ) {
158 return false;
159 }
160 }
161
162 return true;
163 }