r109904 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109903‎ | r109904 | r109905 >
Date:05:54, 24 January 2012
Author:aaron
Status:resolved (Comments)
Tags:filebackend 
Comment:
Made FileOp classes enforce required params. Also reverts r109823.
Modified paths:
  • /trunk/phase3/includes/filerepo/backend/FileOp.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php
@@ -884,11 +884,13 @@
885885 // Does nothing
886886 array( 'op' => 'move', 'src' => $fileC, 'dst' => $fileC, 'overwriteSame' => 1 ),
887887 // Does nothing
 888+ array( 'op' => 'null' ),
 889+ // Does nothing
888890 ) );
889891
890892 $this->assertEquals( array(), $status->errors, "Operation batch succeeded" );
891893 $this->assertEquals( true, $status->isOK(), "Operation batch succeeded" );
892 - $this->assertEquals( 12, count( $status->success ),
 894+ $this->assertEquals( 13, count( $status->success ),
893895 "Operation batch has correct success array" );
894896
895897 $this->assertEquals( false, $this->backend->fileExists( array( 'src' => $fileA ) ),
Index: trunk/phase3/includes/filerepo/backend/FileOp.php
@@ -7,12 +7,11 @@
88
99 /**
1010 * Helper class for representing operations with transaction support.
11 - * FileBackend::doOperations() will require these classes for supported operations.
1211 * Do not use this class from places outside FileBackend.
 12+ *
 13+ * Methods called from attemptBatch() should avoid throwing exceptions at all costs.
 14+ * FileOp objects should be lightweight in order to support large arrays in memory.
1315 *
14 - * Use of large fields should be avoided as we want to support
15 - * potentially many FileOp classes in large arrays in memory.
16 - *
1716 * @ingroup FileBackend
1817 * @since 1.19
1918 */
@@ -29,6 +28,10 @@
3029 protected $sourceSha1; // string
3130 protected $destSameAsSource; // boolean
3231
 32+ /* Operation parameters */
 33+ protected static $requiredParams = array();
 34+ protected static $optionalParams = array();
 35+
3336 /* Object life-cycle */
3437 const STATE_NEW = 1;
3538 const STATE_CHECKED = 2;
@@ -43,14 +46,23 @@
4447 *
4548 * @params $backend FileBackend
4649 * @params $params Array
 50+ * @throws MWException
4751 */
4852 final public function __construct( FileBackendBase $backend, array $params ) {
4953 $this->backend = $backend;
50 - foreach ( $this->allowedParams() as $name ) {
 54+ $class = get_class( $this ); // simulate LSB
 55+ foreach ( $class::$requiredParams as $name ) {
5156 if ( isset( $params[$name] ) ) {
5257 $this->params[$name] = $params[$name];
 58+ } else {
 59+ throw new MWException( "File operation missing parameter '$name'." );
5360 }
5461 }
 62+ foreach ( $class::$optionalParams as $name ) {
 63+ if ( isset( $params[$name] ) ) {
 64+ $this->params[$name] = $params[$name];
 65+ }
 66+ }
5567 $this->params = $params;
5668 }
5769
@@ -223,13 +235,6 @@
224236 }
225237
226238 /**
227 - * @return Array List of allowed parameters
228 - */
229 - protected function allowedParams() {
230 - return array();
231 - }
232 -
233 - /**
234239 * @return Status
235240 */
236241 protected function doPrecheck( array &$predicates ) {
@@ -382,9 +387,8 @@
383388 * overwriteSame : override any existing file at destination
384389 */
385390 class StoreFileOp extends FileOp {
386 - protected function allowedParams() {
387 - return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
388 - }
 391+ protected static $requiredParams = array( 'src', 'dst' );
 392+ protected static $optionalParams = array( 'overwrite', 'overwriteSame' );
389393
390394 protected function doPrecheck( array &$predicates ) {
391395 $status = Status::newGood();
@@ -438,15 +442,14 @@
439443 /**
440444 * Create a file in the backend with the given content.
441445 * Parameters similar to FileBackend::createInternal(), which include:
442 - * content : a string of raw file content. Let unset to create an empty file.
 446+ * content : the raw file contents
443447 * dst : destination storage path
444448 * overwrite : do nothing and pass if an identical file exists at destination
445449 * overwriteSame : override any existing file at destination
446450 */
447451 class CreateFileOp extends FileOp {
448 - protected function allowedParams() {
449 - return array( 'content', 'dst', 'overwrite', 'overwriteSame' );
450 - }
 452+ protected static $requiredParams = array( 'content', 'dst' );
 453+ protected static $optionalParams = array( 'overwrite', 'overwriteSame' );
451454
452455 protected function doPrecheck( array &$predicates ) {
453456 $status = Status::newGood();
@@ -496,9 +499,8 @@
497500 * overwriteSame : override any existing file at destination
498501 */
499502 class CopyFileOp extends FileOp {
500 - protected function allowedParams() {
501 - return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
502 - }
 503+ protected static $requiredParams = array( 'src', 'dst' );
 504+ protected static $optionalParams = array( 'overwrite', 'overwriteSame' );
503505
504506 protected function doPrecheck( array &$predicates ) {
505507 $status = Status::newGood();
@@ -551,9 +553,8 @@
552554 * overwriteSame : override any existing file at destination
553555 */
554556 class MoveFileOp extends FileOp {
555 - protected function allowedParams() {
556 - return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
557 - }
 557+ protected static $requiredParams = array( 'src', 'dst' );
 558+ protected static $optionalParams = array( 'overwrite', 'overwriteSame' );
558559
559560 protected function doPrecheck( array &$predicates ) {
560561 $status = Status::newGood();
@@ -610,12 +611,11 @@
611612 * ignoreMissingSource : don't return an error if the file does not exist
612613 */
613614 class DeleteFileOp extends FileOp {
 615+ protected static $requiredParams = array( 'src' );
 616+ protected static $optionalParams = array( 'ignoreMissingSource' );
 617+
614618 protected $needsDelete = true;
615619
616 - protected function allowedParams() {
617 - return array( 'src', 'ignoreMissingSource' );
618 - }
619 -
620620 protected function doPrecheck( array &$predicates ) {
621621 $status = Status::newGood();
622622 // Check if the source file exists

Follow-up revisions

RevisionCommit summaryAuthorDate
r109938r109904: worked around PHP < 5.3 suckage to keep this compatibleaaron18:49, 24 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r109823FileOp: allow creation of an empty file...hashar14:48, 23 January 2012

Comments

#Comment by Platonides (talk | contribs)   17:04, 24 January 2012

$class::$requiredParams is a 5.3 feature.

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

#Comment by Aaron Schulz (talk | contribs)   18:35, 24 January 2012

Heh, I was using that too avoid static:: due to it being 5.3. I'll just go back to verbose functions.

Status & tagging log