#3364: pt_br name typo
[mediawiki.git] / includes / Hooks.php
blob3c12332012cd787bc680f0d8628a82d40f86bbc7
1 <?php
2 /**
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * @author Evan Prodromou <evan@wikitravel.org>
21 * @package MediaWiki
22 * @see hooks.txt
25 if (defined('MEDIAWIKI')) {
27 /**
28 * Because programmers assign to $wgHooks, we need to be very
29 * careful about its contents. So, there's a lot more error-checking
30 * in here than would normally be necessary.
33 function wfRunHooks($event, $args = null) {
35 global $wgHooks;
36 $fname = 'wfRunHooks';
37 wfProfileIn( $fname );
39 if (!is_array($wgHooks)) {
40 wfDieDebugBacktrace("Global hooks array is not an array!\n");
41 wfProfileOut( $fname );
42 return false;
45 if (!array_key_exists($event, $wgHooks)) {
46 wfProfileOut( $fname );
47 return true;
50 if (!is_array($wgHooks[$event])) {
51 wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
52 wfProfileOut( $fname );
53 return false;
56 foreach ($wgHooks[$event] as $hook) {
58 $object = NULL;
59 $method = NULL;
60 $func = NULL;
61 $data = NULL;
62 $have_data = false;
64 /* $hook can be: a function, an object, an array of $function and $data,
65 * an array of just a function, an array of object and method, or an
66 * array of object, method, and data.
69 if (is_array($hook)) {
70 if (count($hook) < 1) {
71 wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
72 } else if (is_object($hook[0])) {
73 $object = $hook[0];
74 if (count($hook) < 2) {
75 $method = "on" . $event;
76 } else {
77 $method = $hook[1];
78 if (count($hook) > 2) {
79 $data = $hook[2];
80 $have_data = true;
83 } else if (is_string($hook[0])) {
84 $func = $hook[0];
85 if (count($hook) > 1) {
86 $data = $hook[1];
87 $have_data = true;
89 } else {
90 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
92 } else if (is_string($hook)) { # functions look like strings, too
93 $func = $hook;
94 } else if (is_object($hook)) {
95 $object = $hook;
96 $method = "on" . $event;
97 } else {
98 wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
101 /* We put the first data element on, if needed. */
103 if ($have_data) {
104 $hook_args = array_merge(array($data), $args);
105 } else {
106 $hook_args = $args;
110 if ( $object ) {
111 $func = get_class( $object ) . '::' . $method;
114 /* Call the hook. */
115 wfProfileIn( $func );
116 if ($object) {
117 $retval = call_user_func_array(array($object, $method), $hook_args);
118 } else {
119 $retval = call_user_func_array($func, $hook_args);
121 wfProfileOut( $func );
123 /* String return is an error; false return means stop processing. */
125 if (is_string($retval)) {
126 global $wgOut;
127 $wgOut->fatalError($retval);
128 wfProfileOut( $fname );
129 return false;
130 } else if (!$retval) {
131 wfProfileOut( $fname );
132 return false;
136 wfProfileOut( $fname );
137 return true;
139 } /* if defined(MEDIAWIKI) */