Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageChangeStorage.php
Go to the documentation of this file.
1<?php
12
14 public const DEFAULT_NAME = 'default';
15
23 public static function writeChanges( array $changes, $file ) {
24 $cache = \Cdb\Writer::open( $file );
25 $keys = array_keys( $changes );
26 $cache->set( '#keys', TranslateUtils::serialize( $keys ) );
27
29 foreach ( $changes as $key => $change ) {
30 $value = TranslateUtils::serialize( $change->getAllModifications() );
31 $cache->set( $key, $value );
32 }
33 $cache->close();
34 }
35
42 public static function isValidCdbName( $name ) {
43 return preg_match( '/^[a-z_-]{1,100}$/i', $name );
44 }
45
52 public static function getCdbPath( $name ) {
53 return TranslateUtils::cacheFile( "messagechanges.$name.cdb" );
54 }
55
62 public static function getGroupChanges( $cdbPath, $groupId ) {
63 $reader = self::getCdbReader( $cdbPath );
64 if ( $reader === null ) {
65 return MessageSourceChange::loadModifications( [] );
66 }
67
68 $groups = TranslateUtils::deserialize( $reader->get( '#keys' ) );
69
70 if ( !in_array( $groupId, $groups, true ) ) {
71 throw new InvalidArgumentException( "Group Id - '$groupId' not found in cdb file " .
72 "(path: $cdbPath)." );
73 }
74
75 return MessageSourceChange::loadModifications(
76 TranslateUtils::deserialize( $reader->get( $groupId ) )
77 );
78 }
79
87 public static function writeGroupChanges( MessageSourceChange $changes, $groupId, $cdbPath ) {
88 $reader = self::getCdbReader( $cdbPath );
89 if ( $reader === null ) {
90 return;
91 }
92
93 $groups = TranslateUtils::deserialize( $reader->get( '#keys' ) );
94
95 $allChanges = [];
96 foreach ( $groups as $id ) {
97 $allChanges[$id] = MessageSourceChange::loadModifications(
98 TranslateUtils::deserialize( $reader->get( $id ) )
99 );
100 }
101 $allChanges[$groupId] = $changes;
102
103 self::writeChanges( $allChanges, $cdbPath );
104 }
105
111 private static function getCdbReader( $cdbPath ) {
112 // File not found, probably no changes.
113 if ( !file_exists( $cdbPath ) ) {
114 return null;
115 }
116
117 return \Cdb\Reader::open( $cdbPath );
118 }
119
126 public static function getLastModifiedTime( $cdbPath ) {
127 // File not found
128 if ( !file_exists( $cdbPath ) ) {
129 return null;
130 }
131
132 $stat = stat( $cdbPath );
133
134 return $stat['mtime'];
135 }
136
143 public static function isModifiedSince( $cdbPath, $time ) {
144 $lastModifiedTime = self::getLastModifiedTime( $cdbPath );
145
146 if ( $lastModifiedTime === null ) {
147 throw new InvalidArgumentException( "CDB file not found - $cdbPath" );
148 }
149
150 return $lastModifiedTime <= $time;
151 }
152}
Class is use to track the changes made when importing messages from the remote sources using processM...
static isModifiedSince( $cdbPath, $time)
Checks if the CDB file has been modified since the time given.
static getLastModifiedTime( $cdbPath)
Gets the last modified time for the CDB file.
static getGroupChanges( $cdbPath, $groupId)
Fetches changes for a group from the message change file.
static writeChanges(array $changes, $file)
Writes change array as a serialized file.
static getCdbPath( $name)
Get a full path to file in a known location.
static writeGroupChanges(MessageSourceChange $changes, $groupId, $cdbPath)
Writes changes for a group.
static isValidCdbName( $name)
Validate a name.