. * * @category Main * @package RPMPHP * * @author Remi Collet * @author Johan Cwiklinski * @copyright 2010 Remi Collet * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version * @link http://github.com/remicollet/rpmphp/ * @since The begining of times. */ chdir(dirname($_SERVER["SCRIPT_FILENAME"])); date_default_timezone_set('Europe/Paris'); if (isset($_SERVER["SERVER_NAME"])) { echo "
";
    ini_set("max_execution_time", "0");
    ini_set("memory_limit", "-1");

    die("This script is launched twice a day, be patient");
}
require "include/main.php";
require "class/CommonTable.php";

if ($_SERVER['argc']>1 && in_array('help', $_SERVER['argv'])) {
    echo "Options in: repo owner R pear pecl old\n";
    echo "Defaults:   repo owner R pear pecl\n";
    die("\n");
}

try {
    $db = new PDO ("mysql:dbname=" . MYBASE . ";host=" . MYHOST, MYUSER, MYPASS);

    // -------------------------------------------------------------------
    //	Fedora Packages
    // -------------------------------------------------------------------
    echo date("r : ") . "Refreshing " . MYBASE . " database\n";

    if ($_SERVER['argc']==1 || in_array('repo', $_SERVER['argv'])) {
        if (in_array('empty', $_SERVER['argv'])) {
            $crit = array('stamp' => null);
        } else if (in_array('old', $_SERVER['argv'])) {
            $crit = array();
        } else {
            $crit = array('active' => 1);
        }
        $rpmrepo = new TableRpmRepo($db);
        $rpmtable = new TableRpm($db);

        foreach ($rpmrepo->request($crit) as $row) {
            echo date("r : ") . "REPOSITORY " . $row['main'] . " " .
                $row['sub'] . "\n";
            $TimRemote = 0;
            $repomd = @simplexml_load_file($row['url'] . "repodata/repomd.xml");
            if ($repomd) {
                foreach ($repomd->data as $data) {
                    if ($data->attributes()=="primary") {
                        $TimRemote = $data->timestamp;
                        $UrlRemote = $row['url'] . $data->location->attributes();
                    }
                }
            }
            if (!$TimRemote) {
                echo date("r : ") . "Can't read $UrlRemote\n";

            } else if ($TimRemote > $row['stamp']) {
                echo date("r : ") . "Loading $UrlRemote\n";

                //$fic=gzopen("primary.xml.gz", "r");
                $fic=gzopen($UrlRemote, "r");
                if ($fic) {
                    $txt="";
                    while ($buf=gzread($fic, 8196)) {
                        $txt .= $buf;
                    }
                    echo date("r : ") . "Read " . strlen($txt) . " bytes\n";
                    gzclose($fic);

                    $primary = simplexml_load_string($txt);
                    echo date("r : ") . "Read " . $primary->attributes() .
                        " packages\n";
                    unset($txt);

                    $crit = array(
                        'repo_main' => $row['main'],
                        'repo_sub'  => $row['sub']
                    );
                    $nb = $rpmtable->delete($crit);
                    echo date("r : ") . "Delete $nb packages\n";

                    $tot=0;
                    foreach ($primary->package as $package) {
                        if ($package->attributes()=='rpm') {
                            $ver = $package->version->attributes();

                            $input = array(
                                'repo_main' => $row['main'],
                                'repo_sub'  => $row['sub'],
                                'name'      => $package->name,
                                'epoch'     => $ver['epoch'],
                                'ver'       => $ver['ver'],
                                'rel'       => $ver['rel'],
                                'summary'   => $package->summary,
                                'url'       => $package->url
                            );
                            if ($rpmtable->add($input)) {
                                $tot++;
                            }
                        }
                    }
                    echo date("r : ") . "Write $tot packages\n";

                    $rpmrepo->update($row['id'], array('stamp' =>$TimRemote));

                    unset($primary);
                } else {
                    echo date("r : ") . "ERROR : can't read $UrlRemote\n";
                }
            } else {
                echo date("r : ") . "no update needed : $TimRemote / " .
                    $row['stamp'] . "\n";
            }
        }
    } // If ask

    // -------------------------------------------------------------------
    //	Upstream Version
    // -------------------------------------------------------------------
    $uptable = new TableUpstream($db);

    // -------------------------------------------------------------------
    if ($_SERVER['argc']==1 || in_array('pecl', $_SERVER['argv'])) {
        echo date("r : ") . "PECL listLatestReleases - stable\n";

        $request = xmlrpc_encode_request("package.listLatestReleases", "stable");
        $context = stream_context_create(
            array(
                'http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
                )
            )
        );
        $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context)
            or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)");
        $stable = xmlrpc_decode($file);
        if (xmlrpc_is_fault($stable)) {
            echo date("r : ") .
                "ERROR xmlrpc: $stable[faultString] ($stable[faultCode])";
        } else {
            $nb = $uptable->delete(array('type'=>'pecl', 'channel'=>'pecl'));
            echo date("r : ") . "Delete $nb packages\n";

            $nb=0;
            foreach ($stable as $name => $info) {
                $rpmname="php-pecl-".str_replace("_", "-", $name);

                $id = $uptable->record(
                    'pecl',
                    'pecl',
                    $rpmname,
                    $info["version"],
                    true
                );
                if ($id) {
                    $nb++;
                }
            }
            echo date("r : ") . "Write $nb packages\n";
        }

        // -------------------------------------------------------------------
        echo date("r : ") . "PECL listLatestReleases - unstable\n";

        $request = xmlrpc_encode_request("package.listLatestReleases", array());
        $context = stream_context_create(
            array(
                'http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
                )
            )
        );
        $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context)
            or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)");
        $unstable = xmlrpc_decode($file);
        if (xmlrpc_is_fault($unstable)) {
            echo date("r : ") . "ERROR xmlrpc: $stable[faultString] ".
                "($stable[faultCode])";
        } else {
            $nb=0;
            foreach ($unstable as $name => $info) {
                $rpmname="php-pecl-".str_replace("_", "-", $name);

                $id = $uptable->record(
                    'pecl',
                    'pecl',
                    $rpmname,
                    $info["version"],
                    true,
                    $info["state"]
                );
                if ($id) {
                    $nb++;
                }
            }
            echo date("r : ") . "Write $nb packages\n";
        }
    } // if in options

    // -------------------------------------------------------------------
    if ($_SERVER['argc']==1 || in_array('pear', $_SERVER['argv'])) {
        echo date("r : ") . "PEAR reading channels\n";
        $pear = new TablePearRepo($db);
        $channels = $pear->getAllRepo(true);
        try {
            $nbtot=0;

            foreach ($channels as $channelname => $channelurl) {
                $channel = @simplexml_load_file("http://$channelurl/channel.xml");
                if (!$channel) {
                    throw new Exception(
                        "can't read PEAR site (channel of $channelname)"
                    );
                }

                $rest = $channel->servers->primary->rest->baseurl[0];
                echo date("r : ") . "PEAR reading channel=$channelname" .
                    ", baseurl = $rest\n";

                $categories = @simplexml_load_file($rest."c/categories.xml");
                if (!$categories) {
                    throw new Exception("can't read PEAR site (categories)");
                }

                $crit = array('type'=>'pear', 'channel'=>$channelname);
                $nb = $uptable->delete($crit);
                echo date("r : ") . "Delete $nb packages\n";

                $nb=0;
                if (!isset($categories->c[0])) {
                    echo date("r : ") . "Reading ALL\n"; // ezc only
                    $pitxt = @file_get_contents($rest."p/packages.xml");
                    if (!$pitxt) {
                        throw new Exception(
                            "can't read PEAR site (".$rest."p/packagesinfo.xml)"
                        );
                    }
                    $allpi = @simplexml_load_string($pitxt);
                    foreach ($allpi->p as $name) {
                        $pitxt = @file_get_contents(
                            $rest."r/".strtolower($name)."/allreleases.xml"
                        );
                        if (!$pitxt) {
                            throw new Exception(
                                "can't read PEAR site (".$rest."r/".
                                strtolower($name)."/allreleases.xml"
                            );
                        }
                        $pi = @simplexml_load_string($pitxt);

                        $rpmname1="php-".$channelname."-".
                            str_replace("_", "-", $name);
                        $rpmname2="php-".$channelname."-".$name;

                        //echo $rpmname ."/".(string)$pi->r[0]->v."/".
                        //  (string)$pi->r[0]->s."\n";
                        $uptable->record(
                            "pear",
                            $channelname,
                            $rpmname1,
                            (string)$pi->r[0]->v,
                            false,
                            (string)$pi->r[0]->s
                        );
                        $uptable->record(
                            "pear",
                            $channelname,
                            $rpmname2,
                            (string)$pi->r[0]->v,
                            false,
                            (string)$pi->r[0]->s
                        );
                        foreach ($pi->r as $rev) {
                            if ($rev->s=='stable') {
                                //echo $rpmname ."/".(string)$rev->v."/".
                                //    (string)$rev->s."\n";
                                $uptable->record(
                                    "pear",
                                    $channelname,
                                    $rpmname1,
                                    (string)$rev->v,
                                    true
                                );
                                $uptable->record(
                                    "pear",
                                    $channelname,
                                    $rpmname2,
                                    (string)$rev->v,
                                    true
                                );
                                break;
                            }
                        }
                        $nb++;
                    }

                } else {
                    foreach ($categories->c as $cat) {
                        echo date("r : ") . "Reading $cat\n";

                        $pitxt = @file_get_contents(
                            $rest."c/".urlencode($cat)."/packagesinfo.xml"
                        );
                        if (!$pitxt) {
                            throw new Exception(
                                "can't read PEAR site (".$rest."c/".
                                urlencode($cat)."/packagesinfo.xml)"
                            );
                        }
                        $pitxt = "/U",
                            "",
                            str_replace("\r\n", "\n", substr($pitxt, 2))
                        );

                        $pi = @simplexml_load_string($pitxt);
                        if (!$pi) {
                            throw new Exception("can't read response ($cat)");
                        }
                        foreach ($pi->pi as $ps) {
                            if (isset($ps->p->n) && isset($ps->a->r)) {
                                $name=(string)$ps->p->n;

                                if ($channelname=='phing' && $name=='phing') {
                                    $rpmname1="php-pear-phing";
                                } else if ($channelname=='phpunit'
                                    && $name=='PHPUnit'
                                ) {
                                    $rpmname1="php-pear-PHPUnit";
                                } else {
                                    $rpmname1="php-".$channelname."-".
                                        str_replace("_", "-", $name);
                                }
                                $rpmname2="php-".$channelname."-".$name;

                                $uptable->record(
                                    "pear",
                                    $channelname,
                                    $rpmname1,
                                    (string)$ps->a->r[0]->v,
                                    false,
                                    (string)$ps->a->r[0]->s
                                );
                                $uptable->record(
                                    "pear",
                                    $channelname,
                                    $rpmname2,
                                    (string)$ps->a->r[0]->v,
                                    false,
                                    (string)$ps->a->r[0]->s
                                );
                                foreach ($ps->a->r as $rev) {
                                    if ($rev->s=='stable') {
                                        $uptable->record(
                                            "pear",
                                            $channelname,
                                            $rpmname1,
                                            (string)$rev->v,
                                            true
                                        );
                                        $uptable->record(
                                            "pear",
                                            $channelname,
                                            $rpmname2,
                                            (string)$rev->v,
                                            true
                                        );
                                        break;
                                    }
                                }
                                $nb++;
                                // echo $ps->p->n."\n";
                            }
                        }
                    }
                }
                echo date("r : ") . "read $nb packages in $channelname\n";
                $nbtot += $nb;
            }
            echo date("r : ") . "read $nbtot packages in all channels\n";

        } catch (Exception $e) {
            echo date("r : ") . 'ERROR ' .  $e->getMessage(), "\n";
        }
    } // if in options

    // -------------------------------------------------------------------
    if ($_SERVER['argc']==1 || in_array('R', $_SERVER['argv'])) {
        $tottot=0;
        $rrepo = new TableRRepo($db);
        foreach ($rrepo->request() as $repo) {
            echo date("r : ") . "Reading " . $repo["name"] . " (" .
                $repo["state"] . ")\n";
            $index = @file_get_contents($repo["url"]);
            if (!$index) {
                echo date("r : ") . "Can't read [" . $repo["url"] .
                    "], skip this channel\n";
                continue;
            }
            if ($repo["state"]=="stable") {
                $crit = array('type'=>'R', 'channel'=>$repo['name']);
                $nb = $uptable->delete($crit);
                echo date("r : ") . "Delete $nb packages\n";
            }
            $results=array();
            $pat = '/Package: *(.*)\nVersion: *(.*)\n/i';
            if (preg_match_all($pat, $index, $results, PREG_SET_ORDER)) {
                $tot=0;
                foreach ($results as $result) {
                    //echo $result[1]." = ".$result[2]."\n";
                    $rpmname = "R-".$result[1];
                    $ver = str_replace('-', '.', $result[2]);
                    $add_upstream = $uptable->record(
                        "R",
                        $repo["name"],
                        $rpmname,
                        $ver,
                        $repo["state"]=='stable',
                        ($repo["state"]=="stable"?"":"devel")
                    );

                    if ($add_upstream) {
                        $tot++;
                    }
                }
                echo date("r : ") . "Write $tot packages in this channel\n";
                $tottot += $tot;
            } else {
                echo date("r : ") . "No package in this channel\n";
            }
        }
        echo date("r : ") . "Write $tottot packages in all channels\n";

    } // If R in options

    // -------------------------------------------------------------------
    //	Package Owners from pkgdb (thanks Smootherfrog)
    // -------------------------------------------------------------------
    if ($_SERVER['argc']==1 || in_array('owner', $_SERVER['argv'])) {

        $sql="CREATE TABLE IF NOT EXISTS `acls` (
            `ID` int(11) NOT NULL AUTO_INCREMENT,
            `collection` varchar(100) NOT NULL,
            `name` varchar(100) NOT NULL,
            `summary` varchar(200) NOT NULL,
            `owner` varchar(50) DEFAULT NULL,
            `qa` varchar(50) DEFAULT NULL,
            `cc` varchar(100) DEFAULT NULL,
            PRIMARY KEY (`ID`),
            KEY `name` (`name`),
            KEY `collection` (`collection`),
            KEY `owner` (`owner`)) ENGINE=MyISAM";

        if ($db->exec($sql)!==false) {
            echo date("r : ") . "Check table 'acls' ok\n";
        } else {
            echo date("r : ") . "SQL ERROR = " .
                implode(" ", $db->errorInfo()) . "\n";
        }

        $fic=fopen(
            "https://admin.fedoraproject.org/pkgdb/lists/bugzilla?tg_format=plain",
            "r"
        );
        if (!$fic) {
            echo date("r : ") . "ERROR reading pkgdb\n";
        } else {
            $sql="TRUNCATE `acls`";
            $nb=$db->exec($sql);
            if ($nb===false) {
                echo date("r : ") . "SQL ERROR = " .
                    implode(" ", $db->errorInfo()) . "\n";
            } else {
                echo date("r : ") . "Delete $nb packages\n";
            }

            for ($tot=0 ; $line=fgetcsv($fic, 1024, '|'); ) {
                if (count($line)>5 && substr($line[0], 0, 1)!='#') {
                    for ($i=0; $i<6; $i++) {
                        $line[$i]=trim($line[$i]);
                    }
                    $sql=sprintf(
                        "INSERT INTO `acls`
                         SET collection='%s', name='%s', summary='%s'",
                        $line[0],
                        $line[1],
                        addslashes($line[2])
                    );
                    if (!empty($line[3])) {
                        $sql .= sprintf(", owner='%s'", $line[3]);
                    }
                    if (!empty($line[4])) {
                        $sql .= sprintf(", qa='%s'", $line[4]);
                    }
                    if (!empty($line[5])) {
                        $sql .= sprintf(", cc='%s'", $line[5]);
                    }
                    $nb=$db->exec($sql);
                    if ($nb) {
                        $tot+=$nb;
                    } else {
                        echo date("r : ") . "SQL ERROR = " .
                            implode(" ", $db->errorInfo()) . "\n";
                    }
                }
            }
            fclose($fic);
            echo date("r : ") . "wrote $tot package's owner\n";
        }
    } // If in options

} catch(PDOException $e) {
    printf("%s ERREUR : %s\n", date("r"), $e->getMessage());
}

if (isset($_SERVER["SERVER_NAME"])) {
    echo "
"; } ?>