Consider, for example, two options in MediaWiki. One reverses the
order of a title before displaying the article; the other converts the
title to all uppercase letters. Currently, in MediaWiki code, we
-handle this as follows:
+would handle this as follows (note: not real code, here):
function showAnArticle($article) {
global $wgReverseTitle, $wgCapitalizeTitle;
becomes:
function showAnArticle($article) {
- if (wfRunHooks('ArticleShow', $article)) {
+
+ if (wfRunHooks('ArticleShow', array(&$article))) {
+
# code to actually show the article goes here
- wfRunHooks('ArticleShowComplete', $article);
+
+ wfRunHooks('ArticleShowComplete', array(&$article));
}
}
In addition, we can take all the code that deals with the little-used
title-reversing options (say) and put it in one place. Instead of
-having a little title-reversing if-block spread all over the codebase
+having little title-reversing if-blocks spread all over the codebase
in showAnArticle, deleteAnArticle, exportArticle, etc., we can
concentrate it all in an extension file:
do a grep-find to see where the $wgReverseTitle variable is used, say.
If the code is well enough isolated, it can even be excluded when not
-used -- making for some slight savings in memory and time at runtime.
-Admins who want to have all the reversed titles can add:
+used -- making for some slight savings in memory and load-up
+performance at runtime. Admins who want to have all the reversed
+titles can add:
require_once('extensions/ReverseTitle.php');
saved: once for 'TimStarling', and once for 'brion'.
Hooks can return three possible values:
+
* true: the hook has operated successfully
* "some string": an error occurred; processing should
stop and the error should be shown to the user
}
Returning false makes less sense for events where the action is
-complete, and will probably be ignored.
+complete, and will normally be ignored.
==Using hooks==
# ...
function protect() {
global $wgUser;
- if (wfRunHooks('ArticleProtect', $this, $wgUser)) {
+ if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser))) {
# protect the article
- wfRunHooks('ArticleProtectComplete', $this, $wgUser);
+ wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser));
}
}
action already). Checking the return value matters more for "before"
hooks than for "complete" hooks.
+Note that hook parameters are passed in an array; this is a necessary
+inconvenience to make it possible to pass reference values (that can
+be changed) into the hook code. Also note that earlier versions of
+wfRunHooks took a variable number of arguments; the array() calling
+protocol came about after MediaWiki 1.4rc1.
+
==Events and parameters==
This is a list of known events and parameters; please add to it if
$subject: subject of the mail
$text: text of the mail
+'TitleMoveComplete': after moving an article (title)
+$old: old title
+$nt: new title
+$user: user who did the move
+$oldid: old article database ID
+$newid: new article database ID
+
'UnknownAction': An unknown "action" has occured (useful for defining
your own actions)
$action: action name
return;
}
- if (wfRunHooks('WatchArticle', $wgUser, $this)) {
+ if (wfRunHooks('WatchArticle', array(&$wgUser, &$this))) {
$wgUser->addWatch( $this->mTitle );
$wgUser->saveSettings();
- wfRunHooks('WatchArticleComplete', $wgUser, $this);
-
+ wfRunHooks('WatchArticleComplete', array(&$wgUser, &$this));
+
$wgOut->setPagetitle( wfMsg( 'addedwatch' ) );
$wgOut->setRobotpolicy( 'noindex,follow' );
return;
}
- if (wfRunHooks('UnwatchArticle', $wgUser, $this)) {
+ if (wfRunHooks('UnwatchArticle', array(&$wgUser, &$this))) {
$wgUser->removeWatch( $this->mTitle );
$wgUser->saveSettings();
- wfRunHooks('UnwatchArticleComplete', $wgUser, $this);
+ wfRunHooks('UnwatchArticleComplete', array(&$wgUser, &$this));
$wgOut->setPagetitle( wfMsg( 'removedwatch' ) );
$wgOut->setRobotpolicy( 'noindex,follow' );
if( !$moveonly ) {
$restrictions .= ":edit=" . $limit;
}
- if (wfRunHooks('ArticleProtect', $this, $wgUser, $limit == 'sysop', $reason, $moveonly)) {
+ if (wfRunHooks('ArticleProtect', array(&$this, &$wgUser, $limit == 'sysop', $reason, $moveonly))) {
$dbw =& wfGetDB( DB_MASTER );
$dbw->update( 'page',
), 'Article::protect'
);
- wfRunHooks('ArticleProtectComplete', $this, $wgUser, $limit == 'sysop', $reason, $moveonly);
+ wfRunHooks('ArticleProtectComplete', array(&$this, &$wgUser, $limit == 'sysop', $reason, $moveonly));
$log = new LogPage( 'protect' );
if ( $limit === '' ) {
$fname = 'Article::doDelete';
wfDebug( $fname."\n" );
- if (wfRunHooks('ArticleDelete', $this, $wgUser, $reason)) {
+ if (wfRunHooks('ArticleDelete', array(&$this, &$wgUser, &$reason))) {
if ( $this->doDeleteArticle( $reason ) ) {
$deleted = $this->mTitle->getPrefixedText();
$wgOut->addHTML( '<p>' . $text . "</p>\n" );
$wgOut->returnToMain( false );
- wfRunHooks('ArticleDeleteComplete', $this, $wgUser, $reason);
+ wfRunHooks('ArticleDeleteComplete', array(&$this, &$wgUser, $reason));
} else {
$wgOut->fatalError( wfMsg( 'cannotdelete' ) );
}
$wgOut->redirect( $this->mTitle->getFullURL() );
return;
}
- if (wfRunHooks('ArticleSave', $this->mArticle, $wgUser, $this->textbox1,
- $this->summary, $this->minoredit, $this->watchthis, NULL))
+ if (wfRunHooks('ArticleSave', array(&$this->mArticle, &$wgUser, &$this->textbox1,
+ &$this->summary, &$this->minoredit, &$this->watchthis, NULL)))
{
$this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
$this->minoredit, $this->watchthis );
- wfRunHooks('ArticleSaveComplete', $this->mArticle, $wgUser, $this->textbox1,
- $this->summary, $this->minoredit, $this->watchthis, NULL);
+ wfRunHooks('ArticleSaveComplete', array(&$this->mArticle, &$wgUser, $this->textbox1,
+ $this->summary, $this->minoredit,
+ $this->watchthis, NULL));
}
return;
}
}
}
- if (wfRunHooks('ArticleSave', $this->mArticle, $wgUser, $text, $this->summary,
- $this->minoredit, $this->watchthis, $sectionanchor))
+ if (wfRunHooks('ArticleSave', array(&$this->mArticle, &$wgUser, &$text,
+ &$this->summary, &$this->minoredit,
+ &$this->watchthis, &$sectionanchor)))
{
# update the article here
if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
$this->watchthis, '', $sectionanchor ))
{
- wfRunHooks('ArticleSaveComplete', $this->mArticle, $wgUser, $text, $this->summary,
- $this->minoredit, $this->watchthis, $sectionanchor);
+ wfRunHooks('ArticleSaveComplete', array(&$this->mArticle, &$wgUser, $text,
+ $this->summary, $this->minoredit,
+ $this->watchthis, $sectionanchor));
return;
}
else
<?php
/**
* Hooks.php -- a tool for running hook functions
- * Copyright 2004, Evan Prodromou <evan@wikitravel.org>.
+ * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
if (defined('MEDIAWIKI')) {
-/**
- * Because programmers assign to $wgHooks, we need to be very
- * careful about its contents. So, there's a lot more error-checking
- * in here than would normally be necessary.
- */
-function wfRunHooks() {
+ /**
+ * Because programmers assign to $wgHooks, we need to be very
+ * careful about its contents. So, there's a lot more error-checking
+ * in here than would normally be necessary.
+ */
+
+ function wfRunHooks($event, $args) {
- global $wgHooks;
-
- if (!is_array($wgHooks)) {
- wfDieDebugBacktrace("Global hooks array is not an array!\n");
- return false;
- }
-
- $args = func_get_args();
-
- if (count($args) < 1) {
- wfDieDebugBacktrace("No event name given for wfRunHooks().\n");
- return false;
- }
-
- $event = array_shift($args);
-
- if (!array_key_exists($event, $wgHooks)) {
- return true;
- }
+ global $wgHooks;
- if (!is_array($wgHooks[$event])) {
- wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
- return false;
- }
+ if (!is_array($wgHooks)) {
+ wfDieDebugBacktrace("Global hooks array is not an array!\n");
+ return false;
+ }
- foreach ($wgHooks[$event] as $hook) {
+ if (!array_key_exists($event, $wgHooks)) {
+ return true;
+ }
- $object = NULL;
- $method = NULL;
- $func = NULL;
- $data = NULL;
- $have_data = false;
-
- /* $hook can be: a function, an object, an array of $function and $data,
- * an array of just a function, an array of object and method, or an
- * array of object, method, and data.
- */
+ if (!is_array($wgHooks[$event])) {
+ wfDieDebugBacktrace("Hooks array for event '$event' is not an array!\n");
+ return false;
+ }
- if (is_array($hook)) {
- if (count($hook) < 1) {
- wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
- } else if (is_object($hook[0])) {
- $object = $hook[0];
- if (count($hook) < 2) {
- $method = "on" . $event;
- } else {
- $method = $hook[1];
- if (count($hook) > 2) {
- $data = $hook[2];
+ foreach ($wgHooks[$event] as $hook) {
+
+ $object = NULL;
+ $method = NULL;
+ $func = NULL;
+ $data = NULL;
+ $have_data = false;
+
+ /* $hook can be: a function, an object, an array of $function and $data,
+ * an array of just a function, an array of object and method, or an
+ * array of object, method, and data.
+ */
+
+ if (is_array($hook)) {
+ if (count($hook) < 1) {
+ wfDieDebugBacktrace("Empty array in hooks for " . $event . "\n");
+ } else if (is_object($hook[0])) {
+ $object = $hook[0];
+ if (count($hook) < 2) {
+ $method = "on" . $event;
+ } else {
+ $method = $hook[1];
+ if (count($hook) > 2) {
+ $data = $hook[2];
+ $have_data = true;
+ }
+ }
+ } else if (is_string($hook[0])) {
+ $func = $hook[0];
+ if (count($hook) > 1) {
+ $data = $hook[1];
$have_data = true;
}
+ } else {
+ wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
}
- } else if (is_string($hook[0])) {
- $func = $hook[0];
- if (count($hook) > 1) {
- $data = $hook[1];
- $have_data = true;
- }
+ } else if (is_string($hook)) { # functions look like strings, too
+ $func = $hook;
+ } else if (is_object($hook)) {
+ $object = $hook;
+ $method = "on" . $event;
} else {
wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
}
- } else if (is_string($hook)) { # functions look like strings, too
- $func = $hook;
- } else if (is_object($hook)) {
- $object = $hook;
- $method = "on" . $event;
- } else {
- wfDieDebugBacktrace("Unknown datatype in hooks for " . $event . "\n");
- }
-
- if ($have_data) {
- $hook_args = array_merge(array($data), $args);
- } else {
- $hook_args = $args;
- }
-
- if ($object) {
- $retval = call_user_func_array(array($object, $method), $hook_args);
- } else {
- $retval = call_user_func_array($func, $hook_args);
+
+ /* We put the first data element on, if needed. */
+
+ if ($have_data) {
+ $hook_args = array_merge(array($data), $args);
+ } else {
+ $hook_args = $args;
+ }
+
+ /* Call the hook. */
+
+ if ($object) {
+ $retval = call_user_func_array(array($object, $method), $hook_args);
+ } else {
+ $retval = call_user_func_array($func, $hook_args);
+ }
+
+ /* String return is an error; false return means stop processing. */
+
+ if (is_string($retval)) {
+ global $wgOut;
+ $wgOut->fatalError($retval);
+ return false;
+ } else if (!$retval) {
+ return false;
+ }
}
- if (is_string($retval)) {
- global $wgOut;
- $wgOut->fatalError($retval);
- return false;
- } else if (!$retval) {
- return false;
- }
+ return true;
}
-
- return true;
-}
} /* if defined(MEDIAWIKI) */
-?>
\ No newline at end of file
+?>
\ No newline at end of file
$ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
$this->BlockReason, wfTimestampNow(), 0, $expiry );
- if (wfRunHooks('BlockIp', $ban, $wgUser)) {
+ if (wfRunHooks('BlockIp', array(&$ban, &$wgUser))) {
+
$ban->insert();
- wfRunHooks('BlockIpComplete', $ban, $wgUser);
+ wfRunHooks('BlockIpComplete', array($ban, $wgUser));
# Make log entry
$log = new LogPage( 'block' );
$from = wfQuotedPrintable( $wgUser->getName() ) . " <" . $wgUser->getEmail() . ">";
$subject = wfQuotedPrintable( $this->subject );
- if (wfRunHooks('EmailUser', $this->mAddress, $from, $subject, $this->text)) {
+ if (wfRunHooks('EmailUser', array(&$this->mAddress, &$from, &$subject, &$this->text))) {
$mailResult = userMailer( $this->mAddress, $from, $subject, $this->text );
$titleObj = Title::makeTitle( NS_SPECIAL, "Emailuser" );
$encTarget = wfUrlencode( $this->target );
$wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
- wfRunHooks('EmailUserComplete', $this->mAddress, $from, $subject, $this->text);
+ wfRunHooks('EmailUserComplete', array($this->mAddress, $from, $subject, $this->text));
} else {
$wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
}
# Run any hooks; ignore results
- wfRunHooks('UserLoginComplete', $wgUser);
+ wfRunHooks('UserLoginComplete', array(&$wgUser));
$wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
$wgOut->setRobotpolicy( 'noindex,nofollow' );
function wfSpecialUserlogout() {
global $wgUser, $wgOut, $returnto;
- if (wfRunHooks('UserLogout', $wgUser)) {
+ if (wfRunHooks('UserLogout', array(&$wgUser))) {
$wgUser->logout();
-
+
+ wfRunHooks('UserLogoutComplete', array(&$wgUser));
+
$wgOut->mCookies = array();
$wgOut->setRobotpolicy( 'noindex,nofollow' );
$wgOut->addHTML( wfMsg( 'logouttext' ) );
$wgOut->returnToMain();
- wfRunHooks('UserLogoutComplete', $wgUser);
}
}
$u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
$u->doUpdate();
- wfRunHooks( 'TitleMoveComplete', $this, $nt, $wgUser, $oldid, $newid );
+ wfRunHooks( 'TitleMoveComplete', array(&$this, &$nt, &$wgUser, $oldid, $newid) );
return true;
}