File: notes_stats.php

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (439 lines) | stat: -rw-r--r-- 10,413 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/*
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2023 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | https://www.php.net/license/3_01.txt.                                |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net, so we can mail you a copy immediately.              |
  +----------------------------------------------------------------------+
  | Authors:    Mehdi Achour <didou@php.net> (Original Author)           |
  |             Vincent Gevers <vincent@php.net>                         |          
  +----------------------------------------------------------------------+

 $Id$
*/

/*
 * Warning: This script uses a lot of memory 
 *
 * Usage:
 * $ php notes_stats.php [mbox-file] > notes.php
 */
 
/*
 * TODO:
 * - Caching the articles (SQLite?)
 * - Speed improvements
 * - Nicer layout
 */

// minimum amount actions
$minact = 100;
// after how many secs should the list be chopped
$after = 182.5*24*60*60; // half year

$inputs = array(); // pair subjects w/ dates from multiple sources

$time_start = getmicrotime();

if (isset($argv[1]) && file_exists($argv[1])) { // from file

    $lines = file($argv[1]);
    $count = count($lines);
    
    for ($i = 0; $i < $count; ++$i) {
        list($time, $subj) = explode(' ', $lines[$i], 2);
        
        $inputs[] = array($time, substr($subj, 12));
    }
    
} elseif (isset($argv[1])) {

    echo "File doesn't exist!";
    exit(1);
 
} else { // from nntp

 $s = nntp_connect("news.php.net")
   or die("failed to connect to news server");

 $res = nntp_cmd($s,"GROUP php.notes",211)
   or die("failed to get infos on news group");

 $new = explode(" ", $res);
 $first = 1;
 $last =  $new[0];

 //$first = 69900;
 //$last =  70000;

 $res = nntp_cmd($s,"XOVER $first-$last", 224)
     or die("failed to XOVER the new items");

 for ($i = $first; $i < $last; $i++) {
 
     $line = fgets($s, 4096);
     list($n,$subj,$author,$odate,$messageid,$references,$bytes,$lines,$extra)= explode("\t", $line, 9);
     $inputs[] = array($odate, $subj);
 }

}

$files = $team = $tmp = array();

$in_old = false;

/*
 * What should be matched:
 * note ID deleted from SECTION by EDITOR
 * note ID rejected from SECTION by EDITOR
 * note ID modified in SECTION by EDITOR
 * note ID moved from SECTION to SECTION by EDITOR (not matched yet)
 */

$reg = '/^note (\d*) (.*) (?:from|in) (\S*) by (\w*)/';

reset($inputs);

while (list( , $inp) = each($inputs)) {
    list($odate, $subj) = $inp;

    if (preg_match($reg, $subj, $d)) {
        if ($d[2] == 'rejected and deleted')
            $d[2] = 'rejected';
        if ($d[2] == 'approved')
            continue;
         
        if(calc_time($odate)) {
            // 'new' before $after
            @$team['n'][$d[4]]['total']++;
            @$team['n'][$d[4]][$d[2]]++; 
            @$tmp['n'][$d[4]]++;
            @$files['n'][$d[3]]++;
        } else {
            // 'old' after $after
            @$team['o'][$d[4]][$d[2]]++; 
            @$tmp['o'][$d[4]]++;
            @$team['o'][$d[4]]['total']++; 
            @$files['o'][$d[3]]++;
        }
        
        // the normal arrays
        @$team[$d[4]]['total']++;
        @$team[$d[4]][$d[2]]++; 
        @$tmp[$d[4]]++;
        @$files[$d[3]]++;
 
    } // end if(preg_match
} // end while(each


ksort($team);
arsort($files);
arsort($tmp);
arsort($tmp['n']);
arsort($tmp['o']);


echo '<html>
<head>
<title>Note Statistics for '.date('j F Y').'</title>
</head>
<body>
';

echo '
<b>' . array_sum($files) . '</b> subjects parsed<br /><br />
';

?>
<table border='0' cellspacing="10"><tr valign="top"><td valign="top">
<table border='1'>
    <tr>
        <td colspan="5" align="center">Editors Stats with more than <?php echo $minact; ?> actions</td>
    </tr>
    <tr>
        <td>user</td>
        <td>deleted</td>
        <td>rejected</td>
        <td>modified</td>
        <td>total</td>
    </tr>

<?php

$bg = '#EBEBEB';
foreach ($tmp as $user => $total) {
    if($user == 'o' or $user =='n')
       continue;

    if($total >= $minact) { 
        echo "<tr bgcolor=\"";
        $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB';
        echo "$bg\">\n\t<td>$user</td>\n\t<td>";
        echo isset($team[$user]['deleted']) ? $team[$user]['deleted'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team[$user]['rejected']) ? $team[$user]['rejected'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team[$user]['modified']) ? $team[$user]['modified'] : '0';
        echo "</td>\n\t<td>";
        echo $total;
        echo "</td>\n</tr>\n";
    }
    
}

?>
</table>

</td><td valign="top">
Last half year (with more than <?php echo $minact; ?> actions counted)
<table border='1'>
    <tr>
        <td colspan="5" align="center">Recent Editors stats</td>
    </tr>
    <tr>
        <td>user</td>
        <td>deleted</td>
        <td>rejected</td>
        <td>modified</td>
        <td>total</td>
    </tr>

<?php

