Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250022

Change subject: Avoid calling User::setPassword() on users not in database
......................................................................

Avoid calling User::setPassword() on users not in database

MediaWiki core change Id3d4074 is removing the ability to call
$user->setPassword() before the user is added to the database. This change
updates the extension to avoid such calls. It remains backwards-compatible with
older versions of MediaWiki through adding a saveSettings() call when
necessary.

Change-Id: I08137e36a1ffbb5b8728135f0d441d688767bc9b
---
M maintenance/importWEPData.php
M maintenance/importWEPFromDB.php
2 files changed, 63 insertions(+), 66 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EducationProgram 
refs/changes/22/250022/1

diff --git a/maintenance/importWEPData.php b/maintenance/importWEPData.php
index 3758271..69435af 100644
--- a/maintenance/importWEPData.php
+++ b/maintenance/importWEPData.php
@@ -194,49 +194,48 @@
                        }
                        else {
                                if ( $user->getId() === 0 ) {
+                                       if ( !$user->addToDatabase()->isOK() ) {
+                                               echo "Failed to insert student 
'$name'. (failed to create user)\n";
+                                               continue;
+                                       }
                                        $user->setPassword( 'ohithere' );
-                                       $user->addToDatabase();
+                                       $user->saveSettings();
                                }
 
-                               if ( $user->getId() === 0 ) {
-                                       echo "Failed to insert student '$name'. 
(failed to create user)\n";
+                               $student = Student::newFromUser( $user );
+
+                               if ( is_null( $student->getId() ) ) {
+                                       if ( !$student->save() ) {
+                                               echo "Failed to insert student 
'$name'. (failed create student profile)\n";
+                                               continue;
+                                       }
                                }
-                               else {
-                                       $student = Student::newFromUser( $user 
);
 
-                                       if ( is_null( $student->getId() ) ) {
-                                               if ( !$student->save() ) {
-                                                       echo "Failed to insert 
student '$name'. (failed create student profile)\n";
-                                                       continue;
-                                               }
-                                       }
+                               $courses = array();
 
-                                       $courses = array();
+                               foreach ( $courseNames as $courseName ) {
+                                       if ( array_key_exists( $courseName, 
$courseIds ) ) {
+                                               $revAction = new 
RevisionAction();
+                                               $revAction->setUser( $user );
+                                               $revAction->setComment( 
'Import' );
 
-                                       foreach ( $courseNames as $courseName ) 
{
-                                               if ( array_key_exists( 
$courseName, $courseIds ) ) {
-                                                       $revAction = new 
RevisionAction();
-                                                       $revAction->setUser( 
$user );
-                                                       $revAction->setComment( 
'Import' );
-
-                                                       /**
-                                                        * @var Course $course
-                                                        */
-                                                       $course = 
Courses::singleton()->selectRow( null, array( 'id' => $courseIds[$courseName] ) 
);
-                                                       $course->enlistUsers( 
array( $user->getId() ), 'student', true, $revAction );
-                                               }
-                                               else {
-                                                       echo "Failed to 
associate student '$name' with course '$courseName'.\n";
-                                               }
-                                       }
-
-                                       if ( $student->associateWithCourses( 
$courses ) ) {
-                                               echo "Inserted student 
'$name'\t\t and associated with courses: " . str_replace( '_', ' ', implode( ', 
', $courseNames ) ) . "\n";
+                                               /**
+                                                * @var Course $course
+                                                */
+                                               $course = 
Courses::singleton()->selectRow( null, array( 'id' => $courseIds[$courseName] ) 
);
+                                               $course->enlistUsers( array( 
$user->getId() ), 'student', true, $revAction );
                                        }
                                        else {
-                                               echo "Failed to insert student 
'$name'. (failed to associate courses)\n";
+                                               echo "Failed to associate 
student '$name' with course '$courseName'.\n";
                                        }
                                }
+
+                               if ( $student->associateWithCourses( $courses ) 
) {
+                                       echo "Inserted student '$name'\t\t and 
associated with courses: " . str_replace( '_', ' ', implode( ', ', $courseNames 
) ) . "\n";
+                               }
+                               else {
+                                       echo "Failed to insert student '$name'. 
(failed to associate courses)\n";
+                               }
                        }
                }
        }
