<commit_before><?php
/**
 * PEAR2_Pyrus_Package_Remote
 *
 * PHP version 5
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    <NAME> <<EMAIL>>
 * @copyright 2008 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
 */

/**
 * Class representing a remote package
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    <NAME> <<EMAIL>>
 * @copyright 2008 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
 */
class PEAR2_Pyrus_Package_Remote extends PEAR2_Pyrus_Package
{
    private $_info;
    protected $_parsedname;
    protected $_explicitState;
    protected $_explicitGroup;
    protected $downloadInfo;
    /**
     * @param string $package path to package file
     */
    function __construct($package, PEAR2_Pyrus_Package $parent)
    {
        $this->_info = $package;
        if (!is_array($package) &&
              (preg_match('#^(http[s]?|ftp[s]?)://#', $package))) {
            $this->internal = $this->_fromUrl($package);
        } else {
            $this->internal = $this->_fromString($package);
        }
        $this->from = $parent;
    }

    /**
     * Convert this remote packagefile into a local .tar, .tgz or .phar
     *
     * @return PEAR2_Pyrus_Package_Base
     */
    function download()
    {
        if ($this->_type === 'url') {
            return $this->internal;
        }

        $internal = $this->internal;
        // first try to download zip, then tgz, then tar
        $errs = new PEAR2_MultiErrors;
        try {
            $this->internal = new PEAR2_Pyrus_Package_Remote(
                $this->downloadInfo['url'] . '.tgz', $this);
        } catch (Exception $e) {
            $errs->E_ERROR[] = $e;
        }

        if (isset($e)) {
            unset($e);
            try {
                $this->internal = new PEAR2_Pyrus_Package_Remote(
                    $this->downloadInfo['url'] . '.tar', $this);
            } catch (Exception $e) {
                $errs->E_ERROR[] = $e;
            }
        }

        if (isset($e)) {
            unset($e);
            try {
                $this->internal = new PEAR2_Pyrus_Package_Remote(
                    $this->downloadInfo['url'] . '.zip', $this);
            } catch (Exception $e) {
                $errs->E_ERROR[] = $e;
                throw new PEAR2_Pyrus_Package_Exception(
                    'Could not download abstract package ' .
                    $this->downloadInfo['info']->channel . '/' .
                    $this->downloadInfo['info']->name, $errs);
            }
        }

        $this->internal->setFrom($internal);
        return $this->internal;
    }

    private function _fromUrl($param, $saveparam = '')
    {
        $this->_type = 'url';
        // for now, we'll use without callback
//            $callback = $this->_downloader->ui ?
//                array(&$this->_downloader, '_downloadCallback') : null;
        $dir = PEAR2_Pyrus_Config::current()->download_dir;
        try {
            $http = new PEAR2_HTTP_Request($param);
            $response = $http->sendRequest();
            $name = 'unknown.tgz';
            if ($response->code != '200') {
                throw new PEAR2_Pyrus_Package_Exception('Download failed, received ' . $response->code);
            }

            if (isset($response->headers['content-disposition'])) {
                if (preg_match('/filename="(.+)"/', $response->headers['content-disposition'], $match)) {
                    $name = $match[1];
                }
            }

            if (!@file_exists($dir)) {
                mkdir($dir, 0755, true);
            }

            file_put_contents($dir . DIRECTORY_SEPARATOR . $name, $response->body);

            // whew, download worked!
            $a = new PEAR2_Pyrus_Package($dir . DIRECTORY_SEPARATOR . $name);
            return $a->getInternalPackage();
        } catch (Exception $e) {
            if (!empty($saveparam)) {
                $saveparam = ", cannot download \"$saveparam\"";
            }
            throw new PEAR2_Pyrus_Package_Exception('Could not download from "' . $param .
                '"' . $saveparam, $e);
        }
    }

