cleanup to mwsuggest.js & fix some things suggested by JSLint
[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 // Return quickly in the most common case
36 if ( !isset( $wgHooks[$event] ) ) {
37 return true;
38 }
39
40 if (!is_array($wgHooks)) {
41 throw new MWException("Global hooks array is not an array!\n");
42 return false;
43 }
44
45 if (!is_array($wgHooks[$event])) {
46 throw new MWException("Hooks array for event '$event' is not an array!\n");
47 return false;
48 }
49
50 foreach ($wgHooks[$event] as $index => $hook) {
51
52 $object = null;
53 $method = null;
54 $func = null;
55 $data = null;
56 $have_data = false;
57 $closure = false;
58
59 /* $hook can be: a function, an object, an array of $function and $data,
60 * an array of just a function, an array of object and method, or an
61 * array of object, method, and data.
62 */
63
64 if ( is_array( $hook ) ) {
65 if ( count( $hook ) < 1 ) {
66 throw new MWException("Empty array in hooks for " . $event . "\n");
67 } else if ( is_object( $hook[0] ) ) {
68 $object = $wgHooks[$event][$index][0];
69 if ( $object instanceof Closure ) {
70 $closure = true;
71 if ( count( $hook ) > 1 ) {
72 $data = $hook[1];
73 $have_data = true;
74 }
75 } else {
76 if ( count( $hook ) < 2 ) {
77 $method = "on" . $event;
78 } else {
79 $method = $hook[1];
80 if ( count( $hook ) > 2 ) {
81 $data = $hook[2];
82 $have_data = true;
83 }
84 }
85 }
86 } else if ( is_string( $hook[0] ) ) {
87 $func = $hook[0];
88 if ( count( $hook ) > 1) {
89 $data = $hook[1];
90 $have_data = true;
91 }
92 } else {
93 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
94 }
95 } else if ( is_string( $hook ) ) { # functions look like strings, too
96 $func = $hook;
97 } else if ( is_object( $hook ) ) {
98 $object = $wgHooks[$event][$index];
99 if ( $object instanceof Closure ) {
100 $closure = true;
101 } else {
102 $method = "on" . $event;
103 }
104 } else {
105 throw new MWException( "Unknown datatype in hooks for " . $event . "\n" );
106 }
107
108 /* We put the first data element on, if needed. */
109
110 if ( $have_data ) {
111 $hook_args = array_merge(array($data), $args);
112 } else {
113 $hook_args = $args;
114 }
115
116 if ( $closure ) {
117 $callback = $object;
118 $func = "hook-$event-closure";
119 } elseif ( isset( $object ) ) {
120 $func = get_class( $object ) . '::' . $method;
121 $callback = array( $object, $method );
122 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
123 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
124 } else {
125 $callback = $func;
126 }
127
128 // Run autoloader (workaround for call_user_func_array bug)
129 is_callable( $callback );
130
131 /* Call the hook. */
132 wfProfileIn( $func );
133 $retval = call_user_func_array( $callback, $hook_args );
134 wfProfileOut( $func );
135
136 /* String return is an error; false return means stop processing. */
137
138 if ( is_string( $retval ) ) {
139 global $wgOut;
140 $wgOut->showFatalError( $retval );
141 return false;
142 } elseif( $retval === null ) {
143 if ( $closure ) {
144 $prettyFunc = "$event closure";
145 } elseif( is_array( $callback ) ) {
146 if( is_object( $callback[0] ) ) {
147 $prettyClass = get_class( $callback[0] );
148 } else {
149 $prettyClass = strval( $callback[0] );
150 }
151 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
152 } else {
153 $prettyFunc = strval( $callback );
154 }
155 throw new MWException( "Detected bug in an extension! " .
156 "Hook $prettyFunc failed to return a value; " .
157 "should return true to continue hook processing or false to abort." );
158 } else if ( !$retval ) {
159 return false;
160 }
161 }
162
163 return true;
164 }