fix gist.github.com clone urls
[phorkie.git] / src / phorkie / HtmlParser.php
1 <?php
2 namespace phorkie;
3
4 class HtmlParser
5 {
6     /**
7      * Contains error message when parse() failed
8      */
9     public $error;
10
11     /**
12      * Array with keys (URL title) and values (arrays of urls)
13      * Only supported URLs are included.
14      *
15      * @var array
16      */
17     protected $arGitUrls;
18
19
20
21     /**
22      * Extract git URLs from the given URL, eventually fetching
23      * HTML and extracting URLs from there.
24      *
25      * Sets $error and $arGitUrls class variables
26      *
27      * @param string $url  Git or HTTP URL
28      * @param string $html HTML content of $url
29      *
30      * @return boolean True when all went well, false in case of an error
31      * @uses   $error
32      * @uses   $arGitUrls
33      */
34     public function extractGitUrls($url, $html = null)
35     {
36         if ($url == '') {
37             $this->error = 'Empty fork URL';
38             return false;
39         }
40
41         $arUrl  = parse_url($url);
42         $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
43
44         if ($scheme == 'https' && isset($arUrl['host'])
45             && $arUrl['host'] == 'gist.github.com'
46         ) {
47             //https://gist.github.com/cweiske/2400389
48             // clone URL: https://gist.github.com/2400389.git
49             $parts = explode('/', ltrim($arUrl['path'], '/'));
50             if (count($parts == 2)) {
51                 //we only want the number, not the user name
52                 $path = $parts[1];
53             } else {
54                 $path = ltrim($arUrl['path'], '/');
55             }
56             $title = $this->getHtmlTitle($url);
57             if ($title === null) {
58                 $this->arGitUrls[][] = 'https://gist.github.com/'
59                     . $path . '.git';
60             } else {
61                 $this->arGitUrls[$title][] = 'https://gist.github.com/'
62                     . $path . '.git';
63             }
64             return true;
65         }
66
67         switch ($scheme) {
68         case 'git':
69             //clearly a git url
70             $this->arGitUrls = array(array($url));
71             return true;
72
73         case 'ssh':
74             //FIXME: maybe loosen this when we know how to skip the
75             //"do you trust this server" question of ssh
76             $this->error = 'ssh:// URLs are not supported';
77             return false;
78
79         case 'http':
80         case 'https':
81             return $this->extractUrlsFromHtml($url, $html);
82         }
83
84         $this->error = 'Unknown URLs scheme: ' . $scheme;
85         return false;
86     }
87
88     protected function extractUrlsFromHtml($url, $html = null)
89     {
90         //HTML is not necessarily well-formed, and Gitorious has many problems
91         // in this regard
92         //$sx = simplexml_load_file($url);
93
94         libxml_use_internal_errors(true);
95         if ($html === null) {
96             $sx = simplexml_import_dom(\DOMDocument::loadHTMLFile($url));
97         } else {
98             $sx = simplexml_import_dom(\DOMDocument::loadHTML($html));
99         }
100
101         $elems = $sx->xpath('//*[@rel="vcs-git"]');
102         $titles = $sx->xpath('/html/head/title');
103         $pageTitle = $this->cleanPageTitle((string) reset($titles));
104
105         $count = $anonymous = 0;
106         foreach ($elems as $elem) {
107             if (!isset($elem['href'])) {
108                 continue;
109             }
110             $str = (string)$elem;
111             if (isset($elem['title'])) {
112                 //<link href=".." rel="vcs-git" title="title" />
113                 $title = (string)$elem['title'];
114             } else if ($str != '') {
115                 //<a href=".." rel="vcs-git">title</a>
116                 $title = $str;
117             } else if ($pageTitle != '') {
118                 $title = $pageTitle;
119             } else {
120                 $title = 'Unnamed repository #' . ++$anonymous;
121             }
122             $url = (string)$elem['href'];
123             if ($this->isSupported($url)) {
124                 ++$count;
125                 $this->arGitUrls[$title][] = $url;
126             }
127         }
128
129         if ($count > 0) {
130             return true;
131         }
132
133         $this->error = 'No git:// clone URL found';
134         return false;
135     }
136
137     public function getGitUrls()
138     {
139         return $this->arGitUrls;
140     }
141
142     /**
143      * Remove application names from HTML page titles
144      *
145      * @param string $title HTML page title
146      *
147      * @return string Cleaned HTML page title
148      */
149     protected function cleanPageTitle($title)
150     {
151         $title = trim($title);
152         if (substr($title, -9) == '- phorkie') {
153             $title = trim(substr($title, 0, -9));
154         }
155
156         return $title;
157     }
158
159     public function isSupported($url)
160     {
161         $scheme = parse_url($url, PHP_URL_SCHEME);
162         return $scheme == 'git'
163             || $scheme == 'http' || $scheme == 'https';
164     }
165
166     /**
167      * Extract the title from a HTML URL
168      *
169      * @param string $url URL to a HTML page
170      *
171      * @return string|null NULL on error, title otherwise
172      */
173     public function getHtmlTitle($url)
174     {
175         libxml_use_internal_errors(true);
176         $doc = \DOMDocument::loadHTMLFile($url);
177         if ($doc === false) {
178             return null;
179         }
180         $sx = simplexml_import_dom($doc);
181         $title = (string) $sx->head->title;
182         if ($title == '') {
183             return null;
184         }
185         return $title;
186     }
187 }
188 ?>