allow remote forking of HTTP and HTTPS URLs
[phorkie.git] / src / phorkie / ForkRemote.php
1 <?php
2 namespace phorkie;
3
4 class ForkRemote
5 {
6     /**
7      * Contains error message when parse() failed
8      */
9     public $error;
10
11     protected $url;
12
13     /**
14      * Array with keys (URL title) and values (arrays of urls)
15      * Only supported URLs are included.
16      *
17      * @var array
18      */
19     protected $arGitUrls;
20
21
22
23     public function __construct($url)
24     {
25         $this->url = trim($url);
26     }
27
28     public function parse()
29     {
30         if ($this->url == '') {
31             $this->error = 'Empty fork URL';
32             return false;
33         }
34
35         $arUrl  = parse_url($this->url);
36         $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
37
38         if ($scheme == 'https' && isset($arUrl['host'])
39             && $arUrl['host'] == 'gist.github.com'
40         ) {
41             $this->arGitUrls[][] = 'git://gist.github.com/'
42                 . ltrim($arUrl['path'], '/') . '.git';
43             return true;
44         }
45
46         switch ($scheme) {
47         case 'git':
48             //clearly a git url
49             $this->arGitUrls = array(array($this->url));
50             return true;
51
52         case 'ssh':
53             //FIXME: maybe loosen this when we know how to skip the
54             //"do you trust this server" question of ssh
55             $this->error = 'ssh:// URLs are not supported';
56             return false;
57
58         case 'http':
59         case 'https':
60             return $this->extractUrlsFromHtml($this->url);
61         }
62
63         $this->error = 'Unknown URLs scheme: ' . $scheme;
64         return false;
65     }
66
67     protected function extractUrlsFromHtml($url)
68     {
69         //HTML is not necessarily well-formed, and Gitorious has many problems
70         // in this regard
71         //$sx = simplexml_load_file($url);
72         libxml_use_internal_errors(true);
73         $sx = simplexml_import_dom(\DomDocument::loadHtmlFile($url));
74         $elems = $sx->xpath('//*[@rel="vcs-git"]');
75
76         $count = $anonymous = 0;
77         foreach ($elems as $elem) {
78             if (!isset($elem['href'])) {
79                 continue;
80             }
81             $str = (string)$elem;
82             if (isset($elem['title'])) {
83                 //<link href=".." rel="vcs-git" title="title" />
84                 $title = (string)$elem['title'];
85             } else if ($str != '') {
86                 //<a href=".." rel="vcs-git">title</a>
87                 $title = $str;
88             } else {
89                 $title = 'Unnamed repository #' . ++$anonymous;
90             }
91             $url = (string)$elem['href'];
92             if ($this->isSupported($url)) {
93                 ++$count;
94                 $this->arGitUrls[$title][] = $url;
95             }
96         }
97
98         if ($count > 0) {
99             return true;
100         }
101
102         $this->error = 'No git:// clone URL found';
103         return false;
104     }
105
106     /**
107      * Iterate through all git urls and return one if there is only
108      * one supported one.
109      *
110      * @return mixed Boolean false or string
111      */
112     public function getUniqueGitUrl()
113     {
114         $nFound = 0;
115         foreach ($this->arGitUrls as $title => $arUrls) {
116             foreach ($arUrls as $url) {
117                 $nFound++;
118                 $uniqueUrl = $url;
119             }
120         }
121
122         if ($nFound == 1) {
123             return $uniqueUrl;
124         }
125         return false;
126     }
127
128     public function getGitUrls()
129     {
130         return $this->arGitUrls;
131     }
132
133     /**
134      * Get the URL from which the git URL was derived, often
135      * the HTTP URL.
136      *
137      * @return string
138      */
139     public function getUrl()
140     {
141         return $this->url;
142     }
143
144     public function setUrl($url)
145     {
146         $this->url = $url;
147     }
148
149     public function isSupported($url)
150     {
151         $scheme = parse_url($url, PHP_URL_SCHEME);
152         return $scheme == 'git'
153             || $scheme == 'http' || $scheme == 'https';
154     }
155 }
156
157 ?>