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