    /**
     *
     * @param string|array pass in an array of format
     *                     array(
     *                      'package' => 'pname',
     *                     ['channel' => 'channame',]
     *                     ['version' => 'version',]
     *                     ['state' => 'state',])
     *                     or a string of format [channame/]pname[-version|-state]
     */
    private function _fromString($param)
    {
        try {
            $pname = PEAR2_Pyrus_Config::parsePackageName($param, true);
        } catch (Exception $e) {
            if ($e->why !== 'channel') {
                throw new PEAR2_Pyrus_Package_Exception('Cannot process remote package', $e);
            }

//            if ($this->_downloader->discover($param['channel'])) {
                if (PEAR2_Pyrus_Config::current()->auto_discover) {
                    try {
                        $pname =
                        PEAR2_Pyrus_Config::parsePackageName($param,
                                PEAR2_Pyrus_Config::current()->default_channel);
                    } catch (Exception $e) {
                        if (is_array($param)) {
                            $param =
                              PEAR2_Pyrus_Config::parsedPackageNameToString($param);
                        }

                        throw new PEAR2_Pyrus_Package_Exception(
                            'invalid package name/package file "' . $param . '"', $e);
                    }
                } else {
//                    if (!isset($options['soft'])) {
                        PEAR2_Pyrus_Log::log(0, 'Channel "' . $param['channel'] .
                            '" is not initialized, use ' .
                            '"pyrus channel-discover ' . $param['channel'] . '" to initialize' .
                            'or pyrus config-set auto_discover 1');
//                    }
                }
//            } else {
//                throw new PEAR2_Pyrus_Package_Exception(
//                    'invalid package name/package file "' . $param . '"', $e);
//            }
        }

        $this->_parsedname    = $pname;
        $this->_explicitState = isset($pname['state']) ? $pname['state'] : false;
        $this->_explicitGroup = isset($pname['group']) ? true            : false;

        $info = $this->getPackageDownloadUrl($pname);
        $this->analyze($info, $param, $pname);
        $this->downloadInfo = $info;
        return $info['info'];
    }

