7 * Contains error message when parse() failed
14 * Array with keys (URL title) and values (arrays of urls)
15 * Only supported URLs are included.
23 public function __construct($url)
25 $this->url = trim($url);
28 public function parse()
30 if ($this->url == '') {
31 $this->error = 'Empty fork URL';
35 $arUrl = parse_url($this->url);
36 $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
38 if ($scheme == 'https' && isset($arUrl['host'])
39 && $arUrl['host'] == 'gist.github.com'
41 $this->arGitUrls[][] = 'git://gist.github.com/'
42 . ltrim($arUrl['path'], '/') . '.git';
49 $this->arGitUrls = array(array($this->url));
53 //FIXME: maybe loosen this when we know how to skip the
54 //"do you trust this server" question of ssh
55 $this->error = 'ssh:// URLs are not supported';
60 return $this->extractUrlsFromHtml($this->url);
63 $this->error = 'Unknown URLs scheme: ' . $scheme;
67 protected function extractUrlsFromHtml($url)
69 //HTML is not necessarily well-formed, and Gitorious has many problems
71 //$sx = simplexml_load_file($url);
72 libxml_use_internal_errors(true);
73 $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
74 $elems = $sx->xpath('//*[@rel="vcs-git"]');
76 $count = $anonymous = 0;
77 foreach ($elems as $elem) {
78 if (!isset($elem['href'])) {
82 if (isset($elem['title'])) {
83 //<link href=".." rel="vcs-git" title="title" />
84 $title = (string)$elem['title'];
85 } else if ($str != '') {
86 //<a href=".." rel="vcs-git">title</a>
89 $title = 'Unnamed repository #' . ++$anonymous;
91 $url = (string)$elem['href'];
92 if ($this->isSupported($url)) {
94 $this->arGitUrls[$title][] = $url;
102 $this->error = 'No git:// clone URL found';
107 * Iterate through all git urls and return one if there is only
110 * @return mixed Boolean false or string
112 public function getUniqueGitUrl()
115 foreach ($this->arGitUrls as $title => $arUrls) {
116 foreach ($arUrls as $url) {
128 public function getGitUrls()
130 return $this->arGitUrls;
134 * Get the URL from which the git URL was derived, often
139 public function getUrl()
144 public function setUrl($url)
149 public function isSupported($url)
151 return parse_url($url, PHP_URL_SCHEME) == 'git';