simple cache for rendered files
[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         //FIXME: handle network error
101
102         $elems = $sx->xpath('//*[@rel="vcs-git"]');
103         $titles = $sx->xpath('/html/head/title');
104         $pageTitle = $this->cleanPageTitle((string) reset($titles));
105
106         $count = $anonymous = 0;
107         foreach ($elems as $elem) {
108             if (!isset($elem['href'])) {
109                 continue;
110             }
111             $str = (string)$elem;
112             if (isset($elem['title'])) {
113                 //<link href=".." rel="vcs-git" title="title" />
114                 $title = (string)$elem['title'];
115             } else if ($str != '') {
116                 //<a href=".." rel="vcs-git">title</a>
117                 $title = $str;
118             } else if ($pageTitle != '') {
119                 $title = $pageTitle;
120             } else {
121                 $title = 'Unnamed repository #' . ++$anonymous;
122             }
123             $url = (string)$elem['href'];
124             if ($this->isSupported($url)) {
125                 ++$count;
126                 $this->arGitUrls[$title][] = $url;
127             }
128         }
129
130         if ($count > 0) {
131             return true;
132         }
133
134         $this->error = 'No git:// clone URL found';
135         return false;
136     }
137
138     public function getGitUrls()
139     {
140         return $this->arGitUrls;
141     }
142
143     /**
144      * Remove application names from HTML page titles
145      *
146      * @param string $title HTML page title
147      *
148      * @return string Cleaned HTML page title
149      */
150     protected function cleanPageTitle($title)
151     {
152         $title = trim($title);
153         if (substr($title, -9) == '- phorkie') {
154             $title = trim(substr($title, 0, -9));
155         }
156
157         return $title;
158     }
159
160     public function isSupported($url)
161     {
162         $scheme = parse_url($url, PHP_URL_SCHEME);
163         return $scheme == 'git'
164             || $scheme == 'http' || $scheme == 'https';
165     }
166
167     /**
168      * Extract the title from a HTML URL
169      *
170      * @param string $url URL to a HTML page
171      *
172      * @return string|null NULL on error, title otherwise
173      */
174     public function getHtmlTitle($url)
175     {
176         libxml_use_internal_errors(true);
177         $doc = \DOMDocument::loadHTMLFile($url);
178         if ($doc === false) {
179             return null;
180         }
181         $sx = simplexml_import_dom($doc);
182         $title = (string) $sx->head->title;
183         if ($title == '') {
184             return null;
185         }
186         return $title;
187     }
188 }
189 ?>