diff options
Diffstat (limited to 'MLEB/Translate/utils/TranslateMetadata.php')
-rw-r--r-- | MLEB/Translate/utils/TranslateMetadata.php | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/MLEB/Translate/utils/TranslateMetadata.php b/MLEB/Translate/utils/TranslateMetadata.php index 9fe7be1a..c6d0027e 100644 --- a/MLEB/Translate/utils/TranslateMetadata.php +++ b/MLEB/Translate/utils/TranslateMetadata.php @@ -11,7 +11,32 @@ */ class TranslateMetadata { - protected static $cache; + /** @var array Map of (group => key => value) */ + private static $cache = []; + + /** + * @param string[] $groups List of translate groups + */ + public static function preloadGroups( array $groups ) { + $missing = array_keys( array_diff_key( array_flip( $groups ), self::$cache ) ); + if ( !$missing ) { + return; + } + + self::$cache += array_fill_keys( $missing, null ); // cache negatives + + $dbr = TranslateUtils::getSafeReadDB(); + $conds = count( $missing ) <= 500 ? [ 'tmd_group' => $missing ] : []; + $res = $dbr->select( + 'translate_metadata', + [ 'tmd_group', 'tmd_key', 'tmd_value' ], + $conds, + __METHOD__ + ); + foreach ( $res as $row ) { + self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value; + } + } /** * Get a metadata value for the given group and key. @@ -20,19 +45,24 @@ class TranslateMetadata { * @return string|bool */ public static function get( $group, $key ) { - if ( self::$cache === null ) { - $dbr = wfGetDB( DB_REPLICA ); - $res = $dbr->select( 'translate_metadata', '*', [], __METHOD__ ); - foreach ( $res as $row ) { - self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value; - } - } + self::preloadGroups( [ $group ] ); - if ( isset( self::$cache[$group][$key] ) ) { - return self::$cache[$group][$key]; - } + return self::$cache[$group][$key] ?? false; + } - return false; + /** + * Get a metadata value for the given group and key. + * If it does not exist, return the default value. + * @param string $group + * @param string $key + * @param string $defaultValue + * @return string + */ + public static function getWithDefaultValue( + string $group, string $key, string $defaultValue + ): string { + $value = self::get( $group, $key ); + return $value === false ? $defaultValue : $value; } /** @@ -47,7 +77,8 @@ class TranslateMetadata { $data = [ 'tmd_group' => $group, 'tmd_key' => $key, 'tmd_value' => $value ]; if ( $value === false ) { unset( $data['tmd_value'] ); - $dbw->delete( 'translate_metadata', $data ); + $dbw->delete( 'translate_metadata', $data, __METHOD__ ); + unset( self::$cache[$group][$key] ); } else { $dbw->replace( 'translate_metadata', @@ -55,9 +86,8 @@ class TranslateMetadata { $data, __METHOD__ ); + self::$cache[$group][$key] = $value; } - - self::$cache = null; } /** @@ -104,6 +134,7 @@ class TranslateMetadata { public static function deleteGroup( $groupId ) { $dbw = wfGetDB( DB_MASTER ); $conds = [ 'tmd_group' => $groupId ]; - $dbw->delete( 'translate_metadata', $conds ); + $dbw->delete( 'translate_metadata', $conds, __METHOD__ ); + self::$cache[$groupId] = null; } } |