diff --git a/maintenance/importWEPFromDB.php b/maintenance/importWEPFromDB.php
index beafdf1..5111f03 100644
--- a/maintenance/importWEPFromDB.php
+++ b/maintenance/importWEPFromDB.php
@@ -279,8 +279,6 @@
                        }
                        else {
                                if ( $user->getId() === 0 ) {
-                                       $user->setPassword( 'ohithere' );
-
                                        if ( $student->student_lastname !== '' 
&& $student->student_firstname !== '' ) {
                                                $user->setRealName( 
$student->student_firstname . ' ' . $student->student_lastname );
                                        }
@@ -289,43 +287,43 @@
                                                $user->setEmail( 
$student->student_email );
                                        }
 
-                                       $user->addToDatabase();
+                                       if ( !$user->addToDatabase()->isOK() ) {
+                                               $this->err( "Failed to insert 
student '$name'. (failed to create user)" );
+                                               continue;
+                                       }
+                                       $user->setPassword( 'ohithere' );
+                                       $user->saveSettings();
                                }
 
-                               if ( $user->getId() === 0 ) {
-                                       $this->err( "Failed to insert student 
'$name'. (failed to create user)" );
-                               }
-                               else {
-                                       $studentObject = Student::newFromUser( 
$user );
+                               $studentObject = Student::newFromUser( $user );
 
-                                       if ( is_null( $studentObject->getId() ) 
) {
-                                               if ( !$studentObject->save() ) {
-                                                       $this->err( "Failed to 
insert student '$name'. (failed create student profile)" );
-                                                       continue;
-                                               }
+                               if ( is_null( $studentObject->getId() ) ) {
+                                       if ( !$studentObject->save() ) {
+                                               $this->err( "Failed to insert 
student '$name'. (failed create student profile)" );
+                                               continue;
+                                       }
+                               }
+
+                               foreach ( array( $student->student_course_id ) 
as $courseId ) {
+                                       $success = false;
+
+                                       if ( array_key_exists( $courseId, 
$this->courseIds ) ) {
+                                               $revAction = new 
RevisionAction();
+                                               $revAction->setUser( $user );
+                                               $revAction->setComment( 
'Import' );
+
+                                               /**
+                                                * @var Course $course
+                                                */
+                                               $course = 
Courses::singleton()->selectRow( null, array( 'id' => 
$this->courseIds[$courseId] ) );
+                                               $success = 
$course->enlistUsers( array( $user->getId() ), 'student', true, $revAction );
                                        }
 
-                                       foreach ( array( 
$student->student_course_id ) as $courseId ) {
-                                               $success = false;
-
-                                               if ( array_key_exists( 
$courseId, $this->courseIds ) ) {
-                                                       $revAction = new 
RevisionAction();
-                                                       $revAction->setUser( 
$user );
-                                                       $revAction->setComment( 
'Import' );
-
-                                                       /**
-                                                        * @var Course $course
-                                                        */
-                                                       $course = 
Courses::singleton()->selectRow( null, array( 'id' => 
$this->courseIds[$courseId] ) );
-                                                       $success = 
$course->enlistUsers( array( $user->getId() ), 'student', true, $revAction );
-                                               }
-
-                                               if ( $success !== false ) {
-                                                       $this->msg( 
"\tAssociated student '$name' with course '$courseId'.", 2 );
-                                               }
-                                               else {
-                                                       $this->msg( "\tFailed 
to associate student '$name' with course '$courseId'." );
-                                               }
+                                       if ( $success !== false ) {
+                                               $this->msg( "\tAssociated 
student '$name' with course '$courseId'.", 2 );
+                                       }
+                                       else {
+                                               $this->msg( "\tFailed to 
associate student '$name' with course '$courseId'." );
                                        }
                                }
                        }

-- 
To view, visit https://gerrit.wikimedia.org/r/250022
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I08137e36a1ffbb5b8728135f0d441d688767bc9b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EducationProgram
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

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

Reply via email to