MediaWiki master
storageTypeStats.php
Go to the documentation of this file.
1<?php
9
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/../Maintenance.php';
12// @codeCoverageIgnoreEnd
13
15 public function execute() {
16 $dbr = $this->getReplicaDB();
17
18 $endId = $dbr->newSelectQueryBuilder()
19 ->select( 'MAX(old_id)' )
20 ->from( 'text' )
21 ->caller( __METHOD__ )->fetchField();
22 if ( !$endId ) {
23 $this->fatalError( 'No text rows!' );
24 }
25
26 $binSize = intval( 10 ** ( floor( log10( $endId ) ) - 3 ) );
27 if ( $binSize < 100 ) {
28 $binSize = 100;
29 }
30 echo "Using bin size of $binSize\n";
31
32 $stats = [];
33
34 $classSql = <<<SQL
35 IF(old_flags LIKE '%external%',
36 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+/[0-9a-f]{32}$',
37 'CGZ pointer',
38 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+/[0-9]{1,6}$',
39 'DHB pointer',
40 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+$',
41 'simple pointer',
42 'UNKNOWN pointer'
43 )
44 )
45 ),
46 IF(old_flags LIKE '%object%',
47 TRIM('"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(old_text, ':', 3), ':', -1)),
48 '[none]'
49 )
50 )
51SQL;
52
53 for ( $rangeStart = 0; $rangeStart < $endId; $rangeStart += $binSize ) {
54 if ( intdiv( $rangeStart, $binSize ) % 10 === 0 ) {
55 echo "$rangeStart\r";
56 }
57 $res = $dbr->newSelectQueryBuilder()
58 ->select( [ 'old_flags', 'class' => $classSql, 'count' => 'COUNT(*)' ] )
59 ->from( 'text' )
60 ->where( $dbr->expr( 'old_id', '>=', intval( $rangeStart ) ) )
61 ->andWhere( $dbr->expr( 'old_id', '<', intval( $rangeStart + $binSize ) ) )
62 ->groupBy( [ 'old_flags', 'class' ] )
63 ->caller( __METHOD__ )->fetchResultSet();
64
65 foreach ( $res as $row ) {
66 $flags = $row->old_flags;
67 if ( $flags === '' ) {
68 $flags = '[none]';
69 }
70 $class = $row->class;
71 $count = $row->count;
72 // @phan-suppress-next-line PhanImpossibleConditionInLoop False positive
73 if ( !isset( $stats[$flags][$class] ) ) {
74 $stats[$flags][$class] = [
75 'count' => 0,
76 'first' => $rangeStart,
77 'last' => 0
78 ];
79 }
80 $entry =& $stats[$flags][$class];
81 $entry['count'] += $count;
82 $entry['last'] = max( $entry['last'], $rangeStart + $binSize );
83 unset( $entry );
84 }
85 }
86 echo "\n\n";
87
88 $format = "%-29s %-39s %-19s %-29s\n";
89 printf( $format, "Flags", "Class", "Count", "old_id range" );
90 echo str_repeat( '-', 120 ) . "\n";
91 foreach ( $stats as $flags => $flagStats ) {
92 foreach ( $flagStats as $class => $entry ) {
93 printf( $format, $flags, $class, $entry['count'],
94 sprintf( "%-13d - %-13d", $entry['first'], $entry['last'] ) );
95 }
96 }
97 }
98}
99
100// @codeCoverageIgnoreStart
101$maintClass = StorageTypeStats::class;
102require_once RUN_MAINTENANCE_IF_MAIN;
103// @codeCoverageIgnoreEnd
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getReplicaDB(string|false $virtualDomain=false)
execute()
Do the actual work.