Index: trunk/phase3/maintenance/language/messages.inc |
— | — | @@ -1363,7 +1363,8 @@ |
1364 | 1364 | 'backend-fail-closetemp', |
1365 | 1365 | 'backend-fail-read', |
1366 | 1366 | 'backend-fail-create', |
1367 | | - 'backend-fail-readonly' |
| 1367 | + 'backend-fail-readonly', |
| 1368 | + 'backend-fail-synced' |
1368 | 1369 | ), |
1369 | 1370 | |
1370 | 1371 | 'lockmanager-errors' => array( |
Index: trunk/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php |
— | — | @@ -7,15 +7,16 @@ |
8 | 8 | |
9 | 9 | function setUp() { |
10 | 10 | parent::setUp(); |
11 | | - $this->backend = new FSFileBackend( array( |
| 11 | + $tmpDir = wfTempDir() . '/' . time() . '-' . mt_rand(); |
| 12 | + $this->singleBackend = new FSFileBackend( array( |
12 | 13 | 'name' => 'localtesting', |
13 | 14 | 'lockManager' => 'fsLockManager', |
14 | 15 | 'containerPaths' => array( |
15 | | - 'cont1' => wfTempDir() . '/localtesting/cont1', |
16 | | - 'cont2' => wfTempDir() . '/localtesting/cont2' ) |
| 16 | + 'cont1' => "$tmpDir/localtesting/cont1", |
| 17 | + 'cont2' => "$tmpDir/localtesting/cont2" ) |
17 | 18 | ) ); |
18 | 19 | $this->multiBackend = new FileBackendMultiWrite( array( |
19 | | - 'name' => 'localtestingmulti', |
| 20 | + 'name' => 'localtesting', |
20 | 21 | 'lockManager' => 'fsLockManager', |
21 | 22 | 'backends' => array( |
22 | 23 | array( |
— | — | @@ -23,8 +24,8 @@ |
24 | 25 | 'class' => 'FSFileBackend', |
25 | 26 | 'lockManager' => 'nullLockManager', |
26 | 27 | 'containerPaths' => array( |
27 | | - 'cont1' => wfTempDir() . '/localtestingmulti1/cont1', |
28 | | - 'cont2' => wfTempDir() . '/localtestingmulti1/cont2' ), |
| 28 | + 'cont1' => "$tmpDir/localtestingmulti1/cont1", |
| 29 | + 'cont2' => "$tmpDir/localtestingmulti1/cont2" ), |
29 | 30 | 'isMultiMaster' => false |
30 | 31 | ), |
31 | 32 | array( |
— | — | @@ -32,8 +33,8 @@ |
33 | 34 | 'class' => 'FSFileBackend', |
34 | 35 | 'lockManager' => 'nullLockManager', |
35 | 36 | 'containerPaths' => array( |
36 | | - 'cont1' => wfTempDir() . '/localtestingmulti2/cont1', |
37 | | - 'cont2' => wfTempDir() . '/localtestingmulti2/cont2' ), |
| 37 | + 'cont1' => "$tmpDir/localtestingmulti2/cont1", |
| 38 | + 'cont2' => "$tmpDir/localtestingmulti2/cont2" ), |
38 | 39 | 'isMultiMaster' => true |
39 | 40 | ) |
40 | 41 | ) |
— | — | @@ -41,10 +42,14 @@ |
42 | 43 | $this->filesToPrune = $this->pathsToPrune = array(); |
43 | 44 | } |
44 | 45 | |
45 | | - private function singleBasePath() { |
| 46 | + private function baseStorePath() { |
46 | 47 | return 'mwstore://localtesting'; |
47 | 48 | } |
48 | 49 | |
| 50 | + private function backendClass() { |
| 51 | + return get_class( $this->backend ); |
| 52 | + } |
| 53 | + |
49 | 54 | /** |
50 | 55 | * @dataProvider provider_testStore |
51 | 56 | */ |
— | — | @@ -52,29 +57,45 @@ |
53 | 58 | $this->filesToPrune[] = $source; |
54 | 59 | $this->pathsToPrune[] = $dest; |
55 | 60 | |
| 61 | + $this->backend = $this->singleBackend; |
| 62 | + $this->doTestStore( $op, $source, $dest ); |
| 63 | + $this->tearDownFiles(); |
| 64 | + |
| 65 | + $this->backend = $this->multiBackend; |
| 66 | + $this->doTestStore( $op, $source, $dest ); |
| 67 | + $this->tearDownFiles(); |
| 68 | + } |
| 69 | + |
| 70 | + function doTestStore( $op, $source, $dest ) { |
| 71 | + $backendName = $this->backendClass(); |
| 72 | + |
56 | 73 | file_put_contents( $source, "Unit test file" ); |
57 | 74 | $status = $this->backend->doOperation( $op ); |
58 | 75 | |
| 76 | + $this->assertEquals( array(), $status->errors, |
| 77 | + "Store from $source to $dest succeeded without warnings ($backendName)." ); |
59 | 78 | $this->assertEquals( true, $status->isOK(), |
60 | | - "Store from $source to $dest succeeded." ); |
61 | | - $this->assertEquals( true, $status->isGood(), |
62 | | - "Store from $source to $dest succeeded without warnings." ); |
| 79 | + "Store from $source to $dest succeeded ($backendName)." ); |
63 | 80 | $this->assertEquals( true, file_exists( $source ), |
64 | | - "Source file $source still exists." ); |
| 81 | + "Source file $source still exists ($backendName)." ); |
65 | 82 | $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ), |
66 | | - "Destination file $dest exists." ); |
| 83 | + "Destination file $dest exists ($backendName)." ); |
67 | 84 | |
| 85 | + $this->assertEquals( filesize( $source ), |
| 86 | + $this->backend->getFileSize( array( 'src' => $dest ) ), |
| 87 | + "Destination file $dest has correct size ($backendName)." ); |
| 88 | + |
68 | 89 | $props1 = FSFile::getPropsFromPath( $source ); |
69 | 90 | $props2 = $this->backend->getFileProps( array( 'src' => $dest ) ); |
70 | 91 | $this->assertEquals( $props1, $props2, |
71 | | - "Source and destination have the same props." ); |
| 92 | + "Source and destination have the same props ($backendName)." ); |
72 | 93 | } |
73 | 94 | |
74 | 95 | public function provider_testStore() { |
75 | 96 | $cases = array(); |
76 | 97 | |
77 | 98 | $tmpName = TempFSFile::factory( "unittests_", 'txt' )->getPath(); |
78 | | - $toPath = $this->singleBasePath() . '/cont1/fun/obj1.txt'; |
| 99 | + $toPath = $this->baseStorePath() . '/cont1/fun/obj1.txt'; |
79 | 100 | $op = array( 'op' => 'store', 'src' => $tmpName, 'dst' => $toPath ); |
80 | 101 | $cases[] = array( |
81 | 102 | $op, // operation |
— | — | @@ -99,31 +120,49 @@ |
100 | 121 | $this->pathsToPrune[] = $source; |
101 | 122 | $this->pathsToPrune[] = $dest; |
102 | 123 | |
| 124 | + $this->backend = $this->singleBackend; |
| 125 | + $this->doTestCopy( $op, $source, $dest ); |
| 126 | + $this->tearDownFiles(); |
| 127 | + |
| 128 | + $this->backend = $this->multiBackend; |
| 129 | + $this->doTestCopy( $op, $source, $dest ); |
| 130 | + $this->tearDownFiles(); |
| 131 | + } |
| 132 | + |
| 133 | + function doTestCopy( $op, $source, $dest ) { |
| 134 | + $backendName = $this->backendClass(); |
| 135 | + |
103 | 136 | $status = $this->backend->doOperation( |
104 | 137 | array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) ); |
105 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." ); |
| 138 | + $this->assertEquals( true, $status->isOK(), |
| 139 | + "Creation of file at $source succeeded ($backendName)." ); |
106 | 140 | |
107 | 141 | $status = $this->backend->doOperation( $op ); |
| 142 | + $this->assertEquals( array(), $status->errors, |
| 143 | + "Copy from $source to $dest succeeded without warnings ($backendName)." ); |
108 | 144 | $this->assertEquals( true, $status->isOK(), |
109 | | - "Copy from $source to $dest succeeded." ); |
110 | | - $this->assertEquals( true, $status->isGood(), |
111 | | - "Copy from $source to $dest succeeded without warnings." ); |
| 145 | + "Copy from $source to $dest succeeded ($backendName)." ); |
112 | 146 | $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $source ) ), |
113 | | - "Source file $source still exists." ); |
| 147 | + "Source file $source still exists ($backendName)." ); |
114 | 148 | $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ), |
115 | | - "Destination file $dest exists after copy." ); |
| 149 | + "Destination file $dest exists after copy ($backendName)." ); |
116 | 150 | |
| 151 | + $this->assertEquals( |
| 152 | + $this->backend->getFileSize( array( 'src' => $source ) ), |
| 153 | + $this->backend->getFileSize( array( 'src' => $dest ) ), |
| 154 | + "Destination file $dest has correct size ($backendName)." ); |
| 155 | + |
117 | 156 | $props1 = $this->backend->getFileProps( array( 'src' => $source ) ); |
118 | 157 | $props2 = $this->backend->getFileProps( array( 'src' => $dest ) ); |
119 | 158 | $this->assertEquals( $props1, $props2, |
120 | | - "Source and destination have the same props." ); |
| 159 | + "Source and destination have the same props ($backendName)." ); |
121 | 160 | } |
122 | 161 | |
123 | 162 | public function provider_testCopy() { |
124 | 163 | $cases = array(); |
125 | 164 | |
126 | | - $source = $this->singleBasePath() . '/cont1/file.txt'; |
127 | | - $dest = $this->singleBasePath() . '/cont2/fileMoved.txt'; |
| 165 | + $source = $this->baseStorePath() . '/cont1/file.txt'; |
| 166 | + $dest = $this->baseStorePath() . '/cont2/fileMoved.txt'; |
128 | 167 | |
129 | 168 | $op = array( 'op' => 'copy', 'src' => $source, 'dst' => $dest ); |
130 | 169 | $cases[] = array( |
— | — | @@ -149,33 +188,51 @@ |
150 | 189 | $this->pathsToPrune[] = $source; |
151 | 190 | $this->pathsToPrune[] = $dest; |
152 | 191 | |
| 192 | + $this->backend = $this->singleBackend; |
| 193 | + $this->doTestMove( $op, $source, $dest ); |
| 194 | + $this->tearDownFiles(); |
| 195 | + |
| 196 | + $this->backend = $this->multiBackend; |
| 197 | + $this->doTestMove( $op, $source, $dest ); |
| 198 | + $this->tearDownFiles(); |
| 199 | + } |
| 200 | + |
| 201 | + public function doTestMove( $op, $source, $dest ) { |
| 202 | + $backendName = $this->backendClass(); |
| 203 | + |
153 | 204 | $status = $this->backend->doOperation( |
154 | 205 | array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) ); |
155 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." ); |
| 206 | + $this->assertEquals( true, $status->isOK(), |
| 207 | + "Creation of file at $source succeeded ($backendName)." ); |
156 | 208 | |
157 | 209 | $status = $this->backend->doOperation( $op ); |
| 210 | + $this->assertEquals( array(), $status->errors, |
| 211 | + "Move from $source to $dest succeeded without warnings ($backendName)." ); |
158 | 212 | $this->assertEquals( true, $status->isOK(), |
159 | | - "Move from $source to $dest succeeded." ); |
160 | | - $this->assertEquals( true, $status->isGood(), |
161 | | - "Move from $source to $dest succeeded without warnings." ); |
| 213 | + "Move from $source to $dest succeeded ($backendName)." ); |
162 | 214 | $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ), |
163 | | - "Source file $source does not still exists." ); |
| 215 | + "Source file $source does not still exists ($backendName)." ); |
164 | 216 | $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ), |
165 | | - "Destination file $dest exists after move." ); |
| 217 | + "Destination file $dest exists after move ($backendName)." ); |
166 | 218 | |
| 219 | + $this->assertNotEquals( |
| 220 | + $this->backend->getFileSize( array( 'src' => $source ) ), |
| 221 | + $this->backend->getFileSize( array( 'src' => $dest ) ), |
| 222 | + "Destination file $dest has correct size ($backendName)." ); |
| 223 | + |
167 | 224 | $props1 = $this->backend->getFileProps( array( 'src' => $source ) ); |
168 | 225 | $props2 = $this->backend->getFileProps( array( 'src' => $dest ) ); |
169 | 226 | $this->assertEquals( false, $props1['fileExists'], |
170 | | - "Source file does not exist accourding to props." ); |
| 227 | + "Source file does not exist accourding to props ($backendName)." ); |
171 | 228 | $this->assertEquals( true, $props2['fileExists'], |
172 | | - "Destination file exists accourding to props." ); |
| 229 | + "Destination file exists accourding to props ($backendName)." ); |
173 | 230 | } |
174 | 231 | |
175 | 232 | public function provider_testMove() { |
176 | 233 | $cases = array(); |
177 | 234 | |
178 | | - $source = $this->singleBasePath() . '/cont1/file.txt'; |
179 | | - $dest = $this->singleBasePath() . '/cont2/fileMoved.txt'; |
| 235 | + $source = $this->baseStorePath() . '/cont1/file.txt'; |
| 236 | + $dest = $this->baseStorePath() . '/cont2/fileMoved.txt'; |
180 | 237 | |
181 | 238 | $op = array( 'op' => 'move', 'src' => $source, 'dst' => $dest ); |
182 | 239 | $cases[] = array( |
— | — | @@ -200,31 +257,52 @@ |
201 | 258 | public function testDelete( $op, $source, $withSource, $okStatus ) { |
202 | 259 | $this->pathsToPrune[] = $source; |
203 | 260 | |
| 261 | + $this->backend = $this->singleBackend; |
| 262 | + $this->doTestDelete( $op, $source, $withSource, $okStatus ); |
| 263 | + $this->tearDownFiles(); |
| 264 | + |
| 265 | + $this->backend = $this->multiBackend; |
| 266 | + $this->doTestDelete( $op, $source, $withSource, $okStatus ); |
| 267 | + $this->tearDownFiles(); |
| 268 | + } |
| 269 | + |
| 270 | + public function doTestDelete( $op, $source, $withSource, $okStatus ) { |
| 271 | + $backendName = $this->backendClass(); |
| 272 | + |
204 | 273 | if ( $withSource ) { |
205 | 274 | $status = $this->backend->doOperation( |
206 | 275 | array( 'op' => 'create', 'content' => 'blahblah', 'dst' => $source ) ); |
207 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $source succeeded." ); |
| 276 | + $this->assertEquals( true, $status->isOK(), |
| 277 | + "Creation of file at $source succeeded ($backendName)." ); |
208 | 278 | } |
209 | 279 | |
210 | 280 | $status = $this->backend->doOperation( $op ); |
211 | 281 | if ( $okStatus ) { |
212 | | - $this->assertEquals( true, $status->isOK(), "Deletion of file at $source succeeded." ); |
| 282 | + $this->assertEquals( array(), $status->errors, |
| 283 | + "Deletion of file at $source succeeded without warnings ($backendName)." ); |
| 284 | + $this->assertEquals( true, $status->isOK(), |
| 285 | + "Deletion of file at $source succeeded ($backendName)." ); |
213 | 286 | } else { |
214 | | - $this->assertEquals( false, $status->isOK(), "Deletion of file at $source failed." ); |
| 287 | + $this->assertEquals( false, $status->isOK(), |
| 288 | + "Deletion of file at $source failed ($backendName)." ); |
215 | 289 | } |
216 | 290 | |
217 | 291 | $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $source ) ), |
218 | | - "Source file $source does not exist after move." ); |
| 292 | + "Source file $source does not exist after move ($backendName)." ); |
219 | 293 | |
| 294 | + $this->assertFalse( |
| 295 | + $this->backend->getFileSize( array( 'src' => $source ) ), |
| 296 | + "Source file $source has correct size (false) ($backendName)." ); |
| 297 | + |
220 | 298 | $props1 = $this->backend->getFileProps( array( 'src' => $source ) ); |
221 | | - $this->assertEquals( false, $props1['fileExists'], |
222 | | - "Source file $source does not exist according to props." ); |
| 299 | + $this->assertFalse( $props1['fileExists'], |
| 300 | + "Source file $source does not exist according to props ($backendName)." ); |
223 | 301 | } |
224 | 302 | |
225 | 303 | public function provider_testDelete() { |
226 | 304 | $cases = array(); |
227 | 305 | |
228 | | - $source = $this->singleBasePath() . '/cont1/myfacefile.txt'; |
| 306 | + $source = $this->baseStorePath() . '/cont1/myfacefile.txt'; |
229 | 307 | |
230 | 308 | $op = array( 'op' => 'delete', 'src' => $source ); |
231 | 309 | $cases[] = array( |
— | — | @@ -258,32 +336,55 @@ |
259 | 337 | public function testCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) { |
260 | 338 | $this->pathsToPrune[] = $dest; |
261 | 339 | |
| 340 | + $this->backend = $this->singleBackend; |
| 341 | + $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ); |
| 342 | + $this->tearDownFiles(); |
| 343 | + |
| 344 | + $this->backend = $this->multiBackend; |
| 345 | + $this->doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ); |
| 346 | + $this->tearDownFiles(); |
| 347 | + } |
| 348 | + |
| 349 | + public function doTestCreate( $op, $dest, $alreadyExists, $okStatus, $newSize ) { |
| 350 | + $backendName = $this->backendClass(); |
| 351 | + |
262 | 352 | $oldText = 'blah...blah...waahwaah'; |
263 | 353 | if ( $alreadyExists ) { |
264 | 354 | $status = $this->backend->doOperation( |
265 | 355 | array( 'op' => 'create', 'content' => $oldText, 'dst' => $dest ) ); |
266 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." ); |
| 356 | + $this->assertEquals( true, $status->isOK(), |
| 357 | + "Creation of file at $dest succeeded ($backendName)." ); |
267 | 358 | } |
268 | 359 | |
269 | 360 | $status = $this->backend->doOperation( $op ); |
270 | 361 | if ( $okStatus ) { |
271 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $dest succeeded." ); |
| 362 | + $this->assertEquals( array(), $status->errors, |
| 363 | + "Creation of file at $dest succeeded without warnings ($backendName)." ); |
| 364 | + $this->assertEquals( true, $status->isOK(), |
| 365 | + "Creation of file at $dest succeeded ($backendName)." ); |
272 | 366 | } else { |
273 | | - $this->assertEquals( false, $status->isOK(), "Creation of file at $dest failed." ); |
| 367 | + $this->assertEquals( false, $status->isOK(), |
| 368 | + "Creation of file at $dest failed ($backendName)." ); |
274 | 369 | } |
275 | 370 | |
276 | 371 | $this->assertEquals( true, $this->backend->fileExists( array( 'src' => $dest ) ), |
277 | | - "Dest file $dest exists after creation." ); |
| 372 | + "Destination file $dest exists after creation ($backendName)." ); |
278 | 373 | |
279 | 374 | $props1 = $this->backend->getFileProps( array( 'src' => $dest ) ); |
280 | 375 | $this->assertEquals( true, $props1['fileExists'], |
281 | | - "Dest file $dest exists according to props." ); |
| 376 | + "Destination file $dest exists according to props ($backendName)." ); |
282 | 377 | if ( $okStatus ) { // file content is what we saved |
283 | 378 | $this->assertEquals( $newSize, $props1['size'], |
284 | | - "Dest file $dest has expected size according to props." ); |
| 379 | + "Destination file $dest has expected size according to props ($backendName)." ); |
| 380 | + $this->assertEquals( $newSize, |
| 381 | + $this->backend->getFileSize( array( 'src' => $dest ) ), |
| 382 | + "Destination file $dest has correct size ($backendName)." ); |
285 | 383 | } else { // file content is some other previous text |
286 | 384 | $this->assertEquals( strlen( $oldText ), $props1['size'], |
287 | | - "Dest file $dest has different size that given text according to props." ); |
| 385 | + "Destination file $dest has original size according to props ($backendName)." ); |
| 386 | + $this->assertEquals( strlen( $oldText ), |
| 387 | + $this->backend->getFileSize( array( 'src' => $dest ) ), |
| 388 | + "Destination file $dest has original size according to props ($backendName)." ); |
288 | 389 | } |
289 | 390 | } |
290 | 391 | |
— | — | @@ -293,7 +394,7 @@ |
294 | 395 | public function provider_testCreate() { |
295 | 396 | $cases = array(); |
296 | 397 | |
297 | | - $source = $this->singleBasePath() . '/cont2/myspacefile.txt'; |
| 398 | + $source = $this->baseStorePath() . '/cont2/myspacefile.txt'; |
298 | 399 | |
299 | 400 | $dummyText = 'hey hey'; |
300 | 401 | $op = array( 'op' => 'create', 'content' => $dummyText, 'dst' => $source ); |
— | — | @@ -330,8 +431,21 @@ |
331 | 432 | */ |
332 | 433 | public function testConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) { |
333 | 434 | $this->pathsToPrune = array_merge( $this->pathsToPrune, $srcs ); |
334 | | - $this->pathsToPrune[] = $op['dst']; |
| 435 | + $this->filesToPrune[] = $op['dst']; |
335 | 436 | |
| 437 | + $this->backend = $this->singleBackend; |
| 438 | + $this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ); |
| 439 | + $this->tearDownFiles(); |
| 440 | + |
| 441 | + # FIXME |
| 442 | + #$this->backend = $this->multiBackend; |
| 443 | + #$this->doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ); |
| 444 | + #$this->tearDownFiles(); |
| 445 | + } |
| 446 | + |
| 447 | + public function doTestConcatenate( $op, $srcs, $srcsContent, $alreadyExists, $okStatus ) { |
| 448 | + $backendName = $this->backendClass(); |
| 449 | + |
336 | 450 | $expContent = ''; |
337 | 451 | // Create sources |
338 | 452 | $ops = array(); |
— | — | @@ -345,40 +459,49 @@ |
346 | 460 | } |
347 | 461 | $status = $this->backend->doOperations( $ops ); |
348 | 462 | |
349 | | - $this->assertEquals( true, $status->isOK(), "Creation of source files succeeded." ); |
| 463 | + $this->assertEquals( true, $status->isOK(), |
| 464 | + "Creation of source files succeeded ($backendName)." ); |
350 | 465 | |
351 | 466 | $dest = $op['dst']; |
352 | 467 | if ( $alreadyExists ) { |
353 | 468 | $ok = file_put_contents( $dest, 'blah...blah...waahwaah' ) !== false; |
354 | | - $this->assertEquals( true, $ok, "Creation of file at $dest succeeded." ); |
| 469 | + $this->assertEquals( true, $ok, |
| 470 | + "Creation of file at $dest succeeded ($backendName)." ); |
355 | 471 | } else { |
356 | 472 | $ok = file_put_contents( $dest, '' ) !== false; |
357 | | - $this->assertEquals( true, $ok, "Creation of 0-byte file at $dest succeeded." ); |
| 473 | + $this->assertEquals( true, $ok, |
| 474 | + "Creation of 0-byte file at $dest succeeded ($backendName)." ); |
358 | 475 | } |
359 | 476 | |
360 | 477 | // Combine them |
361 | 478 | $status = $this->backend->doOperation( $op ); |
362 | 479 | if ( $okStatus ) { |
363 | | - $this->assertEquals( true, $status->isOK(), "Creation of concat file at $dest succeeded." ); |
| 480 | + $this->assertEquals( array(), $status->errors, |
| 481 | + "Creation of concat file at $dest succeeded without warnings ($backendName)." ); |
| 482 | + $this->assertEquals( true, $status->isOK(), |
| 483 | + "Creation of concat file at $dest succeeded ($backendName)." ); |
364 | 484 | } else { |
365 | | - $this->assertEquals( false, $status->isOK(), "Creation of concat file at $dest failed." ); |
| 485 | + $this->assertEquals( false, $status->isOK(), |
| 486 | + "Creation of concat file at $dest failed ($backendName)." ); |
366 | 487 | } |
367 | 488 | |
368 | 489 | if ( $okStatus ) { |
369 | 490 | $this->assertEquals( true, is_file( $dest ), |
370 | | - "Dest concat file $dest exists after creation." ); |
| 491 | + "Dest concat file $dest exists after creation ($backendName)." ); |
371 | 492 | } else { |
372 | 493 | $this->assertEquals( true, is_file( $dest ), |
373 | | - "Dest concat file $dest exists after failed creation." ); |
| 494 | + "Dest concat file $dest exists after failed creation ($backendName)." ); |
374 | 495 | } |
375 | 496 | |
376 | 497 | $contents = file_get_contents( $dest ); |
377 | | - $this->assertNotEquals( false, $contents, "File at $dest exists." ); |
| 498 | + $this->assertNotEquals( false, $contents, "File at $dest exists ($backendName)." ); |
378 | 499 | |
379 | 500 | if ( $okStatus ) { |
380 | | - $this->assertEquals( $expContent, $contents, "Concat file at $dest has correct contents." ); |
| 501 | + $this->assertEquals( $expContent, $contents, |
| 502 | + "Concat file at $dest has correct contents ($backendName)." ); |
381 | 503 | } else { |
382 | | - $this->assertNotEquals( $expContent, $contents, "Concat file at $dest has correct contents." ); |
| 504 | + $this->assertNotEquals( $expContent, $contents, |
| 505 | + "Concat file at $dest has correct contents ($backendName)." ); |
383 | 506 | } |
384 | 507 | } |
385 | 508 | |
— | — | @@ -388,16 +511,16 @@ |
389 | 512 | $rand = mt_rand( 0, 2000000000 ) . time(); |
390 | 513 | $dest = wfTempDir() . "/randomfile!$rand.txt"; |
391 | 514 | $srcs = array( |
392 | | - $this->singleBasePath() . '/cont1/file1.txt', |
393 | | - $this->singleBasePath() . '/cont1/file2.txt', |
394 | | - $this->singleBasePath() . '/cont1/file3.txt', |
395 | | - $this->singleBasePath() . '/cont1/file4.txt', |
396 | | - $this->singleBasePath() . '/cont1/file5.txt', |
397 | | - $this->singleBasePath() . '/cont1/file6.txt', |
398 | | - $this->singleBasePath() . '/cont1/file7.txt', |
399 | | - $this->singleBasePath() . '/cont1/file8.txt', |
400 | | - $this->singleBasePath() . '/cont1/file9.txt', |
401 | | - $this->singleBasePath() . '/cont1/file10.txt' |
| 515 | + $this->baseStorePath() . '/cont1/file1.txt', |
| 516 | + $this->baseStorePath() . '/cont1/file2.txt', |
| 517 | + $this->baseStorePath() . '/cont1/file3.txt', |
| 518 | + $this->baseStorePath() . '/cont1/file4.txt', |
| 519 | + $this->baseStorePath() . '/cont1/file5.txt', |
| 520 | + $this->baseStorePath() . '/cont1/file6.txt', |
| 521 | + $this->baseStorePath() . '/cont1/file7.txt', |
| 522 | + $this->baseStorePath() . '/cont1/file8.txt', |
| 523 | + $this->baseStorePath() . '/cont1/file9.txt', |
| 524 | + $this->baseStorePath() . '/cont1/file10.txt' |
402 | 525 | ); |
403 | 526 | $content = array( |
404 | 527 | 'egfage', |
— | — | @@ -425,7 +548,7 @@ |
426 | 549 | $op, // operation |
427 | 550 | $srcs, // sources |
428 | 551 | $content, // content for each source |
429 | | - true, // no dest already exists |
| 552 | + true, // dest already exists |
430 | 553 | false, // succeeds |
431 | 554 | ); |
432 | 555 | |
— | — | @@ -438,20 +561,38 @@ |
439 | 562 | public function testGetFileContents( $src, $content ) { |
440 | 563 | $this->pathsToPrune[] = $src; |
441 | 564 | |
| 565 | + $this->backend = $this->singleBackend; |
| 566 | + $this->doTestGetFileContents( $src, $content ); |
| 567 | + $this->tearDownFiles(); |
| 568 | + |
| 569 | + $this->backend = $this->multiBackend; |
| 570 | + $this->doTestGetFileContents( $src, $content ); |
| 571 | + $this->tearDownFiles(); |
| 572 | + } |
| 573 | + |
| 574 | + /** |
| 575 | + * @dataProvider provider_testGetFileContents |
| 576 | + */ |
| 577 | + public function doTestGetFileContents( $src, $content ) { |
| 578 | + $backendName = $this->backendClass(); |
| 579 | + |
442 | 580 | $status = $this->backend->doOperation( |
443 | 581 | array( 'op' => 'create', 'content' => $content, 'dst' => $src ) ); |
444 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." ); |
| 582 | + $this->assertEquals( true, $status->isOK(), |
| 583 | + "Creation of file at $src succeeded ($backendName)." ); |
445 | 584 | |
446 | 585 | $newContents = $this->backend->getFileContents( array( 'src' => $src ) ); |
447 | | - $this->assertNotEquals( false, $newContents, "Read of file at $src succeeded." ); |
| 586 | + $this->assertNotEquals( false, $newContents, |
| 587 | + "Read of file at $src succeeded ($backendName)." ); |
448 | 588 | |
449 | | - $this->assertEquals( $content, $newContents, "Contents read match data at $src." ); |
| 589 | + $this->assertEquals( $content, $newContents, |
| 590 | + "Contents read match data at $src ($backendName)." ); |
450 | 591 | } |
451 | 592 | |
452 | 593 | function provider_testGetFileContents() { |
453 | 594 | $cases = array(); |
454 | 595 | |
455 | | - $base = $this->singleBasePath(); |
| 596 | + $base = $this->baseStorePath(); |
456 | 597 | $cases[] = array( "$base/cont1/b/z/some_file.txt", "some file contents" ); |
457 | 598 | $cases[] = array( "$base/cont1/b/some-other_file.txt", "more file contents" ); |
458 | 599 | |
— | — | @@ -464,21 +605,35 @@ |
465 | 606 | public function testGetLocalCopy( $src, $content ) { |
466 | 607 | $this->pathsToPrune[] = $src; |
467 | 608 | |
| 609 | + $this->backend = $this->singleBackend; |
| 610 | + $this->doTestGetLocalCopy( $src, $content ); |
| 611 | + $this->tearDownFiles(); |
| 612 | + |
| 613 | + $this->backend = $this->multiBackend; |
| 614 | + $this->doTestGetLocalCopy( $src, $content ); |
| 615 | + $this->tearDownFiles(); |
| 616 | + } |
| 617 | + |
| 618 | + public function doTestGetLocalCopy( $src, $content ) { |
| 619 | + $backendName = $this->backendClass(); |
| 620 | + |
468 | 621 | $status = $this->backend->doOperation( |
469 | 622 | array( 'op' => 'create', 'content' => $content, 'dst' => $src ) ); |
470 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." ); |
| 623 | + $this->assertEquals( true, $status->isOK(), |
| 624 | + "Creation of file at $src succeeded ($backendName)." ); |
471 | 625 | |
472 | 626 | $tmpFile = $this->backend->getLocalCopy( array( 'src' => $src ) ); |
473 | | - $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." ); |
| 627 | + $this->assertNotNull( $tmpFile, |
| 628 | + "Creation of local copy of $src succeeded ($backendName)." ); |
474 | 629 | |
475 | 630 | $contents = file_get_contents( $tmpFile->getPath() ); |
476 | | - $this->assertNotEquals( false, $contents, "Local copy of $src exists." ); |
| 631 | + $this->assertNotEquals( false, $contents, "Local copy of $src exists ($backendName)." ); |
477 | 632 | } |
478 | 633 | |
479 | 634 | function provider_testGetLocalCopy() { |
480 | 635 | $cases = array(); |
481 | 636 | |
482 | | - $base = $this->singleBasePath(); |
| 637 | + $base = $this->baseStorePath(); |
483 | 638 | $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" ); |
484 | 639 | $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" ); |
485 | 640 | |
— | — | @@ -491,21 +646,35 @@ |
492 | 647 | public function testGetLocalReference( $src, $content ) { |
493 | 648 | $this->pathsToPrune[] = $src; |
494 | 649 | |
| 650 | + $this->backend = $this->singleBackend; |
| 651 | + $this->doTestGetLocalReference( $src, $content ); |
| 652 | + $this->tearDownFiles(); |
| 653 | + |
| 654 | + $this->backend = $this->multiBackend; |
| 655 | + $this->doTestGetLocalReference( $src, $content ); |
| 656 | + $this->tearDownFiles(); |
| 657 | + } |
| 658 | + |
| 659 | + public function doTestGetLocalReference( $src, $content ) { |
| 660 | + $backendName = $this->backendClass(); |
| 661 | + |
495 | 662 | $status = $this->backend->doOperation( |
496 | 663 | array( 'op' => 'create', 'content' => $content, 'dst' => $src ) ); |
497 | | - $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." ); |
| 664 | + $this->assertEquals( true, $status->isOK(), |
| 665 | + "Creation of file at $src succeeded ($backendName)." ); |
498 | 666 | |
499 | 667 | $tmpFile = $this->backend->getLocalReference( array( 'src' => $src ) ); |
500 | | - $this->assertNotNull( $tmpFile, "Creation of local copy of $src succeeded." ); |
| 668 | + $this->assertNotNull( $tmpFile, |
| 669 | + "Creation of local copy of $src succeeded ($backendName)." ); |
501 | 670 | |
502 | 671 | $contents = file_get_contents( $tmpFile->getPath() ); |
503 | | - $this->assertNotEquals( false, $contents, "Local copy of $src exists." ); |
| 672 | + $this->assertNotEquals( false, $contents, "Local copy of $src exists ($backendName)." ); |
504 | 673 | } |
505 | 674 | |
506 | 675 | function provider_testGetLocalReference() { |
507 | 676 | $cases = array(); |
508 | 677 | |
509 | | - $base = $this->singleBasePath(); |
| 678 | + $base = $this->baseStorePath(); |
510 | 679 | $cases[] = array( "$base/cont1/a/z/some_file.txt", "some file contents" ); |
511 | 680 | $cases[] = array( "$base/cont1/a/some-other_file.txt", "more file contents" ); |
512 | 681 | |
— | — | @@ -521,7 +690,19 @@ |
522 | 691 | // @TODO: testDoOperations |
523 | 692 | |
524 | 693 | public function testGetFileList() { |
525 | | - $base = $this->singleBasePath(); |
| 694 | + $this->backend = $this->singleBackend; |
| 695 | + $this->doTestGetFileList(); |
| 696 | + $this->tearDownFiles(); |
| 697 | + |
| 698 | + $this->backend = $this->multiBackend; |
| 699 | + $this->doTestGetFileList(); |
| 700 | + $this->tearDownFiles(); |
| 701 | + } |
| 702 | + |
| 703 | + public function doTestGetFileList() { |
| 704 | + $backendName = $this->backendClass(); |
| 705 | + |
| 706 | + $base = $this->baseStorePath(); |
526 | 707 | $files = array( |
527 | 708 | "$base/cont1/test1.txt", |
528 | 709 | "$base/cont1/test2.txt", |
— | — | @@ -546,7 +727,8 @@ |
547 | 728 | $ops[] = array( 'op' => 'create', 'content' => 'xxy', 'dst' => $file ); |
548 | 729 | } |
549 | 730 | $status = $this->backend->doOperations( $ops ); |
550 | | - $this->assertEquals( true, $status->isOK(), "Creation of files succeeded." ); |
| 731 | + $this->assertEquals( true, $status->isOK(), |
| 732 | + "Creation of files succeeded ($backendName)." ); |
551 | 733 | |
552 | 734 | // Expected listing |
553 | 735 | $expected = array( |
— | — | @@ -575,7 +757,7 @@ |
576 | 758 | } |
577 | 759 | sort( $list ); |
578 | 760 | |
579 | | - $this->assertEquals( $expected, $list, "Correct file listing." ); |
| 761 | + $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." ); |
580 | 762 | |
581 | 763 | // Actual listing (with trailing slash) |
582 | 764 | $list = array(); |
— | — | @@ -585,7 +767,7 @@ |
586 | 768 | } |
587 | 769 | sort( $list ); |
588 | 770 | |
589 | | - $this->assertEquals( $expected, $list, "Correct file listing." ); |
| 771 | + $this->assertEquals( $expected, $list, "Correct file listing ($backendName)." ); |
590 | 772 | |
591 | 773 | foreach ( $files as $file ) { |
592 | 774 | $this->backend->doOperation( array( 'op' => 'delete', 'src' => "$base/$file" ) ); |
— | — | @@ -595,16 +777,16 @@ |
596 | 778 | foreach ( $iter as $iter ) {} // no errors |
597 | 779 | } |
598 | 780 | |
599 | | - function tearDown() { |
600 | | - parent::tearDown(); |
| 781 | + function tearDownFiles() { |
601 | 782 | foreach ( $this->filesToPrune as $file ) { |
602 | 783 | @unlink( $file ); |
603 | 784 | } |
604 | 785 | foreach ( $this->pathsToPrune as $file ) { |
605 | 786 | $this->backend->doOperation( array( 'op' => 'delete', 'src' => $file ) ); |
606 | | - $this->multiBackend->doOperation( array( 'op' => 'delete', 'src' => $file ) ); |
607 | 787 | } |
608 | | - $this->backend = $this->multiBackend = null; |
609 | | - $this->filesToPrune = $this->pathsToPrune = array(); |
610 | 788 | } |
| 789 | + |
| 790 | + function tearDown() { |
| 791 | + parent::tearDown(); |
| 792 | + } |
611 | 793 | } |
Index: trunk/phase3/includes/filerepo/file/File.php |
— | — | @@ -811,7 +811,7 @@ |
812 | 812 | // overriding File::getThumbPath() to use a different zone (e.g. 'temp'). |
813 | 813 | $status = $this->repo->getBackend()->store( |
814 | 814 | array( 'src' => $tmpThumbPath, 'dst' => $thumbPath ), |
815 | | - array( 'ignoreErrors' => 1, 'nonLocking' => 1, 'allowStale' => 1 ) |
| 815 | + array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1 ) |
816 | 816 | ); |
817 | 817 | if ( $status->isOK() ) { |
818 | 818 | $thumb->setStoragePath( $thumbPath ); |
Index: trunk/phase3/includes/filerepo/backend/FileBackendMultiWrite.php |
— | — | @@ -12,22 +12,16 @@ |
13 | 13 | * At least one of the backends must be declared the "master" backend. |
14 | 14 | * |
15 | 15 | * Only use this class when transitioning from one storage system to another. |
16 | | - * |
17 | | - * The order that the backends are defined sets the priority of which |
18 | | - * backend is read from or written to first. Functions like fileExists() |
19 | | - * and getFileProps() will return information based on the first backend |
20 | | - * that has the file. Special cases are listed below: |
21 | | - * a) getFileTimestamp() will always check only the master backend to |
22 | | - * avoid confusing and inconsistent results. |
23 | | - * |
24 | | - * All write operations are performed on all backends. |
| 16 | + * |
| 17 | + * Read operations are only done on the 'master' backend for consistency. |
| 18 | + * All write operations are performed on all backends, in the order defined. |
25 | 19 | * If an operation fails on one backend it will be rolled back from the others. |
26 | 20 | * |
27 | 21 | * @ingroup FileBackend |
28 | 22 | */ |
29 | 23 | class FileBackendMultiWrite extends FileBackendBase { |
30 | 24 | /** @var Array Prioritized list of FileBackend objects */ |
31 | | - protected $fileBackends = array(); // array of (backend index => backends) |
| 25 | + protected $backends = array(); // array of (backend index => backends) |
32 | 26 | protected $masterIndex = -1; // index of master backend |
33 | 27 | |
34 | 28 | /** |
— | — | @@ -49,7 +43,7 @@ |
50 | 44 | throw new MWException( 'No class given for a backend config.' ); |
51 | 45 | } |
52 | 46 | $class = $config['class']; |
53 | | - $this->fileBackends[$index] = new $class( $config ); |
| 47 | + $this->backends[$index] = new $class( $config ); |
54 | 48 | if ( !empty( $config['isMultiMaster'] ) ) { |
55 | 49 | if ( $this->masterIndex >= 0 ) { |
56 | 50 | throw new MWException( 'More than one master backend defined.' ); |
— | — | @@ -69,41 +63,52 @@ |
70 | 64 | $status = Status::newGood(); |
71 | 65 | |
72 | 66 | $performOps = array(); // list of FileOp objects |
73 | | - $filesLockEx = $filesLockSh = array(); // storage paths to lock |
| 67 | + $filesRead = $filesChanged = array(); // storage paths used |
74 | 68 | // Build up a list of FileOps. The list will have all the ops |
75 | 69 | // for one backend, then all the ops for the next, and so on. |
76 | 70 | // These batches of ops are all part of a continuous array. |
77 | | - // Also build up a list of files to lock... |
78 | | - foreach ( $this->fileBackends as $index => $backend ) { |
79 | | - $backendOps = $this->substOpPaths( $ops, $backend ); |
| 71 | + // Also build up a list of files read/changed... |
| 72 | + foreach ( $this->backends as $index => $backend ) { |
| 73 | + $backendOps = $this->substOpBatchPaths( $ops, $backend ); |
| 74 | + // Add on the operation batch for this backend |
80 | 75 | $performOps = array_merge( $performOps, $backend->getOperations( $backendOps ) ); |
81 | | - if ( $index == 0 && empty( $opts['nonLocking'] ) ) { |
82 | | - // Set "files to lock" from the first batch so we don't try to set all |
83 | | - // locks two or three times over (depending on the number of backends). |
84 | | - // A lock on one storage path is a lock on all the backends. |
| 76 | + if ( $index == 0 ) { // first batch |
| 77 | + // Get the files used for these operations. Each backend has a batch of |
| 78 | + // the same operations, so we only need to get them from the first batch. |
85 | 79 | foreach ( $performOps as $fileOp ) { |
86 | | - $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() ); |
87 | | - $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() ); |
| 80 | + $filesRead = array_merge( $filesRead, $fileOp->storagePathsRead() ); |
| 81 | + $filesChanged = array_merge( $filesChanged, $fileOp->storagePathsChanged() ); |
88 | 82 | } |
89 | | - // Optimization: if doing an EX lock anyway, don't also set an SH one |
90 | | - $filesLockSh = array_diff( $filesLockSh, $filesLockEx ); |
91 | | - // Lock the paths under the proxy backend's name |
92 | | - $this->unsubstPaths( $filesLockSh ); |
93 | | - $this->unsubstPaths( $filesLockEx ); |
| 83 | + // Get the paths under the proxy backend's name |
| 84 | + $this->unsubstPaths( $filesRead ); |
| 85 | + $this->unsubstPaths( $filesChanged ); |
94 | 86 | } |
95 | 87 | } |
96 | 88 | |
97 | 89 | // Try to lock those files for the scope of this function... |
98 | | - $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status ); |
99 | | - $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status ); |
100 | | - if ( !$status->isOK() ) { |
101 | | - return $status; // abort |
| 90 | + if ( empty( $opts['nonLocking'] ) ) { |
| 91 | + $filesLockSh = array_diff( $filesRead, $filesChanged ); // optimization |
| 92 | + $filesLockEx = $filesChanged; |
| 93 | + $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager::LOCK_UW, $status ); |
| 94 | + $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status ); |
| 95 | + if ( !$status->isOK() ) { |
| 96 | + return $status; // abort |
| 97 | + } |
102 | 98 | } |
103 | 99 | |
104 | 100 | // Clear any cache entries (after locks acquired) |
105 | | - foreach ( $this->fileBackends as $backend ) { |
| 101 | + foreach ( $this->backends as $backend ) { |
106 | 102 | $backend->clearCache(); |
107 | 103 | } |
| 104 | + |
| 105 | + // Do a consistency check to see if the backends agree |
| 106 | + if ( count( $this->backends ) > 1 ) { |
| 107 | + $status->merge( $this->consistencyCheck( array_merge( $filesRead, $filesChanged ) ) ); |
| 108 | + if ( !$status->isOK() ) { |
| 109 | + return $status; // abort |
| 110 | + } |
| 111 | + } |
| 112 | + |
108 | 113 | // Actually attempt the operation batch... |
109 | 114 | $status->merge( FileOp::attemptBatch( $performOps, $opts ) ); |
110 | 115 | |
— | — | @@ -111,18 +116,61 @@ |
112 | 117 | } |
113 | 118 | |
114 | 119 | /** |
| 120 | + * Check that a set of files are consistent across all internal backends |
| 121 | + * |
| 122 | + * @param $paths Array |
| 123 | + * @return Status |
| 124 | + */ |
| 125 | + public function consistencyCheck( array $paths ) { |
| 126 | + $status = Status::newGood(); |
| 127 | + |
| 128 | + $mBackend = $this->backends[$this->masterIndex]; |
| 129 | + foreach ( array_unique( $paths ) as $path ) { |
| 130 | + $params = array( 'src' => $path ); |
| 131 | + // Stat the file on the 'master' backend |
| 132 | + $mStat = $mBackend->getFileStat( $this->substOpPaths( $params, $mBackend ) ); |
| 133 | + // Check of all clone backends agree with the master... |
| 134 | + foreach ( $this->backends as $index => $cBackend ) { |
| 135 | + if ( $index === $this->masterIndex ) { |
| 136 | + continue; // master |
| 137 | + } |
| 138 | + $cStat = $cBackend->getFileStat( $this->substOpPaths( $params, $cBackend ) ); |
| 139 | + if ( $mStat ) { // file is in master |
| 140 | + if ( !$cStat ) { // file should exist |
| 141 | + $status->fatal( 'backend-fail-synced', $path ); |
| 142 | + } elseif ( $cStat['size'] != $mStat['size'] ) { // wrong size |
| 143 | + $status->fatal( 'backend-fail-synced', $path ); |
| 144 | + } else { |
| 145 | + $mTs = wfTimestamp( TS_UNIX, $mStat['mtime'] ); |
| 146 | + $cTs = wfTimestamp( TS_UNIX, $cStat['mtime'] ); |
| 147 | + if ( abs( $mTs - $cTs ) > 30 ) { // outdated file somewhere |
| 148 | + $status->fatal( 'backend-fail-synced', $path ); |
| 149 | + } |
| 150 | + } |
| 151 | + } else { // file is not in master |
| 152 | + if ( $cStat ) { // file should not exist |
| 153 | + $status->fatal( 'backend-fail-synced', $path ); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return $status; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
115 | 163 | * Substitute the backend name in storage path parameters |
116 | | - * for a set of operations with a that of a given backend. |
| 164 | + * for a set of operations with that of a given backend. |
117 | 165 | * |
118 | 166 | * @param $ops Array List of file operation arrays |
119 | 167 | * @param $backend FileBackend |
120 | 168 | * @return Array |
121 | 169 | */ |
122 | | - protected function substOpPaths( array $ops, FileBackend $backend ) { |
| 170 | + protected function substOpBatchPaths( array $ops, FileBackend $backend ) { |
123 | 171 | $newOps = array(); // operations |
124 | 172 | foreach ( $ops as $op ) { |
125 | 173 | $newOp = $op; // operation |
126 | | - foreach ( array( 'src', 'srcs', 'dst' ) as $par ) { |
| 174 | + foreach ( array( 'src', 'srcs', 'dst', 'dir' ) as $par ) { |
127 | 175 | if ( isset( $newOp[$par] ) ) { |
128 | 176 | $newOp[$par] = preg_replace( |
129 | 177 | '!^mwstore://' . preg_quote( $this->name ) . '/!', |
— | — | @@ -137,6 +185,18 @@ |
138 | 186 | } |
139 | 187 | |
140 | 188 | /** |
| 189 | + * Same as substOpBatchPaths() but for a single operation |
| 190 | + * |
| 191 | + * @param $op File operation array |
| 192 | + * @param $backend FileBackend |
| 193 | + * @return Array |
| 194 | + */ |
| 195 | + protected function substOpPaths( array $ops, FileBackend $backend ) { |
| 196 | + $newOps = $this->substOpBatchPaths( array( $ops ), $backend ); |
| 197 | + return $newOps[0]; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
141 | 201 | * Replace the backend part of storage paths with this backend's name |
142 | 202 | * |
143 | 203 | * @param &$paths Array |
— | — | @@ -151,7 +211,7 @@ |
152 | 212 | /** |
153 | 213 | * @see FileBackendBase::prepare() |
154 | 214 | */ |
155 | | - function prepare( array $params ) { |
| 215 | + public function prepare( array $params ) { |
156 | 216 | $status = Status::newGood(); |
157 | 217 | foreach ( $this->backends as $backend ) { |
158 | 218 | $realParams = $this->substOpPaths( $params, $backend ); |
— | — | @@ -163,7 +223,7 @@ |
164 | 224 | /** |
165 | 225 | * @see FileBackendBase::secure() |
166 | 226 | */ |
167 | | - function secure( array $params ) { |
| 227 | + public function secure( array $params ) { |
168 | 228 | $status = Status::newGood(); |
169 | 229 | foreach ( $this->backends as $backend ) { |
170 | 230 | $realParams = $this->substOpPaths( $params, $backend ); |
— | — | @@ -175,7 +235,7 @@ |
176 | 236 | /** |
177 | 237 | * @see FileBackendBase::clean() |
178 | 238 | */ |
179 | | - function clean( array $params ) { |
| 239 | + public function clean( array $params ) { |
180 | 240 | $status = Status::newGood(); |
181 | 241 | foreach ( $this->backends as $backend ) { |
182 | 242 | $realParams = $this->substOpPaths( $params, $backend ); |
— | — | @@ -187,134 +247,88 @@ |
188 | 248 | /** |
189 | 249 | * @see FileBackendBase::fileExists() |
190 | 250 | */ |
191 | | - function fileExists( array $params ) { |
192 | | - # Hit all backends in case of failed operations (out of sync) |
193 | | - foreach ( $this->backends as $backend ) { |
194 | | - $realParams = $this->substOpPaths( $params, $backend ); |
195 | | - if ( $backend->fileExists( $realParams ) ) { |
196 | | - return true; |
197 | | - } |
198 | | - } |
199 | | - return false; |
| 251 | + public function fileExists( array $params ) { |
| 252 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 253 | + return $this->backends[$this->masterIndex]->fileExists( $realParams ); |
200 | 254 | } |
201 | 255 | |
202 | 256 | /** |
203 | 257 | * @see FileBackendBase::getFileTimestamp() |
204 | 258 | */ |
205 | | - function getFileTimestamp( array $params ) { |
206 | | - // Skip non-master for consistent timestamps |
| 259 | + public function getFileTimestamp( array $params ) { |
207 | 260 | $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
208 | 261 | return $this->backends[$this->masterIndex]->getFileTimestamp( $realParams ); |
209 | 262 | } |
210 | 263 | |
211 | 264 | /** |
| 265 | + * @see FileBackendBase::getFileSize() |
| 266 | + */ |
| 267 | + public function getFileSize( array $params ) { |
| 268 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 269 | + return $this->backends[$this->masterIndex]->getFileSize( $realParams ); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * @see FileBackendBase::getFileStat() |
| 274 | + */ |
| 275 | + public function getFileStat( array $params ) { |
| 276 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 277 | + return $this->backends[$this->masterIndex]->getFileStat( $realParams ); |
| 278 | + } |
| 279 | + |
| 280 | + /** |
212 | 281 | * @see FileBackendBase::getFileContents() |
213 | 282 | */ |
214 | | - function getFileContents( array $params ) { |
215 | | - # Hit all backends in case of failed operations (out of sync) |
216 | | - foreach ( $this->backends as $backend ) { |
217 | | - $realParams = $this->substOpPaths( $params, $backend ); |
218 | | - $data = $backend->getFileContents( $realParams ); |
219 | | - if ( $data !== false ) { |
220 | | - return $data; |
221 | | - } |
222 | | - } |
223 | | - return false; |
| 283 | + public function getFileContents( array $params ) { |
| 284 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 285 | + return $this->backends[$this->masterIndex]->getFileContents( $realParams ); |
224 | 286 | } |
225 | 287 | |
226 | 288 | /** |
227 | 289 | * @see FileBackendBase::getFileSha1Base36() |
228 | 290 | */ |
229 | | - function getFileSha1Base36( array $params ) { |
230 | | - # Hit all backends in case of failed operations (out of sync) |
231 | | - foreach ( $this->backends as $backend ) { |
232 | | - $realParams = $this->substOpPaths( $params, $backend ); |
233 | | - $hash = $backend->getFileSha1Base36( $realParams ); |
234 | | - if ( $hash !== false ) { |
235 | | - return $hash; |
236 | | - } |
237 | | - } |
238 | | - return false; |
| 291 | + public function getFileSha1Base36( array $params ) { |
| 292 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 293 | + return $this->backends[$this->masterIndex]->getFileSha1Base36( $realParams ); |
239 | 294 | } |
240 | 295 | |
241 | 296 | /** |
242 | 297 | * @see FileBackendBase::getFileProps() |
243 | 298 | */ |
244 | | - function getFileProps( array $params ) { |
245 | | - # Hit all backends in case of failed operations (out of sync) |
246 | | - foreach ( $this->backends as $backend ) { |
247 | | - $realParams = $this->substOpPaths( $params, $backend ); |
248 | | - $props = $backend->getFileProps( $realParams ); |
249 | | - if ( $props !== null ) { |
250 | | - return $props; |
251 | | - } |
252 | | - } |
253 | | - return null; |
| 299 | + public function getFileProps( array $params ) { |
| 300 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 301 | + return $this->backends[$this->masterIndex]->getFileProps( $realParams ); |
254 | 302 | } |
255 | 303 | |
256 | 304 | /** |
257 | 305 | * @see FileBackendBase::streamFile() |
258 | 306 | */ |
259 | | - function streamFile( array $params ) { |
260 | | - $status = Status::newGood(); |
261 | | - foreach ( $this->backends as $backend ) { |
262 | | - $realParams = $this->substOpPaths( $params, $backend ); |
263 | | - $subStatus = $backend->streamFile( $realParams ); |
264 | | - $status->merge( $subStatus ); |
265 | | - if ( $subStatus->isOK() ) { |
266 | | - // Pass isOK() despite fatals from other backends |
267 | | - $status->setResult( true ); |
268 | | - return $status; |
269 | | - } else { // failure |
270 | | - if ( headers_sent() ) { |
271 | | - return $status; // died mid-stream...so this is already fubar |
272 | | - } elseif ( strval( ob_get_contents() ) !== '' ) { |
273 | | - ob_clean(); // output was buffered but not sent; clear it |
274 | | - } |
275 | | - } |
276 | | - } |
277 | | - return $status; |
| 307 | + public function streamFile( array $params ) { |
| 308 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 309 | + return $this->backends[$this->masterIndex]->streamFile( $realParams ); |
278 | 310 | } |
279 | 311 | |
280 | 312 | /** |
281 | 313 | * @see FileBackendBase::getLocalReference() |
282 | 314 | */ |
283 | | - function getLocalReference( array $params ) { |
284 | | - # Hit all backends in case of failed operations (out of sync) |
285 | | - foreach ( $this->backends as $backend ) { |
286 | | - $realParams = $this->substOpPaths( $params, $backend ); |
287 | | - $fsFile = $backend->getLocalReference( $realParams ); |
288 | | - if ( $fsFile ) { |
289 | | - return $fsFile; |
290 | | - } |
291 | | - } |
292 | | - return null; |
| 315 | + public function getLocalReference( array $params ) { |
| 316 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 317 | + return $this->backends[$this->masterIndex]->getLocalReference( $realParams ); |
293 | 318 | } |
294 | 319 | |
295 | 320 | /** |
296 | 321 | * @see FileBackendBase::getLocalCopy() |
297 | 322 | */ |
298 | | - function getLocalCopy( array $params ) { |
299 | | - # Hit all backends in case of failed operations (out of sync) |
300 | | - foreach ( $this->backends as $backend ) { |
301 | | - $realParams = $this->substOpPaths( $params, $backend ); |
302 | | - $tmpFile = $backend->getLocalCopy( $realParams ); |
303 | | - if ( $tmpFile ) { |
304 | | - return $tmpFile; |
305 | | - } |
306 | | - } |
307 | | - return null; |
| 323 | + public function getLocalCopy( array $params ) { |
| 324 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 325 | + return $this->backends[$this->masterIndex]->getLocalCopy( $realParams ); |
308 | 326 | } |
309 | 327 | |
310 | 328 | /** |
311 | 329 | * @see FileBackendBase::getFileList() |
312 | 330 | */ |
313 | | - function getFileList( array $params ) { |
314 | | - foreach ( $this->backends as $backend ) { |
315 | | - # Get results from the first backend |
316 | | - $realParams = $this->substOpPaths( $params, $backend ); |
317 | | - return $backend->getFileList( $realParams ); |
318 | | - } |
319 | | - return array(); // sanity |
| 331 | + public function getFileList( array $params ) { |
| 332 | + $realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 333 | + return $this->backends[$this->masterIndex]->getFileList( $realParams ); |
320 | 334 | } |
321 | 335 | } |
Index: trunk/phase3/includes/filerepo/backend/FileOp.php |
— | — | @@ -84,7 +84,7 @@ |
85 | 85 | $status = Status::newGood(); |
86 | 86 | |
87 | 87 | $allowStale = isset( $opts['allowStale'] ) && $opts['allowStale']; |
88 | | - $ignoreErrors = isset( $opts['ignoreErrors'] ) && $opts['ignoreErrors']; |
| 88 | + $ignoreErrors = isset( $opts['force'] ) && $opts['force']; |
89 | 89 | $predicates = FileOp::newPredicates(); // account for previous op in prechecks |
90 | 90 | // Do pre-checks for each operation; abort on failure... |
91 | 91 | foreach ( $performOps as $index => $fileOp ) { |
Index: trunk/phase3/includes/filerepo/backend/FSFileBackend.php |
— | — | @@ -339,27 +339,28 @@ |
340 | 340 | /** |
341 | 341 | * @see FileBackend::doFileExists() |
342 | 342 | */ |
343 | | - protected function doFileExists( array $params ) { |
| 343 | + protected function doGetFileStat( array $params ) { |
344 | 344 | list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] ); |
345 | 345 | if ( $source === null ) { |
346 | 346 | return false; // invalid storage path |
347 | 347 | } |
| 348 | + |
348 | 349 | wfSuppressWarnings(); |
349 | | - $exists = is_file( $source ); |
| 350 | + if ( is_file( $source ) ) { // regular file? |
| 351 | + $stat = stat( $source ); |
| 352 | + } else { |
| 353 | + $stat = false; |
| 354 | + } |
350 | 355 | wfRestoreWarnings(); |
351 | | - return $exists; |
352 | | - } |
353 | 356 | |
354 | | - /** |
355 | | - * @see FileBackend::doGetFileTimestamp() |
356 | | - */ |
357 | | - public function doGetFileTimestamp( array $params ) { |
358 | | - list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] ); |
359 | | - if ( $source === null ) { |
360 | | - return false; // invalid storage path |
| 357 | + if ( $stat ) { |
| 358 | + return array( |
| 359 | + 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ), |
| 360 | + 'size' => $stat['size'] |
| 361 | + ); |
| 362 | + } else { |
| 363 | + return false; |
361 | 364 | } |
362 | | - $fsFile = new FSFile( $source ); |
363 | | - return $fsFile->getTimestamp(); |
364 | 365 | } |
365 | 366 | |
366 | 367 | /** |
Index: trunk/phase3/includes/filerepo/backend/FileBackend.php |
— | — | @@ -152,20 +152,20 @@ |
153 | 153 | * contents as the new contents to be written there. |
154 | 154 | * |
155 | 155 | * $opts is an associative of boolean flags, including: |
156 | | - * 'ignoreErrors' : Errors that would normally cause a rollback do not. |
| 156 | + * 'force' : Errors that would normally cause a rollback do not. |
157 | 157 | * The remaining operations are still attempted if any fail. |
158 | 158 | * 'nonLocking' : No locks are acquired for the operations. |
159 | 159 | * This can increase performance for non-critical writes. |
160 | | - * This has no effect unless the 'ignoreErrors' flag is set. |
| 160 | + * This has no effect unless the 'force' flag is set. |
161 | 161 | * 'allowStale' : Don't require the latest available data. |
162 | 162 | * This can increase performance for non-critical writes. |
163 | | - * This has no effect unless the 'ignoreErrors' flag is set. |
| 163 | + * This has no effect unless the 'force' flag is set. |
164 | 164 | * |
165 | 165 | * Return value: |
166 | 166 | * This returns a Status, which contains all warnings and fatals that occured |
167 | 167 | * during the operation. The 'failCount', 'successCount', and 'success' members |
168 | 168 | * will reflect each operation attempted. The status will be "OK" unless any |
169 | | - * of the operations failed and the 'ignoreErrors' parameter was not set. |
| 169 | + * of the operations failed and the 'force' parameter was not set. |
170 | 170 | * |
171 | 171 | * @param $ops Array List of operations to execute in order |
172 | 172 | * @param $opts Array Batch operation options |
— | — | @@ -175,7 +175,7 @@ |
176 | 176 | if ( $this->readOnly != '' ) { |
177 | 177 | return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly ); |
178 | 178 | } |
179 | | - if ( empty( $opts['ignoreErrors'] ) ) { // sanity |
| 179 | + if ( empty( $opts['force'] ) ) { // sanity |
180 | 180 | unset( $opts['nonLocking'] ); |
181 | 181 | unset( $opts['allowStale'] ); |
182 | 182 | } |
— | — | @@ -372,6 +372,33 @@ |
373 | 373 | abstract public function getFileContents( array $params ); |
374 | 374 | |
375 | 375 | /** |
| 376 | + * Get the size (bytes) of a file at a storage path in the backend. |
| 377 | + * |
| 378 | + * $params include: |
| 379 | + * src : source storage path |
| 380 | + * latest : use the latest available data |
| 381 | + * |
| 382 | + * @param $params Array |
| 383 | + * @return integer|false Returns false on failure |
| 384 | + */ |
| 385 | + abstract public function getFileSize( array $params ); |
| 386 | + |
| 387 | + /** |
| 388 | + * Get quick information about a file at a storage path in the backend. |
| 389 | + * The result is an associative array that includes: |
| 390 | + * mtime : the last-modified timestamp (TS_MW) or false |
| 391 | + * size : the file size (bytes) or false |
| 392 | + * |
| 393 | + * $params include: |
| 394 | + * src : source storage path |
| 395 | + * latest : use the latest available data |
| 396 | + * |
| 397 | + * @param $params Array |
| 398 | + * @return Array|false Returns false on failure |
| 399 | + */ |
| 400 | + abstract public function getFileStat( array $params ); |
| 401 | + |
| 402 | + /** |
376 | 403 | * Get a SHA-1 hash of the file at a storage path in the backend. |
377 | 404 | * |
378 | 405 | * $params include: |
— | — | @@ -398,6 +425,7 @@ |
399 | 426 | |
400 | 427 | /** |
401 | 428 | * Stream the file at a storage path in the backend. |
| 429 | + * If the file does not exists, a 404 error will be given. |
402 | 430 | * Appropriate HTTP headers (Status, Content-Type, Content-Length) |
403 | 431 | * must be sent if streaming began, while none should be sent otherwise. |
404 | 432 | * Implementations should flush the output buffer before sending data. |
— | — | @@ -807,43 +835,53 @@ |
808 | 836 | * @see FileBackendBase::fileExists() |
809 | 837 | */ |
810 | 838 | final public function fileExists( array $params ) { |
811 | | - $path = $params['src']; |
812 | | - if ( isset( $this->cache[$path]['exists'] ) ) { |
813 | | - return $this->cache[$path]['exists']; |
| 839 | + return (bool)$this->getFileStat( $params ); |
| 840 | + } |
| 841 | + |
| 842 | + /** |
| 843 | + * @see FileBackendBase::getFileTimestamp() |
| 844 | + */ |
| 845 | + final public function getFileTimestamp( array $params ) { |
| 846 | + $stat = $this->getFileStat( $params ); |
| 847 | + if ( $stat ) { |
| 848 | + return $stat['mtime']; |
| 849 | + } else { |
| 850 | + return false; |
814 | 851 | } |
815 | | - $exists = $this->doFileExists( $params ); |
816 | | - if ( $exists ) { // don't cache negatives |
817 | | - $this->trimCache(); // limit memory |
818 | | - $this->cache[$path]['exists'] = $exists; |
819 | | - } |
820 | | - return $exists; |
821 | 852 | } |
822 | 853 | |
823 | 854 | /** |
824 | | - * @see FileBackend::fileExists() |
| 855 | + * @see FileBackendBase::getFileSize() |
825 | 856 | */ |
826 | | - abstract protected function doFileExists( array $params ); |
| 857 | + final public function getFileSize( array $params ) { |
| 858 | + $stat = $this->getFileStat( $params ); |
| 859 | + if ( $stat ) { |
| 860 | + return $stat['size']; |
| 861 | + } else { |
| 862 | + return false; |
| 863 | + } |
| 864 | + } |
827 | 865 | |
828 | 866 | /** |
829 | | - * @see FileBackendBase::getFileTimestamp() |
| 867 | + * @see FileBackendBase::getFileStat() |
830 | 868 | */ |
831 | | - final public function getFileTimestamp( array $params ) { |
| 869 | + final public function getFileStat( array $params ) { |
832 | 870 | $path = $params['src']; |
833 | | - if ( isset( $this->cache[$path]['timestamp'] ) ) { |
834 | | - return $this->cache[$path]['timestamp']; |
| 871 | + if ( isset( $this->cache[$path]['stat'] ) ) { |
| 872 | + return $this->cache[$path]['stat']; |
835 | 873 | } |
836 | | - $timestamp = $this->doGetFileTimestamp( $params ); |
837 | | - if ( $timestamp ) { // don't cache negatives |
| 874 | + $stat = $this->doGetFileStat( $params ); |
| 875 | + if ( is_array( $stat ) ) { // don't cache negatives |
838 | 876 | $this->trimCache(); // limit memory |
839 | | - $this->cache[$path]['timestamp'] = $timestamp; |
| 877 | + $this->cache[$path]['stat'] = $stat; |
840 | 878 | } |
841 | | - return $timestamp; |
| 879 | + return $stat; |
842 | 880 | } |
843 | 881 | |
844 | 882 | /** |
845 | | - * @see FileBackend::getFileTimestamp() |
| 883 | + * @see FileBackend::getFileStat() |
846 | 884 | */ |
847 | | - abstract protected function doGetFileTimestamp( array $params ); |
| 885 | + abstract protected function doGetFileStat( array $params ); |
848 | 886 | |
849 | 887 | /** |
850 | 888 | * @see FileBackendBase::getFileContents() |
— | — | @@ -862,7 +900,7 @@ |
863 | 901 | /** |
864 | 902 | * @see FileBackendBase::getFileSha1Base36() |
865 | 903 | */ |
866 | | - public function getFileSha1Base36( array $params ) { |
| 904 | + final public function getFileSha1Base36( array $params ) { |
867 | 905 | $path = $params['src']; |
868 | 906 | if ( isset( $this->cache[$path]['sha1'] ) ) { |
869 | 907 | return $this->cache[$path]['sha1']; |
— | — | @@ -918,23 +956,39 @@ |
919 | 957 | /** |
920 | 958 | * @see FileBackendBase::streamFile() |
921 | 959 | */ |
922 | | - public function streamFile( array $params ) { |
| 960 | + final public function streamFile( array $params ) { |
923 | 961 | $status = Status::newGood(); |
924 | 962 | |
925 | | - $fsFile = $this->getLocalReference( $params ); |
926 | | - if ( !$fsFile ) { |
| 963 | + $info = $this->getFileStat( $params ); |
| 964 | + if ( !$info ) { // let StreamFile handle the 404 |
| 965 | + $status->fatal( 'backend-fail-notexists', $params['src'] ); |
| 966 | + } |
| 967 | + |
| 968 | + // Set output buffer and HTTP headers for stream |
| 969 | + $extraHeaders = $params['headers'] ? $params['headers'] : array(); |
| 970 | + $res = StreamFile::prepareForStream( $params['src'], $info, $extraHeaders ); |
| 971 | + if ( $res == StreamFile::NOT_MODIFIED ) { |
| 972 | + // do nothing; client cache is up to date |
| 973 | + } elseif ( $res == StreamFile::READY_STREAM ) { |
| 974 | + $status = $this->doStreamFile( $params ); |
| 975 | + } else { |
927 | 976 | $status->fatal( 'backend-fail-stream', $params['src'] ); |
928 | | - return $status; |
929 | 977 | } |
930 | 978 | |
931 | | - $extraHeaders = isset( $params['headers'] ) |
932 | | - ? $params['headers'] |
933 | | - : array(); |
| 979 | + return $status; |
| 980 | + } |
934 | 981 | |
935 | | - $ok = StreamFile::stream( $fsFile->getPath(), $extraHeaders, false ); |
936 | | - if ( !$ok ) { |
| 982 | + /** |
| 983 | + * @see FileBackend::streamFile() |
| 984 | + */ |
| 985 | + protected function doStreamFile( array $params ) { |
| 986 | + $status = Status::newGood(); |
| 987 | + |
| 988 | + $fsFile = $this->getLocalReference( $params ); |
| 989 | + if ( !$fsFile ) { |
937 | 990 | $status->fatal( 'backend-fail-stream', $params['src'] ); |
938 | | - return $status; |
| 991 | + } elseif ( !readfile( $fsFile->getPath() ) ) { |
| 992 | + $status->fatal( 'backend-fail-stream', $params['src'] ); |
939 | 993 | } |
940 | 994 | |
941 | 995 | return $status; |
Index: trunk/phase3/includes/filerepo/FileRepo.php |
— | — | @@ -695,7 +695,7 @@ |
696 | 696 | } |
697 | 697 | |
698 | 698 | // Execute the store operation for each triplet |
699 | | - $opts = array( 'ignoreErrors' => true ); |
| 699 | + $opts = array( 'force' => true ); |
700 | 700 | if ( $flags & self::SKIP_LOCKING ) { |
701 | 701 | $opts['nonLocking'] = true; |
702 | 702 | } |
— | — | @@ -750,7 +750,7 @@ |
751 | 751 | } |
752 | 752 | } |
753 | 753 | // Actually delete files from storage... |
754 | | - $opts = array( 'ignoreErrors' => true ); |
| 754 | + $opts = array( 'force' => true ); |
755 | 755 | $this->backend->doOperations( $operations, $opts ); |
756 | 756 | // Cleanup for disk source files... |
757 | 757 | foreach ( $sourceFSFilesToDelete as $file ) { |
— | — | @@ -814,7 +814,7 @@ |
815 | 815 | |
816 | 816 | // Delete the sources if required |
817 | 817 | if ( $deleteOperations ) { |
818 | | - $opts = array( 'ignoreErrors' => true ); |
| 818 | + $opts = array( 'force' => true ); |
819 | 819 | $status->merge( $this->backend->doOperations( $deleteOperations, $opts ) ); |
820 | 820 | } |
821 | 821 | |
— | — | @@ -967,7 +967,7 @@ |
968 | 968 | } |
969 | 969 | |
970 | 970 | // Execute the operations for each triplet |
971 | | - $opts = array( 'ignoreErrors' => true ); |
| 971 | + $opts = array( 'force' => true ); |
972 | 972 | $status->merge( $backend->doOperations( $operations, $opts ) ); |
973 | 973 | // Cleanup for disk source files... |
974 | 974 | foreach ( $sourceFSFilesToDelete as $file ) { |
— | — | @@ -1102,7 +1102,7 @@ |
1103 | 1103 | // Move the files by execute the operations for each pair. |
1104 | 1104 | // We're now committed to returning an OK result, which will |
1105 | 1105 | // lead to the files being moved in the DB also. |
1106 | | - $opts = array( 'ignoreErrors' => true ); |
| 1106 | + $opts = array( 'force' => true ); |
1107 | 1107 | $status->merge( $backend->doOperations( $operations, $opts ) ); |
1108 | 1108 | |
1109 | 1109 | return $status; |
Index: trunk/phase3/includes/StreamFile.php |
— | — | @@ -5,6 +5,9 @@ |
6 | 6 | * @file |
7 | 7 | */ |
8 | 8 | class StreamFile { |
| 9 | + const READY_STREAM = 1; |
| 10 | + const NOT_MODIFIED = 2; |
| 11 | + |
9 | 12 | /** |
10 | 13 | * Stream a file to the browser, adding all the headings and fun stuff. |
11 | 14 | * Headers sent include: Content-type, Content-Length, Last-Modified, |
— | — | @@ -16,17 +19,44 @@ |
17 | 20 | * @return bool Success |
18 | 21 | */ |
19 | 22 | public static function stream( $fname, $headers = array(), $sendErrors = true ) { |
20 | | - global $wgLanguageCode; |
21 | | - |
22 | 23 | wfSuppressWarnings(); |
23 | 24 | $stat = stat( $fname ); |
24 | 25 | wfRestoreWarnings(); |
25 | | - if ( !$stat ) { |
| 26 | + |
| 27 | + $res = self::prepareForStream( $fname, $stat, $headers, $sendErrors ); |
| 28 | + if ( $res == self::NOT_MODIFIED ) { |
| 29 | + return true; // use client cache |
| 30 | + } elseif ( $res == self::READY_STREAM ) { |
| 31 | + return readfile( $fname ); |
| 32 | + } else { |
| 33 | + return false; // failed |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Call this function used in preparation before streaming a file. |
| 39 | + * This function does the following: |
| 40 | + * (a) sends Last-Modified, Content-type, and Content-Disposition headers |
| 41 | + * (b) cancels any PHP output buffering and automatic gzipping of output |
| 42 | + * (c) sends Content-Length header based on HTTP_IF_MODIFIED_SINCE check |
| 43 | + * |
| 44 | + * @param $path string Storage path or file system path |
| 45 | + * @param $info Array File stat info with 'mtime' and 'size' fields |
| 46 | + * @param $headers Array Additional headers to send |
| 47 | + * @param $sendErrors bool Send error messages if errors occur (like 404) |
| 48 | + * @return int|false READY_STREAM, NOT_MODIFIED, or false on failure |
| 49 | + */ |
| 50 | + public static function prepareForStream( |
| 51 | + $path, array $info, $headers = array(), $sendErrors = true |
| 52 | + ) { |
| 53 | + global $wgLanguageCode; |
| 54 | + |
| 55 | + if ( !$info ) { |
26 | 56 | if ( $sendErrors ) { |
27 | 57 | header( 'HTTP/1.0 404 Not Found' ); |
28 | 58 | header( 'Cache-Control: no-cache' ); |
29 | 59 | header( 'Content-Type: text/html; charset=utf-8' ); |
30 | | - $encFile = htmlspecialchars( $fname ); |
| 60 | + $encFile = htmlspecialchars( $path ); |
31 | 61 | $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] ); |
32 | 62 | echo "<html><body> |
33 | 63 | <h1>File not found</h1> |
— | — | @@ -38,12 +68,13 @@ |
39 | 69 | return false; |
40 | 70 | } |
41 | 71 | |
42 | | - header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' ); |
| 72 | + // Sent Last-Modified HTTP header for client-side caching |
| 73 | + header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) ); |
43 | 74 | |
44 | 75 | // Cancel output buffering and gzipping if set |
45 | 76 | wfResetOutputBuffers(); |
46 | 77 | |
47 | | - $type = self::getType( $fname ); |
| 78 | + $type = self::getType( $path ); |
48 | 79 | if ( $type && $type != 'unknown/unknown' ) { |
49 | 80 | header( "Content-type: $type" ); |
50 | 81 | } else { |
— | — | @@ -57,7 +88,7 @@ |
58 | 89 | } |
59 | 90 | |
60 | 91 | header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . |
61 | | - urlencode( basename( $fname ) ) ); |
| 92 | + urlencode( basename( $path ) ) ); |
62 | 93 | |
63 | 94 | // Send additional headers |
64 | 95 | foreach ( $headers as $header ) { |
— | — | @@ -67,26 +98,25 @@ |
68 | 99 | // Don't send if client has up to date cache |
69 | 100 | if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { |
70 | 101 | $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); |
71 | | - $sinceTime = strtotime( $modsince ); |
72 | | - if ( $stat['mtime'] <= $sinceTime ) { |
| 102 | + if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) { |
73 | 103 | ini_set( 'zlib.output_compression', 0 ); |
74 | 104 | header( "HTTP/1.0 304 Not Modified" ); |
75 | | - return true; // ok |
| 105 | + return self::NOT_MODIFIED; // ok |
76 | 106 | } |
77 | 107 | } |
78 | 108 | |
79 | | - header( 'Content-Length: ' . $stat['size'] ); |
| 109 | + header( 'Content-Length: ' . $info['size'] ); |
80 | 110 | |
81 | | - return readfile( $fname ); |
| 111 | + return self::READY_STREAM; // ok |
82 | 112 | } |
83 | 113 | |
84 | 114 | /** |
85 | 115 | * Determine the filetype we're dealing with |
86 | | - * @param $filename string |
87 | | - * @param $safe bool |
| 116 | + * @param $filename string Storage path or file system path |
| 117 | + * @param $safe bool Whether to do retroactive upload blacklist checks |
88 | 118 | * @return null|string |
89 | 119 | */ |
90 | | - private static function getType( $filename, $safe = true ) { |
| 120 | + protected static function getType( $filename, $safe = true ) { |
91 | 121 | global $wgTrivialMimeDetection; |
92 | 122 | |
93 | 123 | $ext = strrchr( $filename, '.' ); |
Index: trunk/phase3/languages/messages/MessagesEn.php |
— | — | @@ -2254,6 +2254,7 @@ |
2255 | 2255 | 'backend-fail-read' => 'Could not read file $1.', |
2256 | 2256 | 'backend-fail-create' => 'Could not create file $1.', |
2257 | 2257 | 'backend-fail-readonly' => 'The backend "$1" is currently read-only. The reason given is: "$2"', |
| 2258 | +'backend-fail-synced' => 'The file "$1" is in an inconsistent state within the internal backends', |
2258 | 2259 | |
2259 | 2260 | # Lock manager |
2260 | 2261 | 'lockmanager-notlocked' => 'Could not unlock "$1"; it is not locked.', |