Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageChangeStorage.php
Go to the documentation of this file.
1<?php
13
15 public const DEFAULT_NAME = 'default';
16
24 public static function writeChanges( array $changes, $file ) {
25 $cache = \Cdb\Writer::open( $file );
26 $keys = array_keys( $changes );
27 $cache->set( '#keys', Utilities::serialize( $keys ) );
28
30 foreach ( $changes as $key => $change ) {
31 $value = Utilities::serialize( $change->getAllModifications() );
32 $cache->set( $key, $value );
33 }
34 $cache->close();
35 }
36
43 public static function isValidCdbName( $name ) {
44 return preg_match( '/^[a-z_-]{1,100}$/i', $name );
45 }
46
53 public static function getCdbPath( $name ) {
54 return Utilities::cacheFile( "messagechanges.$name.cdb" );
55 }
56
63 public static function getGroupChanges( $cdbPath, $groupId ) {
64 $reader = self::getCdbReader( $cdbPath );
65 if ( $reader === null ) {
66 return MessageSourceChange::loadModifications( [] );
67 }
68
69 $groups = Utilities::deserialize( $reader->get( '#keys' ) );
70
71 if ( !in_array( $groupId, $groups, true ) ) {
72 throw new InvalidArgumentException( "Group Id - '$groupId' not found in cdb file " .
73 "(path: $cdbPath)." );
74 }
75
76 return MessageSourceChange::loadModifications(
77 Utilities::deserialize( $reader->get( $groupId ) )
78 );
79 }
80
88 public static function writeGroupChanges( MessageSourceChange $changes, $groupId, $cdbPath ) {
89 $reader = self::getCdbReader( $cdbPath );
90 if ( $reader === null ) {
91 return;
92 }
93
94 $groups = Utilities::deserialize( $reader->get( '#keys' ) );
95
96 $allChanges = [];
97 foreach ( $groups as $id ) {
98 $allChanges[$id] = MessageSourceChange::loadModifications(
99 Utilities::deserialize( $reader->get( $id ) )
100 );
101 }
102 $allChanges[$groupId] = $changes;
103
104 self::writeChanges( $allChanges, $cdbPath );
105 }
106
112 private static function getCdbReader( $cdbPath ) {
113 // File not found, probably no changes.
114 if ( !file_exists( $cdbPath ) ) {
115 return null;
116 }
117
118 return \Cdb\Reader::open( $cdbPath );
119 }
120
127 public static function getLastModifiedTime( $cdbPath ) {
128 // File not found
129 if ( !file_exists( $cdbPath ) ) {
130 return null;
131 }
132
133 $stat = stat( $cdbPath );
134
135 return $stat['mtime'];
136 }
137
144 public static function isModifiedSince( $cdbPath, $time ) {
145 $lastModifiedTime = self::getLastModifiedTime( $cdbPath );
146
147 if ( $lastModifiedTime === null ) {
148 throw new InvalidArgumentException( "CDB file not found - $cdbPath" );
149 }
150
151 return $lastModifiedTime <= $time;
152 }
153}
Class is used to track the changes made when importing messages from the remote sources using importE...
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31
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.