r114414 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114413‎ | r114414 | r114415 >
Date:21:58, 21 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on diff stuff
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/EducationProgram.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/EducationProgram.php
@@ -121,6 +121,7 @@
122122 $wgAutoloadClasses['EPRevisions'] = dirname( __FILE__ ) . '/includes/EPRevisions.php';
123123 $wgAutoloadClasses['EPArticles'] = dirname( __FILE__ ) . '/includes/EPArticles.php';
124124 $wgAutoloadClasses['EPStudentActivityPager'] = dirname( __FILE__ ) . '/includes/EPStudentActivityPager.php';
 125+$wgAutoloadClasses['EPRevisionDiff'] = dirname( __FILE__ ) . '/includes/EPRevisionDiff.php';
125126
126127 $wgAutoloadClasses['CoursePage'] = dirname( __FILE__ ) . '/pages/CoursePage.php';
127128 $wgAutoloadClasses['EPPage'] = dirname( __FILE__ ) . '/pages/EPPage.php';
Index: trunk/extensions/EducationProgram/actions/EPUndoAction.php
@@ -64,8 +64,21 @@
6565 $success = $this->doUndo( $object, $revision );
6666 }
6767 else {
68 - $this->displayForm( $object, $revision );
69 - $success = null;
 68+ $diff = $object->getUndoDiff( $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 }
Index: trunk/extensions/EducationProgram/includes/EPRevisionDiff.php
@@ -17,40 +17,49 @@
1818
1919 protected $changedFields = array();
2020
21 - public static function newFromUndoRevision( IContextSource $context, EPRevision $revison, array $fields = null ) {
 21+ protected $isValid = true;
 22+
 23+ public static function newFromUndoRevision( EPRevisionedObject $currentObject, EPRevision $revison, array $fields = null ) {
2224 $changedFields = array();
2325
24 - $oldObject = $revison->getPreviousRevision()->getObject();
 26+ $targetObject = $revison->getPreviousRevision()->getObject();
2527
26 - if ( $oldObject !== false ) {
27 - $newObject = $revison->getObject();
 28+ if ( $targetObject !== false ) {
 29+ $sourceObject = $revison->getObject();
2830
29 - $fields = is_null( $fields ) ? $newObject->getFieldNames() : $fields;
 31+ $fields = is_null( $fields ) ? $sourceObject->getFieldNames() : $fields;
3032
3133 foreach ( $fields as $fieldName ) {
32 - if ( $this->getField( $fieldName ) === $newObject->getField( $fieldName ) ) {
33 - $changedFields[$fieldName] = array( );
34 - $this->restoreField( $fieldName, $oldObject->getField( $fieldName ) );
 34+ if ( $currentObject->getField( $fieldName ) === $sourceObject->getField( $fieldName )
 35+ && $sourceObject->getField( $fieldName ) !== $targetObject->getField( $fieldName ) ) {
 36+
 37+ $changedFields[$fieldName] = array(
 38+ $sourceObject->getField( $fieldName ),
 39+ $targetObject->getField( $fieldName )
 40+ );
3541 }
3642 }
3743 }
3844
39 - return new self( $context, $changedFields );
 45+ $diff = new self( $changedFields );
 46+
 47+ $diff->setIsValid( $targetObject !== false );
 48+
 49+ return $diff;
4050 }
4151
42 - public function __construct( IContextSource $context, array $changedFields ) {
43 - $this->setContext( $context );
 52+ public function __construct( array $changedFields ) {
4453 $this->changedFields = $changedFields;
4554 }
4655
4756 public function display() {
4857 $out = $this->getOutput();
4958
50 - $out->addHTML( '<table><tr>' );
 59+ $out->addHTML( '<table class="wikitable sortable"><tr>' );
5160
5261 $out->addElement( 'th', array(), '' );
53 - $out->addElement( 'th', array(), $this->msg()->plain( 'ep-diff-old' ) );
54 - $out->addElement( 'th', array(), $this->msg()->plain( 'ep-diff-new' ) );
 62+ $out->addElement( 'th', array(), $this->msg( 'ep-diff-old' )->plain() );
 63+ $out->addElement( 'th', array(), $this->msg( 'ep-diff-new' )->plain() );
5564
5665 $out->addHTML( '</tr>' );
5766
@@ -59,13 +68,34 @@
6069
6170 $out->addHtml( '<tr>' );
6271
63 - $out->addElement( '<th>', array(), $field );
64 - $out->addElement( '<td>', array(), $old );
65 - $out->addElement( '<td>', array(), $new );
 72+ $out->addElement( 'th', array(), $field );
 73+ $out->addElement( 'td', array(), $old );
 74+ $out->addElement( 'td', array(), $new );
6675
6776 $out->addHtml( '</tr>' );
6877 }
 78+
 79+ $out->addHTML( '</table>' );
6980 }
7081
 82+ /**
 83+ * @return array
 84+ */
 85+ public function getChangedFields() {
 86+ return $this->changedFields;
 87+ }
7188
 89+ public function isValid() {
 90+ return $this->isValid;
 91+ }
 92+
 93+ public function setIsValid( $isValid ) {
 94+ $this->isValid = $isValid;
 95+ }
 96+
 97+ public function hasChanges() {
 98+ return !empty( $this->changedFields );
 99+ }
 100+
 101+
72102 }
\ No newline at end of file
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
@@ -376,26 +376,17 @@
377377 * @return boolean Success indicator
378378 */
379379 public function undoRevision( EPRevision $revison, array $fields = null ) {
380 - $oldObject = $revison->getPreviousRevision()->getObject();
 380+ $diff = $this->getUndoDiff( $revison, $fields );
381381
382 - if ( $oldObject === false ) {
383 - return false;
 382+ foreach ( $diff->getChangedFields() as $fieldName => $values ) {
 383+ $this->restoreField( $fieldName, $values[1] );
384384 }
385385
386 - $newObject = $revison->getObject();
387 -
388 - $fields = is_null( $fields ) ? $newObject->getFieldNames() : $fields;
389 -
390 - foreach ( $fields as $fieldName ) {
391 - if ( $this->getField( $fieldName ) === $newObject->getField( $fieldName ) ) {
392 - $this->restoreField( $fieldName, $oldObject->getField( $fieldName ) );
393 - }
394 - }
395 -
396 - return true;
 386+ return $diff->isValid();
397387 }
398388
399389 /**
 390+ * Get a diff for the changes that will happen when undoing the provided revision.
400391 *
401392 * @since 0.1
402393 *
@@ -405,21 +396,7 @@
406397 * @return EPRevisionDiff
407398 */
408399 public function getUndoDiff( EPRevision $revison, array $fields = null ) {
409 - $oldObject = $revison->getPreviousRevision()->getObject();
410 -
411 - if ( $oldObject === false ) {
412 - return false;
413 - }
414 -
415 - $newObject = $revison->getObject();
416 -
417 - $fields = is_null( $fields ) ? $newObject->getFieldNames() : $fields;
418 -
419 - foreach ( $fields as $fieldName ) {
420 - if ( $this->getField( $fieldName ) === $newObject->getField( $fieldName ) ) {
421 - $this->restoreField( $fieldName, $oldObject->getField( $fieldName ) );
422 - }
423 - }
 400+ return EPRevisionDiff::newFromUndoRevision( $this, $revison, $fields );
424401 }
425402
426403 /**
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -794,6 +794,10 @@
795795 'duration-weeks' => '$1 {{PLURAL:$1|week|weeks}}',
796796 'duration-years' => '$1 {{PLURAL:$1|year|years}}',
797797 'duration-centuries' => '$1 {{PLURAL:$1|century|centuries}}',
 798+
 799+ // Diffs
 800+ 'ep-diff-old' => 'Old value',
 801+ 'ep-diff-new' => 'New value',
798802 );
799803
800804 /** Message documentation (Message documentation)

Status & tagging log