doqau82086 2011-04-11 21:42
浏览 39
已采纳

如何让mediawiki通过电子邮件向我发送每个数据库错误?

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.

  • 写回答

1条回答 默认 最新

  • duanbogan5878 2011-04-17 10:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改