| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
require_once __DIR__ . '/Maintenance.php'; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class PopulateChangeTagDef extends LoggedUpdateMaintenance { |
| 29 |
public function __construct() { |
| 30 |
parent::__construct(); |
| 31 |
$this->addDescription( 'Populate and improve accuracy of change_tag_def statistics' ); |
| 32 |
$this->addOption( 'dry-run', 'Print debug info instead of actually deleting' ); |
| 33 |
$this->setBatchSize( 1000 ); |
| 34 |
$this->addOption( |
| 35 |
'sleep', |
| 36 |
'Sleep time (in seconds) between every batch, defaults to zero', |
| 37 |
false, |
| 38 |
true |
| 39 |
); |
| 40 |
$this->addOption( 'populate-only', 'Do not update change_tag_def table' ); |
| 41 |
$this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' ); |
| 42 |
} |
| 43 |
|
| 44 |
protected function doDBUpdates() { |
| 45 |
$this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) ); |
| 46 |
|
| 47 |
$dbw = $this->getDB( DB_PRIMARY ); |
| 48 |
if ( $dbw->fieldExists( |
| 49 |
'change_tag', |
| 50 |
'ct_tag', |
| 51 |
__METHOD__ |
| 52 |
) |
| 53 |
) { |
| 54 |
if ( $this->hasOption( 'set-user-tags-only' ) ) { |
| 55 |
$this->setUserDefinedTags(); |
| 56 |
return true; |
| 57 |
} |
| 58 |
if ( !$this->hasOption( 'populate-only' ) ) { |
| 59 |
$this->updateCountTag(); |
| 60 |
} |
| 61 |
$this->backpopulateChangeTagId(); |
| 62 |
$this->setUserDefinedTags(); |
| 63 |
} else { |
| 64 |
$this->updateCountTagId(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
private function setUserDefinedTags() { |
| 74 |
$dbw = $this->getDB( DB_PRIMARY ); |
| 75 |
|
| 76 |
$userTags = null; |
| 77 |
if ( $dbw->tableExists( 'valid_tag', __METHOD__ ) ) { |
| 78 |
$userTags = $dbw->newSelectQueryBuilder() |
| 79 |
->select( 'vt_tag' ) |
| 80 |
->from( 'valid_tag' ) |
| 81 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 82 |
} |
| 83 |
|
| 84 |
if ( !$userTags ) { |
| 85 |
$this->output( "No user defined tags to set, moving on...\n" ); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 90 |
$this->output( |
| 91 |
'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n" |
| 92 |
); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$dbw->newUpdateQueryBuilder() |
| 97 |
->update( 'change_tag_def' ) |
| 98 |
->set( [ 'ctd_user_defined' => 1 ] ) |
| 99 |
->where( [ 'ctd_name' => $userTags ] ) |
| 100 |
->caller( __METHOD__ ) |
| 101 |
->execute(); |
| 102 |
$this->waitForReplication(); |
| 103 |
$this->output( "Finished setting user defined tags in change_tag_def table\n" ); |
| 104 |
} |
| 105 |
|
| 106 |
private function updateCountTagId() { |
| 107 |
$dbr = $this->getReplicaDB(); |
| 108 |
|
| 109 |
|
| 110 |
$res = $dbr->newSelectQueryBuilder() |
| 111 |
->select( [ 'ct_tag_id', 'hitcount' => 'count(*)' ] ) |
| 112 |
->from( 'change_tag' ) |
| 113 |
->groupBy( 'ct_tag_id' ) |
| 114 |
->caller( __METHOD__ )->fetchResultSet(); |
| 115 |
|
| 116 |
$dbw = $this->getPrimaryDB(); |
| 117 |
|
| 118 |
foreach ( $res as $row ) { |
| 119 |
if ( !$row->ct_tag_id ) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 124 |
$this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" ); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$dbw->newUpdateQueryBuilder() |
| 129 |
->update( 'change_tag_def' ) |
| 130 |
->set( [ 'ctd_count' => $row->hitcount ] ) |
| 131 |
->where( [ 'ctd_id' => $row->ct_tag_id ] ) |
| 132 |
->caller( __METHOD__ ) |
| 133 |
->execute(); |
| 134 |
} |
| 135 |
$this->waitForReplication(); |
| 136 |
} |
| 137 |
|
| 138 |
private function updateCountTag() { |
| 139 |
$dbr = $this->getReplicaDB(); |
| 140 |
|
| 141 |
|
| 142 |
$res = $dbr->newSelectQueryBuilder() |
| 143 |
->select( [ 'ct_tag', 'hitcount' => 'count(*)' ] ) |
| 144 |
->from( 'change_tag' ) |
| 145 |
->groupBy( 'ct_tag' ) |
| 146 |
->caller( __METHOD__ )->fetchResultSet(); |
| 147 |
|
| 148 |
$dbw = $this->getPrimaryDB(); |
| 149 |
|
| 150 |
foreach ( $res as $row ) { |
| 151 |
|
| 152 |
if ( !$row->ct_tag ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 157 |
$this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" ); |
| 158 |
continue; |
| 159 |
} |
| 160 |
$dbw->newInsertQueryBuilder() |
| 161 |
->insertInto( 'change_tag_def' ) |
| 162 |
->row( [ |
| 163 |
'ctd_name' => $row->ct_tag, |
| 164 |
'ctd_user_defined' => 0, |
| 165 |
'ctd_count' => $row->hitcount |
| 166 |
] ) |
| 167 |
->onDuplicateKeyUpdate() |
| 168 |
->uniqueIndexFields( [ 'ctd_name' ] ) |
| 169 |
->set( [ 'ctd_count' => $row->hitcount ] ) |
| 170 |
->caller( __METHOD__ )->execute(); |
| 171 |
} |
| 172 |
$this->waitForReplication(); |
| 173 |
} |
| 174 |
|
| 175 |
private function backpopulateChangeTagId() { |
| 176 |
$dbr = $this->getReplicaDB(); |
| 177 |
$changeTagDefs = $dbr->newSelectQueryBuilder() |
| 178 |
->select( [ 'ctd_name', 'ctd_id' ] ) |
| 179 |
->from( 'change_tag_def' ) |
| 180 |
->orderBy( 'ctd_id' ) |
| 181 |
->caller( __METHOD__ )->fetchResultSet(); |
| 182 |
|
| 183 |
foreach ( $changeTagDefs as $row ) { |
| 184 |
$this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private function backpopulateChangeTagPerTag( $tagName, $tagId ) { |
| 189 |
$dbr = $this->getReplicaDB(); |
| 190 |
$dbw = $this->getPrimaryDB(); |
| 191 |
$sleep = (int)$this->getOption( 'sleep', 0 ); |
| 192 |
$lastId = 0; |
| 193 |
$this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" ); |
| 194 |
while ( true ) { |
| 195 |
|
| 196 |
$ids = $dbr->newSelectQueryBuilder() |
| 197 |
->select( 'ct_id' ) |
| 198 |
->from( 'change_tag' ) |
| 199 |
->where( [ 'ct_tag' => $tagName, 'ct_tag_id' => null, $dbr->expr( 'ct_id', '>', $lastId ) ] ) |
| 200 |
->orderBy( 'ct_id' ) |
| 201 |
->limit( $this->getBatchSize() ) |
| 202 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 203 |
|
| 204 |
if ( !$ids ) { |
| 205 |
break; |
| 206 |
} |
| 207 |
$lastId = end( $ids ); |
| 208 |
|
| 209 |
if ( $this->hasOption( 'dry-run' ) ) { |
| 210 |
$this->output( |
| 211 |
"These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n" |
| 212 |
); |
| 213 |
continue; |
| 214 |
} else { |
| 215 |
$this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" ); |
| 216 |
} |
| 217 |
|
| 218 |
$dbw->newUpdateQueryBuilder() |
| 219 |
->update( 'change_tag' ) |
| 220 |
->set( [ 'ct_tag_id' => $tagId ] ) |
| 221 |
->where( [ 'ct_id' => $ids ] ) |
| 222 |
->caller( __METHOD__ ) |
| 223 |
->execute(); |
| 224 |
|
| 225 |
$this->waitForReplication(); |
| 226 |
if ( $sleep > 0 ) { |
| 227 |
sleep( $sleep ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
$this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" ); |
| 232 |
} |
| 233 |
|
| 234 |
protected function getUpdateKey() { |
| 235 |
return __CLASS__; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
$maintClass = PopulateChangeTagDef::class; |
| 241 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 242 |
|