+ //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;
+ }
+
+ public function isSupported($url)
+ {
+ return parse_url($url, PHP_URL_SCHEME) == 'git';