Manual talk:Hooks

This page and it's sub-pages were originally created from information contained at MediaWiki Hooks and their Parameters. --HappyDog 23:48, 17 January 2006 (UTC)

It has subsequently used information from hooks.txt (part of the docs bundled with MediaWiki, and previously called hooks.doc).

Correct up to 1.6.3
I have done a thorough search through all versions of MediaWiki from v1.4.0 (where hooks were first introduced) up to 1.6.3 and included all hooks that I found (there were many that had so far been missed) and updated the version numbers for existing hooks, most of which were wrong.

There is still a lot to do here, in particular:
 * I have no idea what a lot of the new hooks are for, or what arguments they take, as they are not documented anywhere. This information needs filling in, and (where necessary) new sub-pages created.
 * I have made some guesses about the function of some of the hooks when placing them in the top table. Feel free to rearrange these.  We may also want to split up some of the larger groups.
 * The version numbers on the sub-pages need updating to reflect the correct version numbers on this page. DONE --HappyDog 15:43, 15 April 2006 (UTC)

These lists are now fully complete up to MediaWiki 1.6.3 and all listed version numbers are now correct. Note that unreleased/beta/development versions of the software have been ignored - the version numbers here reflect when the hook was introduced into the public stable version of the software.

--HappyDog 05:42, 15 April 2006 (UTC)


 * I ran "grep -R wfRunHooks *" on the source for 1.6.7 and then manually crossed off everything already referenced in the text of this page, and I saw no new ones. I assume this is complete. I did not see any wfRunHooks calls that passed a non-constant event (i.e., dynamically called). Is this sufficient to call it good for 1.6.7? - Grubber 02:40, 6 December 2006 (UTC)


 * I've done the same process for 1.6.8, 1.7.0, 1.7.1, 1.8.0, 1.8.1, and 1.8.2. I have inserted the roughly 5 missing ones and marked the text complete up to 1.8.2. - Grubber 03:54, 6 December 2006 (UTC)


 * Thank you! --HappyDog 13:19, 6 December 2006 (UTC)

Writing an event handler
If you want the object to be passed by reference, preceed it with a "&". Otherwise, the event handler will work with a copy of your object.

More Robust ldapLogin Function
mailto:jon@myrealmail.net

This function is a bit more robust that the example given in the main document.

Simply set some LDAP server variables like this:

$ldap['server'] ="ldaps://myldap.company.com:636"; $ldap['base']   = "o=company"; $ldap['admindn'] = "cn=read,o=company"; $ldap['adminpw'] = "readPassword";

Then call ldapLogin:

$loginOK = ldapLogin ($ldap['server'], $ldap['base'], $username, $password, $ldap['admindn'], $ldap['adminpw']);

 function ldapLogin ( $server, $base, $username, $password, $admindn="", $adminpw="" ) {       # Create a filter that will find objects in most LDAP servers and AD        $filter = "(|(uid=". $username. ")(cn=". $username. ")(samAcountname=". $username. "))";

# Connect to the LDAP server $ds=ldap_connect($server); ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); # Force Version 3 Protocol

if (! $ds) {               print ("Unable to connect to LDAP server $server"); return (FALSE); }

# If we can't bind as anonymous to search LDAP, us the $admindn/$adminpw credentials if ( $admindn != "" ) {               $bind=ldap_bind($ds, $admindn, $adminpw); if ( ! $bind ) {                       print ("Unable to bind as admin user $admindn to search"); ldap_close ($ds); return (FALSE); }       }        else # Bind anonymously {               $bind=ldap_bind($ds); if ( ! $bind ) {                       print ("Unable to bind as anonymous user to search $server"); ldap_close ($ds); return (FALSE); }       }

# Search for the $userid using the filter and the search base $sr=ldap_search($ds, $base, $filter); if ( ! $sr ) {               print ("Unable to perform search: $server, base=$base, filter=$filter"); ldap_close ($ds); return (FALSE); }

# Return the entries we found $info = ldap_get_entries($ds, $sr);

# If we get 0 entries, no userid match if ( $info["count"] == 0 ) {               print ("Can't find object $username!"); ldap_close ($ds); return (FALSE); }

# If we get more than one entry, our search is too loose? if ( $info["count"] > 1 ) {               print ("Found more than one $username, set the search base differently?"); ldap_close ($ds); return (FALSE); }

# We have one entry, get the DN       $dn = $info[0][dn];

# Close this connection ldap_close ($ds);

# Open a new connection $ds=ldap_connect($server); ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); # Force Version 3 Protocol if (! $ds) {               print ("Unable to connect to LDAP server $server"); return (FALSE); }

# Bind as the user's DN with password $bind=ldap_bind($ds, $dn, $password); if ( $bind ) {               ldap_close ($ds); return (TRUE); }

ldap_close ($ds); return (FALSE); } 

