array ( 'type' => 'TINYINT', 'length' => 3, 'unsigned' => true, 'not_null' => true, 'auto_increment' => true ), 'pkgdir' => array ( 'type' => 'VARCHAR', 'length' => 255, 'not_null' => true, 'default' => '', 'unique' => true ), 'name' => array ( 'type' => 'VARCHAR', 'length' => 255, 'unique' => true ), 'order' => array ( 'type' => 'TINYINT', 'length' => 4 ), 'flags' => array ( 'type' => 'VARCHAR', 'length' => 255, 'not_null' => true, 'default' => '' ), 'headers' => array ( 'type' => 'TEXT', 'not_null' => true ) ); private $headers_cache; // Returns $this->headers as an array public function &get_headers() { if (isset($headers_cache)) return $headers_cache; foreach (explode("\n", $this->headers) as $line) { if (!$line) continue; list($name, $val)=explode(': ', $line, 2); $this->headers_cache[strtolower($name)]=$val; } return $this->headers_cache; } // Reads the data from the Packages file in $this->pkgdir public function read_Packages($update_pkgs=false, $verbose=false) { global $conf; if (!is_readable($file="{$conf['pkgdir_root']}/$this->pkgdir/Packages")) { throw_exception("Packages file doesn't exist for pkgdir $this->pkgdir"); } $file=fopen($file, 'r'); $this->headers=''; while (!feof($file)) { $line=rtrim(fgets($file)); if (strlen($line) == 0) { break; } else { list($name, $val)=array_merge(explode(': ', $line, 2), array(null)); if ($val !== null) { $this->headers.="$name: $val\n"; } } } $p=array(); $cur=null; $this->write(); while (!feof($file)) { $line=rtrim(fgets($file)); if (strlen($line) == 0) { unset($cur); continue; } list($name, $val)=array_merge(explode(': ', $line, 2), array(null)); if ($name == 'CPV') { if (preg_match('#^([^/-]+)([^/]*)/(.+?)-([^-]+)((?:-r[0-9]+)?)$#', $val, $match)) { list(, $bcat, $lcat, $name, $ver, $r)=$match; $ver.=$r; } else { debug("Unsplittable atom: $val"); continue; } if (isset($p[$bcat][$lcat][$name][$ver])) { debug("Duplicate package $bcat$lcat/$name-$ver"); continue; } $p[$bcat][$lcat][$name][$ver]=''; $cur=&$p[$bcat][$lcat][$name][$ver]; } elseif (isset($cur, $val)) { $cur.="$name: $val\n"; } } unset($cur); $u=$d=$t=0; if ($update_pkgs) { global $S; $r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) { $pkg=new sql_gentoo_package($pkg); if (isset($p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version])) { $t++; if ($pkg->data != $p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]) { $u++; $pkg->data=$p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]; if ($verbose) echo "U $pkg->bcat$pkg->lcat/$pkg->name/$pkg->version\n"; $pkg->write(); } unset($p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]); } else { $d++; if ($verbose) echo "D $pkg->bcat$pkg->lcat/$pkg->name-$pkg->version\n"; $pkg->delete(); } } } $n=0; foreach ($p as $bcat => $lcats) { foreach ($lcats as $lcat => $pkgs) { foreach ($pkgs as $pkg => $vers) { foreach ($vers as $ver => $data) { $t++; $n++; $gp=new sql_gentoo_package(null, $this->id, $bcat, $lcat, $pkg, $ver, $data); if ($verbose) echo "A $bcat$lcat/$pkg-$ver\n"; $gp->write(); } } } } return array($n, $u, $d, $t); } public function read_pkgsets($update=false) { global $S; $file=realpath(BACKEND.'/../gentoo_pkgsets.csv'); if (!is_readable($file)) return false; $file=fopen($file, 'r'); if ($update) $exists=array(); while (!feof($file)) { $add=array(); $pkgs=explode("\t", trim(fgets($file))); if (substr($pkgs[0], 0, 1) == '#') continue; $name=array_shift($pkgs); $obj=new sql_gentoo_pkgset(); if ($update) { $r=$S['pdo']->query('SELECT * FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.' AND `name`="'.$name.'" LIMIT 1'); if ($r->rowCount()) $obj->from_array($r->fetch(PDO::FETCH_ASSOC), true); } foreach ($pkgs as $pkg) { if ($pkg=sql_gentoo_package::from_atom($pkg, $this)) $add[]="$pkg->bcat$pkg->lcat/$pkg->name"; } if (count($add)) { $obj->profile=$this->id; $obj->name=$name; $obj->packages=implode("\n", $add); $obj->write(); if ($update) $exists[]=$obj->id; } } if ($update) $S['pdo']->query('DELETE FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.($exists?' AND `id` NOT IN ('.implode(',', $exists).')':'')); } public function &get_packages() { global $S; $r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id); $p=array(); while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) { $pkg=new sql_gentoo_package($pkg); $p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]=$pkg->to_array(); } return $p; } } ?>