This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathmigrateUaID.php
More file actions
175 lines (146 loc) · 5.29 KB
/
migrateUaID.php
File metadata and controls
175 lines (146 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Migrate user agent IDs.
*
* @author Timo Tijhof
* @since 1.0.0
* @package TestSwarm
*/
define( 'SWARM_ENTRY', 'SCRIPT' );
require_once __DIR__ . '/../inc/init.php';
class MigrateUaIDScript extends MaintenanceScript {
protected function init() {
$this->setDescription( 'Migrate one uaID to another.' );
$this->registerOption( 'find-unknown', 'boolean', 'Search for uaID references that are not (anymore) defined in localSettings.' );
$this->registerOption( 'replace-unknown', 'boolean', 'Search and replace unknown uaID references (interactive).' );
$this->registerOption( 'from', 'value', 'Replace this uaID with the "to" value.' );
$this->registerOption( 'to', 'value', 'Replace the "from" uaID with this one.' );
$this->registerOption( 'batch-size', 'value', 'How many rows to analyize at one time (default: 500).' );
}
protected function execute() {
$browserIndex = BrowserInfo::getBrowserIndex();
$batchSize = $this->getOption( 'batch-size' );
if ( !$batchSize ) {
$batchSize = 500;
}
$findUnknown = $this->getOption( 'find-unknown' );
$replaceUnknown = $this->getOption( 'replace-unknown' );
if ( $findUnknown || $replaceUnknown ) {
$found1 = $this->findUnknown( 'clients', $batchSize, $replaceUnknown );
$found2 = $this->findUnknown( 'run_useragent', $batchSize, $replaceUnknown );
if ( !$replaceUnknown ) {
$found = array_values( array_unique( array_merge( $found1, $found2 ) ) );
natsort( $found );
if ( $found ) {
$this->out( 'Found ' . count( $found ) . " unknown uaIDs: \n* " . implode( "\n* ", $found ) );
} else {
$this->out( 'No unknown uaIDs found.' );
}
}
return;
}
$fromId = $this->getOption( 'from' );
$toId = $this->getOption( 'to' );
if ( !$fromId && !$toId ) {
$this->displayHelp();
return;
}
if ( !$fromId || !$toId ) {
$this->error( 'Option "from" and "to" are both required.' );
}
if ( !isset( $browserIndex->$toId ) ) {
$this->error( 'Define uaID "' . $toId . '" in the userAgents index before the migration.' );
}
$this->runBatch( $fromId, $toId, $batchSize );
}
protected function runBatch( $fromId, $toId, $batchSize ) {
$c1 = $this->runBatchTable( 'clients', $fromId, $toId, $batchSize );
$c2 = $this->runBatchTable( 'run_useragent', $fromId, $toId, $batchSize );
$this->out( "Migration '$fromId' -> '$toId' complete [$c1 clients rows, $c2 run_useragent rows]" );
}
protected function runBatchTable( $table, $fromId, $toId, $batchSize ) {
$db = $this->getContext()->getDB();
$start = $db->getOne( "SELECT MIN(id) FROM $table;" );
$end = $db->getOne( "SELECT MAX(id) FROM $table;" );
if ( !$start || !$end ) {
$this->error( "The $table table appears empty." );
}
$count = 0;
$end += $batchSize - 1; // Do remaining chunk
$blockStart = $start;
$blockEnd = min( $start + $batchSize - 1, $end );
while ( $blockEnd <= $end ) {
$this->out( "...migrating $table.id $blockStart to $blockEnd" );
$db->query( str_queryf(
"UPDATE $table
SET useragent_id = %s
WHERE id BETWEEN $blockStart AND $blockEnd
AND useragent_id = %s;
",
$toId,
$fromId
) );
$blockStart += $batchSize;
$blockEnd += $batchSize;
// Optimise for continuing a batch, don't wait if this chunk didn't
// affect any rows. This way we fast-forward to the next affectable chunk.
if ( $db->getAffectedRows() ) {
$count += $db->getAffectedRows();
usleep( 100 * 1000 ); // Wait 100ms
}
}
return $count;
}
protected function findUnknown( $table, $batchSize, $interactive ) {
$db = $this->getContext()->getDB();
$browserIndex = BrowserInfo::getBrowserIndex();
$uaIDs = array_keys( (array) $browserIndex );
$start = $db->getOne( "SELECT MIN(id) FROM $table;" );
$end = $db->getOne( "SELECT MAX(id) FROM $table;" );
if ( !$start || !$end ) {
$this->error( "The $table table appears empty." );
}
$found = array();
$end += $batchSize - 1; // Do remaining chunk
$blockStart = $start;
$blockEnd = min( $start + $batchSize - 1, $end );
while ( $blockEnd <= $end ) {
$this->out( "...scanning $table.id $blockStart to $blockEnd" );
$rows = $db->getRows( str_queryf(
"SELECT DISTINCT(useragent_id) FROM $table
WHERE id BETWEEN $blockStart AND $blockEnd
AND useragent_id NOT IN %l;
",
$uaIDs
) );
$blockStart += $batchSize;
$blockEnd += $batchSize;
// Optimise for continuing a batch, don't wait if this chunk didn't
// affect any rows. This way we fast-forward to the next affectable chunk.
if ( $rows ) {
foreach ( $rows as $row ) {
// Don't list the same one twice.
if ( in_array( $row->useragent_id, $found ) ) {
continue;
}
$found[] = $row->useragent_id;
$this->out( '* ' . $row->useragent_id );
if ( $interactive ) {
$this->out( 'Enter replacement uaID (or leave blank to skip):' );
// Input should either be empty (to skip) or be a valid uaID,
// otherwise, repeat the question.
while ( ( $input = $this->cliInput() ) && !isset( $browserIndex->$input ) ) {
$this->out( 'The entered uaID was not found. Try again.' );
}
if ( $input ) {
$this->runBatch( $row->useragent_id, $input, $batchSize );
}
}
}
}
}
return $found;
}
}
$script = MigrateUaIDScript::newFromContext( $swarmContext );
$script->run();