https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113181

Revision: 113181
Author:   jeroendedauw
Date:     2012-03-06 21:31:31 +0000 (Tue, 06 Mar 2012)
Log Message:
-----------
work on undo action

Modified Paths:
--------------
    trunk/extensions/EducationProgram/actions/EPRestoreAction.php
    trunk/extensions/EducationProgram/actions/EPUndoAction.php
    trunk/extensions/EducationProgram/includes/EPCourse.php
    trunk/extensions/EducationProgram/includes/EPCourses.php
    trunk/extensions/EducationProgram/includes/EPRevision.php
    trunk/extensions/EducationProgram/includes/EPRevisionPager.php
    trunk/extensions/EducationProgram/includes/EPRevisionedObject.php

Modified: trunk/extensions/EducationProgram/actions/EPRestoreAction.php
===================================================================
--- trunk/extensions/EducationProgram/actions/EPRestoreAction.php       
2012-03-06 21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/actions/EPRestoreAction.php       
2012-03-06 21:31:31 UTC (rev 113181)
@@ -165,7 +165,6 @@
                );
                
                $out->addHTML( Html::hidden( 'revid', 
$this->getRequest()->getInt( 'revid' ) ) );
-
                $out->addHTML( Html::hidden( 'restoreToken', 
$this->getUser()->getEditToken( $this->getSalt() ) ) );
 
                $out->addHTML( '</form>' );

Modified: trunk/extensions/EducationProgram/actions/EPUndoAction.php
===================================================================
--- trunk/extensions/EducationProgram/actions/EPUndoAction.php  2012-03-06 
21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/actions/EPUndoAction.php  2012-03-06 
21:31:31 UTC (rev 113181)
@@ -55,8 +55,13 @@
                        $req = $this->getRequest();
                        
                        if ( $req->wasPosted() && 
$this->getUser()->matchEditToken( $req->getText( 'undoToken' ), 
$this->getSalt() ) ) {
-                               $success = $this->doUndo( $object );
-                               
+                               if ( $req->getCheck( 'revid' ) ) {
+                                       $success = $this->doUndo( $object, 
$req->getInt( 'revid' ) );
+                               }
+                               else {
+                                       $success = false;
+                               }
+
                                if ( $success ) {
                                        $query = array( 'undid' => '1' ); // 
TODO: handle
                                }
@@ -80,17 +85,27 @@
         * @since 0.1
         * 
         * @param EPPageObject $object
+        * @param integer $revId
         * 
         * @return boolean Success indicator
         */
-       protected function doUndo( EPPageObject $object ) {
-               $revAction = new EPRevisionAction();
+       protected function doUndo( EPPageObject $object, $revId ) {
+               $success = $object->undoRevisionId( $revId, 
$object->getTable()->getRevertableFields() );
+
+               if ( $success ) {
+                       $revAction = new EPRevisionAction();
+
+                       $revAction->setUser( $this->getUser() );
+                       $revAction->setComment( $this->getRequest()->getText( 
'summary', '' ) );
+
+                       $success = $object->revisionedSave( $revAction );
+
+                       if ( $success ) {
+                               // TODO: log
+                               // Already logged - just alter message?
+                       }
+               }
                
-               $revAction->setUser( $this->getUser() );
-               $revAction->setComment( $this->getRequest()->getText( 
'summary', '' ) );
-               
-               // TODO
-               
                return false;
        }
 
@@ -149,6 +164,7 @@
                        wfMsg( $this->prefixMsg( 'cancel-button' ) )
                );
 
+               $out->addHTML( Html::hidden( 'revid', 
$this->getRequest()->getInt( 'revid' ) ) );
                $out->addHTML( Html::hidden( 'undoToken', 
$this->getUser()->getEditToken( $this->getSalt() ) ) );
 
                $out->addHTML( '</form>' );

Modified: trunk/extensions/EducationProgram/includes/EPCourse.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPCourse.php     2012-03-06 
21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/includes/EPCourse.php     2012-03-06 
21:31:31 UTC (rev 113181)
@@ -190,7 +190,7 @@
                        }
                }
 
-               if ( count( $newUsers ) > 0 ) {
+               if ( !empty( $newUsers ) ) {
                        $dbw->begin();
 
                        foreach ( $newUsers as $userLink ) {
@@ -706,10 +706,14 @@
                EPUtils::log( $info );
        }
 
-       protected function restoreField( $fieldName, EPRevisionedObject $object 
) {
+       /**
+        * (non-PHPdoc)
+        * @see EPRevionedObject::restoreField()
+        */
+       protected function restoreField( $fieldName, $newValue ) {
                if ( $fieldName !== 'org_id'
-                       || EPOrgs::singleton()->has( array( 'id' => 
$object->getField( 'org_id' ) ) ) ) {
-                       parent::restoreField( $fieldName, $object );
+                       || EPOrgs::singleton()->has( array( 'id' => $newValue ) 
) ) {
+                       parent::restoreField( $fieldName, $newValue );
                }
        }
 

Modified: trunk/extensions/EducationProgram/includes/EPCourses.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPCourses.php    2012-03-06 
21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/includes/EPCourses.php    2012-03-06 
21:31:31 UTC (rev 113181)
@@ -118,11 +118,6 @@
                        'instructor_count',
                        'oa_count',
                        'ca_count',
-                       'field',
-                       'level',
-                       'term',
-                       'lang',
-                       'mc',
                );
        }
        

Modified: trunk/extensions/EducationProgram/includes/EPRevision.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRevision.php   2012-03-06 
21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/includes/EPRevision.php   2012-03-06 
21:31:31 UTC (rev 113181)
@@ -78,4 +78,18 @@
                return $this->user;
        }
 
