The only practical way of doing this is probably registering custom exception handler. There we check if the exception is a database error and send an e-mail accordingly.
I’ve come up with this simple code:
$wgHooks['SetupAfterCache'][] = 'onSetupAfterCache';
function onSetupAfterCache() {
set_exception_handler( 'customExceptionHandler' );
return true;
}
First we have to set up a hook to register our exception handler. If we registered it just so in LocalSettings.php
it would get overriden by wfInstallExceptionHandler()
.
function customExceptionHandler( $e ) {
if( $e instanceof DBError ) {
$from = new MailAddress( 'dberror@example.com' );
$to = new MailAddress( 'personal.mail@example.com' );
$body = 'A database error occured on My Wiki. Details follow:' . "
" .
$e->getText();
UserMailer::send( $to, $from, 'Database error on My Wiki', $body );
}
Here we check if the exception was caused by the database and send the e-mail. You should customize the $from
, $to
and $body
variables. For more info about the UserMailer
and MailAddress
classes see the documentation.
wfExceptionHandler( $e );
}
In the end we pass the exception to MediaWiki’s handler, which we’ve overriden earlier. It takes care of outputting the error to the user and other important stuff.