<commit_before><?php
namespace phorkie;

class ForkRemote
{
    /**
     * Contains error message when parse() failed
     */
    public $error;

    protected $url;

    /**
     * Array with keys (URL title) and values (arrays of urls)
     * Only supported URLs are included.
     *
     * @var array
     */
    protected $arGitUrls;



    public function __construct($url)
    {
        $this->url = trim($url);
    }

    public function parse()
    {
        if ($this->url == '') {
            $this->error = 'Empty fork URL';
            return false;
        }

        $arUrl  = parse_url($this->url);
        $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';

        if ($scheme == 'https' && isset($arUrl['host'])
            && $arUrl['host'] == 'gist.github.com'
        ) {
            $this->arGitUrls[][] = 'git://gist.github.com/'
                . ltrim($arUrl['path'], '/') . '.git';
            return true;
        }

        switch ($scheme) {
        case 'git':
            //clearly a git url
            $this->arGitUrls = array(array($this->url));
            return true;

        case 'ssh':
            //FIXME: maybe loosen this when we know how to skip the
            //"do you trust this server" question of ssh
            $this->error = 'ssh:// URLs are not supported';
            return false;

        case 'http':
        case 'https':
            return $this->extractUrlsFromHtml($this->url);
        }

        $this->error = 'Unknown URLs scheme: ' . $scheme;
        return false;
    }

    protected function extractUrlsFromHtml($url)
    {
        //HTML is not necessarily well-formed, and Gitorious has many problems
        // in this regard
        //$sx = simplexml_load_file($url);
        libxml_use_internal_errors(true);
        $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
        $elems = $sx->xpath('//*[@rel="vcs-git"]');

        $count = $anonymous = 0;
        foreach ($elems as $elem) {
            if (!isset($elem['href'])) {
                continue;
            }
            $str = (string)$elem;
            if (isset($elem['title'])) {
                //<link href=".." rel="vcs-git" title="title" />
                $title = (string)$elem['title'];
            } else if ($str != '') {
                //<a href=".." rel="vcs-git">title</a>
                $title = $str;
            } else {
                $title = 'Unnamed repository #' . ++$anonymous;
            }
            $url = (string)$elem['href'];
            if ($this->isSupported($url)) {
                ++$count;
                $this->arGitUrls[$title][] = $url;
            }
        }

        if ($count > 0) {
            return true;
        }

        $this->error = 'No git:// clone URL found';
        return false;
    }

    /**
     * Iterate through all git urls and return one if there is only
     * one supported one.
     *
     * @return mixed Boolean false or string
     */
    public function getUniqueGitUrl()
    {
        $nFound = 0;
        foreach ($this->arGitUrls as $title => $arUrls) {
            foreach ($arUrls as $url) {
                $nFound++;
                $uniqueUrl = $url;
            }
        }

        if ($nFound == 1) {
            return $uniqueUrl;
        }
        return false;
    }

    public function getGitUrls()
    {
        return $this->arGitUrls;
    }

    /**
     * Get the URL from which the git URL was derived, often
     * the HTTP URL.
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    public function setUrl($url)
    {
        $this->url = $url;
    }

    public function isSupported($url)
    {
        return parse_url($url, PHP_URL_SCHEME) == 'git';
    }
}

?><commit_msg>allow remote forking of HTTP and HTTPS URLs<commit_after><?php
namespace phorkie;

class ForkRemote
{
    /**
     * Contains error message when parse() failed
     */
    public $error;

    protected $url;

    /**
     * Array with keys (URL title) and values (arrays of urls)
     * Only supported URLs are included.
     *
     * @var array
     */
    protected $arGitUrls;



    public function __construct($url)
    {
        $this->url = trim($url);
    }

    public function parse()
    {
        if ($this->url == '') {
            $this->error = 'Empty fork URL';
            return false;
        }

        $arUrl  = parse_url($this->url);
        $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';

        if ($scheme == 'https' && isset($arUrl['host'])
            && $arUrl['host'] == 'gist.github.com'
        ) {
            $this->arGitUrls[][] = 'git://gist.github.com/'
                . ltrim($arUrl['path'], '/') . '.git';
            return true;
        }

        switch ($scheme) {
        case 'git':
            //clearly a git url
            $this->arGitUrls = array(array($this->url));
            return true;

        case 'ssh':
            //FIXME: maybe loosen this when we know how to skip the
            //"do you trust this server" question of ssh
            $this->error = 'ssh:// URLs are not supported';
            return false;

        case 'http':
        case 'https':
            return $this->extractUrlsFromHtml($this->url);
        }

        $this->error = 'Unknown URLs scheme: ' . $scheme;
        return false;
    }

    protected function extractUrlsFromHtml($url)
    {
        //HTML is not necessarily well-formed, and Gitorious has many problems
        // in this regard
        //$sx = simplexml_load_file($url);
        libxml_use_internal_errors(true);
        $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
        $elems = $sx->xpath('//*[@rel="vcs-git"]');

        $count = $anonymous = 0;
        foreach ($elems as $elem) {
            if (!isset($elem['href'])) {
                continue;
            }
            $str = (string)$elem;
            if (isset($elem['title'])) {
                //<link href=".." rel="vcs-git" title="title" />
                $title = (string)$elem['title'];
            } else if ($str != '') {
                //<a href=".." rel="vcs-git">title</a>
                $title = $str;
            } else {
                $title = 'Unnamed repository #' . ++$anonymous;
            }
            $url = (string)$elem['href'];
            if ($this->isSupported($url)) {
                ++$count;
                $this->arGitUrls[$title][] = $url;
            }
        }

        if ($count > 0) {
            return true;
        }

        $this->error = 'No git:// clone URL found';
        return false;
    }

    /**
     * Iterate through all git urls and return one if there is only
     * one supported one.
     *
     * @return mixed Boolean false or string
     */
    public function getUniqueGitUrl()
    {
        $nFound = 0;
        foreach ($this->arGitUrls as $title => $arUrls) {
            foreach ($arUrls as $url) {
                $nFound++;
                $uniqueUrl = $url;
            }
        }

        if ($nFound == 1) {
            return $uniqueUrl;
        }
        return false;
    }

    public function getGitUrls()
    {
        return $this->arGitUrls;
    }

    /**
     * Get the URL from which the git URL was derived, often
     * the HTTP URL.
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    public function setUrl($url)
    {
        $this->url = $url;
    }

    public function isSupported($url)
    {
        $scheme = parse_url($url, PHP_URL_SCHEME);
        return $scheme == 'git'
            || $scheme == 'http' || $scheme == 'https';
    }
}

?>
{'fraction_non_alphanumeric': 0.14750260145681582, 'fraction_numerical': 0.001040582726326743, 'mean_word_length': 1.5053763440860215, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7028368', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2234, 'n_words': 573}