Jump to: navigation, search
This page is a translated version of the page Manual:How to debug and the translation is 34% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎日本語 • ‎한국어 • ‎Nederlands • ‎polski • ‎português • ‎português do Brasil • ‎русский • ‎中文

このページでは、MediaWiki をデバッグするソフトウェアの基礎を紹介します。

最初に気付くことの 1 つは、"echo" が全般的に動作しないということでしょう。この挙動は全般的な設計の一部です。

PHP のエラー[edit]

PHP のエラーを閲覧するには、LocalSettings.phpManual:LocalSettings.php の末尾に以下を追加します:

error_reporting( -1 );
ini_set( 'display_errors', 1 );

または php.iniManual:php.ini 内で設定します:

error_reporting = E_ALL
display_errors = On

あるいは .htaccess 内で設定します:

php_value error_reporting -1
php_flag display_errors On

This will cause PHP errors to be shown on-page. This might make it easier for attackers to find a way into your server, so disable it again when you have found the problem.

Note that fatal PHP errors may happen before the lines above are ever executed, or may prevent them from being shown. Fatal PHP errors are usually logged to Apache's error log – check the error_log setting in php.ini (or use phpinfo())

There are several configuration options to aid debugging. The following are all false by default. Enable them by setting them to true in your LocalSettings.phpManual:LocalSettings.php:

  • $wgShowExceptionDetails Enable more details (like a stack trace) to be shown on the "Fatal error" page.
  • $wgDebugToolbar Shows a toolbar on the page with profiling, log messages and more.
  • $wgShowDebug Adds the "log messages" part of wgDebugToolbar as a raw list to the page.
  • $wgDevelopmentWarnings MediaWiki will throw notices for some possible error conditions and for deprecated functions.


Some providers turn display_startup_errors off, which hides the errors even if you raise the error_reporting level. Turning it on within the program is too late! Instead you'll have to create a wrapper file around your file. In the case of MediaWiki you can just add this on top of mediawiki/index.php:

--- index.php
    error_reporting( -1 );
    ini_set( 'display_startup_errors', 1 );
    ini_set( 'display_errors', 1 );


--- myTestFile.php
    error_reporting( -1 );
    ini_set( 'display_startup_errors', 1 );
    ini_set( 'display_errors', 1 );
    require 'your_file.php';


To display SQL errors in error messages instead of "(SQL query hidden)", add the following to LocalSettings.php:

$wgShowSQLErrors = true;
$wgDebugDumpSql  = true;

You can also enable backtrace on SQL error by setting $wgShowDBErrorBacktraceManual:$wgShowDBErrorBacktrace:

$wgShowDBErrorBacktrace = true;

In-depth debugging[edit]



If you are using the Zend PHP interpreter, you can debug your code with XDebug. MediaWiki-Vagrant has built in settings for this. If you're not using MediaWiki-Vagrant, but your setup is similar, you can reuse those values. In some cases (e.g. due to a firewall), you may have to use the IDE on the same machine as the web server. In this case, you can simply set:

xdebug.remote_enable = 1
xdebug.remote_host = 'localhost'

If you use this configuration, you may run into an issue if HHVM is also listening on the machine. Both XDebug and HHVM use port 9000 by default. However, you can change this for XDebug (and many clients). For the XDebug side, use:

xdebug.remote_port = 9001

Remember if you change it for XDebug, you must also change it for your IDE.

詳細情報については XDebug の説明文書を参照してください。

To debug a command-line script (e.g. PHPUnit, or a maintenance script) on MediaWiki-Vagrant, use:

xdebug_on; php5 /vagrant/mediawiki/tests/phpunit/phpunit.php --wiki=wiki /vagrant/mediawiki/extensions/Extension/tests/phpunit/SomeTest.php; xdebug_off

Adjust the script, parameters, and remote host (it should be the IP of the computer where your IP is, should work for MediaWiki-Vagrant) as needed.


