7 * Contains error message when parse() failed
12 * Array with keys (URL title) and values (arrays of urls)
13 * Only supported URLs are included.
22 * Extract git URLs from the given URL, eventually fetching
23 * HTML and extracting URLs from there.
25 * Sets $error and $arGitUrls class variables
27 * @param string $url Git or HTTP URL
28 * @param string $html HTML content of $url
30 * @return boolean True when all went well, false in case of an error
34 public function extractGitUrls($url, $html = null)
37 $this->error = 'Empty fork URL';
41 $arUrl = parse_url($url);
42 $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
44 if ($scheme == 'https' && isset($arUrl['host'])
45 && $arUrl['host'] == 'gist.github.com'
48 $this->arGitUrls[][] = 'git://gist.github.com/'
49 . ltrim($arUrl['path'], '/') . '.git';
56 $this->arGitUrls = array(array($url));
60 //FIXME: maybe loosen this when we know how to skip the
61 //"do you trust this server" question of ssh
62 $this->error = 'ssh:// URLs are not supported';
67 return $this->extractUrlsFromHtml($url, $html);
70 $this->error = 'Unknown URLs scheme: ' . $scheme;
74 protected function extractUrlsFromHtml($url, $html = null)
76 //HTML is not necessarily well-formed, and Gitorious has many problems
78 //$sx = simplexml_load_file($url);
80 libxml_use_internal_errors(true);
82 $sx = simplexml_import_dom(\DOMDocument::loadHTMLFile($url));
84 $sx = simplexml_import_dom(\DOMDocument::loadHTML($html));
87 $elems = $sx->xpath('//*[@rel="vcs-git"]');
88 $titles = $sx->xpath('/html/head/title');
89 $pageTitle = $this->cleanPageTitle((string) reset($titles));
91 $count = $anonymous = 0;
92 foreach ($elems as $elem) {
93 if (!isset($elem['href'])) {
97 if (isset($elem['title'])) {
98 //<link href=".." rel="vcs-git" title="title" />
99 $title = (string)$elem['title'];
100 } else if ($str != '') {
101 //<a href=".." rel="vcs-git">title</a>
103 } else if ($pageTitle != '') {
106 $title = 'Unnamed repository #' . ++$anonymous;
108 $url = (string)$elem['href'];
109 if ($this->isSupported($url)) {
111 $this->arGitUrls[$title][] = $url;
119 $this->error = 'No git:// clone URL found';
123 public function getGitUrls()
125 return $this->arGitUrls;
129 * Remove application names from HTML page titles
131 * @param string $title HTML page title
133 * @return string Cleaned HTML page title
135 protected function cleanPageTitle($title)
137 $title = trim($title);
138 if (substr($title, -9) == '- phorkie') {
139 $title = trim(substr($title, 0, -9));
145 public function isSupported($url)
147 $scheme = parse_url($url, PHP_URL_SCHEME);
148 return $scheme == 'git'
149 || $scheme == 'http' || $scheme == 'https';