diff options
Diffstat (limited to 'MLEB/Translate/utils/MessageChangeStorage.php')
-rw-r--r-- | MLEB/Translate/utils/MessageChangeStorage.php | 120 |
1 files changed, 111 insertions, 9 deletions
diff --git a/MLEB/Translate/utils/MessageChangeStorage.php b/MLEB/Translate/utils/MessageChangeStorage.php index 5c23a3a6..6fbd8a0a 100644 --- a/MLEB/Translate/utils/MessageChangeStorage.php +++ b/MLEB/Translate/utils/MessageChangeStorage.php @@ -1,6 +1,6 @@ <?php /** - * Handles storage of message change files. + * Handles storage / retrieval of data from message change files. * * @author Niklas Laxström * @license GPL-2.0-or-later @@ -8,23 +8,28 @@ * @file */ +use MediaWiki\Extensions\Translate\MessageSync\MessageSourceChange; + class MessageChangeStorage { - const DEFAULT_NAME = 'default'; + public const DEFAULT_NAME = 'default'; /** * Writes change array as a serialized file. * - * @param array $array Array of changes as returned by processGroup + * @param MessageSourceChange[] $changes Array of changes as returned by processGroup * indexed by message group id. * @param string $file Which file to use. */ - public static function writeChanges( $array, $file ) { + public static function writeChanges( array $changes, $file ) { $cache = \Cdb\Writer::open( $file ); - $keys = array_keys( $array ); - $cache->set( '#keys', serialize( $keys ) ); + $keys = array_keys( $changes ); + $cache->set( '#keys', TranslateUtils::serialize( $keys ) ); - foreach ( $array as $key => $value ) { - $value = serialize( $value ); + /** + * @var MessageSourceChange $change + */ + foreach ( $changes as $key => $change ) { + $value = TranslateUtils::serialize( $change->getAllModifications() ); $cache->set( $key, $value ); } $cache->close(); @@ -37,7 +42,7 @@ class MessageChangeStorage { * @return bool */ public static function isValidCdbName( $name ) { - return preg_match( '/^[a-zA-Z_-]{1,100}$/', $name ); + return preg_match( '/^[a-z_-]{1,100}$/i', $name ); } /** @@ -49,4 +54,101 @@ class MessageChangeStorage { public static function getCdbPath( $name ) { return TranslateUtils::cacheFile( "messagechanges.$name.cdb" ); } + + /** + * Fetches changes for a group from the message change file. + * @param string $cdbPath Path of the cdb file. + * @param string $groupId Group Id + * @return MessageSourceChange + */ + public static function getGroupChanges( $cdbPath, $groupId ) { + $reader = self::getCdbReader( $cdbPath ); + if ( $reader === null ) { + return MessageSourceChange::loadModifications( [] ); + } + + $groups = TranslateUtils::deserialize( $reader->get( '#keys' ) ); + + if ( !in_array( $groupId, $groups, true ) ) { + throw new InvalidArgumentException( "Group Id - '$groupId' not found in cdb file " . + "(path: $cdbPath)." ); + } + + return MessageSourceChange::loadModifications( + TranslateUtils::deserialize( $reader->get( $groupId ) ) + ); + } + + /** + * Writes changes for a group. Has to read the changes first from the file, + * and then re-write them to the file. + * @param MessageSourceChange $changes + * @param string $groupId Group Id + * @param string $cdbPath Path of the cdb file. + */ + public static function writeGroupChanges( MessageSourceChange $changes, $groupId, $cdbPath ) { + $reader = self::getCdbReader( $cdbPath ); + if ( $reader === null ) { + return; + } + + $groups = TranslateUtils::deserialize( $reader->get( '#keys' ) ); + + $allChanges = []; + foreach ( $groups as $id ) { + $allChanges[$id] = MessageSourceChange::loadModifications( + TranslateUtils::deserialize( $reader->get( $id ) ) + ); + } + $allChanges[$groupId] = $changes; + + self::writeChanges( $allChanges, $cdbPath ); + } + + /** + * Validate and return a reader reference to the CDB file + * @param string $cdbPath + * @return \Cdb\Reader + */ + private static function getCdbReader( $cdbPath ) { + // File not found, probably no changes. + if ( !file_exists( $cdbPath ) ) { + return null; + } + + return \Cdb\Reader::open( $cdbPath ); + } + + /** + * Gets the last modified time for the CDB file. + * + * @param string $cdbPath + * @return int time of last modification (Unix timestamp) + */ + public static function getLastModifiedTime( $cdbPath ) { + // File not found + if ( !file_exists( $cdbPath ) ) { + return null; + } + + $stat = stat( $cdbPath ); + + return $stat['mtime']; + } + + /** + * Checks if the CDB file has been modified since the time given. + * @param string $cdbPath + * @param int $time Unix timestamp + * @return bool + */ + public static function isModifiedSince( $cdbPath, $time ) { + $lastModifiedTime = self::getLastModifiedTime( $cdbPath ); + + if ( $lastModifiedTime === null ) { + throw new InvalidArgumentException( "CDB file not found - $cdbPath" ); + } + + return $lastModifiedTime <= $time; + } } |