Avoid division by zero
[lhc/web/wiklou.git] / maintenance / storage / orphanStats.php
1 <?php
2
3 /**
4 * Show some statistics on the blob_orphans table, created with trackBlobs.php
5 */
6 require_once( dirname(__FILE__).'/../commandLine.inc' );
7
8 $stats = new OrphanStats;
9 $stats->execute();
10
11 class OrphanStats {
12 function getDB( $cluster ) {
13 $lb = wfGetLBFactory()->getExternalLB( $cluster );
14 return $lb->getConnection( DB_SLAVE );
15 }
16
17 function execute() {
18 $extDBs = array();
19 $dbr = wfGetDB( DB_SLAVE );
20 $res = $dbr->select( 'blob_orphans', '*', false, __METHOD__ );
21
22 $num = 0;
23 $totalSize = 0;
24 $hashes = array();
25 $maxSize = 0;
26
27 foreach ( $res as $boRow ) {
28 $extDB = $this->getDB( $boRow->bo_cluster );
29 $blobRow = $extDB->selectRow( 'blobs', '*', array( 'blob_id' => $boRow->bo_blob_id ), __METHOD__ );
30
31 $num++;
32 $size = strlen( $blobRow->blob_text );
33 $totalSize += $size;
34 $hashes[ sha1( $blobRow->blob_text ) ] = true;
35 $maxSize = max( $size, $maxSize );
36 }
37 unset( $res );
38
39 echo "Number of orphans: $num\n";
40 if ( $num > 0 ) {
41 echo "Average size: " . round( $totalSize / $num, 0 ) . " bytes\n" .
42 "Max size: $maxSize\n" .
43 "Number of unique texts: " . count( $hashes ) . "\n";
44 }
45 }
46 }