2eecfd72f2e84683fbc06310f624d223993fc16a
[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 * @package MediaWiki
22 * @see hooks.txt
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 = null) {
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
57 /* $hook can be: a function, an object, an array of $function and $data,
58 * an array of just a function, an array of object and method, or an
59 * array of object, method, and data.
60 */
61
62 if (is_array($hook)) {
63 if (count($hook) < 1) {
64 throw new MWException("Empty array in hooks for " . $event . "\n");
65 } else if (is_object($hook[0])) {
66 $object = $wgHooks[$event][$index][0];
67 if (count($hook) < 2) {
68 $method = "on" . $event;
69 } else {
70 $method = $hook[1];
71 if (count($hook) > 2) {
72 $data = $hook[2];
73 $have_data = true;
74 }
75 }
76 } else if (is_string($hook[0])) {
77 $func = $hook[0];
78 if (count($hook) > 1) {
79 $data = $hook[1];
80 $have_data = true;
81 }
82 } else {
83 var_dump( $wgHooks );
84 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
85 }
86 } else if (is_string($hook)) { # functions look like strings, too
87 $func = $hook;
88 } else if (is_object($hook)) {
89 $object = $wgHooks[$event][$index];
90 $method = "on" . $event;
91 } else {
92 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
93 }
94
95 /* We put the first data element on, if needed. */
96
97 if ($have_data) {
98 $hook_args = array_merge(array($data), $args);
99 } else {
100 $hook_args = $args;
101 }
102
103 if ( isset( $object ) ) {
104 $func = get_class( $object ) . '::' . $method;
105 $callback = array( $object, $method );
106 } elseif ( false !== ( $pos = strpos( '::', $func ) ) ) {
107 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
108 } else {
109 $callback = $func;
110 }
111
112 /* Call the hook. */
113 wfProfileIn( $func );
114 $retval = call_user_func_array( $callback, $hook_args );
115 wfProfileOut( $func );
116
117 /* String return is an error; false return means stop processing. */
118
119 if (is_string($retval)) {
120 global $wgOut;
121 $wgOut->showFatalError($retval);
122 return false;
123 } else if (!$retval) {
124 return false;
125 }
126 }
127
128 return true;
129 }
130 ?>