diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-09-19 23:55:07 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-09-19 23:55:07 +0200 |
| commit | c85fb1700ce938f28da420af9636e246912a996b (patch) | |
| tree | 4d95a1e62726e3d218335e63c983406e0da1e3cf /src | |
| parent | 8701af280fa5a9c83827cdd0e2ae335d2512a3a7 (diff) | |
| download | phorkie-c85fb1700ce938f28da420af9636e246912a996b.tar.gz phorkie-c85fb1700ce938f28da420af9636e246912a996b.zip | |
implement request #13: simple remote forking works now
Diffstat (limited to 'src')
| -rw-r--r-- | src/phorkie/ForkRemote.php | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/src/phorkie/ForkRemote.php b/src/phorkie/ForkRemote.php index f3639b2..524255a 100644 --- a/src/phorkie/ForkRemote.php +++ b/src/phorkie/ForkRemote.php @@ -5,6 +5,16 @@ 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 = $url; @@ -16,11 +26,11 @@ class ForkRemote switch ($scheme) { case 'git': //clearly a git url - $this->gitUrl = $this->url; + $this->arGitUrls = array(array($this->url)); return true; case 'ssh': - //FIXME: maybe loosen this when we know how to skip the + //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; @@ -36,6 +46,68 @@ class ForkRemote 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; + } + + public function isSupported($url) + { + return parse_url($url, PHP_URL_SCHEME) == 'git'; } } |
