r113181 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113180‎ | r113181 | r113182 >
Date:21:31, 6 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on undo action
Modified paths:
  • /trunk/extensions/EducationProgram/actions/EPRestoreAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPUndoAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourses.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevision.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionPager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionedObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/actions/EPRestoreAction.php
@@ -165,7 +165,6 @@
166166 );
167167
168168 $out->addHTML( Html::hidden( 'revid', $this->getRequest()->getInt( 'revid' ) ) );
169 -
170169 $out->addHTML( Html::hidden( 'restoreToken', $this->getUser()->getEditToken( $this->getSalt() ) ) );
171170
172171 $out->addHTML( '</form>' );
Index: trunk/extensions/EducationProgram/actions/EPUndoAction.php
@@ -55,8 +55,13 @@
5656 $req = $this->getRequest();
5757
5858 if ( $req->wasPosted() && $this->getUser()->matchEditToken( $req->getText( 'undoToken' ), $this->getSalt() ) ) {
59 - $success = $this->doUndo( $object );
60 -
 59+ if ( $req->getCheck( 'revid' ) ) {
 60+ $success = $this->doUndo( $object, $req->getInt( 'revid' ) );
 61+ }
 62+ else {
 63+ $success = false;
 64+ }
 65+
6166 if ( $success ) {
6267 $query = array( 'undid' => '1' ); // TODO: handle
6368 }
@@ -80,17 +85,27 @@
8186 * @since 0.1
8287 *
8388 * @param EPPageObject $object
 89+ * @param integer $revId
8490 *
8591 * @return boolean Success indicator
8692 */
87 - protected function doUndo( EPPageObject $object ) {
88 - $revAction = new EPRevisionAction();
 93+ protected function doUndo( EPPageObject $object, $revId ) {
 94+ $success = $object->undoRevisionId( $revId, $object->getTable()->getRevertableFields() );
 95+
 96+ if ( $success ) {
 97+ $revAction = new EPRevisionAction();
 98+
 99+ $revAction->setUser( $this->getUser() );
 100+ $revAction->setComment( $this->getRequest()->getText( 'summary', '' ) );
 101+
 102+ $success = $object->revisionedSave( $revAction );
 103+
 104+ if ( $success ) {
 105+ // TODO: log
 106+ // Already logged - just alter message?
 107+ }
 108+ }
89109
90 - $revAction->setUser( $this->getUser() );
91 - $revAction->setComment( $this->getRequest()->getText( 'summary', '' ) );
92 -
93 - // TODO
94 -
95110 return false;
96111 }
97112
@@ -149,6 +164,7 @@
150165 wfMsg( $this->prefixMsg( 'cancel-button' ) )
151166 );
152167
 168+ $out->addHTML( Html::hidden( 'revid', $this->getRequest()->getInt( 'revid' ) ) );
153169 $out->addHTML( Html::hidden( 'undoToken', $this->getUser()->getEditToken( $this->getSalt() ) ) );
154170
155171 $out->addHTML( '</form>' );
Index: trunk/extensions/EducationProgram/includes/EPRevisionPager.php
@@ -128,8 +128,10 @@
129129 );
130130 }
131131
132 - $html .= '&#160;.&#160;.&#160;';
133 - $html .= '(' . $this->getLanguage()->pipeList( $actionLinks ) . ')';
 132+ if ( !empty( $actionLinks ) ) {
 133+ $html .= '&#160;.&#160;.&#160;';
 134+ $html .= '(' . $this->getLanguage()->pipeList( $actionLinks ) . ')';
 135+ }
134136 }
135137
136138 $this->rowNr++;
Index: trunk/extensions/EducationProgram/includes/EPCourses.php
@@ -118,11 +118,6 @@
119119 'instructor_count',
120120 'oa_count',
121121 'ca_count',
122 - 'field',
123 - 'level',
124 - 'term',
125 - 'lang',
126 - 'mc',
127122 );
128123 }
129124
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
@@ -283,8 +283,31 @@
284284 $conditions
285285 ), $options );
286286 }
287 -
 287+
