00001 <?php
00031 function wfRunHooks($event, $args = array()) {
00032
00033 global $wgHooks;
00034
00035 if (!is_array($wgHooks)) {
00036 throw new MWException("Global hooks array is not an array!\n");
00037 return false;
00038 }
00039
00040 if (!array_key_exists($event, $wgHooks)) {
00041 return true;
00042 }
00043
00044 if (!is_array($wgHooks[$event])) {
00045 throw new MWException("Hooks array for event '$event' is not an array!\n");
00046 return false;
00047 }
00048
00049 foreach ($wgHooks[$event] as $index => $hook) {
00050
00051 $object = NULL;
00052 $method = NULL;
00053 $func = NULL;
00054 $data = NULL;
00055 $have_data = false;
00056
00057
00058
00059
00060
00061
00062 if (is_array($hook)) {
00063 if (count($hook) < 1) {
00064 throw new MWException("Empty array in hooks for " . $event . "\n");
00065 } else if (is_object($hook[0])) {
00066 $object = $wgHooks[$event][$index][0];
00067 if (count($hook) < 2) {
00068 $method = "on" . $event;
00069 } else {
00070 $method = $hook[1];
00071 if (count($hook) > 2) {
00072 $data = $hook[2];
00073 $have_data = true;
00074 }
00075 }
00076 } else if (is_string($hook[0])) {
00077 $func = $hook[0];
00078 if (count($hook) > 1) {
00079 $data = $hook[1];
00080 $have_data = true;
00081 }
00082 } else {
00083 var_dump( $wgHooks );
00084 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
00085 }
00086 } else if (is_string($hook)) { # functions look like strings, too
00087 $func = $hook;
00088 } else if (is_object($hook)) {
00089 $object = $wgHooks[$event][$index];
00090 $method = "on" . $event;
00091 } else {
00092 throw new MWException("Unknown datatype in hooks for " . $event . "\n");
00093 }
00094
00095
00096
00097 if ($have_data) {
00098 $hook_args = array_merge(array($data), $args);
00099 } else {
00100 $hook_args = $args;
00101 }
00102
00103 if ( isset( $object ) ) {
00104 $func = get_class( $object ) . '::' . $method;
00105 $callback = array( $object, $method );
00106 } elseif ( false !== ( $pos = strpos( $func, '::' ) ) ) {
00107 $callback = array( substr( $func, 0, $pos ), substr( $func, $pos + 2 ) );
00108 } else {
00109 $callback = $func;
00110 }
00111
00112
00113 is_callable( $callback );
00114
00115
00116 wfProfileIn( $func );
00117 $retval = call_user_func_array( $callback, $hook_args );
00118 wfProfileOut( $func );
00119
00120
00121
00122 if (is_string($retval)) {
00123 global $wgOut;
00124 $wgOut->showFatalError($retval);
00125 return false;
00126 } elseif( $retval === null ) {
00127 if( is_array( $callback ) ) {
00128 if( is_object( $callback[0] ) ) {
00129 $prettyClass = get_class( $callback[0] );
00130 } else {
00131 $prettyClass = strval( $callback[0] );
00132 }
00133 $prettyFunc = $prettyClass . '::' . strval( $callback[1] );
00134 } else {
00135 $prettyFunc = strval( $callback );
00136 }
00137 throw new MWException( "Detected bug in an extension! " .
00138 "Hook $prettyFunc failed to return a value; " .
00139 "should return true to continue hook processing or false to abort." );
00140 } else if (!$retval) {
00141 return false;
00142 }
00143 }
00144
00145 return true;
00146 }