aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance
diff options
context:
space:
mode:
authorBartosz Dziewoński <matma.rex@gmail.com>2022-09-04 00:48:19 +0200
committerBartosz Dziewoński <matma.rex@gmail.com>2022-09-10 04:22:19 +0200
commitec79aa394312d62b598ad29601e2c80eaaf0dd19 (patch)
tree7218ca927c1945bb252892370d3d2991a3585d67 /maintenance
parentaf60bf699c7de2b52fef6cd66e79b151a9f81ab8 (diff)
downloadmediawikicore-ec79aa394312d62b598ad29601e2c80eaaf0dd19.tar.gz
mediawikicore-ec79aa394312d62b598ad29601e2c80eaaf0dd19.zip
SQLPlatform: Introduce buildComparison()
Builds a condition comparing multiple values, for use with indexes that cover multiple fields, common when e.g. paging through results or doing batch operations. Can also be to generate a simple comparison without writing raw SQL (see T210206). Update a few manually constructed conditions to use this method. There are more maintenance scripts and API classes that use the same patterns, but this is a start. As you can see by the code I'm replacing, there are many ways to do this. I picked the one used by maintenance/TableCleanup.php, since I found it the easiest to understand. Change-Id: Ic368a87fb5ce4c13608b03206cd68518ec9732d4
Diffstat (limited to 'maintenance')
-rw-r--r--maintenance/TableCleanup.php16
-rw-r--r--maintenance/namespaceDupes.php13
-rw-r--r--maintenance/updateCollation.php22
3 files changed, 15 insertions, 36 deletions
diff --git a/maintenance/TableCleanup.php b/maintenance/TableCleanup.php
index 53bc915c9e14..779f7c9f84af 100644
--- a/maintenance/TableCleanup.php
+++ b/maintenance/TableCleanup.php
@@ -148,20 +148,12 @@ class TableCleanup extends Maintenance {
}
// Update the conditions to select the next batch.
- // Construct a condition string by starting with the least significant part
- // of the index, and adding more significant parts progressively to the left
- // of the string.
- $nextCond = '';
- foreach ( array_reverse( $index ) as $field ) {
+ $conds = [];
+ foreach ( $index as $field ) {
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
- $encValue = $dbr->addQuotes( $row->$field );
- if ( $nextCond === '' ) {
- $nextCond = "$field > $encValue";
- } else {
- $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
- }
+ $conds[ $field ] = $row->$field;
}
- $indexConds = [ $nextCond ];
+ $indexConds = [ $dbr->buildComparison( '>', $conds ) ];
}
$this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php
index 3e1ff809b9b5..e44275aa6c15 100644
--- a/maintenance/namespaceDupes.php
+++ b/maintenance/namespaceDupes.php
@@ -488,14 +488,13 @@ class NamespaceDupes extends Maintenance {
$this->resolvableLinks -= $dbw->affectedRows();
}
- // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
- $encLastTitle = $dbw->addQuotes( $row->$titleField );
- // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
- $encLastFrom = $dbw->addQuotes( $row->$fromField );
-
$batchConds = [
- "$titleField > $encLastTitle " .
- "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)"
+ $dbw->buildComparison( '>', [
+ // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
+ $titleField => $dbw->addQuotes( $row->$titleField ),
+ // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
+ $fromField => $dbw->addQuotes( $row->$fromField ),
+ ] )
];
$lbFactory->waitForReplication();
diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php
index 6230ef09d140..3f6bb19096ec 100644
--- a/maintenance/updateCollation.php
+++ b/maintenance/updateCollation.php
@@ -271,31 +271,19 @@ TEXT
} else {
$fields = [ 'cl_collation', 'cl_to', 'cl_type', 'cl_from' ];
}
- $first = true;
- $cond = false;
- $prefix = false;
+ $conds = [];
foreach ( $fields as $field ) {
if ( $dbw->getType() === 'mysql' && $field === 'cl_type' ) {
// Range conditions with enums are weird in mysql
// This must be a numeric literal, or it won't work.
- $encValue = intval( $row->cl_type_numeric );
+ $value = intval( $row->cl_type_numeric );
} else {
- $encValue = $dbw->addQuotes( $row->$field );
- }
- $inequality = "$field > $encValue";
- $equality = "$field = $encValue";
- if ( $first ) {
- $cond = $inequality;
- $prefix = $equality;
- $first = false;
- } else {
- // @phan-suppress-next-line PhanTypeSuspiciousStringExpression False positive
- $cond .= " OR ($prefix AND $inequality)";
- $prefix .= " AND $equality";
+ $value = $row->$field;
}
+ $conds[ $field ] = $value;
}
- return $cond;
+ return $dbw->buildComparison( '>', $conds );
}
/**