xref: /web-master/scripts/update-user-notes (revision fc7ab27c)
1#!/usr/local/bin/php -q
2<?php /* vim: set noet ts=2 sw=2 ft=php: : */
3
4// Build complete file with all notes
5define("BUILD_COMPLETE_FILE", TRUE);
6
7if ($argc != 2 && $argc != 3) {
8    die("usage: $argv[0] directory [since]");
9}
10
11$token = getenv("TOKEN");
12if (!$token) {
13    die("you have to set the TOKEN environment variable");
14}
15$token = rawurlencode($token);
16
17$root = $argv[1];
18if (substr($root,-1) != '/') {
19    $root = "$root/";
20}
21$since = ($argc == 3 ? $argv[2] : "");
22
23$SSL_fopen = false;
24if(in_array('https', stream_get_wrappers())) {
25    $SSL_fopen = true;
26}
27
28/* get user notes */
29if($SSL_fopen) {
30    $ctx = stream_context_create([
31        'ssl' => [
32            'ciphers' => 'DEFAULT:!DH',
33        ],
34    ]);
35    $fp = @fopen(
36        "https://master.php.net/fetch/user-notes.php?token=$token".($since?"&since=$since":""),
37        "r",
38        false,
39        $ctx
40    );
41} else {
42    $url = escapeshellarg("https://master.php.net/fetch/user-notes.php?token=$token".($since?"&since=$since":""));
43    $fp = popen("curl -s $url",'r');
44}
45
46if (!$fp) {
47    exit(1);
48}
49
50$count = 0;
51
52if (BUILD_COMPLETE_FILE) {
53    $bz = popen("bzip2 -9 -c > $root/all.bz2", "w");
54}
55
56while (!feof($fp)) {
57    $line = chop(fgets($fp,8096));
58
59    if (!strstr($line,"|")) {
60        continue; # skip invalid lines
61    }
62
63    list($id,$sect,$rate,$ts,$user,$note,$up,$down) = explode("|",$line);
64
65    $hash = substr(md5($sect),0,16);
66    @mkdir($root.substr($hash,0,2),0755);
67
68    $file = $root.substr($hash,0,2)."/$hash";
69    if ($since && !$restarted[$file]++) {
70        unlink($file);
71    }
72
73    $mtime = @filemtime($file);
74    if (!($nf = @fopen($file,"a"))) {
75        echo "unable to open $notes file $hash\n";
76        continue;
77    }
78    if (strlen($note) == 0) {
79        echo "note id $note is broken";
80    } else {
81        $note = gzuncompress(base64_decode($note)) or die ("$id failed\n");
82        fputs($nf, "$id|$sect|$rate|$ts|$user|" . base64_encode($note) . "|$up|$down\n");
83        if (BUILD_COMPLETE_FILE) {
84            fputs($bz, "$id|$sect|$rate|$ts|$user|" . base64_encode($note) . "|$up|$down\n");
85        }
86        fclose($nf);
87    }
88
89    touch($file, $mtime < $ts ? $ts : $mtime);
90    $count++;
91}
92
93if (BUILD_COMPLETE_FILE) {
94    pclose($bz);
95}
96
97exit($count ? 0 : 1);
98