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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
<?php
namespace phorkie;
class HtmlParser
{
/**
* Contains error message when parse() failed
*/
public $error;
/**
* Array with keys (URL title) and values (arrays of urls)
* Only supported URLs are included.
*
* @var array
*/
protected $arGitUrls;
/**
* Extract git URLs from the given URL, eventually fetching
* HTML and extracting URLs from there.
*
* Sets $error and $arGitUrls class variables
*
* @param string $url Git or HTTP URL
* @param string $html HTML content of $url
*
* @return boolean True when all went well, false in case of an error
* @uses $error
* @uses $arGitUrls
*/
public function extractGitUrls($url, $html = null)
{
if ($url == '') {
$this->error = 'Empty fork URL';
return false;
}
$arUrl = parse_url($url);
$scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
if ($scheme == 'https' && isset($arUrl['host'])
&& $arUrl['host'] == 'gist.github.com'
) {
//https://gist.github.com/cweiske/2400389
// clone URL: https://gist.github.com/2400389.git
$parts = explode('/', ltrim($arUrl['path'], '/'));
if (count($parts == 2)) {
//we only want the number, not the user name
$path = $parts[1];
} else {
$path = ltrim($arUrl['path'], '/');
}
$title = $this->getHtmlTitle($url);
if ($title === null) {
$this->arGitUrls[][] = 'https://gist.github.com/'
. $path . '.git';
} else {
$this->arGitUrls[$title][] = 'https://gist.github.com/'
. $path . '.git';
}
return true;
}
switch ($scheme) {
case 'git':
//clearly a git url
$this->arGitUrls = array(array($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($url, $html);
}
$this->error = 'Unknown URLs scheme: ' . $scheme;
return false;
}
protected function extractUrlsFromHtml($url, $html = null)
{
//HTML is not necessarily well-formed, and Gitorious has many problems
// in this regard
//$sx = simplexml_load_file($url);
libxml_use_internal_errors(true);
if ($html === null) {
$sx = simplexml_import_dom(\DOMDocument::loadHTMLFile($url));
} else {
$sx = simplexml_import_dom(\DOMDocument::loadHTML($html));
}
//FIXME: handle network error
$elems = $sx->xpath('//*[@rel="vcs-git"]');
$titles = $sx->xpath('/html/head/title');
$pageTitle = $this->cleanPageTitle((string) reset($titles));
$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 if ($pageTitle != '') {
$title = $pageTitle;
} 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;
}
public function getGitUrls()
{
return $this->arGitUrls;
}
/**
* Remove application names from HTML page titles
*
* @param string $title HTML page title
*
* @return string Cleaned HTML page title
*/
protected function cleanPageTitle($title)
{
$title = trim($title);
if (substr($title, -9) == '- phorkie') {
$title = trim(substr($title, 0, -9));
}
return $title;
}
public function isSupported($url)
{
$scheme = parse_url($url, PHP_URL_SCHEME);
return $scheme == 'git'
|| $scheme == 'http' || $scheme == 'https';
}
/**
* Extract the title from a HTML URL
*
* @param string $url URL to a HTML page
*
* @return string|null NULL on error, title otherwise
*/
public function getHtmlTitle($url)
{
libxml_use_internal_errors(true);
$doc = \DOMDocument::loadHTMLFile($url);
if ($doc === false) {
return null;
}
$sx = simplexml_import_dom($doc);
$title = (string) $sx->head->title;
if ($title == '') {
return null;
}
return $title;
}
}
?>
|