288288 /**
 289+ * Returns the most recently stored revision for this object
 290+ * matching the provided contions or false if there is none.
 291+ *
 292+ * @since 0.1
 293+ *
 294+ * @param array $conditions
 295+ * @param array $options
 296+ *
 297+ * @return EPRevision|false
 298+ */
 299+ public function getLatestRevision( array $conditions = array(), array $options = array() ) {
 300+ $options['ORDER BY'] = EPRevisions::singleton()->getPrefixedField( 'id' ) . ' DESC';
 301+
 302+ return EPRevisions::singleton()->selectRow( null, array_merge(
 303+ array(
 304+ 'type' => get_called_class(),
 305+ 'object_id' => $this->getId(),
 306+ ),
 307+ $conditions
 308+ ), $options );
 309+ }
 310+
 311+ /**
289312 * Undeletes ab object by inserting the current object.
290313 * Only call this method when the object does not exist in
291314 * it's database table and has the current version in the revision table.
@@ -324,15 +347,59 @@
325348 $fields = is_null( $fields ) ? $object->getFieldNames() : $fields;
326349
327350 foreach ( $fields as $fieldName ) {
328 - $this->restoreField( $fieldName, $object );
 351+ $this->restoreField( $fieldName, $object->getField( $fieldName ) );
329352 }
330353
331354 return true;
332355 }
333356
334 - protected function restoreField( $fieldName, EPRevisionedObject $object ) {
335 - $this->setField( $fieldName, $object->getField( $fieldName ) );
 357+ /**
 358+ * Undo the changes of a single revision to this object.
 359+ * Changes are compared on field level. If a field is no
 360+ * longer the same as in the revision being undone, it
 361+ * will not be reverted.
 362+ *
 363+ * At some point we might want to have more fine grained
 364+ * reverts for text fields.
 365+ *
 366+ * @since 0.1
 367+ *
 368+ * @param EPRevision $revison
 369+ * @param array|null $fields
 370+ *
 371+ * @return boolean Success indicator
 372+ */
 373+ public function undoRevision( EPRevision $revison, array $fields = null ) {
 374+ $oldObject = $revison->getPreviousRevision()->getObject();
 375+
 376+ if ( $oldObject === false ) {
 377+ return false;
 378+ }
 379+
 380+ $newObject = $revison->getObject();
 381+
 382+ $fields = is_null( $fields ) ? $newObject->getFieldNames() : $fields;
 383+
 384+ foreach ( $fields as $fieldName ) {
 385+ if ( $this->getField( $fieldName ) === $newObject->getField( $fieldName ) ) {
 386+ $this->restoreField( $fieldName, $oldObject->getField( $fieldName ) );
 387+ }
 388+ }
 389+
 390+ return true;
336391 }
 392+
 393+ /**
 394+ * Set a field to the value of the corresponding field in the provided object.
 395+ *
 396+ * @since 0.1
 397+ *
 398+ * @param string $fieldName
 399+ * @param mixed $newValue
 400+ */
 401+ protected function restoreField( $fieldName, $newValue ) {
 402+ $this->setField( $fieldName, $newValue );
 403+ }
337404
338405 /**
339406 * Retore the object to a revision with the provided id.
@@ -348,5 +415,20 @@
349416 $revision = $this->getRevisionById( $revId );
350417 return $revision === false ? false : $this->restoreToRevision( $revision, $fields );
351418 }
 419+
 420+ /**
 421+ * Undo the changes of the revision with the provided id to this object.
 422+ *
 423+ * @since 0.1
 424+ *
 425+ * @param integer $revId
 426+ * @param array|null $fields
 427+ *
 428+ * @return boolean Success indicator
 429+ */
 430+ public function undoRevisionId( $revId, array $fields = null ) {
 431+ $revision = $this->getRevisionById( $revId );
 432+ return $revision === false ? false : $this->undoRevision( $revision, $fields );
 433+ }
352434
353435 }
Index: trunk/extensions/EducationProgram/includes/EPRevision.php
@@ -78,4 +78,18 @@
7979 return $this->user;
8080 }
8181
 82+ /**
 83+ * Return the previous revision, ie the most recent revision of the object of this revsion
 84+ * that's older then this revion. If there is none, false is returned.
 85+ *
 86+ * @since 0.1
 87+ *
 88+ * @return EPRevision|false
 89+ */
 90+ public function getPreviousRevision() {
 91+ return $this->getObject()->getLatestRevision( array(
 92+ 'id < ' . wfGetDB( DB_SLAVE )->addQuotes( $this->getId() )
 93+ ) );
 94+ }
 95+
8296 }
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -190,7 +190,7 @@
191191 }
192192 }
193193
194 - if ( count( $newUsers ) > 0 ) {
 194+ if ( !empty( $newUsers ) ) {
195195 $dbw->begin();
196196
197197 foreach ( $newUsers as $userLink ) {
@@ -706,10 +706,14 @@
707707 EPUtils::log( $info );
708708 }
709709
710 - protected function restoreField( $fieldName, EPRevisionedObject $object ) {
 710+ /**
 711+ * (non-PHPdoc)
 712+ * @see EPRevionedObject::restoreField()
 713+ */
 714+ protected function restoreField( $fieldName, $newValue ) {
711715 if ( $fieldName !== 'org_id'
712 - || EPOrgs::singleton()->has( array( 'id' => $object->getField( 'org_id' ) ) ) ) {
713 - parent::restoreField( $fieldName, $object );
 716+ || EPOrgs::singleton()->has( array( 'id' => $newValue ) ) ) {
 717+ parent::restoreField( $fieldName, $newValue );
714718 }
715719 }
716720

Status & tagging log