<?PHP
/******************************************************
    ebuild class

- used to import and retreive ebuild information
******************************************************/

class ebuild{
    
    var 
$id 0;
    var 
$data = array();
    var 
$keywords = array();
    var 
$urls = array();

    function 
ebuild($id 0){
        if(
$id != 0){
            
$id intval($id);
            
// load up the ebuild's data
            
$sql "SELECT ebuild_display, ebuild_description, ebuild_urls, ebuild_keywords, ebuild_use, ebuild_licence, ebuild_version, ebuild_revision FROM ebuild WHERE ebuild_p = {$id}";
            
$results mysql_query($sql) or die(mysql_error());
            if(
$row mysql_fetch_assoc($results)){
                
$this->id $id;
                
$this->data $row;
            }
        }
    }
    
    function 
content(){
        
$sql "SELECT ebuild_contents FROM ebuild_contents WHERE ebuild_f = {$this->id}";
        
$results mysql_query($sql) or die(mysql_error());
        if(
$row mysql_fetch_assoc($results))
            return 
$row['ebuild_contents'];
        return 
'';
    }
    
    function 
urls(){
        if(
count($this->urls) == 0){
            
$tmpurls explode(' '$this->data['ebuild_urls']);
            if(
count($tmpurls) >0)
                foreach(
$tmpurls as $value)
                    
$this->urls[] = array('url' => $value);
        }
        
        return 
$this->urls;
    }
    
    function 
keywords(){
        if(
count($this->keywords) == 0){
            
$sql "SELECT `keyword` FROM `ebuild_keyword`, `keyword` WHERE `ebuild_f` = {$this->id} AND `keyword_f` = `keyword_p`";
            
$results mysql_query($sql) or die(mysql_error());
            while(
$row mysql_fetch_assoc($results)){
                
$this->keywords[] = $row['keyword'];
            }
        }
        
        return 
$this->keywords;
    }
    
    function 
import($ebuild$details$packageid false$categoryid false){
        global 
$GLOBAL;
        
        
// check for existing category
        
if(!$categoryid){
            
$category = new category($details['category'], true);
            
$categoryid $category->id;
        }
        
// check for existing package
        
if(!$packageid){
            
$package = new package($categoryid$details['title'], true);
            
$packageid $package->id;
        }
        
// check for existing ebuild
        
$sql "SELECT `ebuild_p`, `ebuild_contents_md5` 
                FROM `ebuild` 
                WHERE `package_f` = {$packageid} AND ebuild_display = '"
.mysqlclean($ebuild)."' AND `_removed` IS NULL LIMIT 0,1";
        
$results mysql_query($sql) or die(mysql_error());
        
$row mysql_fetch_assoc($results);
        if(
is_array($row)){
             
$this->id $row['ebuild_p'];
            
// set marker
            
$sql "UPDATE `ebuild` SET `_marker` = 0 WHERE ebuild_p = {$this->id}";
            
mysql_query($sql) or die(mysql_error());
        }
        
        
        
// setup ebuild data for checking
        
$ebuild_path $GLOBAL['portage_dir'].'/'.$details['category'].'/'.$details['title'].'/'.$ebuild.'.ebuild';
        
$ebuild_meta_path $GLOBAL['portage_dir'].'/metadata/cache/'.$details['category'].'/'.$ebuild;
        if(!
is_file($ebuild_path)) return false;
        
$ebuild_md5 md5_file($ebuild_path);

        if(!
is_array($row) || $row['ebuild_contents_md5'] != $ebuild_md5){
            
// read the metadata
            
$meta $this->read_meta_file($ebuild_meta_path);
        
            
// read the ebuild
            
$ebuild_contents file_get_contents($ebuild_path);
        
            
// import/update record
            
$sql_data "
             ebuild_display        = '"
.mysqlclean($ebuild)."',
             ebuild_search        = '"
.mysqlclean($details['category'].'/'.$ebuild)."',
             ebuild_description    = '"
.mysqlclean($meta['description'])."',
             ebuild_urls        = '"
.mysqlclean($meta['urls'])."',
             ebuild_keywords    = '"
.mysqlclean($meta['keywords'])."',
             ebuild_use            = '"
.mysqlclean($meta['use'])."',
             ebuild_licence        = '"
.mysqlclean($meta['license'])."',
             ebuild_version        = '"
.mysqlclean($details['version'])."',
             ebuild_revision    = '"
.mysqlclean($details['revision'])."',
             ebuild_ideps        = '"
.mysqlclean($meta['ideps'])."',
             ebuild_rdeps        = '"
.mysqlclean($meta['rdeps'])."',
             ebuild_contents_md5 = '"
.mysqlclean($ebuild_md5)."'
            "
;
            if(!
$row){
                
// insert new row
                
$sql "INSERT INTO `ebuild` SET `package_f` = {$packageid}, `_added` = NOW(), " $sql_data;
                
mysql_query($sql) or die(mysql_error());
                
                
$this->id mysql_insert_id();
                
                
$sql "INSERT INTO ebuild_contents (`ebuild_f`, `ebuild_contents`) VALUES ({$this->id}, '".mysqlclean($ebuild_contents)."')";
                
mysql_query($sql) or die(mysql_error());
            }else{
                
$this->id $row['ebuild_p'];
                
// update existing
                
$sql "UPDATE `ebuild` SET " $sql_data " WHERE `ebuild_p` = {$this->id}";
                
mysql_query($sql) or die(mysql_error());
                
$sql "UPDATE `ebuild_contents` SET ebuild_contents = '".mysqlclean($ebuild_contents)."' WHERE `ebuild_f` = {$this->id}";
                
mysql_query($sql) or die(mysql_error());
            }
            
            
// update the keywords
            
$this->_update_keywords($meta['keywords']);

            
// update the USE flags

            // update the dependancies
        
}
        
    }
    
