blob: 3f6a4787ce818f89d4b37a940ffc5abcd0ff126e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php
namespace phorkie;
class ForkRemote
{
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()
{
$arUrl = parse_url($this->url);
$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;
}
}
return $count > 0;
}
/**
* 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';
}
}
?>
|