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).

Article page outdated
And 11:42, 10 February 2009 (UTC)
 * The page contains references to UserLogin, which is no longer a valid hook - at least, it's not in the list of hooks. As I'm struggling to get my UserLoadFromSession hook to work, I'm not the right person to update it!

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.
 * Use ParserBeforeStrip. Jean-Lou Dupont 15:59, 25 October 2007 (UTC)


 * I'm fairly certain that was the second hook with which I attempted this, and I think I got result #1. I'll try it again when I get a chance just to make sure.  Does it work when you try it?  What version of MediaWiki are you running?  —Sledged (talk) 20:15, 25 October 2007 (UTC)
 * What about using ParserBeforeStrip and ParserAfterStrip? I'm fairly certain that you can point them to the same function.

SpecialSearchNogomatch
Any information on SpecialSearchNogomatch? 68.80.149.10 15:37, 25 October 2007 (UTC)

Preview Hook?
I'm not very familiar with MediaWiki's core code. I was surprised to find there is no hook such as "onArticlePreview," and would like to suggest that one be added for future versions of MediaWiki. I want to modify the wiki markup used to generate the preview before it is displayed as a preview. I am assuming the preview is generated from  but I still can't find a proper hook for a good entry point. Any ideas? -Wikitonic 21:14, 3 March 2008 (UTC)


 * UPDATE - I found a crazy work-around for my above problem (it would take too long to explain what I was trying to do and how I got around it), but nevertheless my suggestion still stands that MediaWiki could benefit from a hook such as "beforeArticlePreview" and/or "beforeArticleShowChanges." -Wikitonic 16:31, 7 March 2008 (UTC)


 * There are a couple hooks you could probably use. Try EditPage::showEditForm:initial and check if $editpage->formtype == 'preview'.  If that doesn't work, the MediaWiki developers are pretty good about adding hooks on request.  Try submitting a bug report here:  https://bugzilla.wikimedia.org/ --Cneubauer 20:45, 8 May 2008 (UTC)


 * So it appears that a hook has been added in 1.13 called, but I only noticed this because I installed FCKeditor and the hook appeared in the information at  . It hasn't yet been documented on the manual page here, though... Perhaps someone with a little more knowledge on it might wish to update it? In any case, thanks to the MediaWiki team for adding this hook! --Wikitonic 16:48, 20 June 2008 (UTC)


 * Looks like I was wrong in the above comment. The hook  has NOT been added to the official core MediaWiki code -- rather, it is a part of the FCKeditor extension code. The   class extends the MediaWiki   class and overloads the   method, adding the aforementioned hook in the process. Oh well. I would really like to see this hook added to the core MediaWiki code in the next release. though. --Wikitonic 17:17, 8 July 2008 (UTC)

Thumbnail creation hook
I have issues on our webserver relating to permissions on uploaded / created files. Basically, I need to "manually" chmod the files after upload to give world-read permissions so they can be seen by the web server.

Whilst I could head into the source code, I would rather write an extension to do this, to make upgrading easier in the future.

Using the "UploadComplete" hook I have done this for files when they are initially uploaded (similarly for deleting and restoring files). However, when images are resized, the new images are created with the wrong permissions, and there don't seem to be any hooks available to know when a thumbnail has been created - have I missed something? --Djomp 14:09, 26 June 2008 (UTC)

Enotif hook
I have been trying to find a way to modify email notifications with an extension, but I have not been able to find a hook suitable to accomplish this. I want the extension to replace the user id with the real name when its available, is there a hook I am overlooking?--Coban 13:29, 4 August 2008 (UTC)