+       /**
+        * Return the previous revision, ie the most recent revision of the 
object of this revsion
+        * that's older then this revion. If there is none, false is returned.
+        *
+        * @since 0.1
+        *
+        * @return EPRevision|false
+        */
+       public function getPreviousRevision() {
+               return $this->getObject()->getLatestRevision( array(
+                       'id < ' . wfGetDB( DB_SLAVE )->addQuotes( 
$this->getId() )
+               ) );
+       }
+
 }

Modified: trunk/extensions/EducationProgram/includes/EPRevisionPager.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRevisionPager.php      
2012-03-06 21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/includes/EPRevisionPager.php      
2012-03-06 21:31:31 UTC (rev 113181)
@@ -128,8 +128,10 @@
                                );
                        }
 
-                       $html .= '&#160;.&#160;.&#160;';
-                       $html .= '(' .  $this->getLanguage()->pipeList( 
$actionLinks ) . ')';
+                       if ( !empty( $actionLinks ) ) {
+                               $html .= '&#160;.&#160;.&#160;';
+                               $html .= '(' .  $this->getLanguage()->pipeList( 
$actionLinks ) . ')';
+                       }
                }
 
                $this->rowNr++;

Modified: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPRevisionedObject.php   
2012-03-06 21:18:30 UTC (rev 113180)
+++ trunk/extensions/EducationProgram/includes/EPRevisionedObject.php   
2012-03-06 21:31:31 UTC (rev 113181)
@@ -283,8 +283,31 @@
                        $conditions
                ), $options );
        }
-       
+
        /**
+        * Returns the most recently stored revision for this object
+        * matching the provided contions or false if there is none.
+        *
+        * @since 0.1
+        *
+        * @param array $conditions
+        * @param array $options
+        *
+        * @return EPRevision|false
+        */
+       public function getLatestRevision( array $conditions = array(), array 
$options = array() ) {
+               $options['ORDER BY'] = 
EPRevisions::singleton()->getPrefixedField( 'id' ) . ' DESC';
+
+               return EPRevisions::singleton()->selectRow( null, array_merge(
+                       array(
+                               'type' => get_called_class(),
+                               'object_id' => $this->getId(),
+                       ),
+                       $conditions
+               ), $options );
+       }
+
+       /**
         * Undeletes ab object by inserting the current object.
         * Only call this method when the object does not exist in
         * it's database table and has the current version in the revision 
table.
@@ -324,15 +347,59 @@
                $fields = is_null( $fields ) ? $object->getFieldNames() : 
$fields;
                
                foreach ( $fields as $fieldName ) {
-                       $this->restoreField( $fieldName, $object );
+                       $this->restoreField( $fieldName, $object->getField( 
$fieldName ) );
                }
                
                return true;
        }
 
-       protected function restoreField( $fieldName, EPRevisionedObject $object 
) {
-               $this->setField( $fieldName, $object->getField( $fieldName ) );
+       /**
+        * Undo the changes of a single revision to this object.
+        * Changes are compared on field level. If a field is no
+        * longer the same as in the revision being undone, it
+        * will not be reverted.
+        *
+        * At some point we might want to have more fine grained
+        * reverts for text fields.
+        *
+        * @since 0.1
+        *
+        * @param EPRevision $revison
+        * @param array|null $fields
+        *
+        * @return boolean Success indicator
+        */
+       public function undoRevision( EPRevision $revison, array $fields = null 
) {
+               $oldObject = $revison->getPreviousRevision()->getObject();
+
+               if ( $oldObject === false ) {
+                       return false;
+               }
+
+               $newObject = $revison->getObject();
+
+               $fields = is_null( $fields ) ? $newObject->getFieldNames() : 
$fields;
+
+               foreach ( $fields as $fieldName ) {
+                       if ( $this->getField( $fieldName ) === 
$newObject->getField( $fieldName ) ) {
+                               $this->restoreField( $fieldName, 
$oldObject->getField( $fieldName ) );
+                       }
+               }
+
+               return true;
        }
+
+       /**
+        * Set a field to the value of the corresponding field in the provided 
object.
+        *
+        * @since 0.1
+        *
+        * @param string $fieldName
+        * @param mixed $newValue
+        */
+       protected function restoreField( $fieldName, $newValue ) {
+               $this->setField( $fieldName, $newValue );
+       }
        
        /**
         * Retore the object to a revision with the provided id.
@@ -348,5 +415,20 @@
                $revision = $this->getRevisionById( $revId );
                return $revision === false ? false : $this->restoreToRevision( 
$revision, $fields );
        }
+
+       /**
+        * Undo the changes of the revision with the provided id to this object.
+        *
+        * @since 0.1
+        *
+        * @param integer $revId
+        * @param array|null $fields
+        *
+        * @return boolean Success indicator
+        */
+       public function undoRevisionId( $revId, array $fields = null ) {
+               $revision = $this->getRevisionById( $revId );
+               return $revision === false ? false : $this->undoRevision( 
$revision, $fields );
+       }
        
 }


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to