    function 
_update_dependancies($new_dependancies){
        
    }
    
    function 
_update_use_flags($new_flags){
        
    }
    
    function 
_update_keywords($new_keywords){
        
$keyworddb = new keywords();
        
$old_keywords $this->keywords();
        
$new_keywords explode(' '$new_keywords);
        
// frontwords lookup
        
foreach($new_keywords as $key => $word){
            
$new_keywords[$key] = trim($word);
            if(!
in_array(trim($word), $old_keywords) && substr(trim($word), 0,1) != '-'){
                
$sql "INSERT INTO `ebuild_keyword` (`ebuild_f`, `keyword_f`, `_added`) VALUES ({$this->id}, ".$keyworddb->getId(trim($word), true).", NOW())";
                
mysql_query($sql) or die(mysql_error());
                
$old_keywords[] = trim($word);
            }
        }
        
//backwards lookup for deletion
        
foreach($old_keywords as $key => $word){
            if(!
in_array(trim($word), $new_keywords) && substr(trim($word), 0,1) != '-'){
                
$sql "DELETE FROM `ebuild_keyword` WHERE ebuild_f = {$this->id} AND keyword_f = ".$keyworddb->getId(trim($word), true)." ";
                
mysql_query($sql) or die(mysql_error());
                
$old_keywords[] = trim($word);
            }
        }
    }
    
    function 
read_meta_file($filename){
        
/****************************************
            line definitions:
        0. Install Dependancies
        1. Runtime Dependancies
        2. 
        3. File URL(s)
        4. 
        5. URL(s)
        6. Licences
        7. Description
        8. Keywords
        9. 
        10. USE Flags
        ****************************************/
        
$data = array();
        
$file_data file_get_contents($filename);
        
$line explode("\n"$file_data);
        
        
$data['ideps']             = $line[0];
        
$data['rdeps']             = $line[1];
        
$data['description']     = $line[7];
        
$data['urls']             = $line[5];
        
$data['keywords']         = $line[8];
        
$data['use']            = $line[10];
        
$data['license']        = $line[6];
        
        return 
$data;
    }
    
}

?>