Jamie Taniguchi
Jamie Taniguchi

Reputation: 387

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

Upvotes: 2

Views: 162

Answers (1)

Matěj G.
Matěj G.

Reputation: 3105

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( '[email protected]' );
        $to = new MailAddress( '[email protected]' );
        $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.

Upvotes: 1

Related Questions