Manual:How to debug

From MediaWiki.org
Jump to: navigation, search

Other languages:
Deutsch • ‎English • ‎British English • ‎español • ‎日本語 • ‎polski • ‎português do Brasil • ‎中文

This page gives a basic introduction to debugging MediaWiki software.

One of the first things you will notice is that "echo" generally does not work; this is part of the general design.

PHP errors[edit | edit source]

To see PHP errors, add this to the bottom of LocalSettings.php:

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

Or set it in php.ini:

error_reporting = E_ALL
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.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.

Turning display_startup_errors on[edit | edit source]

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 );

In other environments:

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

SQL errors[edit | edit source]

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 $wgShowDBErrorBacktrace:

$wgShowDBErrorBacktrace = true;

In-depth debugging[edit | edit source]

Logging[edit | edit source]

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

Setting up a debug log file[edit | edit source]

To save errors and debugging information to a log, add $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 $wgDebugDBTransactions.

Warning Warning: 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.

Creating a custom log file[edit | edit source]

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

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

Creating custom log groups[edit | edit source]

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. But 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',
 
	// 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.

Send debug data to an HTML comment in the output[edit | edit source]

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 | edit source]

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

The MediaWiki-Vagrant portable virtual machine integrates the interactive PHP shell phpsh.

See also[edit | edit source]