Main Content

Helping new arrivals find out about source code examples

Archive - Originally posted on "The Horse's Mouth" - 2009-08-03 03:23:38 - Graham Ellis

On 30th July, nearly 5300 people arrived at source code examples on our web site from Google searches, from 4000 unique IP addresses.

How do I know that? From analysing our log files:

-bash-3.2$ grep 'GET \/resources\/ex\.php.*\.google\.' ac_20090730 | grep -v Googlebot | wc
5382 129055 1832168
-bash-3.2$ grep 'GET \/resources\/ex\.php.*\.google\.' ac_20090730 | grep -v Googlebot | awk '{print }' | sort | uniq | wc
4000 4000 56647
-bash-3.2$


... and 30th July was just a typical weekday; the number halves on Saturday and Sunday (down from 4000 to 1594) which confirms to me that the majority of these are genuine professional visitors and not automata.

-bash-3.2$ grep 'GET \/resources\/ex\.php.*\.google\.' ac_20090801 | grep -v Googlebot | awk '{print }' | sort | uniq | wc
1594 1594 22433
-bash-3.2$


Do these people find what they're looking for? Now that is a far more difficult question. And to be honest with you (gawd - I hate that phrase - I try to always be honest!), I don't know the answer, nor even how to find out.

I do know that I write about our source code examples here on The Horse's Mouth from time to time, but that the search engines are often landing our visitors at the source code itself, which until a few minutes ago had no link back to the relevant entry here - so a valuable resource that could help people (and has taken a lot of putting together) was underutilised.

There's no way I want to add a manual operation to each source code page to place back links in there, and fortunately the source code pages are all generated by a single PHP script. So I run the following in my business logic:

# File exists - is it described on the blog?
#desc_line = "";
$qblog = mysql_query("select entry_id, entry_title from mt_entry where entry_text like \"%$module/$name%\"");
while ($rro = mysql_fetch_row($qblog)) {
if ($desc_line == "") {
$desc_line = "For further documentation on this example, ";
$pnn = $rro[0]."_".preg_replace('/[^a-z0-9]+/i','-',$rro[1]);
$desc_line .= "<a href=http://www.wellho.net/mouth/$pnn.html>select here</a>";
} else {
$pnn = $rro[0]."_".preg_replace('/[^a-z0-9]+/i','-',$rro[1]);
$desc_line .= " or <a href=http://www.wellho.net/mouth/$pnn.html>here</a>";
}
}


and the following in my web helpers:

<?php if ($desc_line) { ?>
<p style='margin: 10px; border: solid; border-color: teal;'>
<table border=0 cellpadding=4><tr><td>
<?= $desc_line ?></td></tr></table></p>
<?php } ?>


The fact that the blogging software is written in a different language (Perl), and wasn't written by us, doesn't make any difference - we can still write code to access the database, just bearing in mind that it's possible that the format could change on a future major upgrade.

How does this work ... Very Well, Thank You! If I was talking about how a Lua function can return multiple values, I might provide you with a link to the source code here ... and if you follow that link to test it out, you'll find an extra, teal coloured box now provided automatically at the top of the page to allow you to link back to the blog (in its archive form, for permanence!)

For examples that have been 'mentioned in dispatches' several times, you get a series of links (could be improved later!), and for pages which are web examples, there is often a similarly highlighted link to a page where you can actually run the example. A good example of both of these is the Spell Checker in PHP which I wrote three years ago now during quiet evenings on a Perl course in Saudi Arabia.

P.S. Our extra dictionary words are here if you want to find the second bit of that last example ... and by telling you I have automatically added a back link to this page!