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'
47 //https://gist.github.com/maddy2101/5764473
48 $parts = explode('/', ltrim($arUrl['path'], '/'));
49 if (count($parts == 2)) {
50 //we only want the number, not the user name
53 $path = ltrim($arUrl['path'], '/');
55 $title = $this->getHtmlTitle($url);
56 if ($title === null) {
57 $this->arGitUrls[][] = 'git://gist.github.com/'
60 $this->arGitUrls[$title][] = 'git://gist.github.com/'
69 $this->arGitUrls = array(array($url));
73 //FIXME: maybe loosen this when we know how to skip the
74 //"do you trust this server" question of ssh
75 $this->error = 'ssh:// URLs are not supported';
80 return $this->extractUrlsFromHtml($url, $html);
83 $this->error = 'Unknown URLs scheme: ' . $scheme;
87 protected function extractUrlsFromHtml($url, $html = null)
89 //HTML is not necessarily well-formed, and Gitorious has many problems
91 //$sx = simplexml_load_file($url);
93 libxml_use_internal_errors(true);
95 $sx = simplexml_import_dom(\DOMDocument::loadHTMLFile($url));
97 $sx = simplexml_import_dom(\DOMDocument::loadHTML($html));
100 $elems = $sx->xpath('//*[@rel="vcs-git"]');
101 $titles = $sx->xpath('/html/head/title');
102 $pageTitle = $this->cleanPageTitle((string) reset($titles));
104 $count = $anonymous = 0;
105 foreach ($elems as $elem) {
106 if (!isset($elem['href'])) {
109 $str = (string)$elem;
110 if (isset($elem['title'])) {
111 //<link href=".." rel="vcs-git" title="title" />
112 $title = (string)$elem['title'];
113 } else if ($str != '') {
114 //<a href=".." rel="vcs-git">title</a>
116 } else if ($pageTitle != '') {
119 $title = 'Unnamed repository #' . ++$anonymous;
121 $url = (string)$elem['href'];
122 if ($this->isSupported($url)) {
124 $this->arGitUrls[$title][] = $url;
132 $this->error = 'No git:// clone URL found';
136 public function getGitUrls()
138 return $this->arGitUrls;
142 * Remove application names from HTML page titles
144 * @param string $title HTML page title
146 * @return string Cleaned HTML page title
148 protected function cleanPageTitle($title)
150 $title = trim($title);
151 if (substr($title, -9) == '- phorkie') {
152 $title = trim(substr($title, 0, -9));
158 public function isSupported($url)
160 $scheme = parse_url($url, PHP_URL_SCHEME);
161 return $scheme == 'git'
162 || $scheme == 'http' || $scheme == 'https';
166 * Extract the title from a HTML URL
168 * @param string $url URL to a HTML page
170 * @return string|null NULL on error, title otherwise
172 public function getHtmlTitle($url)
174 libxml_use_internal_errors(true);
175 $doc = \DOMDocument::loadHTMLFile($url);
176 if ($doc === false) {
179 $sx = simplexml_import_dom($doc);
180 $title = (string) $sx->head->title;