/** * @covers Hooks::isRegistered * @covers Hooks::register * @covers Hooks::getHandlers * @covers Hooks::run */ public function testNewStyleHookInteraction() { global $wgHooks; $a = new NothingClass(); $b = new NothingClass(); $wgHooks['MediaWikiHooksTest001'][] = $a; $this->assertTrue(Hooks::isRegistered('MediaWikiHooksTest001'), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered'); Hooks::register('MediaWikiHooksTest001', $b); $this->assertEquals(2, count(Hooks::getHandlers('MediaWikiHooksTest001')), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register'); $foo = 'quux'; $bar = 'qaax'; Hooks::run('MediaWikiHooksTest001', array(&$foo, &$bar)); $this->assertEquals(1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'); $this->assertEquals(1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register'); }
/** * Call a legacy hook that uses text instead of Content objects. * Will log a warning when a matching hook function is registered. * If the textual representation of the content is changed by the * hook function, a new Content object is constructed from the new * text. * * @param string $event Event name * @param array $args Parameters passed to hook functions * @param bool $warn Whether to log a warning. * Default to self::$enableDeprecationWarnings. * May be set to false for testing. * * @return bool True if no handler aborted the hook * * @see ContentHandler::$enableDeprecationWarnings */ public static function runLegacyHooks($event, $args = array(), $warn = null) { if ($warn === null) { $warn = self::$enableDeprecationWarnings; } if (!Hooks::isRegistered($event)) { return true; // nothing to do here } if ($warn) { // Log information about which handlers are registered for the legacy hook, // so we can find and fix them. $handlers = Hooks::getHandlers($event); $handlerInfo = array(); wfSuppressWarnings(); foreach ($handlers as $handler) { if (is_array($handler)) { if (is_object($handler[0])) { $info = get_class($handler[0]); } else { $info = $handler[0]; } if (isset($handler[1])) { $info .= '::' . $handler[1]; } } elseif (is_object($handler)) { $info = get_class($handler[0]); $info .= '::on' . $event; } else { $info = $handler; } $handlerInfo[] = $info; } wfRestoreWarnings(); wfWarn("Using obsolete hook {$event} via ContentHandler::runLegacyHooks()! Handlers: " . implode(', ', $handlerInfo), 2); } // convert Content objects to text $contentObjects = array(); $contentTexts = array(); foreach ($args as $k => $v) { if ($v instanceof Content) { /* @var Content $v */ $contentObjects[$k] = $v; $v = $v->serialize(); $contentTexts[$k] = $v; $args[$k] = $v; } } // call the hook functions $ok = wfRunHooks($event, $args); // see if the hook changed the text foreach ($contentTexts as $k => $orig) { /* @var Content $content */ $modified = $args[$k]; $content = $contentObjects[$k]; if ($modified !== $orig) { // text was changed, create updated Content object $content = $content->getContentHandler()->unserializeContent($modified); } $args[$k] = $content; } return $ok; }