File: fetch-chms.php

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (118 lines) | stat: -rw-r--r-- 3,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
define("DEBUG", 0);
$BUILDDIR = "https://dl.dropboxusercontent.com/u/15537366/chm/";
$TMPDIR = sys_get_temp_dir();
$CHMDIR = "/local/mirrors/phpweb/distributions/manual";

$chminfo = "$BUILDDIR/build.log";
fetch($chminfo, tempnam(sys_get_temp_dir(), "chm"));

$chms = file($chminfo, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
if (!$chms) {
    err("No chm info\n");
    return 1;
}

foreach($chms as $line) {
    list($filename, $hash, $date) = explode("\t", $line);
    $tmpfilename  = "$TMPDIR/$filename.new";
    $realfilename = "$CHMDIR/$filename";
    debug("\n\nWorking on $filename ($tmpfilename => $realfilename)\n");

    $dlsize = fetch("$BUILDDIR/$filename", $tmpfilename);
    if (!$dlsize) {
        $err = error_get_last();
        err($err["message"], $filename);
        continue;
    }
    debug("Fetched $dlsize");

    $realhash = md5_file($tmpfilename);
    if ($realhash != $hash) {
        err("\nMD5 Failed for $filename ($realhash != $hash)\n");
        continue;
    }
    debug("\nMD5 OK");

    debug("Renaming $tmpfilename to $realfilename");
    rename($tmpfilename, $realfilename);
    debug("All done with $filename");
}

$errors = err();
if ($errors) {
    mail("phpdoc@lists.php.net", "Errors copying CHM files", var_export($errors, true), "From: phpdoc@lists.php.net\r\n", "-fnoreply@php.net");
    return 2;
}


function err($error = null) {
    static $errors = array();
    if ($error) {
        $errors[] = $error;
    }
    return $errors;
}
function debug($msg) {
    if (DEBUG) {
        echo $msg, "\n";
    }
}
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
    static $filesize = null;

    switch($notification_code) {
    case STREAM_NOTIFY_RESOLVE:
    case STREAM_NOTIFY_AUTH_REQUIRED:
    case STREAM_NOTIFY_COMPLETED:
    case STREAM_NOTIFY_FAILURE:
    case STREAM_NOTIFY_AUTH_RESULT:
        /* Ignore */
        break;

    case STREAM_NOTIFY_REDIRECTED:
        debug("Being redirected to: ". $message);
        break;

    case STREAM_NOTIFY_CONNECT:
        debug("Connected...");
        break;

    case STREAM_NOTIFY_FILE_SIZE_IS:
        $filesize = $bytes_max;
        debug("Filesize: ". $filesize);
        break;

    case STREAM_NOTIFY_MIME_TYPE_IS:
        debug("Mime-type: ". $message);
        break;

    case STREAM_NOTIFY_PROGRESS:
        if ($bytes_transferred > 0) {
            if (DEBUG) {
                if (!isset($filesize)) {
                    printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024);
                } else {
                    $length = (int)(($bytes_transferred/$filesize)*100);
                    printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024);
                }
            }
        }
        break;
    }
}

function fetch($filename, $outputname) {
    debug("Fetching $filename");
    $ctx = stream_context_create(null, array("notification" => "stream_notification_callback"));

    $fp = fopen($filename, "r", false, $ctx);
    if (!is_resource($fp)) {
        err("Fetch failed");
        return 0;
    }
    return file_put_contents($outputname, $fp);
}