Genericize paths.
[phpv.git] / get-url.php
blobb0581a54bd65c3b507f9e38cdd63b3ced0061e0e
1 #!/usr/bin/env php
2 <?php
4 function extract_links($url) {
5 $doc = new DOMDocument();
6 @$doc->loadHTMLFile($url);
7 $xpath = new DOMXPath($doc);
8 return $xpath->query('//a');
11 if (!isset($_SERVER['argv'][1])) {
12 echo <<<EOF
13 Usage: php get-url.php version
15 EOF;
16 exit(1);
18 $install_version = $_SERVER['argv'][1];
20 $url_mirror = 'http://us2.php.net';
21 $url_releases = $url_mirror . '/releases/';
22 $url_downloads = $url_mirror . '/downloads.php';
23 $url_qa = 'http://qa.php.net';
25 $link_sets = array(
26 extract_links($url_releases),
27 extract_links($url_downloads),
28 extract_links($url_qa)
30 $index = array();
32 foreach ($link_sets as $links) {
33 foreach ($links as $link) {
34 $url = $link->getAttribute('href');
35 if (!$url) continue;
36 $result = preg_match('#/php-([^-]+?)\.tar\.gz#', $url, $matches);
37 if (!$result) continue;
38 list($full, $version) = $matches;
39 $url = str_replace('/from/a/mirror', '/from/this/mirror', $url); // make download ready
40 if ($url[0] == '/') $url = $url_mirror . $url; // heuristic assumes absolute paths are used
41 $index[$version] = $url;
45 // maybe add qa support
47 if (!isset($index[$install_version])) {
48 fwrite(STDERR, "Could not find $install_version\n");
49 exit(1);
52 echo $index[$install_version];