Index: trunk/extensions/EducationProgram/actions/EPRestoreAction.php |
— | — | @@ -165,7 +165,6 @@ |
166 | 166 | ); |
167 | 167 | |
168 | 168 | $out->addHTML( Html::hidden( 'revid', $this->getRequest()->getInt( 'revid' ) ) ); |
169 | | - |
170 | 169 | $out->addHTML( Html::hidden( 'restoreToken', $this->getUser()->getEditToken( $this->getSalt() ) ) ); |
171 | 170 | |
172 | 171 | $out->addHTML( '</form>' ); |
Index: trunk/extensions/EducationProgram/actions/EPUndoAction.php |
— | — | @@ -55,8 +55,13 @@ |
56 | 56 | $req = $this->getRequest(); |
57 | 57 | |
58 | 58 | 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 | + |
61 | 66 | if ( $success ) { |
62 | 67 | $query = array( 'undid' => '1' ); // TODO: handle |
63 | 68 | } |
— | — | @@ -80,17 +85,27 @@ |
81 | 86 | * @since 0.1 |
82 | 87 | * |
83 | 88 | * @param EPPageObject $object |
| 89 | + * @param integer $revId |
84 | 90 | * |
85 | 91 | * @return boolean Success indicator |
86 | 92 | */ |
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 | + } |
89 | 109 | |
90 | | - $revAction->setUser( $this->getUser() ); |
91 | | - $revAction->setComment( $this->getRequest()->getText( 'summary', '' ) ); |
92 | | - |
93 | | - // TODO |
94 | | - |
95 | 110 | return false; |
96 | 111 | } |
97 | 112 | |
— | — | @@ -149,6 +164,7 @@ |
150 | 165 | wfMsg( $this->prefixMsg( 'cancel-button' ) ) |
151 | 166 | ); |
152 | 167 | |
| 168 | + $out->addHTML( Html::hidden( 'revid', $this->getRequest()->getInt( 'revid' ) ) ); |
153 | 169 | $out->addHTML( Html::hidden( 'undoToken', $this->getUser()->getEditToken( $this->getSalt() ) ) ); |
154 | 170 | |
155 | 171 | $out->addHTML( '</form>' ); |
Index: trunk/extensions/EducationProgram/includes/EPRevisionPager.php |
— | — | @@ -128,8 +128,10 @@ |
129 | 129 | ); |
130 | 130 | } |
131 | 131 | |
132 | | - $html .= ' . . '; |
133 | | - $html .= '(' . $this->getLanguage()->pipeList( $actionLinks ) . ')'; |
| 132 | + if ( !empty( $actionLinks ) ) { |
| 133 | + $html .= ' . . '; |
| 134 | + $html .= '(' . $this->getLanguage()->pipeList( $actionLinks ) . ')'; |
| 135 | + } |
134 | 136 | } |
135 | 137 | |
136 | 138 | $this->rowNr++; |
Index: trunk/extensions/EducationProgram/includes/EPCourses.php |
— | — | @@ -118,11 +118,6 @@ |
119 | 119 | 'instructor_count', |
120 | 120 | 'oa_count', |
121 | 121 | 'ca_count', |
122 | | - 'field', |
123 | | - 'level', |
124 | | - 'term', |
125 | | - 'lang', |
126 | | - 'mc', |
127 | 122 | ); |
128 | 123 | } |
129 | 124 | |
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php |
— | — | @@ -283,8 +283,31 @@ |
284 | 284 | $conditions |
285 | 285 | ), $options ); |
286 | 286 | } |
287 | | - |
| 287 | + |
288 | 288 | /** |
| 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 | + /** |
289 | 312 | * Undeletes ab object by inserting the current object. |
290 | 313 | * Only call this method when the object does not exist in |
291 | 314 | * it's database table and has the current version in the revision table. |
— | — | @@ -324,15 +347,59 @@ |
325 | 348 | $fields = is_null( $fields ) ? $object->getFieldNames() : $fields; |
326 | 349 | |
327 | 350 | foreach ( $fields as $fieldName ) { |
328 | | - $this->restoreField( $fieldName, $object ); |
| 351 | + $this->restoreField( $fieldName, $object->getField( $fieldName ) ); |
329 | 352 | } |
330 | 353 | |
331 | 354 | return true; |
332 | 355 | } |
333 | 356 | |
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; |
336 | 391 | } |
| 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 | + } |
337 | 404 | |
338 | 405 | /** |
339 | 406 | * Retore the object to a revision with the provided id. |
— | — | @@ -348,5 +415,20 @@ |
349 | 416 | $revision = $this->getRevisionById( $revId ); |
350 | 417 | return $revision === false ? false : $this->restoreToRevision( $revision, $fields ); |
351 | 418 | } |
| 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 | + } |
352 | 434 | |
353 | 435 | } |
Index: trunk/extensions/EducationProgram/includes/EPRevision.php |
— | — | @@ -78,4 +78,18 @@ |
79 | 79 | return $this->user; |
80 | 80 | } |
81 | 81 | |
| 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 | + |
82 | 96 | } |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -190,7 +190,7 @@ |
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | | - if ( count( $newUsers ) > 0 ) { |
| 194 | + if ( !empty( $newUsers ) ) { |
195 | 195 | $dbw->begin(); |
196 | 196 | |
197 | 197 | foreach ( $newUsers as $userLink ) { |
— | — | @@ -706,10 +706,14 @@ |
707 | 707 | EPUtils::log( $info ); |
708 | 708 | } |
709 | 709 | |
710 | | - protected function restoreField( $fieldName, EPRevisionedObject $object ) { |
| 710 | + /** |
| 711 | + * (non-PHPdoc) |
| 712 | + * @see EPRevionedObject::restoreField() |
| 713 | + */ |
| 714 | + protected function restoreField( $fieldName, $newValue ) { |
711 | 715 | 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 ); |
714 | 718 | } |
715 | 719 | } |
716 | 720 | |