<?php
defined('WikyBlog') or die("Not an entry point...");

//need to define join instead of the "USING( )" method

function emptyTrash(&$list,$dbTable=false,$deletedFlag=true){
	global $wbTables,$dbInfo;
	
	if( !is_array($list) ){
		return;
	}
	if( count($list) < 1){
		return;
	}
	
	//
	//	Where Array
	//	
		$whereA = Array();
		$whereA2 = Array();
		foreach($list as $id){
			if( !is_numeric($id) ){
				continue;
			}
			$whereA[] = ' ( '.$wbTables['all_files'].'.`file_id` = "'.wbDB::escape($id).'") ';
			$whereA2[] = ' ( `to_id` = "'.wbDB::escape($id).'") ';
			$whereA3[] = ' ( `incl_id` = "'.wbDB::escape($id).'") ';
		}
		$where = '';
		if( $deletedFlag ){
			$where .= ' FIND_IN_SET("deleted", flags) AND ';
		}
		$where .= '('.implode(' OR ',$whereA).')';

	//
	//	Breaking up large query to make things easier on the database
	//	Delete from all_links, all_history, all_included and all_blog
	//	
		$query = 'DELETE /* emptyTrash.php (1) */ ';
		$query .= $wbTables['all_links'].', '.$wbTables['all_history'].', '.$wbTables['all_included'].', '.$wbTables['all_blog'];
		$query .= ' FROM ';
		$query .= $wbTables['all_files'].' LEFT JOIN '.$wbTables['all_links'].' USING(`file_id`) '; //this using is ok, since it's the first one
		$query .= ' LEFT JOIN '.$wbTables['all_history'].' ON '.$wbTables['all_history'].'.`file_id` =  '.$wbTables['all_files'].'.`file_id` ';  
		$query .= ' LEFT JOIN '.$wbTables['all_included'].' ON '.$wbTables['all_included'].'.`file_id` =  '.$wbTables['all_files'].'.`file_id` ';  
		$query .= ' LEFT JOIN '.$wbTables['all_blog'].' ON '.$wbTables['all_blog'].'.`file_id` =  '.$wbTables['all_files'].'.`file_id` ';  
		$query .= ' WHERE '.$where;
		wbDB::runQuery($query,true);
		
		
	//
	//	Delete from all_files and dbTable(s)
	//	
		if( $dbTable === false ){
			$deleteFrom = '';
			$join = '';
			foreach($dbInfo as $space => $info){
				if( !isset($info['dbTable']) ){
					continue;
				}
				$deleteFrom .= ', '.$info['dbTable'];
				$join .= ' LEFT JOIN '.$info['dbTable'].' ON '.$info['dbTable'].'.`file_id` =  '.$wbTables['all_files'].'.`file_id` ';  
			}
			
		}else{
			$deleteFrom = ', '.$dbTable;
			$join = ' LEFT JOIN '.$dbTable.' ON '.$dbTable.'.`file_id` =  '.$wbTables['all_files'].'.`file_id` ';  
		}
	
		$query = 'DELETE  /* emptyTrash.php (2) */ ';
		$query .= $wbTables['all_files'];
		$query .= $deleteFrom;
		$query .= ' FROM ';
		$query .= $wbTables['all_files'];
		$query .= $join;
		$query .= ' WHERE '.$where;
		$num = wbDB::runQuery($query,true);
			
		
	
	//
	//	Remove `to_id` from all_links for deleted files (exactly the same as scheduledTasks.php)
	//	
		$query = 'UPDATE '.$wbTables['all_links'];
		$query .= ' SET `to_id` = NULL ';
		$query .= ' WHERE ';
		$query .= '('.implode(' OR ',$whereA2).')';
		wbDB::runQuery($query);
		
	//
	//	Remove `incl_id` from all_included
	//	
				
		$query = 'UPDATE '.$wbTables['all_included'];
		$query .= ' SET `incl_id` = NULL ';
		$query .= ' WHERE ';
		$query .= '('.implode(' OR ',$whereA3).')';
		wbDB::runQuery($query);
		
		
		return $num;
}