r107986 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107985‎ | r107986 | r107987 >
Date:02:15, 4 January 2012
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
* Added FileBackendBase::getFileContents() function with a default FileBackend version.
* Added read-only mode to FileBackendBase config.
* Moved FileBackendBase::getFileTimestamp() up slightly.
Modified paths:
  • /trunk/phase3/includes/filerepo/backend/FileBackend.php (modified) (history)
  • /trunk/phase3/includes/filerepo/backend/FileBackendMultiWrite.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)
  • /trunk/phase3/maintenance/language/messages.inc (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/language/messages.inc
@@ -1362,6 +1362,7 @@
13631363 'backend-fail-closetemp',
13641364 'backend-fail-read',
13651365 'backend-fail-create',
 1366+ 'backend-fail-readonly'
13661367 ),
13671368
13681369 'lockmanager-errors' => array(
Index: trunk/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php
@@ -433,6 +433,32 @@
434434 }
435435
436436 /**
 437+ * @dataProvider provider_testGetFileContents
 438+ */
 439+ public function testGetFileContents( $src, $content ) {
 440+ $this->pathsToPrune[] = $src;
 441+
 442+ $status = $this->backend->doOperation(
 443+ array( 'op' => 'create', 'content' => $content, 'dst' => $src ) );
 444+ $this->assertEquals( true, $status->isOK(), "Creation of file at $src succeeded." );
 445+
 446+ $newContents = $this->backend->getFileContents( array( 'src' => $src ) );
 447+ $this->assertNotEquals( false, $newContents, "Read of file at $src succeeded." );
 448+
 449+ $this->assertEquals( $content, $newContents, "Contents read match data at $src." );
 450+ }
 451+
 452+ function provider_testGetFileContents() {
 453+ $cases = array();
 454+
 455+ $base = $this->singleBasePath();
 456+ $cases[] = array( "$base/cont1/b/z/some_file.txt", "some file contents" );
 457+ $cases[] = array( "$base/cont1/b/some-other_file.txt", "more file contents" );
 458+
 459+ return $cases;
 460+ }
 461+
 462+ /**
437463 * @dataProvider provider_testGetLocalCopy
438464 */
439465 public function testGetLocalCopy( $src, $content ) {
@@ -460,7 +486,7 @@
461487 }
462488
463489 /**
464 - * @dataProvider provider_testGetReference
 490+ * @dataProvider provider_testGetLocalReference
465491 */
466492 public function testGetLocalReference( $src, $content ) {
467493 $this->pathsToPrune[] = $src;
@@ -476,7 +502,7 @@
477503 $this->assertNotEquals( false, $contents, "Local copy of $src exists." );
478504 }
479505
480 - function provider_testGetReference() {
 506+ function provider_testGetLocalReference() {
481507 $cases = array();
482508
483509 $base = $this->singleBasePath();
Index: trunk/phase3/includes/filerepo/backend/FileBackendMultiWrite.php
@@ -210,6 +210,21 @@
211211 }
212212
213213 /**
 214+ * @see FileBackendBase::getFileContents()
 215+ */
 216+ function getFileContents( array $params ) {
 217+ # Hit all backends in case of failed operations (out of sync)
 218+ foreach ( $this->backends as $backend ) {
 219+ $realParams = $this->substOpPaths( $params, $backend );
 220+ $data = $backend->getFileContents( $realParams );
 221+ if ( $data !== false ) {
 222+ return $data;
 223+ }
 224+ }
 225+ return false;
 226+ }
 227+
 228+ /**
214229 * @see FileBackendBase::getFileSha1Base36()
215230 */
216231 function getFileSha1Base36( array $params ) {
Index: trunk/phase3/includes/filerepo/backend/FileBackend.php
@@ -28,6 +28,7 @@
2929 abstract class FileBackendBase {
3030 protected $name; // unique backend name
3131 protected $wikiId; // unique wiki name
 32+ protected $readOnly; // string
3233 /** @var LockManager */
3334 protected $lockManager;
3435
@@ -36,9 +37,11 @@
3738 * This should only be called from within FileBackendGroup.
3839 *
3940 * $config includes:
40 - * 'name' : The name of this backend
 41+ * 'name' : The unique name of this backend
4142 * 'wikiId' : Prefix to container names that is unique to this wiki
42 - * 'lockManager' : Registered name of the file lock manager to use
 43+ * 'lockManager' : Registered name of a file lock manager to use
 44+ * 'readOnly' : Write operations are disallowed if this is a non-empty string.
 45+ * It should be an explanation for the backend being read-only.
4346 *
4447 * @param $config Array
4548 */
@@ -48,6 +51,9 @@
4952 ? $config['wikiId']
5053 : wfWikiID();
5154 $this->lockManager = LockManagerGroup::singleton()->get( $config['lockManager'] );
 55+ $this->readOnly = isset( $config['readOnly'] )
 56+ ? (string)$config['readOnly']
 57+ : '';
5258 }
5359
5460 /**
@@ -149,6 +155,9 @@
150156 * @return Status
151157 */
152158 final public function doOperations( array $ops, array $opts = array() ) {
 159+ if ( $this->readOnly != '' ) {
 160+ return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
 161+ }
153162 if ( empty( $opts['ignoreErrors'] ) ) { // sanity
154163 unset( $opts['nonLocking'] );
155164 unset( $opts['allowStale'] );
@@ -320,30 +329,43 @@
321330 abstract public function fileExists( array $params );
322331
323332 /**
324 - * Get a SHA-1 hash of the file at a storage path in the backend.
 333+ * Get the last-modified timestamp of the file at a storage path.
325334 *
326335 * $params include:
327336 * src : source storage path
328337 * latest : use the latest available data
329338 *
330339 * @param $params Array
331 - * @return string|false Hash string or false on failure
 340+ * @return string|false TS_MW timestamp or false on failure
332341 */
333 - abstract public function getFileSha1Base36( array $params );
 342+ abstract public function getFileTimestamp( array $params );
334343
335344 /**
336 - * Get the last-modified timestamp of the file at a storage path.
 345+ * Get the contents of a file at a storage path in the backend.
 346+ * This should be avoided for potentially large files.
337347 *
338348 * $params include:
339349 * src : source storage path
340350 * latest : use the latest available data
341351 *
342352 * @param $params Array
343 - * @return string|false TS_MW timestamp or false on failure
 353+ * @return string|false Returns false on failure
344354 */
345 - abstract public function getFileTimestamp( array $params );
 355+ abstract public function getFileContents( array $params );
346356
347357 /**
 358+ * Get a SHA-1 hash of the file at a storage path in the backend.
 359+ *
 360+ * $params include:
 361+ * src : source storage path
 362+ * latest : use the latest available data
 363+ *
 364+ * @param $params Array
 365+ * @return string|false Hash string or false on failure
 366+ */
 367+ abstract public function getFileSha1Base36( array $params );
 368+
 369+ /**
348370 * Get the properties of the file at a storage path in the backend.
349371 * Returns FSFile::placeholderProps() on failure.
350372 *
@@ -754,6 +776,20 @@
755777 }
756778
757779 /**
 780+ * @see FileBackendBase::getFileContents()
 781+ */
 782+ public function getFileContents( array $params ) {
 783+ $tmpFile = $this->getLocalReference( $params );
 784+ if ( !$tmpFile ) {
 785+ return false;
 786+ }
 787+ wfSuppressWarnings();
 788+ $data = file_get_contents( $tmpFile->getPath() );
 789+ wfRestoreWarnings();
 790+ return $data;
 791+ }
 792+
 793+ /**
758794 * @see FileBackendBase::getFileSha1Base36()
759795 */
760796 public function getFileSha1Base36( array $params ) {
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -2253,6 +2253,7 @@
22542254 'backend-fail-closetemp' => 'Could not close temporary file.',
22552255 'backend-fail-read' => 'Could not read file $1.',
22562256 'backend-fail-create' => 'Could not create file $1.',
 2257+'backend-fail-readonly' => 'The backend "$1" is currently read-only. The reason given is: "$2"',
22572258
22582259 # Lock manager
22592260 'lockmanager-notlocked' => 'Could not unlock "$1"; it is not locked.',

Follow-up revisions

RevisionCommit summaryAuthorDate
r108377* r107986: Added readOnly checks to prepare(), secure(), and clean() in FileB...aaron00:20, 9 January 2012

Comments

#Comment by Nikerabbit (talk | contribs)   11:10, 4 January 2012

Message docs? And could be a little more specific than just a 'backend'.

Status & tagging log