How can I get mediawiki to email me on every db error?
I'm looking for a hook or some way to easily send out emails every time I get a database error on mediawiki. I realize that I might have to add this functionality to their database class but I'm not sure at the momen开发者_如何学Pythont if I should be doing that.
I do not want a solution that includes daemons, crons, or anything else that'll read and send out emails based on the SQL query log.
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:' . "\n\n" .
$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.
精彩评论