<?php
/**
 * @version $Id: tag.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

/**
 * 删除标签
 * 
 * @param Array $tagid 标签ID数组
 */
function delTag($tagid)
{
	global $DB, $db_prefix;
	if (empty($tagid) || !is_array($tagid))
	{
		return FALSE;
	}
	$tagid = implode(',', $tagid);
	$DB->db_query("DELETE FROM {$db_prefix}tagcache WHERE tagid IN ($tagid)");
	$DB->db_query("DELETE FROM {$db_prefix}tags WHERE tagid IN ($tagid)");
	return TRUE;
}

/**
 * 清理TAG
 * 
 * @param Int $start 开始查询标签ID
 * @param Int $limit 每次清理数量
 */
function tidyTag($start, $limit)
{
	global $DB, $db_prefix;
	$tag = array();
	$DB->db_query("SELECT tagid FROM {$db_prefix}tags LIMIT $start, $limit");
	while ($rs = $DB->db_fetch_array())
	{
		$tag[] = $rs['tagid'];
	}
	if ($tag)
	{
		foreach ($tag as $tagid)
		{
			$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}tagcache WHERE tagid = ".$tagid);
			if ($rs['num'])
			{
				$DB->db_query("UPDATE {$db_prefix}tags SET usenum = ".$rs['num']." WHERE tagid = ".$tagid);
			}
			else
			{
				$DB->db_query("DELETE FROM {$db_prefix}tags WHERE tagid = ".$tagid);
			}
		}
		return TRUE;
	}
	return FALSE;
}
?>