]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/Hooks.php
MediaWiki 1.11.0
[autoinstallsdev/mediawiki.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  */
23
24
25 /**
26  * Because programmers assign to $wgHooks, we need to be very
27  * careful about its contents. So, there's a lot more error-checking
28  * in here than would normally be necessary.
29  */
30 function wfRunHooks($event, $args = null) {
31
32         global $wgHooks;
33
34         if (!is_array($wgHooks)) {
35                 throw new MWException("Global hooks array is not an array!\n");
36                 return false;
37         }
38
39         if (!array_key_exists($event, $wgHooks)) {
40                 return true;
41         }
42
43         if (!is_array($wgHooks[$event])) {
44                 throw new MWException("Hooks array for event '$event' is not an array!\n");
45                 return false;
46         }
47
48         foreach ($wgHooks[$event] as $index => $hook) {
49
50                 $object = NULL;
51                 $method = NULL;
52                 $func = NULL;
53                 $data = NULL;
54                 $have_data = false;
55
56                 /* $hook can be: a function, an object, an array of $function and $data,
57                  * an array of just a function, an array of object and method, or an
58                  * array of object, method, and data.
59                  */
60
61                 if (is_array($hook)) {
62                         if (count($hook) < 1) {
63                                 throw new MWException("Empty array in hooks for " . $event . "\n");
64                         } else if (is_object($hook[0])) {
65                                 $object = $wgHooks[$event][$index][0];
66                                 if (count($hook) < 2) {
67                                         $method = "on" . $event;
68                                 } else {
69                                         $method = $hook[1];
70                                         if (count($hook) > 2) {
71                                                 $data = $hook[2];
72                                                 $have_data = true;
73                                         }
74                                 }
75                         } else if (is_string($hook[0])) {
76                                 $func = $hook[0];
77                                 if (count($hook) > 1) {
78                                         $data = $hook[1];
79                                         $have_data = true;
80                                 }
81                         } else {
82                                 var_dump( $wgHooks );
83                                 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
84                         }
85                 } else if (is_string($hook)) { # functions look like strings, too
86                         $func = $hook;
87                 } else if (is_object($hook)) {
88                         $object = $wgHooks[$event][$index];
89                         $method = "on" . $event;
90                 } else {
91                         throw new MWException("Unknown datatype in hooks for " . $event . "\n");
92                 }
93
94                 /* We put the first data element on, if needed. */
95
96                 if ($have_data) {
97                         $hook_args = array_merge(array($data), $args);
98                 } else {
99                         $hook_args = $args;
100                 }
101
102                 if ( isset( $object ) ) {
103                         $func = get_class( $object ) . '::' . $method;
104                         $callback = array( $object, $method );
105                 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
106                         $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
107                 } else {
108                         $callback = $func;
109                 }
110
111                 /* Call the hook. */
112                 wfProfileIn( $func );
113                 $retval = call_user_func_array( $callback, $hook_args );
114                 wfProfileOut( $func );
115
116                 /* String return is an error; false return means stop processing. */
117
118                 if (is_string($retval)) {
119                         global $wgOut;
120                         $wgOut->showFatalError($retval);
121                         return false;
122                 } elseif( $retval === null ) {
123                         if( is_array( $callback ) ) {
124                                 if( is_object( $callback[0] ) ) {
125                                         $prettyClass = get_class( $callback[0] );
126                                 } else {
127                                         $prettyClass = strval( $callback[0] );
128                                 }
129                                 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
130                         } else {
131                                 $prettyFunc = strval( $callback );
132                         }
133                         throw new MWException( "Detected bug in an extension! " .
134                                 "Hook $prettyFunc failed to return a value; " .
135                                 "should return true to continue hook processing or false to abort." );
136                 } else if (!$retval) {
137                         return false;
138                 }
139         }
140
141         return true;
142 }
143