<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"><title>hConsole: A Live Habari Console</title><author><name>Matt Read</name></author><link rel="alternate" href="https://mattread.com/hconsole-a-live-habari-debugging-console"/><link rel="edit" href="https://mattread.com/hconsole-a-live-habari-debugging-console/atom"/><id>tag:mattread.com,2013:hconsole-a-live-habari-debugging-console/1359403524</id><updated>2013-10-03T10:55:19-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2020-02-06T12:56:28-05:00</app:edited><published>2013-10-02T14:54:06-04:00</published><category term="habari"/><category term="code"/><content type="html">If you have ever written code before, you've come across a situation where you needed to debug or test something simple in your code. Doing this with a Command Line Interpreter is quite simple. Type your code, see what comes out. But when it comes to web applications, it's a little harder. There are HTTP requests, Sessions, Cookies, and a Webserver involved. This is where [hConsole](https://github.com/habari-extras/hconsole "hConsole github repo") comes in.&#xD;
&#xD;
hConsole is a live [Habari](https://habariproject.org/en/ "Habari Project's main site") console, or command line, that allows you to evaluate your code directly in your Habari installation, with all the HTTP request, Sessions, etc. there. You can access the entire Habari core, even trigger plugin actions and filters, and view the live result on screen.&#xD;
&#xD;
Once installed, you simply click on the hConsole button in the bottom right of your screen to bring up the console. In the console you can evaluate any PHP code and utilize the entire Habari base. You can also execute SQL, to query your Habari's database directly (Tip: press &lt;kbd&gt;ctrl&lt;/kbd&gt;+&lt;kbd&gt;Q&lt;/kbd&gt; to run code without the mouse).&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;a href="//farm9.staticflickr.com/8099/8590516105_9d9462457c_b.jpg" class="fancybox"&gt;&lt;img alt="hconsole_php" src="//farm9.static.flickr.com/8099/8590516105_9d9462457c.jpg" class="card center"&gt;&lt;/a&gt; &#xD;
&lt;figcaption&gt;&lt;b&gt;Figure 1&lt;/b&gt; Using the debug area to output code.&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
###Debugging PHP and Habari&#xD;
&#xD;
hConsole has 2 parts; A debug section, for outputting variables and such (`Utils::debug()` comes in handy here), and the ability to trigger plugin hooks and affect the output of, say, post titles. There is also an option to `htmlspecialchars()` the output of the debugging code if you wanted to output HTML tags, for example. Let's look at two basic examples:&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;pre class="highlight php"&gt;&#xD;
$test = "this is a test";&#xD;
echo $test;&#xD;
&lt;/pre&gt;&#xD;
&lt;figcaption&gt;&lt;b&gt;Example 1&lt;/b&gt; A simple Debug echo&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
The code in example 1 will simply echo the variable `$test` above the command line in hConsole; The so called "Debug Area". We can also implement plugin hooks.&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;pre class="highlight php"&gt;&#xD;
filter_post_title_out {&#xD;
    function boo ($title)&#xD;
    {&#xD;
        return $title . ' Boo!';&#xD;
    }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&lt;figcaption&gt;&lt;b&gt;Example 2&lt;/b&gt; Filtering Post Titles&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
The code in example 2 will register the function `boo()` to the `post_title_out` filter. Function `boo()` then appends `" Boo!"` to all post titles for the page you are looking at. We can also combine the two and have debug output along will running filters or actions.&#xD;
&#xD;
Of course these are very simple examples of what can be done with hConsole. In the real world it becomes a very powerful tool. &#xD;
&#xD;
###Debugging With hConsole&#xD;
&#xD;
Let's look at an example of a real debugging situation where hConsole came in handy. When trying to debug Habari's internal Cron system it's hard to tell why it's failing when PHP is set to use CURL. Recently [Scott](http://skippy.net "Scott's website") was having an issue with a local install, where crons weren't running.&#xD;
&#xD;
The issue comes from the fact that Habari makes an asynchronous HTTP call to itself to trigger all the Cronjobs to run. So you never actually see any errors from this, and when CURL throws an exception, nothing is in the logs. So to test why Scott's crons where not running he used hConsole.&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;pre class="highlight php"&gt;&#xD;
$cronurl = URL::get( 'cron',array( 'asyncronous' =&gt; Utils::crypt( Options::get( 'public-GUID' ) ) ) );&#xD;
$request = new RemoteRequest( $cronurl, 'GET', 100 );&#xD;
$request-&gt;execute();&#xD;
&lt;/pre&gt;&#xD;
&lt;figcaption&gt;&lt;b&gt;Example 3&lt;/b&gt; Making a request to the cron.&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
In example 3 we run the cron trigger using hConsole with a longer timeout so we can see what is happening. And Scott was able to get the error `Exception: CURL Error 60: SSL certificate problem, verify that the CA ...`. He was then able to add his  SSL certificate chain to his Apache config so CURL accepted the certificate, and crons ran smoothly.&#xD;
&#xD;
###Debugging SQL&#xD;
&#xD;
hConsole will also let you directly run SQL statements on your database. It will even substitute proper table names when using "curly brackets", like `{posts}` becomes `habari__posts`, or whatever you chose during install. You can run any database query by simply checking the SQL checkbox, and the results will output in a nicely formatted table.&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;pre class="highlight sql"&gt;&#xD;
select * from {posts} limit 3;&#xD;
&lt;/pre&gt;&#xD;
&lt;figcaption&gt;&lt;b&gt;Example 4&lt;/b&gt; SQL selecting the 3 most recent posts.&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
And the results from running example 4 show in a nice tabular format so we can easily see what's in our database.&#xD;
&#xD;
&lt;div&gt;&lt;figure&gt;&#xD;
&lt;a href="//farm9.static.flickr.com/8506/8591609336_bf476f433f_b.jpg" class="fancybox"&gt;&lt;img alt="hconsole_sql" src="//farm9.static.flickr.com/8506/8591609336_bf476f433f.jpg" class="center card"&gt;&lt;/a&gt;&#xD;
&lt;figcaption&gt;&lt;b&gt;Figure 2&lt;/b&gt; Results of selecting the 3 most recent posts.&lt;/figcaption&gt;&#xD;
&lt;/figure&gt;&lt;/div&gt;&#xD;
&#xD;
You can also, of course, run other statements, such as delete, update, show tables, etc. All statements will output their results, or show an error if there are troubles.&#xD;
&#xD;
##Developer's Best Friend&#xD;
&#xD;
As you can see hConsole is the plugin and theme developers best friend. Whether you are debugging a core Habari issue, or simply looking at the contents of an object. You can pretty much debug and test any code you need to; PHP, SQL, and Habari.&#xD;
&#xD;
&lt;ul class="download"&gt;&#xD;
&lt;li&gt;&lt;a href="https://github.com/habari-extras/hconsole/archive/master.zip"&gt;hConsole Master &lt;i class="icon-box-add"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;&#xD;
&lt;li&gt;&lt;a href="https://github.com/habari-extras/hconsole"&gt;GitHub Repo &lt;i class="icon-github"&gt;&lt;/i&gt;&lt;/a&gt; &lt;/li&gt;&#xD;
&lt;/ul&gt;&#xD;
&#xD;
*[PHP]: PHP: Hypertext Preprocessor&#xD;
*[HTML]: Hyper Text Markup Language&#xD;
*[SQL]: Structured Query Language&#xD;
*[SSL]: Secure Socket layer&#xD;
*[HTTP]: HyperText Transfer Protocol&#xD;
*[CURL]: Client URLs</content></entry>
