Debugging Tips

From Habari Project

Jump to: navigation, search

Habari provides some features that allow you to better debug things when problems arise. Here are some tips that will help speed your progress.

Debug Mode

Normally, Habari runs in a silent mode, where error details are not displayed. This protects your site's code and data from potential exposure if problems arise.

Habari has a "debug mode" that will cause the error details to be displayed with any error.

To enable this mode, edit the config.php file for your site and add this line:

define('DEBUG', true);

To enable more extensive debugging information, edit the config.php file for your site and add this line in additon to the previous one:

define('DEBUG_ARGS', true);

Any error that occurs will have a complete call stack displayed, which is very useful for tracing execution through the code that led to the error.


When tracing through the Habari source code, sometimes it's useful to display the value of a variable at a certain place in execution. Normally, you might use the PHP command print_r() to output this information, but without some extra markup, this may cause some formatting issues, and doesn't provide all of the information that might be useful for debugging.

Instead, Habari provides the Utils::debug command, which produces output like print_r(), but formats it nicely and provides a call stack. The call stack can be expanded to show the functions that were executed to arrive at that point in code. Each of the functions displayed also shows the parameter types passed to that function, and if you click on them they will toggle, revealing the parameter values.

To call Utils::debug(), you may pass zero or more parameters, the values of each will appear in the output along with their type information:

Utils::debug($foo, $bar, $baz);

Determine the Query Used for Posts::get()

Sometimes it's useful to determine what query Habari executes to retrieve posts, especially when your call to Posts::get() doesn't return the results you were expecting.

If Posts::get() returns an instance of Posts, you can obtain the query used to get those posts by calling ->get_query() on the Posts object:

$posts = Posts::get($criteria);

If the result of Posts::get() is false (no posts were returned), you can obtain the query used by passing 'get_query' as the 'fetch_fn' argument to Posts::get():

Utils::debug(Posts::get(array_merge($criteria, array('fetch_fn'=>'get_query'))));

If all else fails, left in the Habari source are several useful debugging statements that can be used for this purpose. Look in the file /system/classes/posts.php for a comment that says:

   DEBUG: Uncomment the following line to display everything that happens in this function

As of this writing, there are three lines after that comment that may each individually provide useful insight into what Habari is doing to fetch posts. Probably the most useful of these is the Utils::debug() line, which uses the function described above.

Personal tools