-
Notifications
You must be signed in to change notification settings - Fork 2
/
TPCStorage.php
320 lines (288 loc) · 8.92 KB
/
TPCStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Flat-file storage front-end.
*
* @file
* @author Nmlgc
*/
class TPCStorage {
const JSON_OPTS = (JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// TPCServer objects.
static protected $servers = null;
/**
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate
* keys to arrays rather than overwriting the value in the first array with the duplicate
* value in the second array, as array_merge does. I.e., with array_merge_recursive,
* this happens (documented behavior):
*
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
* => array('key' => array('org value', 'new value'));
*
* arrayMergeRecursiveDistinct does not change the datatypes of the values in the arrays.
* Matching keys' values in the second array overwrite those in the first array, as is the
* case with array_merge, i.e.:
*
* arrayMergeRecursiveDistinct(array('key' => 'org value'), array('key' => 'new value'));
* => array('key' => array('new value'));
*
* Parameters are passed by reference, though only for performance reasons. They're not
* altered by this function.
*
* @param array $array1 Base array.
* @param array $array2 Prioritized array.
* @param bool $changed Receives true if there has been a change.
* @return array
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
*/
public static function arrayMergeRecursiveDistinct( array &$array1, array &$array2, &$changed ) {
$merged = $array1;
foreach ( $array2 as $key => &$value ) {
if (
TPCUtil::isAssoc ( $value ) &&
isset ( $merged[$key] ) &&
TPCUtil::isAssoc ( $merged[$key] )
) {
$merged[$key] = self::arrayMergeRecursiveDistinct( $merged[$key], $value, $changed );
} else if ( !isset( $merged[$key] ) or $merged[$key] !== $value ) {
$merged[$key] = $value;
$changed = true;
}
}
return $merged;
}
protected static function mergeOldFile( &$server, &$array, &$fn, &$changed ) {
if ( !file_exists( $fn ) ) {
$changed = true;
return $array;
}
$oldJson = $server->get( $fn );
if ( !$oldJson ) {
$changed = true;
return $array;
}
$oldArray = json_decode( $oldJson, true );
if ( $oldArray === null ) {
throw new MWException(
"`$fn` is invalid JSON. Ask an admin to fix the error on the server."
);
}
return self::arrayMergeRecursiveDistinct( $oldArray, $array, $changed );
}
protected static function getServersForPatch( &$patch ) {
global $wgTPCServers;
$ret = array();
foreach ( $wgTPCServers as $i ) {
if ( isset( $i['url'] ) ) {
$serverURL = "{$i['url']}/$patch/";
$ret[] = preg_replace( '/([^:])(\/{2,})/', '$1/', $serverURL );
}
}
return $ret;
}
protected static function chdirPatch( &$server, $patch, &$file ) {
$curDir = '';
if ( $patch ) {
$curDir = $patch;
}
$server->mkdir( $patch );
$server->chdir( $patch );
// Current directory is now patch-relative, don't go further.
// Create file's directory if necessary - but don't change to it!
$dirName = dirname( $file );
if ( $dirName and $dirName != '.' ) {
$curDir .= '/' . $dirName;
}
$server->mkdir( $curDir );
}
/**
* Writes a JSON file to a certain patch, merging any previously created content.
*
* @return int Hash of the target file's full merged content.
*/
protected static function writeJSONFile( $fn, &$array, $patch = null ) {
$ret = null;
// Don't write "null" for files that were requested but never edited
if ( !$array or !$fn ) {
return;
}
$renderFile = true;
foreach ( self::$servers as $server ) {
self::chdirPatch( $server, $patch, $fn );
if ( $renderFile ) {
// If this file already exists, merge its copy on the first server.
$changed = false;
$array = self::mergeOldFile( $server, $array, $fn, $changed );
if( !$changed ) {
// Nothing to do here.
return;
}
$json = json_encode( (object)$array, self::JSON_OPTS );
$renderFile = false;
$ret = crc32( $json );
}
$server->put( $fn, $json );
}
return $ret;
}
protected static function writeCopyFile( $target, &$source, $patch = null ) {
foreach ( self::$servers as $server ) {
self::chdirPatch( $server, $patch, $target );
$server->copy( $target, $source );
}
$sourceData = file_get_contents( $source );
return crc32( $sourceData );
}
protected static function writeDeletion( $target, $patch = null ) {
foreach ( self::$servers as $server ) {
self::chdirPatch( $server, $patch, $target );
$server->delete( $target );
}
}
/**
* @param function $cacheFunc
* Function to call for each element. Should return a hash or equivalent
* integer identifying the element's current version.
*
* @param string $cacheFunc Name of the cache function. Must be a static method of this
* class.
* @param array $cache
* @param string $patch
* @return array Array of the form ( [filename] => [hash] )
*/
protected static function writeCache( $cacheFunc, &$cache, $patch = null ) {
$ret = array();
foreach ( $cache as $target => &$source ) {
$hash = self::$cacheFunc( $target, $source, $patch );
if ( $hash ) {
$ret[$target] = $hash;
}
}
return $ret;
}
protected static function writeJSONCache( &$jsonCache, $patch = null ) {
return self::writeCache( 'writeJSONFile', $jsonCache, $patch );
}
protected static function writeCopyCache( &$copyCache, $patch = null ) {
return self::writeCache( 'writeCopyFile', $copyCache, $patch );
}
protected static function writeDeletionCache( &$deletionCache, $patch = null ) {
$ret = array();
foreach ( $deletionCache as $i ) {
self::writeDeletion( $i, $patch );
$ret[ $i ] = null;
}
return $ret;
}
/**
* Updates the main repository definition (repo.js).
* Also adds an optional $patchList.
*/
protected static function writeRepoFile( $patchList = null ) {
global $wgTPCServers;
global $wgTPCRepoID;
global $wgTPCRepoTitle;
global $wgTPCRepoContact;
global $wgTPCRepoNeighbors;
global $wgTPCRepoDescURL;
$repoJS = array(
'id' => $wgTPCRepoID,
'title' => $wgTPCRepoTitle,
'contact' => $wgTPCRepoContact,
'neighbors' => $wgTPCRepoNeighbors,
'url_desc' => $wgTPCRepoDescURL
);
if ( $patchList ) {
$repoJS['patches'] = $patchList;
}
foreach ( $wgTPCServers as $i ) {
if ( isset( $i['url'] ) ) {
$repoJS['servers'][] = $i['url'];
}
}
self::writeJSONFile( 'repo.js', $repoJS );
}
protected static function newServer( &$server ) {
$class = $server['class'];
if ( class_exists( $class ) ) {
return new $class( $server );
} else if ( is_string( $class ) ) {
throw new MWException(
"Required back-end server class '$class' not available!\n" .
"(Did you run 'composer install'?)"
);
}
}
/**
* Initializes the server back-end classes.
*/
public static function init() {
global $wgTPCServers;
if ( self::$servers ) {
return;
}
self::$servers = array();
foreach ( $wgTPCServers as $i ) {
if ( isset( $i['class'] ) ) {
self::$servers[] = self::newServer( $i );
}
}
}
/**
* The main "write state to all servers" function.
*/
public static function writeState( &$tpcState ) {
$prevDir = getcwd();
$files = $tpcState->listFiles();
$patchJS = &$tpcState->getFile( null, 'patch.js' );
if ( empty ( $files ) and empty ( $patchJS ) ) {
return;
}
self::init();
// --------------
// Other settings
// --------------
$patchJS['update'] = true;
// List fonts
$fonts = preg_grep( '/\.(ttf|otf)$/i', $files );
// Nope, we can't do an array because this would overwrite any previous
// assignment. It shouldn't matter for fonts, but it's still unexpected
// behavior...
foreach ( $fonts as $i ) {
$patchJS['fonts'][$i] = true;
}
// --------------
$patchList = array();
foreach ( $tpcState->patches as $patch ) {
// Write patch base URLs.
// The if() is necessary because we do not want to accidentally null
$servers = self::getServersForPatch( $patch );
if ( $servers ) {
$patchJS['servers'] = $servers;
}
// Whenever we have a title, we're evaluating just one patch anyway.
// Yes, patches will not show up unless they have a thcrap_patch_info
// associated with them.
if ( isset( $patchJS['title'] ) ) {
$patchList[$patch] = $patchJS['title'];
}
$filesJS = array_merge(
self::writeJSONCache( $tpcState->jsonCache, $patch ),
self::writeCopyCache( $tpcState->copyCache, $patch ),
self::writeDeletionCache( $tpcState->deletionCache, $patch )
);
self::writeJSONFile( 'files.js', $filesJS, $patch );
}
self::writeRepoFile( $patchList );
// Shouldn't matter on the server, but offline testers will thank you
chdir( $prevDir );
}
/// =========================
/// Wrappers around TPCServer
/// =========================
public static function wipe() {
foreach ( self::$servers as $server ) {
$server->wipe();
}
}
}