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