    /**
     * @param array output of {@link parsePackageName()}
     * @access private
     */
    function getPackageDownloadUrl($parr)
    {
        $curchannel = PEAR2_Pyrus_Config::current()->default_channel;
        PEAR2_Pyrus_Config::current()->default_channel = $parr['channel'];
        // getDownloadURL returns an array.  On error, it only contains information
        // on the latest release as array(version, info).  On success it contains
        // array(version, info, download url string)
        $state = isset($parr['state']) ? $parr['state'] :  PEAR2_Pyrus_Config::current()->preferred_state;
        if (!isset(PEAR2_Pyrus_Config::current()->channelregistry[$parr['channel']])) {
            do {
                if (PEAR2_Pyrus_Config::current()->auto_discover) {
                    if (PEAR2_Pyrus_Config::current()->channelregistry->discover($parr['channel'])) {
                        break;
                    }
                }
                PEAR2_Pyrus_Config::current()->default_channel = $curchannel;
                throw new PEAR2_Pyrus_Package_Exception(
                    'Unknown remote channel: ' . $remotechannel);
            } while (false);
        }

        try {
            $chan = PEAR2_Pyrus_Config::current()->channelregistry[$parr['channel']];
        } catch (Exception $e) {
            throw new PEAR2_Pyrus_Package_Exception('Cannot retrieve download information ' .
                'for remote abstract package ' . $parr['channel'] . '/' . $parr['package'], $e);
        }

        try {
            $version = PEAR2_Pyrus_Config::current()->registry->info($parr['package'],
                $parr['channel'], 'version');
        } catch (Exception $e) {
            $version = null;
        }

        $base2    = false;
        $mirrors  = $chan->mirrors;
        $p_mirror = PEAR2_Pyrus_Config::current()->preferred_mirror;
        $mirror   = isset($mirrors[$p_mirror]) ? $mirrors[$p_mirror] : $chan;

        if (!$mirror->supportsREST() ||
              !(($base2 = $mirror->getBaseURL('REST1.3')) ||
              ($base = $mirror->getBaseURL('REST1.0')))) {
            throw new PEAR2_Pyrus_Package_Exception('Cannot retrieve remote information, ' .
                'channel ' . $chan->name . ' does not support REST');
        }

        if ($base2) {
            $rest = new PEAR2_Pyrus_REST_13;
            $base = $base2;<commit_msg>Use new syntax for determining rest base url.<commit_after><?php
/**
 * PEAR2_Pyrus_Package_Remote
 *
 * PHP version 5
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    <NAME> <<EMAIL>>
 * @copyright 2008 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
 */

/**
 * Class representing a remote package
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    <NAME> <<EMAIL>>
 * @copyright 2008 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
 */
class PEAR2_Pyrus_Package_Remote extends PEAR2_Pyrus_Package
{
    private $_info;
    protected $_parsedname;
    protected $_explicitState;
    protected $_explicitGroup;
    protected $downloadInfo;
    /**
     * @param string $package path to package file
     */
    function __construct($package, PEAR2_Pyrus_Package $parent)
    {
        $this->_info = $package;
        if (!is_array($package) &&
              (preg_match('#^(http[s]?|ftp[s]?)://#', $package))) {
            $this->internal = $this->_fromUrl($package);
        } else {
            $this->internal = $this->_fromString($package);
        }
        $this->from = $parent;
    }

    /**
     * Convert this remote packagefile into a local .tar, .tgz or .phar
     *
     * @return PEAR2_Pyrus_Package_Base
     */
    function download()
    {
        if ($this->_type === 'url') {
            return $this->internal;
        }

        $internal = $this->internal;
        // first try to download zip, then tgz, then tar
        $errs = new PEAR2_MultiErrors;
        try {
            $this->internal = new PEAR2_Pyrus_Package_Remote(
                $this->downloadInfo['url'] . '.tgz', $this);
        } catch (Exception $e) {
            $errs->E_ERROR[] = $e;
        }

        if (isset($e)) {
            unset($e);
            try {
                $this->internal = new PEAR2_Pyrus_Package_Remote(
                    $this->downloadInfo['url'] . '.tar', $this);
            } catch (Exception $e) {
                $errs->E_ERROR[] = $e;
            }
        }

        if (isset($e)) {
            unset($e);
            try {
                $this->internal = new PEAR2_Pyrus_Package_Remote(
                    $this->downloadInfo['url'] . '.zip', $this);
            } catch (Exception $e) {
                $errs->E_ERROR[] = $e;
                throw new PEAR2_Pyrus_Package_Exception(
                    'Could not download abstract package ' .
                    $this->downloadInfo['info']->channel . '/' .
                    $this->downloadInfo['info']->name, $errs);
            }
        }

        $this->internal->setFrom($internal);
        return $this->internal;
    }

    private function _fromUrl($param, $saveparam = '')
    {
        $this->_type = 'url';
        // for now, we'll use without callback
//            $callback = $this->_downloader->ui ?
//                array(&$this->_downloader, '_downloadCallback') : null;
        $dir = PEAR2_Pyrus_Config::current()->download_dir;
        try {
            $http = new PEAR2_HTTP_Request($param);
            $response = $http->sendRequest();
            $name = 'unknown.tgz';
            if ($response->code != '200') {
                throw new PEAR2_Pyrus_Package_Exception('Download failed, received ' . $response->code);
            }

            if (isset($response->headers['content-disposition'])) {
                if (preg_match('/filename="(.+)"/', $response->headers['content-disposition'], $match)) {
                    $name = $match[1];
                }
            }

            if (!@file_exists($dir)) {
                mkdir($dir, 0755, true);
            }

            file_put_contents($dir . DIRECTORY_SEPARATOR . $name, $response->body);

            // whew, download worked!
            $a = new PEAR2_Pyrus_Package($dir . DIRECTORY_SEPARATOR . $name);
            return $a->getInternalPackage();
        } catch (Exception $e) {
            if (!empty($saveparam)) {
                $saveparam = ", cannot download \"$saveparam\"";
            }
            throw new PEAR2_Pyrus_Package_Exception('Could not download from "' . $param .
                '"' . $saveparam, $e);
        }
    }

    /**
     *
     * @param string|array pass in an array of format
     *                     array(
     *                      'package' => 'pname',
     *                     ['channel' => 'channame',]
     *                     ['version' => 'version',]
     *                     ['state' => 'state',])
     *                     or a string of format [channame/]pname[-version|-state]
     */
    private function _fromString($param)
    {
        try {
            $pname = PEAR2_Pyrus_Config::parsePackageName($param, true);
        } catch (Exception $e) {
            if ($e->why !== 'channel') {
                throw new PEAR2_Pyrus_Package_Exception('Cannot process remote package', $e);
            }

//            if ($this->_downloader->discover($param['channel'])) {
                if (PEAR2_Pyrus_Config::current()->auto_discover) {
                    try {
                        $pname =
                        PEAR2_Pyrus_Config::parsePackageName($param,
                                PEAR2_Pyrus_Config::current()->default_channel);
                    } catch (Exception $e) {
                        if (is_array($param)) {
                            $param =
                              PEAR2_Pyrus_Config::parsedPackageNameToString($param);
                        }

                        throw new PEAR2_Pyrus_Package_Exception(
                            'invalid package name/package file "' . $param . '"', $e);
                    }
                } else {
//                    if (!isset($options['soft'])) {
                        PEAR2_Pyrus_Log::log(0, 'Channel "' . $param['channel'] .
                            '" is not initialized, use ' .
                            '"pyrus channel-discover ' . $param['channel'] . '" to initialize' .
                            'or pyrus config-set auto_discover 1');
//                    }
                }
//            } else {
//                throw new PEAR2_Pyrus_Package_Exception(
//                    'invalid package name/package file "' . $param . '"', $e);
//            }
        }

        $this->_parsedname    = $pname;
        $this->_explicitState = isset($pname['state']) ? $pname['state'] : false;
        $this->_explicitGroup = isset($pname['group']) ? true            : false;

        $info = $this->getPackageDownloadUrl($pname);
        $this->analyze($info, $param, $pname);
        $this->downloadInfo = $info;
        return $info['info'];
    }

    /**
     * @param array output of {@link parsePackageName()}
     * @access private
     */
    function getPackageDownloadUrl($parr)
    {
        $curchannel = PEAR2_Pyrus_Config::current()->default_channel;
        PEAR2_Pyrus_Config::current()->default_channel = $parr['channel'];
        // getDownloadURL returns an array.  On error, it only contains information
        // on the latest release as array(version, info).  On success it contains
        // array(version, info, download url string)
        $state = isset($parr['state']) ? $parr['state'] :  PEAR2_Pyrus_Config::current()->preferred_state;
        if (!isset(PEAR2_Pyrus_Config::current()->channelregistry[$parr['channel']])) {
            do {
                if (PEAR2_Pyrus_Config::current()->auto_discover) {
                    if (PEAR2_Pyrus_Config::current()->channelregistry->discover($parr['channel'])) {
                        break;
                    }
                }
                PEAR2_Pyrus_Config::current()->default_channel = $curchannel;
                throw new PEAR2_Pyrus_Package_Exception(
                    'Unknown remote channel: ' . $remotechannel);
            } while (false);
        }

        try {
            $chan = PEAR2_Pyrus_Config::current()->channelregistry[$parr['channel']];
        } catch (Exception $e) {
            throw new PEAR2_Pyrus_Package_Exception('Cannot retrieve download information ' .
                'for remote abstract package ' . $parr['channel'] . '/' . $parr['package'], $e);
        }

        try {
            $version = PEAR2_Pyrus_Config::current()->registry->info($parr['package'],
                $parr['channel'], 'version');
        } catch (Exception $e) {
            $version = null;
        }

        $base2    = false;
        $mirrors  = $chan->mirrors;
        $p_mirror = PEAR2_Pyrus_Config::current()->preferred_mirror;
        $mirror   = isset($mirrors[$p_mirror]) ? $mirrors[$p_mirror] : $chan;

        if (!$mirror->supportsREST() ||
              !(($base2 = $mirror->protocols->rest['REST1.3']->baseurl) ||
              ($base = $mirror->protocols->rest['REST1.0']->baseurl))) {
            throw new PEAR2_Pyrus_Package_Exception('Cannot retrieve remote information, ' .
                'channel ' . $chan->name . ' does not support REST');
        }

        if ($base2) {
            $rest = new PEAR2_Pyrus_REST_13;
            $base = $base2;
{'fraction_non_alphanumeric': 0.14198662640207074, 'fraction_numerical': 0.007657463330457291, 'mean_word_length': 1.6756600779108355, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 157, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1181297', 'n_tokens_mistral': 5495, 'n_tokens_neox': 5213, 'n_words': 1014}