ArticleFromTitle hook
Does anyone know from which file this hook is called? Nazly.elshazly 14:25, 29 January 2007 (UTC)

A grep shows it in Wiki.php (in 1.8.2) --Eric Myers 15:30, 3 March 2007 (UTC)

static function calls?
Are static calls like myclass::myfunction possible?


 * Only if the method has no reference to $this or self unless there's a sanity check like this:

$className = __CLASS__; if (!is_object($this)) $this = new $className;
 * self will not work at all from a static context.

-- Egingell 08:18, 16 August 2007 (UTC)
 * After some testing, it looks like you can't statically call a class method using $wgHooks (not without a rewrite, anyway. -- Egingell 09:04, 16 August 2007 (UTC)

Categorization
I think the hooks should be categorized by function and then the tables on Manual:MediaWiki hooks dynamically generated (with DPL or something) from the catgories. -Eep² 10:11, 31 July 2007 (UTC)


 * I think this is a good idea. Something like the Extension Matrix could work as well.  Step 1 would be to properly categorize each hook page or better yet, add a good Template:MediaWikiHook entry to each page. --Cneubauer 19:06, 28 August 2007 (UTC)


 * That would be the best approach, I think, so the tables can be removed from this page altogether. Name and version are already passed to the template, so it would be easy to add these categories automatically, but function would need to be added to each of the hooks manually.  I recommend passing it as a new template parameter so that the categorisation is more flexible, and missing cats are easier to spot. --HappyDog 21:53, 28 August 2007 (UTC)

Function Hooks
Event hooks are nice, but wouldn't it be cool if there was a function hook for every function (I would post this to the bug (suggestion) tracker, but I'm 92% sure that it won't be implemented as it requires going through every single file and pasting the code to every single function and method):

Removing Hooks Groups By Version
I would like to propose removing the third table of hooks grouped by version. The second table can be sorted by version so we could reduce the cost of maintaining the page by getting rid of the last table. See also this discussion. --Cneubauer 19:03, 28 August 2007 (UTC)


 * Also note, you can find out a hook's version by checking the annotated SVN page (for example, http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/OutputPage.php?view=annotate) and comparing that to the release notes, http://www.mediawiki.org/wiki/Important_Release_Notes. --Cneubauer 23:15, 28 August 2007 (UTC)


 * Okay, I removed the table. It was out of date anyway.  You can still see the old table here:  http://www.mediawiki.org/w/index.php?title=Manual:Hooks&oldid=126022.  --Cneubauer 12:18, 30 August 2007 (UTC)

Hook Missing in "Hooks grouped by function" Table
/BeforeParserFetchTemplateAndTitle, /BeforeParserMakeImageLinkObj, and /BeforeParserRenderImageGallery are listed in the "Alphabetical list of hooks" table but not the "Hooks grouped by function" table. —Sledged (talk) 03:51, 1 October 2007 (UTC)

Hooks and Templates
I'm trying to create a hook that process custom wiki markup before parser function extensions have executed. I started with ParserAfterStrip which did the job nicely until I tried to include a template with the custom markup. I've tried various hooks getting only one of three results: I'm running v1.11.0. Anyone have any ideas? —Sledged (talk) 18:23, 1 October 2007 (UTC)
 * The custom markup is parsed in the page but not any included pages.
 * The custom markup is parsed in the page and in the included but pages, but not until after the parser functions have executed.
 * The custom markup is not parsed at all.