MediaWiki master
orphanStats.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/../Maintenance.php';
28// @codeCoverageIgnoreEnd
29
36class OrphanStats extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 "Show some statistics on the blob_orphans table, created with trackBlobs.php" );
41 }
42
43 protected function getExternalDB( $db, $cluster ) {
44 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
45 $lb = $lbFactory->getExternalLB( $cluster );
46
47 return $lb->getMaintenanceConnectionRef( $db );
48 }
49
50 public function execute() {
51 if ( !$this->getDB( DB_PRIMARY )->tableExists( 'blob_orphans', __METHOD__ ) ) {
52 $this->fatalError( "blob_orphans doesn't seem to exist, need to run trackBlobs.php first" );
53 }
54 $dbr = $this->getReplicaDB();
55 $res = $dbr->newSelectQueryBuilder()
56 ->select( '*' )
57 ->from( 'blob_orphans' )
58 ->caller( __METHOD__ )->fetchResultSet();
59
60 $num = 0;
61 $totalSize = 0;
62 $hashes = [];
63 $maxSize = 0;
64
65 foreach ( $res as $row ) {
66 $extDB = $this->getExternalDB( DB_REPLICA, $row->bo_cluster );
67 $blobRow = $extDB->newSelectQueryBuilder()
68 ->select( '*' )
69 ->from( 'blobs' )
70 ->where( [ 'blob_id' => $row->bo_blob_id ] )
71 ->caller( __METHOD__ )->fetchRow();
72
73 $num++;
74 $size = strlen( $blobRow->blob_text );
75 $totalSize += $size;
76 $hashes[sha1( $blobRow->blob_text )] = true;
77 $maxSize = max( $size, $maxSize );
78 }
79 unset( $res );
80
81 $this->output( "Number of orphans: $num\n" );
82 if ( $num > 0 ) {
83 $this->output( "Average size: " . round( $totalSize / $num, 0 ) . " bytes\n" .
84 "Max size: $maxSize\n" .
85 "Number of unique texts: " . count( $hashes ) . "\n" );
86 }
87 }
88}
89
90// @codeCoverageIgnoreStart
91$maintClass = OrphanStats::class;
92require_once RUN_MAINTENANCE_IF_MAIN;
93// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script that shows some statistics on the blob_orphans table, created with trackBlobs....
getExternalDB( $db, $cluster)
__construct()
Default constructor.
execute()
Do the actual work.
$maintClass
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28