$bg = '#EBEBEB';
foreach ($tmp['n'] as $user => $total) {

    if($total >= $minact) {    
        echo "<tr bgcolor=\"";
        $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB';
        echo "$bg\">\n\t<td>$user</td>\n\t<td>";
        echo isset($team['n'][$user]['deleted']) ? $team['n'][$user]['deleted'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team['n'][$user]['rejected']) ? $team['n'][$user]['rejected'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team['n'][$user]['modified']) ? $team['n'][$user]['modified'] : '0';
        echo "</td>\n\t<td>";
        echo $total;
        echo "</td>\n</tr>\n";
    }

}


?>
</table>

<br />
Before the last half year (with more than <?php echo $minact; ?> actions counted)
<table border='1'>
    <tr>
        <td colspan="5" align="center">Older Editors Stats</td>
    </tr>
    <tr>
        <td>user</td>
        <td>deleted</td>
        <td>rejected</td>
        <td>modified</td>
        <td>total</td>
    </tr>

<?php

$bg = '#EBEBEB';
foreach ($tmp['o'] as $user => $total) {

    if($total >= $minact) {
        echo "<tr bgcolor=\"";
        $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB';
        echo "$bg\">\n\t<td>$user</td>\n\t<td>";
        echo isset($team['o'][$user]['deleted']) ? $team['o'][$user]['deleted'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team['o'][$user]['rejected']) ? $team['o'][$user]['rejected'] : '0';
        echo "</td>\n\t<td>";
        echo isset($team['o'][$user]['modified']) ? $team['o'][$user]['modified'] : '0';
        echo "</td>\n\t<td>";
        echo $total;
        echo "</td>\n</tr>\n";
    }

}


?>
</table>
</td></tr></table>


<br />

<table border="0" cellspacing="10"><tr valign="top"><td valign="top">
<table border='1'>
    <tr>
        <td colspan="5" align="center">Total Editors Stats</td>
    </tr>
    <tr>
        <td>user</td>
        <td>deleted</td>
        <td>rejected</td>
        <td>modified</td>
        <td>total</td>
    </tr>

<?php

$bg = '#EBEBEB';

foreach ($team as $user => $actions) {
    if($user == 'o' or $user =='n' or $user == '')
       continue;
       
    echo "<tr bgcolor=\"";
    $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB';
    echo "$bg\">\n\t<td>$user</td>\n\t<td>";
    echo isset($actions['deleted']) ? $actions['deleted'] : '0';
    echo "</td>\n\t<td>";
    echo isset($actions['rejected']) ? $actions['rejected'] : '0';
    echo "</td>\n\t<td>";
    echo isset($actions['modified']) ? $actions['modified'] : '0';
    echo "</td>\n\t<td>";
    echo $actions['total'];
    echo "</td>\n</tr>\n";

}


?>
</table>

</td><td valign="top">
<table border='1'>
    <tr>
        <td colspan="3" align="center">Editors top 15</td>
    </tr>
    <tr>
        <td>rank</td>
        <td>user</td>
        <td>total</td>
    </tr>
<?php
$i = 0;
    foreach($tmp as $k => $v) {
    if($k == 'o' or $k =='n')
       continue;
       
        $i++;
        echo "<tr bgcolor=\"";
        $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB'; 
        echo "$bg\">\n\t<td>$i</td>\n\t<td>$k</td>\n\t<td>$v</td>\n</tr>\n";
    
    if ($i == 15)
        break;
}       

?>
</table>

<br />
<table border='1'>
    <tr>
        <td colspan="3" align="center">Manual pages most active top 20</td>
    </tr>
    <tr>
        <td>rank</td>
        <td>page</td>
        <td>total</td>
    </tr>
<?php
$i = 0;
    foreach($files as $k => $v) {
    if($k == 'o' or $k =='n')
       continue;
       
        $i++;
        echo "<tr bgcolor=\"";
       $bg = ($bg == '#EBEBEB') ? '#BEBEBE' : '#EBEBEB'; 
        echo "$bg\">\n\t<td>$i</td>\n\t<td>$k</td>\n\t<td>$v</td>\n</tr>\n";
    
    if ($i == 20)
        break;
}       

?>
</table>

</td></tr></table>

<?php
 $time_end = getmicrotime();
 $time = $time_end - $time_start;
 
 echo "All done in $time seconds\n";
 ?>
</body>
</html>

<?php

function nntp_connect($server,$port=119) {
  $s = fsockopen($server,$port,$errno,$errstr,30);

  if (!$s) {
    echo "<!-- error connecting to nntp server: $errstr -->\n";
    return false;
  }
  $hello = fgets($s, 1024);
  if (substr($hello,0,4) != "200 ") {
    echo "<!-- unexpected greeting: $hello -->\n";
    return false;
  }
  #echo "<!-- $hello -->\n";
  return $s;
}

function nntp_cmd($conn,$command,$expected) {
  if (strlen($command) > 510) die("command too long: $command");
  fputs($conn, "$command\r\n");
  $res = fgets($conn, 1024);
  list($code,$extra) = explode(" ", $res, 2);
  return $code == $expected ? $extra : false;
}

function getmicrotime() { 
    list($usec, $sec) = explode(" ", microtime()); 
    return ((float)$usec + (float)$sec); 
} 

function calc_time ($before) {
global $after;

// simple caching
if(@$in_old == true) 
   return false;

if (!is_numeric($before)) {
    $before = strtotime($before);
}

$afterall =  $before - (time() - $after);

if($afterall > 0) {
    // more then $after
    $in_old = false;    
    return true;
} else {
    // older then $after
    $in_old = true;
    return false;
}

}


?>