I have just upgraded to Blogger Beta. I love that they have finally added support for labels, however I cannot get a list of my labels to appear in my template. In fact I am not even getting the new WYSIWYG template editor that they are bragging about. I think it may be because I publish to my own server via FTP.
Anyway, I created a quick solution for the time being and am posting it hear in case anyone else has the same problem and wants to steal it:
This PHP code creates a label cloud (put it anywhere on the server you publish to via FTP and change the obvious constants):
Update 30 April 2007 to remove duplicate entries added by Blogger.
<?php
define('PREFIX', '/labels/');
define('SEARCH_DIR',
'/home/httpd/vhosts/da.vidnicholson.com/httpdocs/labels/');
define('THIS_FILE', 'cloud.php');
if(file_exists(SEARCH_DIR.'_cloud_include_cache.html') &&
filemtime(SEARCH_DIR.'_cloud_include_cache.html')>(time()-(60*60)))
{
echo file_get_contents(SEARCH_DIR.'_cloud_include_cache.html');
}
else
{
$output = '';
$files = array();
$dir = opendir(SEARCH_DIR);
$max_size = -1; $min_size = -1;
while($file = readdir($dir))
{
if($file != '.' && $file != '..' && $file != THIS_FILE && $file !=
'_cloud_include_cache.html')
{
$files[$file] = filesize(SEARCH_DIR.$file);
if($max_size==-1 || $max_size<$files[$file]) $max_size = $files[$file];
if($min_size==-1 || $min_size>$files[$file]) $min_size = $files[$file];
}
}
closedir($dir);
$displayed = array();
foreach($files as $name=>$size)
{
if(!isset($displayed[urldecode(str_replace('.html','',$name))]))
{
$displayed[urldecode(str_replace('.html','',$name))] = true;
$output .= "<span style='margin: 3px; font-size: ".
(10+round((1+(($size-((($max_size-$min_size)/2)+$min_size))/($max_size-$min_size)))*100)).
"%;'><a href='".PREFIX.htmlentities($name)."'>".
htmlentities(urldecode(str_replace('.html','',$name)))."</a></span> ";
}
}
echo $output;
$fp = fopen(SEARCH_DIR.'_cloud_include_cache.html','w');
fwrite($fp, $output);
fclose($fp);
}
?>
Now setup some mechanism whereby this file is included in your sidebar. A naive way would be to use an include() statement in your template and make your server parse .html files as PHP (e.g. by adding
AddType application/x-httpd-php .html
to a
.htaccess file) but you are relying on Blogger correctly escaping any comments so malicious users cannot run arbitrary PHP code on your server. A better way is to introduce some keyword to your template and have code on your server scan for this keyword and overwrite the correct content. That is all beyond the scope of this post though.
You will notice that my code caches the cloud so that the labels directly does not get scanned on every single page view (just once per hour). For this to work your SEARCH_DIR must be writable by your web server. Also I am hoping that Blogger do not paginate label pages, if they do this will break my code, none of mine seem to be paginated though.
All source code and related services that I release are "dontation-ware". If you use the above please
make a dontation (pay whatever you think the it is worth) or (where applicable) leave the link to my site attached.
Labels: Programming