Debugging Mediawiki- Part 1

Just set up a PHP Debug environment for Mediawiki. Couldn’t find a good overall documentation for that so writing this blog post.

The part 1 of the post includes the different methods that are supported by the Mediawiki software itself. Mediawiki software ensures a lot of Debugging tools for the developers.

It is very important to ensure that the debugging tools are disabled on production sites, they might actually be used by attackers in such cases.
So, here we go:

* Mediawiki is complex software, PHP commands such as echo will not work.

* Checking PHP errors- Include this code in LocalSettings.php.

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


 

* Startup errors might be disabled using display_startup_errors which might hide errors even in the above case. To ensure that the above works, add this to top of index.php

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


 

Some important Mediawiki configuration variables for debugging

  • $wgShowExceptionDetails: Enable details like stack traces to be shown on the “Fatal error” page.
  • $wgDebugToolbar: Very effective toolbar on all webpages. Contains options of profiling, log messages, PHP includes etc. Can be used to view custom log messages as well.
  • $wgDevelopmentWarnings: Show possible error conditions and for deprecated functions.

View Debug output-Set these configuration values to true in LocalSettings.php

  • $wgShowDebug: Adds a list of “log messages” to the page. Can be used to view all the database queries as well.
  • $wgDebugComments: Adds Debug output as HTML comments for the user.
  • $wgShowSQLErrors: Show SQL errors
  • $wgDebugDumpSql: Show SQL debug dump
  • $wgShowDBErrorBacktrace: Show SQL errors call stack

Adding to the Debug output

  • Debug text passed to function wfDebug() is printed with the Debug output.
  • The call stack can be viewed as text in the Debug data using the function wfBacktrace()

Logging debugging data
A log file can be set up by adding this in LocalSettings.php


    $wgDebugLogFile = "/var/log/mediawiki-debug.log";

Mediawiki allows creation of custom log files and groups.
For instance, to print output of a specific function to a file, use


    wfErrorLog( "Some debug text", '/var/custom-debug.log' );

To create custom log groups, add this to LocalSettings.php


    $wgDebugLogGroups = array(
	'mydebug' => '/var/log/mediawiki-mydebug.log',
    );

Now logging to this group can be done by:


    wfDebugLog( 'mydebug', "Debugging comment" );

Working with Mediawiki objects using PHP
eval.php is an interactive script which allows users to work live with Mediawiki objects and database.
To run it, simply type in terminal:


    php maintenance/eval.php

The script automatically loads Mediawiki classes so can be very handy.
Some examples:


    > print wfMessage( "Recentchanges" )->plain();
    Recent changes

Interaction with the database can be done using the Database Abstraction Layer in Mediawiki. For example:

    > $dbr = wfGetDB( DB_SLAVE );
    > $result = $dbr->select( 'page','page_len' )
    > foreach( $result as $row ) { /* DO SOMETHING */ }


Further ways to access the database can be found here

That’s all for today. In the next post, I will cover Debugging methods using IDEs, they are independent of the application used, Mediawiki in the current case.

Advertisements

2 thoughts on “Debugging Mediawiki- Part 1

  1. I just like the valuable info you provide for your articles.
    I’ll bookmark your weblog and check again here regularly.
    I am moderately certain I’ll be told a lot of new stuff right
    right here! Good luck for the following!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s