r114433 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114432‎ | r114433 | r114434 >
Date:00:14, 23 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on revision diffs
Modified paths:
  • /trunk/extensions/EducationProgram/actions/EPRestoreAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPUndoAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionDiff.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionedObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/actions/EPRestoreAction.php
@@ -64,8 +64,21 @@
6565 $success = $this->doRestore( $object, $revision );
6666 }
6767 else {
68 - $this->displayForm( $object, $revision );
69 - $success = null;
 68+ $diff = $object->getRestoreDiff( $revision );
 69+
 70+ if ( $diff->isValid() ) {
 71+ if ( $diff->hasChanges() ) {
 72+ $diff->setContext( $this->getContext() );
 73+ $diff->display();
 74+
 75+ $this->displayForm( $object, $revision );
 76+ }
 77+ else {
 78+ // TODO
 79+ }
 80+
 81+ $success = null;
 82+ }
7083 }
7184 }
7285 }
@@ -102,7 +115,7 @@
103116 * @return boolean Success indicator
104117 */
105118 protected function doRestore( EPPageObject $object, EPRevision $revision ) {
106 - $success = $object->restoreToRevision( $revision, $object->getTable()->getRevertableFields() );
 119+ $success = $object->restoreToRevision( $revision );
107120
108121 if ( $success ) {
109122 $revAction = new EPRevisionAction();
Index: trunk/extensions/EducationProgram/actions/EPUndoAction.php
@@ -115,7 +115,7 @@
116116 * @return boolean Success indicator
117117 */
118118 protected function doUndo( EPPageObject $object, EPRevision $revision ) {
119 - $success = $object->undoRevision( $revision, $object->getTable()->getRevertableFields() );
 119+ $success = $object->undoRevision( $revision );
120120
121121 if ( $success ) {
122122 $revAction = new EPRevisionAction();
Index: trunk/extensions/EducationProgram/includes/EPRevisionDiff.php
@@ -19,6 +19,34 @@
2020
2121 protected $isValid = true;
2222
 23+ public static function newFromRestoreRevision( EPRevisionedObject $sourceObject, EPRevision $revison, array $fields = null ) {
 24+ $changedFields = array();
 25+
 26+ $targetObject = $revison->getObject();
 27+ $fields = is_null( $fields ) ? $targetObject->getFieldNames() : $fields;
 28+
 29+ foreach ( $fields as $fieldName ) {
 30+ $sourceHasField = $sourceObject->hasField( $fieldName );
 31+ $targetHasField = $targetObject->hasField( $fieldName );
 32+
 33+ if ( ( $sourceHasField XOR $targetHasField )
 34+ || $sourceObject->getField( $fieldName, null ) !== $targetObject->getField( $fieldName, null ) ) {
 35+
 36+ $changedFields[$fieldName] = array();
 37+
 38+ if ( $sourceHasField ) {
 39+ $changedFields[$fieldName]['source'] = $sourceObject->getField( $fieldName );
 40+ }
 41+
 42+ if ( $targetHasField ) {
 43+ $changedFields[$fieldName]['target'] = $targetObject->getField( $fieldName );
 44+ }
 45+ }
 46+ }
 47+
 48+ return new self( $changedFields );
 49+ }
 50+
2351 public static function newFromUndoRevision( EPRevisionedObject $currentObject, EPRevision $revison, array $fields = null ) {
2452 $changedFields = array();
2553
@@ -30,13 +58,25 @@
3159 $fields = is_null( $fields ) ? $sourceObject->getFieldNames() : $fields;
3260
3361 foreach ( $fields as $fieldName ) {
34 - if ( $currentObject->getField( $fieldName ) === $sourceObject->getField( $fieldName )
35 - && $sourceObject->getField( $fieldName ) !== $targetObject->getField( $fieldName ) ) {
 62+ $sourceHasField = $sourceObject->hasField( $fieldName );
 63+ $targetHasField = $targetObject->hasField( $fieldName );
3664
37 - $changedFields[$fieldName] = array(
38 - $sourceObject->getField( $fieldName ),
39 - $targetObject->getField( $fieldName )
40 - );
 65+ if ( $currentObject->getField( $fieldName, null ) === $sourceObject->getField( $fieldName, null )
 66+ &&
 67+ ( ( $sourceHasField XOR $targetHasField )
 68+ ||
 69+ $sourceObject->getField( $fieldName, null ) !== $targetObject->getField( $fieldName, null )
 70+ ) ) {
 71+
 72+ $changedFields[$fieldName] = array();
 73+
 74+ if ( $sourceHasField ) {
 75+ $changedFields[$fieldName]['source'] = $sourceObject->getField( $fieldName );
 76+ }
 77+
 78+ if ( $targetHasField ) {
 79+ $changedFields[$fieldName]['target'] = $targetObject->getField( $fieldName );
 80+ }
4181 }
4282 }
4383 }
@@ -64,13 +104,14 @@
65105 $out->addHTML( '</tr>' );
66106
67107 foreach ( $this->changedFields as $field => $values ) {
68 - list( $old, $new ) = $values;
69 -
70108 $out->addHtml( '<tr>' );
71109
 110+ $source = array_key_exists( 'source', $values ) ? $values['source'] : '';
 111+ $target = array_key_exists( 'target', $values ) ? $values['target'] : '';
 112+
72113 $out->addElement( 'th', array(), $field );
73 - $out->addElement( 'td', array(), $old );
74 - $out->addElement( 'td', array(), $new );
 114+ $out->addElement( 'td', array(), $source );
 115+ $out->addElement( 'td', array(), $target );
75116
76117 $out->addHtml( '</tr>' );
77118 }
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
@@ -345,21 +345,31 @@
346346 *
347347 * @param EPRevision $revison
348348 * @param array|null $fields
349 - *
 349+ *
350350 * @return boolean Success indicator
351351 */
352352 public function restoreToRevision( EPRevision $revison, array $fields = null ) {
353 - $object = $revison->getObject();
354 - $fields = is_null( $fields ) ? $object->getFieldNames() : $fields;
355 -
356 - foreach ( $fields as $fieldName ) {
357 - $this->restoreField( $fieldName, $object->getField( $fieldName ) );
358 - }
359 -
360 - return true;
 353+ $diff = $this->getRestoreDiff( $revison, $fields );
 354+ $this->applyDiff( $diff );
 355+ return $diff->isValid();
361356 }
362357
363358 /**
 359+ * Get a diff for the changes that will happen when retoring to the provided revision.
 360+ *
 361+ * @since 0.1
 362+ *
 363+ * @param EPRevision $revison
 364+ * @param array|null $fields
 365+ *
 366+ * @return EPRevisionDiff
 367+ */
 368+ public function getRestoreDiff( EPRevision $revison, array $fields = null ) {
 369+ $fields = is_null( $fields ) ? $this->table->getRevertableFields() : $fields;
 370+ return EPRevisionDiff::newFromRestoreRevision( $this, $revison, $fields );
 371+ }
 372+
 373+ /**
364374 * Undo the changes of a single revision to this object.
365375 * Changes are compared on field level. If a field is no
366376 * longer the same as in the revision being undone, it
@@ -377,12 +387,19 @@
378388 */
379389 public function undoRevision( EPRevision $revison, array $fields = null ) {
380390 $diff = $this->getUndoDiff( $revison, $fields );
 391+ $this->applyDiff( $diff );
 392+ return $diff->isValid();
 393+ }
381394
 395+ public function applyDiff( EPRevisionDiff $diff ) {
382396 foreach ( $diff->getChangedFields() as $fieldName => $values ) {
383 - $this->restoreField( $fieldName, $values[1] );
 397+ if ( array_key_exists( 'target', $values ) ) {
 398+ $this->restoreField( $fieldName, $values['target'] );
 399+ }
 400+ else {
 401+ $this->removeField( $fieldName );
 402+ }
384403 }
385 -
386 - return $diff->isValid();
387404 }
388405
389406 /**
@@ -396,6 +413,7 @@
397414 * @return EPRevisionDiff
398415 */
399416 public function getUndoDiff( EPRevision $revison, array $fields = null ) {
 417+ $fields = is_null( $fields ) ? $this->table->getRevertableFields() : $fields;
400418 return EPRevisionDiff::newFromUndoRevision( $this, $revison, $fields );
401419 }
402420

Status & tagging log