Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.git] / maintenance / storage / storageTypeStats.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Maintenance ExternalStorage
20 */
21
22 require_once __DIR__ . '/../Maintenance.php';
23
24 class StorageTypeStats extends Maintenance {
25 function execute() {
26 $dbr = $this->getDB( DB_REPLICA );
27
28 $endId = $dbr->selectField( 'text', 'MAX(old_id)', false, __METHOD__ );
29 if ( !$endId ) {
30 echo "No text rows!\n";
31 exit( 1 );
32 }
33
34 $binSize = intval( pow( 10, floor( log10( $endId ) ) - 3 ) );
35 if ( $binSize < 100 ) {
36 $binSize = 100;
37 }
38 echo "Using bin size of $binSize\n";
39
40 $stats = [];
41
42 $classSql = <<<SQL
43 IF(old_flags LIKE '%external%',
44 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+/[0-9a-f]{32}$',
45 'CGZ pointer',
46 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+/[0-9]{1,6}$',
47 'DHB pointer',
48 IF(old_text REGEXP '^DB://[[:alnum:]]+/[0-9]+$',
49 'simple pointer',
50 'UNKNOWN pointer'
51 )
52 )
53 ),
54 IF(old_flags LIKE '%object%',
55 TRIM('"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(old_text, ':', 3), ':', -1)),
56 '[none]'
57 )
58 )
59 SQL;
60
61 for ( $rangeStart = 0; $rangeStart < $endId; $rangeStart += $binSize ) {
62 if ( $rangeStart / $binSize % 10 == 0 ) {
63 echo "$rangeStart\r";
64 }
65 $res = $dbr->select(
66 'text',
67 [
68 'old_flags',
69 "$classSql AS class",
70 'COUNT(*) as count',
71 ],
72 [
73 'old_id >= ' . intval( $rangeStart ),
74 'old_id < ' . intval( $rangeStart + $binSize )
75 ],
76 __METHOD__,
77 [ 'GROUP BY' => 'old_flags, class' ]
78 );
79
80 foreach ( $res as $row ) {
81 $flags = $row->old_flags;
82 if ( $flags === '' ) {
83 $flags = '[none]';
84 }
85 $class = $row->class;
86 $count = $row->count;
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 $maintClass = 'StorageTypeStats';
115 require_once RUN_MAINTENANCE_IF_MAIN;