diff options
-rw-r--r-- | class/PackagistClient.php | 115 | ||||
-rw-r--r-- | class/Parser.php | 49 | ||||
-rw-r--r-- | refresh.php | 14 |
3 files changed, 175 insertions, 3 deletions
diff --git a/class/PackagistClient.php b/class/PackagistClient.php new file mode 100644 index 0000000..2cb1260 --- /dev/null +++ b/class/PackagistClient.php @@ -0,0 +1,115 @@ +<?php + +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + +/** + * PackagistClient is a PHP class to interact with web services + * + * PHP version 5 + * + * Copyright (C) 2014 Remi Collet + * http://github.com/remicollet/rpmphp. + * + * Inspired from python-fedora + * + * PackagistClient is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * PackagistClient is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * See <http://www.gnu.org/licenses/> + * + * @category Main + * @package PackagistClient + * + * @author Remi Collet <remi@fedoraproject.org> + * @copyright 2010-2014 Remi Collet + * @license http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. + */ + +if (!function_exists('curl_version')) { + die("curl extension required\n"); +} +require_once 'Cache/Lite.php'; + +class PackagistClient +{ + const URL = 'https://packagist.org/'; + protected $cache; + + function __construct () + { + $dir = "/tmp/pkgist-".posix_getlogin()."/"; + @mkdir($dir); + $this->cache = new Cache_Lite( + array( + 'memoryCaching' => true, + 'cacheDir' => $dir, + 'automaticSerialization' => true + ) + ); + } + + function getPackageData($name) + { + $url = self::URL.'packages/'.$name.'.json'; + $rep = $this->cache->get(__METHOD__, $url); + if (!$rep) { + $rep = @file_get_contents($url); + $this->cache->save($rep, __METHOD__, $url); + } + return ($rep ? json_decode($rep, true) : false); + } + + function getPackage($name) + { + $unstable = array('alpha', 'beta', 'rc'); + + $ret = false; + $pkgs = $this->getPackageData($name); + if ($pkgs) { + $ret = array( + 'name' => $name, + 'stable' => NULL, + 'unstable' => NULL, + 'state' => NULL, + ); + foreach ($pkgs['package']['versions'] as $pkver => $pkg) { + if (preg_match('/^v[0-9]/', $pkver)) { + $pkver = substr($pkver, 1); + } + if (strpos($pkver, 'dev')) { + continue; + } + $type = 'stable'; + $subt = false; + foreach($unstable as $i) { + if (stripos($pkver, $i)) { + $type = 'unstable'; + $subt = $i; + } + } + if (version_compare($pkver, $ret[$type], 'gt')) { + $ret[$type] = $pkver; + if ($subt) { + $ret['state'] = $subt; + } + } + } + if ($ret['stable']) { + if (version_compare($ret['stable'], $ret['unstable'], 'gt')) { + $ret['unstable'] = NULL; + $ret['state'] = NULL; + } + } + } + return $ret; + } +} diff --git a/class/Parser.php b/class/Parser.php index 31a000a..cdb984f 100644 --- a/class/Parser.php +++ b/class/Parser.php @@ -463,6 +463,55 @@ class Parser } /** + * Parse the content of all Packagist repository + * + * @param TableUpstream $uptable the table to write to + * @param TablePackagist $pktable the table to read from + * + * @return integer number of parsed line + */ + static public function readPackagist(TableUpstream $uptable, TablePackagist $pktable) + { + self::log("Packagist search releases"); + $pk = new PackagistClient(); + + $nb = $uptable->delete(array('type'=>'composer', 'channel'=>'packagist')); + self::log("Delete $nb packages"); + + $tot = 0; + foreach($pktable->request(array('ORDER'=>'rpmname')) as $rec) { + if ($rep = $pk->getPackage($rec['pkgname'])) { + $id = false; + if ($rep['stable']) { + $id = $uptable->record( + 'composer', + 'packagist', + $rec['rpmname'], + $rep['stable'], + true + ); + } + if ($rep['unstable']) { + $id = $uptable->record( + 'composer', + 'packagist', + $rec['rpmname'], + $rep['unstable'], + false, + $rep['state'] + ); + } + if ($id) { + $tot++; + } + } + } + self::log("Write $tot packages"); + + return $tot; + } + + /** * Parse the content of all PEAR repository * * @param TableUpstream $uptable the table to write to diff --git a/refresh.php b/refresh.php index 37895cf..cff4d03 100644 --- a/refresh.php +++ b/refresh.php @@ -49,8 +49,8 @@ require "include/main.php"; require "class/CommonTable.php"; if ($_SERVER['argc']>1 && in_array('help', $_SERVER['argv'])) { - echo "Options in: owner R pear pecl optimize repo old empty\n"; - echo "Defaults: owner R pear pecl optimize repo \n"; + echo "Options in: owner pkgist R pear pecl optimize repo old empty\n"; + echo "Defaults: owner pkgist R pear pecl optimize repo \n"; die("\n"); } @@ -62,7 +62,9 @@ try { // ------------------------------------------------------------------- echo date("r : ") . "Refreshing " . MYBASE . " database\n"; + $pkgist = new TablePackagist($db); $rpmtable = new TableRpm($db); + if ($_SERVER['argc']==1 || in_array('repo', $_SERVER['argv'])) { $crit = array('id' => '>0'); @@ -78,7 +80,7 @@ try { Parser::readRpm($rpmtable, new TableRpmRepo($db), $crit); $crit['id'] = '<0'; - Parser::readProvides(new TablePackagist($db), new TableRpmRepo($db), $crit); + Parser::readProvides($pkgist, new TableRpmRepo($db), $crit); } // ------------------------------------------------------------------- @@ -87,6 +89,12 @@ try { $uptable = new TableUpstream($db); // ------------------------------------------------------------------- + if ($_SERVER['argc']==1 || in_array('pkgist', $_SERVER['argv'])) { + + Parser::readPackagist($uptable, $pkgist); + } + + // ------------------------------------------------------------------- if ($_SERVER['argc']==1 || in_array('pecl', $_SERVER['argv'])) { Parser::readPecl($uptable, 'http://pecl.php.net/xmlrpc.php'); |