summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/TranslateMetadata.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/utils/TranslateMetadata.php')
-rw-r--r--www/wiki/extensions/Translate/utils/TranslateMetadata.php62
1 files changed, 36 insertions, 26 deletions
diff --git a/www/wiki/extensions/Translate/utils/TranslateMetadata.php b/www/wiki/extensions/Translate/utils/TranslateMetadata.php
index 9d3c8b9e..4294ca30 100644
--- a/www/wiki/extensions/Translate/utils/TranslateMetadata.php
+++ b/www/wiki/extensions/Translate/utils/TranslateMetadata.php
@@ -7,65 +7,74 @@
* @author Niklas Laxström
* @author Santhosh Thottingal
* @copyright Copyright © 2012-2013, Niklas Laxström, Santhosh Thottingal
- * @license GPL-2.0+
+ * @license GPL-2.0-or-later
*/
class TranslateMetadata {
- protected static $cache;
+ /** @var array Map of (group => key => value) */
+ private static $cache = [];
/**
- * Get a metadata value for the given group and key.
- * @param $group string The group name
- * @param $key string Metadata key
- * @return String
+ * @param string[] $groups List of translate groups
*/
- public static function get( $group, $key ) {
- if ( self::$cache === null ) {
- $dbr = wfGetDB( DB_SLAVE );
- $res = $dbr->select( 'translate_metadata', '*', array(), __METHOD__ );
- foreach ( $res as $row ) {
- self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
- }
+ public static function preloadGroups( array $groups ) {
+ $missing = array_diff( $groups, array_keys( self::$cache ) );
+ if ( !$missing ) {
+ return;
}
- if ( isset( self::$cache[$group][$key] ) ) {
- return self::$cache[$group][$key];
+ self::$cache += array_fill_keys( $missing, null ); // cache negatives
+
+ $dbr = TranslateUtils::getSafeReadDB();
+ $conds = count( $groups ) <= 500 ? [ 'tmd_group' => $missing ] : [];
+ $res = $dbr->select( 'translate_metadata', '*', $conds, __METHOD__ );
+ foreach ( $res as $row ) {
+ self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
}
+ }
- return false;
+ /**
+ * Get a metadata value for the given group and key.
+ * @param string $group The group name
+ * @param string $key Metadata key
+ * @return string|bool
+ */
+ public static function get( $group, $key ) {
+ self::preloadGroups( [ $group ] );
+
+ return self::$cache[$group][$key] ?? false;
}
/**
* Set a metadata value for the given group and metadata key. Updates the
* value if already existing.
- * @param $group string The group id
- * @param $key string Metadata key
- * @param $value string Metadata value
+ * @param string $group The group id
+ * @param string $key Metadata key
+ * @param string $value Metadata value
*/
public static function set( $group, $key, $value ) {
$dbw = wfGetDB( DB_MASTER );
- $data = array( 'tmd_group' => $group, 'tmd_key' => $key, 'tmd_value' => $value );
+ $data = [ 'tmd_group' => $group, 'tmd_key' => $key, 'tmd_value' => $value ];
if ( $value === false ) {
unset( $data['tmd_value'] );
$dbw->delete( 'translate_metadata', $data );
+ unset( self::$cache[$group][$key] );
} else {
$dbw->replace(
'translate_metadata',
- array( array( 'tmd_group', 'tmd_key' ) ),
+ [ [ 'tmd_group', 'tmd_key' ] ],
$data,
__METHOD__
);
+ self::$cache[$group][$key] = $value;
}
-
- self::$cache = null;
}
/**
* Wrapper for getting subgroups.
* @param string $groupId
- * @return array|String
+ * @return string[]|bool
* @since 2012-05-09
- * return array|false
*/
public static function getSubgroups( $groupId ) {
$groups = self::get( $groupId, 'subgroups' );
@@ -104,7 +113,8 @@ class TranslateMetadata {
*/
public static function deleteGroup( $groupId ) {
$dbw = wfGetDB( DB_MASTER );
- $conds = array( 'tmd_group' => $groupId );
+ $conds = [ 'tmd_group' => $groupId ];
$dbw->delete( 'translate_metadata', $conds );
+ self::$cache[$groupId] = null;
}
}