<?php
/*
	WordPress CLI Theme
	R. McFarland, 2006
	
	This generates a tag cloud based on the contents of your blog.
	Set the $chaff array in the cloud_parse function to contain words 
	you don't want counted.
	Call with $regen=true to regenerate the cloud, otherwise it will
	return the cached cloud (which is probably from /my/ blog). From the CLI, 
\	use 'cloud regenerate' at the prompt.

	Heavily modified from 
	http://www.prism-perfect.net/archive/php-tag-cloud-tutorial/ 
*/


function cloud_parse($query){
	global $wpdb;
	$raw=$wpdb->get_col($query,0);
	$words=array();
	$chaff=array(null,
		'the', 'and', 'that', 'with', 'this', 'for', 'if', 'than' ,'then', 'quot', 'ccedil', '100n', 'you', 
		'your'
		);
	$txt="";
	for($i=0;$i<count($raw); $i++){
		$txt.=$raw[$i];
	}
	$txt=str_replace("\n"," ", $txt);
	$txt=preg_replace('/<[^>]+?'.'>/',' ',$txt);
	$txt=preg_replace('/[\!&;\s"-$\(\)\{\},\.><\/=\*:\?\-\[\]\\\]/',' ',$txt);
	$txt=preg_replace('/\'(s|m|ll|re)?\s/',' ',$txt);
	$txt=preg_replace('/\s\'/',' ',$txt);
	$txt=preg_replace('/&[^\s]{1,5};/',' ',$txt);
	$txt=preg_replace('/(^|$)/',' ',$txt);
	$txt=preg_replace('/\s+/',' ',$txt);
	$txt=preg_split('/ /',$txt,-1,PREG_SPLIT_NO_EMPTY);
	$counts=array_count_values($txt);
	foreach($counts as $w=>$n){
		$w=trim($w);
		if(preg_match('/[a-zA-Z]/',$w) && !array_search($w,$chaff) && strlen($w)>2) {
			if(isset($words[$w])){
				$words[$w]=$n;
			}else{
				$words[$w]+=$n;
			}
		}
	}
	return $words;
}


if(!file_exists('cloudcache.html') || (isset($regen) && $regen)) {
	$words=cloud_parse("SELECT LCASE(CONCAT(`post_title`, ' ', `post_content`, ' ')) as body FROM ${table_prefix}posts");	

//	ksort($words);
	// change these font sizes if you will
	$max_size = 250; // max font size in %
	$min_size = 70; // min font size in %
	
	// get the largest and smallest array values
	$forcemin=10;
	$max_qty = max(array_values($words));
//	if($max_qty>50){$max_qty=50;}
	$min_qty = min(array_values($words));
	$min_qty = max(array($forcemin,$min_qty));
	// find the range of values
	$spread = $max_qty - $min_qty;
	if (0 == $spread) { // we don't want to divide by zero
	$spread = 1;
	}
	
	// determine the font-size increment
	// this is the increase per word quantity (times used)
	$step = ($max_size - $min_size)/($spread);
	
	$fh=fopen('cloudcache.html','w');
	// loop through our word array
	fwrite($fh,'<div id="cloud">');
	foreach ($words as $key => $value) {
		if($value>=$forcemin){
//			$vs=min(array($value,50));
			$vs=$value;
			// calculate CSS font-size
			// find the $value in excess of $min_qty
			// multiply by the font-size increment ($size)
			// and add the $min_size set above
			$size = $min_size + (($vs - $min_qty) * $step);
			// uncomment if you want sizes in whole %:
			$size = ceil($size);
			
			// you'll need to put the link destination in place of the #
			// (assuming your word links to some sort of details page)
			fwrite($fh, '<span class="linky" onclick="autosearch(\''.str_replace("'","\\'",$key).'\');" style="text-decoration:none;font-size: '.$size.'%"');
			// perhaps adjust this title attribute for the things that are tagged
			fwrite($fh, ' title="'.$value.' occurences of '.$key.'"');
			fwrite($fh, '>'.$key.'</span> ');
			// notice the space at the end of the link
		}
	}
	fwrite($fh,'</div>');
	fclose($fh);
}
include('cloudcache.html');
?>
