9 * Array with keys (URL title) and values (arrays of urls)
10 * Only supported URLs are included.
18 public function __construct($url)
20 $this->url = trim($url);
23 public function parse()
25 if ($this->url == '') {
26 $this->error = 'Empty fork URL';
30 $arUrl = parse_url($this->url);
31 $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
33 if ($scheme == 'https' && isset($arUrl['host'])
34 && $arUrl['host'] == 'gist.github.com'
36 $this->arGitUrls[][] = 'git://gist.github.com/'
37 . ltrim($arUrl['path'], '/') . '.git';
44 $this->arGitUrls = array(array($this->url));
48 //FIXME: maybe loosen this when we know how to skip the
49 //"do you trust this server" question of ssh
50 $this->error = 'ssh:// URLs are not supported';
55 return $this->extractUrlsFromHtml($this->url);
58 $this->error = 'Unknown URLs scheme: ' . $scheme;
62 protected function extractUrlsFromHtml($url)
64 //HTML is not necessarily well-formed, and Gitorious has many problems
66 //$sx = simplexml_load_file($url);
67 libxml_use_internal_errors(true);
68 $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
69 $elems = $sx->xpath('//*[@rel="vcs-git"]');
71 $count = $anonymous = 0;
72 foreach ($elems as $elem) {
73 if (!isset($elem['href'])) {
77 if (isset($elem['title'])) {
78 //<link href=".." rel="vcs-git" title="title" />
79 $title = (string)$elem['title'];
80 } else if ($str != '') {
81 //<a href=".." rel="vcs-git">title</a>
84 $title = 'Unnamed repository #' . ++$anonymous;
86 $url = (string)$elem['href'];
87 if ($this->isSupported($url)) {
89 $this->arGitUrls[$title][] = $url;
97 * Iterate through all git urls and return one if there is only
100 * @return mixed Boolean false or string
102 public function getUniqueGitUrl()
105 foreach ($this->arGitUrls as $title => $arUrls) {
106 foreach ($arUrls as $url) {
118 public function getGitUrls()
120 return $this->arGitUrls;
124 * Get the URL from which the git URL was derived, often
129 public function getUrl()
134 public function setUrl($url)
139 public function isSupported($url)
141 return parse_url($url, PHP_URL_SCHEME) == 'git';