If you are using the HHVM PHP runtime, you can debug your code with the built in CLI debugger and debug server. このデバッガーは GDB に何となく似ています。

In order for the cli debugger to attach to web requests the hhvm.debug.enable_debugger parameter needs to be enabled. This is enabled by default on the mediawiki-vagrant development environment.

With this enabled the process is:

  $ hhvm -m debug --debug-host localhost --debug-port 8089
  localhost> machine list
  machine list
    1     vagrant's default sandbox at /vagrant/www/
    2     __builtin's default sandbox at /
  localhost> machine attach 2
  machine attach 2
  Attaching to __builtin's default sandbox at / and pre-loading, please wait...
  localhost> break MediaWiki::run()
  break MediaWiki::run()
  Breakpoint 1 set upon entering MediaWiki::run()
  localhost> continue

machine attach 2 が必要です。 It may not always be machine 2, but it will always be the one identified as "__builtin's default sandbox at /". This is only true on configurations that pass a DOCUMENT_ROOT over fastcgi to hhvm (such as MediaWiki-VagrantMediaWiki-Vagrant and wmf production), alternate configurations may vary.

At this point the next web request that enters MediaWiki::run() (all of them) will break and put you on a command prompt:

       Breakpoint 1 reached at MediaWiki::run() on line 450 of /vagrant/mediawiki/includes/MediaWiki.php
        449            try {
        450*                   $this->checkMaxLag();
        451                    try {

The HHVM CLI debugger has an excellent help system, for example try help break for details on the syntax of choosing break points.


For much greater detail, you need to profile and log errors.

The instructions below are only valid for the default configuration. If you change $wgMWLoggerDefaultSpiManual:$wgMWLoggerDefaultSpi, for example enable the psr3 role on a vagrantMediaWiki-Vagrant box, these settings will probably be ignored.

デバッグ ログ ファイルのセットアップ[edit]

To save errors and debugging information to a log, add $wgDebugLogFileManual:$wgDebugLogFile to the LocalSettings.php file. Change the value to a text file where you want to save the debug trace output.

The MediaWiki software must have permissions from your operating system to create and write to this file, for example in a default Ubuntu install it runs as user & group www-data:www-data. Here's a sample setting:

 * The debug log file must never be publicly accessible because it
 * contains private data. But ensure that the directory is writeable by the
 * PHP script running within your Web server.
 * The filename is with the database name of the wiki.
$wgDebugLogFile = "/var/log/mediawiki/debug-{$wgDBname}.log";

This file will contain much debug information from MediaWiki core and extensions. Some subsystems write to custom logs, see #Creating a custom log file to capture their output.

Database transaction lifecycle debugging can be enabled for some databases with $wgDebugDBTransactionsManual:$wgDebugDBTransactions.

警告 警告: The debug log file can contain private information such as login credentials, session cookies, and values of submitted forms. If this information is publicly accessible, attackers can use it to hack and compromise your machine and user account. If you need to share a debug log for diagnostic purposes, access the wiki without being logged in, and remove from the debug log any COOKIE lines, and don't capture any login attempt.


To create a custom log file that only holds your specific debug statements, use the wfErrorLog() function (use of wfErrorLog was deprecated in MediaWiki 1.25). This function takes two arguments, the text string to log and the path to the log file:

wfErrorLog( "An error occurred.\n", '/var/log/mediawiki/my-custom-debug.log' );


If you're debugging several different components, it may be useful to direct certain log groups to write to a separate file.

To set up custom log groups, use the following to LocalSettings.php:

 * The debug log file should be not be publicly accessible if it is used, as it
 * may contain private data. However, it must be in a directory to which PHP run
 * within your web server can write.
 * Contrary to wgDebugLogFile, it is not necessary to include a wiki-id in these log file names
 * if you have multiple wikis. These log entries are prefixed with sufficient information to
 * identify the relevant wiki (web server hostname and wiki-id).

// Groups from MediaWiki core
$wgDBerrorLog = '/var/log/mediawiki/dberror.log';
$wgRateLimitLog = '/var/log/mediawiki/ratelimit.log';
$wgDebugLogGroups = array(
	'resourceloader' => '/var/log/mediawiki/resourceloader.log',
	'exception' => '/var/log/mediawiki/exception.log',
	'error' => '/var/log/mediawiki/error.log',
	#'exception-json' => '/var/log/mediawiki/exception.json',

	// Extra log groups from your extension
	#'myextension' => '/var/log/mediawiki/myextension.log',
	#'somegroup' => '/var/log/mediawiki/somegroup.log',

To log to one of these groups, call wfDebugLog like this:

if ( $module->hasFailed ) {
    wfDebugLog( 'myextension', "Something is not right, module {$module->name} failed." );
If you have carefully followed the instructions above but nothing gets written to your logging file(s), and if your system is using SELinux, have a look at the logging section on the SELinux page to get around this SELinux context issue.
Writing log files to the /tmp directory may not generate any log file at all, even if the /tmp directory is supposed to be writable by anyone. This could happen if your system is using one of the systemd features that create a virtual /tmp directory for that process. If that's the case, configure your log file to be written into a different directory, like /var/log/mediawiki


MediaWiki バージョン: 1.25

構成した記録によりログにフィールドを追加できます。 詳細情報は Structured loggingStructured logging を参照してください。

You will need to configure a better logger to collect the extra fields, for example Monolog.


Advanced client-side logging can be performed with Extension:EventLogging, which requires a complex setup and careful inspection of privacy issues.

Simple counting of certain kind of events is possible (since MediaWiki 1.25) using StatsD. StatsD offers meters, gauges, counters, and timing metrics.


$stats = $context->getStats();
$stats->increment( 'resourceloader.cache.hits' );
$stats->timing( 'resourceloader.cache.rtt', $rtt );

The metrics can be sent to a StatsD server, which may be specified via the wgStatsdServer configuration variable. (If not set, the metrics are discarded.) You can work with StatsD locally (without needing a Graphite server) by starting a StatsD server and configuring it with the "backends/console" backend, which will output metrics to the console.

As of MediaWiki 1.25, wfIncrStats() is a shortcut for the increment() method on the main RequestContext::getStats() instance.


This may occasionally be useful when supporting a non-technical end-user. It's more secure than exposing the debug log file to the web, since the output only contains private data for the current user. But it's not ideal for development use since data is lost on fatal errors and redirects. Use on production sites is not recommended. Debug comments reveal information in page views which could potentially expose security risks.

$wgDebugComments = true;

Working live with MediaWiki objects[edit]

eval.phpManual:Eval.php is an interactive script to evaluate and interact with MediaWiki objects and functions in a fully initialized environment.

 $ php maintenance/eval.php
 > print wfMessage("Recentchanges")->plain();
 Recent changes

MediaWiki-VagrantMediaWiki-Vagrant 携帯用の VM は対話型 PHP シェル hhvmsh (既定の HHVM 使用時) もしくは phpsh (Zend 使用時) を統合する。

Callable updates[edit]

Code embedded in the DeferredUpdates::addCallableUpdate() function, such as $rc->save() in RecentChange.php, is not executed during the web request, so no error message will be displayed if it fails. For debugging, it may be helpful to temporarily remove the code from within the function so that it is executed live.


Wikipedia offers a rich set of tools for debugging client side JavaScript. In addition to the MediaWiki tools, other techniques are available to assist with diagnosing client interactions.


  • ResourceLoader offers a means to ensure JavaScript is easily viewable by client-side tools.
  • ResourceLoader also provides a safety oriented way to log to the client console. Beyond the native JavaScript logging function, it provides a check to ensure that a console is available and that logging does not produce its own error. ResourceLoader/Features#Debug_mode also describes this feature.
  • Browser tools may provide native functionality to debug client side script.
  • Network tracers, like Wireshark can provide insight into the script that is being provided by a page.